File: test_exceptions.py

package info (click to toggle)
python-papermill 2.6.0-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 2,216 kB
  • sloc: python: 4,977; makefile: 17; sh: 5
file content (48 lines) | stat: -rw-r--r-- 1,575 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import pickle
import tempfile

import pytest

from papermill import exceptions


@pytest.fixture
def temp_file():
    """NamedTemporaryFile must be set in wb mode, closed without delete, opened with open(file, "rb"),
    then manually deleted. Otherwise, file fails to be read due to permission error on Windows."""
    with tempfile.NamedTemporaryFile(mode="wb", delete=False) as f:
        yield f
        os.unlink(f.name)


@pytest.mark.parametrize(
    "exc,args",
    [
        (
            exceptions.PapermillExecutionError,
            (1, 2, "TestSource", "Exception", Exception(), ["Traceback", "Message"]),
        ),
        (exceptions.PapermillMissingParameterException, ("PapermillMissingParameterException",)),
        (exceptions.AwsError, ("AwsError",)),
        (exceptions.FileExistsError, ("FileExistsError",)),
        (exceptions.PapermillException, ("PapermillException",)),
        (exceptions.PapermillRateLimitException, ("PapermillRateLimitException",)),
        (
            exceptions.PapermillOptionalDependencyException,
            ("PapermillOptionalDependencyException",),
        ),
    ],
)
def test_exceptions_are_unpickleable(temp_file, exc, args):
    """Ensure exceptions can be unpickled"""
    err = exc(*args)
    pickle.dump(err, temp_file)
    temp_file.close()  # close to re-open for reading

    # Read the Pickled File
    with open(temp_file.name, "rb") as read_file:
        read_file.seek(0)
        data = read_file.read()
        pickled_err = pickle.loads(data)
        assert str(pickled_err) == str(err)