PyTest

Modules:

Name Description
conftest

Pytest top-level conftest.py.

test_pyobjson

Pytest test for Python Object JSON Tool code.

conftest

Pytest top-level conftest.py.

Classes:

Name Description
ThirdClass

ThirdClass for testing.

SecondClass

SecondClass for testing.

FirstClass

FirstClass for testing.

Functions:

Name Description
ext_func

Function external_function for testing.

external_function

External function for testing.

first_class_with_empty_arguments

Create FirstClass instance with empty arguments for testing.

first_class_with_nested_child_classes

Create FirstClass instance for testing.

first_class_json_str

Create JSON string from FirstClass instance for testing.

ThirdClass

Bases: PythonObjectJson

ThirdClass for testing.

Source code in tests/conftest.py
21
22
23
24
25
26
class ThirdClass(PythonObjectJson):
    """ThirdClass for testing."""

    def __init__(self, third_class_param: str):
        super().__init__()
        self.third_class_param: str = third_class_param

SecondClass

Bases: PythonObjectJson

SecondClass for testing.

Source code in tests/conftest.py
29
30
31
32
33
34
35
class SecondClass(PythonObjectJson):
    """SecondClass for testing."""

    def __init__(self, third_class_list: List[ThirdClass]):
        super().__init__()
        self.third_class_list: List[ThirdClass] = third_class_list
        self.third_class_list_dict: Dict[str, List[ThirdClass]] = {"third_class_list_1": third_class_list}

FirstClass

Bases: PythonObjectJson

FirstClass for testing.

Source code in tests/conftest.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class FirstClass(PythonObjectJson):
    """FirstClass for testing."""

    def __init__(
        self,
        second_class_dict: Dict[str, SecondClass],
        second_class_list: List[SecondClass],
        first_class_attribute: Optional[str],
        first_class_set: Optional[Set[str]],
        first_class_tuple: Optional[Tuple[str]],
        first_class_bytes: Optional[bytes],
        first_class_file: Optional[Path],
        first_class_external_function: Optional[Callable],
        first_class_datetime: Optional[datetime],
    ):
        super().__init__()
        self.second_class_dict: Dict[str, SecondClass] = second_class_dict
        self.second_class_list: List[SecondClass] = second_class_list
        self.first_class_attribute: str = first_class_attribute
        self.first_class_set: Set[str] = first_class_set
        self.first_class_tuple: Tuple[str] = first_class_tuple
        self.first_class_bytes: bytes = first_class_bytes
        self.first_class_file: Path = first_class_file
        self.first_class_external_function: Optional[Callable] = first_class_external_function
        self.first_class_datetime: datetime = first_class_datetime

ext_func

ext_func(param_1, param_2)

Function external_function for testing.

Source code in tests/conftest.py
16
17
18
def ext_func(param_1: str, param_2: str):
    """Function external_function for testing."""
    return f"{param_1}.{param_2}"

external_function

external_function()

External function for testing.

Source code in tests/conftest.py
65
66
67
68
@fixture(scope="module")
def external_function() -> Callable:
    """External function for testing."""
    return ext_func

first_class_with_empty_arguments

first_class_with_empty_arguments()

Create FirstClass instance with empty arguments for testing.

Returns:
  • FirstClass( FirstClass ) –

    Instance of FirstClass with all empty arguments.

Source code in tests/conftest.py
71
72
73
74
75
76
77
78
79
@fixture(scope="module")
def first_class_with_empty_arguments() -> FirstClass:
    """Create FirstClass instance with empty arguments for testing.

    Returns:
        FirstClass: Instance of FirstClass with all empty arguments.

    """
    return FirstClass({}, [], None, None, None, None, None, None, None)

first_class_with_nested_child_classes

first_class_with_nested_child_classes(external_function)

Create FirstClass instance for testing.

Returns:
  • FirstClass( FirstClass ) –

    Instance of FirstClass.

Source code in tests/conftest.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
@fixture(scope="module")
def first_class_with_nested_child_classes(external_function) -> FirstClass:
    """Create FirstClass instance for testing.

    Returns:
        FirstClass: Instance of FirstClass.

    """
    return FirstClass(
        {"second_class_1": SecondClass([ThirdClass("test_third_class_argument_in_dict")])},
        [SecondClass([ThirdClass("test_third_class_argument_in_list")])],
        "test_first_class_attribute_string",
        {"test_first_class_collection_element"},
        ("test_first_class_collection_element",),
        b"test_first_class_collection_element",
        Path(__name__),
        external_function,
        datetime(2024, 1, 1, 0, 0, 0),
    )

first_class_json_str

first_class_json_str()

Create JSON string from FirstClass instance for testing.

Returns:
  • str( str ) –

    JSON string derived from serialized FirstClass instance.

Source code in tests/conftest.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
@fixture(scope="module")
def first_class_json_str() -> str:
    """Create JSON string from FirstClass instance for testing.

    Returns:
        str: JSON string derived from serialized FirstClass instance.

    """
    return json.dumps(
        {
            "conftest.firstclass": {
                "collection:dict.second_class_dict": {
                    "second_class_1": {
                        "conftest.secondclass": {
                            "collection:list.third_class_list": [
                                {"conftest.thirdclass": {"third_class_param": "test_third_class_argument_in_dict"}}
                            ],
                            "collection:dict.third_class_list_dict": {
                                "third_class_list_1": [
                                    {"conftest.thirdclass": {"third_class_param": "test_third_class_argument_in_dict"}}
                                ]
                            },
                        }
                    }
                },
                "collection:list.second_class_list": [
                    {
                        "conftest.secondclass": {
                            "collection:list.third_class_list": [
                                {"conftest.thirdclass": {"third_class_param": "test_third_class_argument_in_list"}}
                            ],
                            "collection:dict.third_class_list_dict": {
                                "third_class_list_1": [
                                    {"conftest.thirdclass": {"third_class_param": "test_third_class_argument_in_list"}}
                                ]
                            },
                        }
                    }
                ],
                "first_class_attribute": "test_first_class_attribute_string",
                "collection:set.first_class_set": ["test_first_class_collection_element"],
                "collection:tuple.first_class_tuple": ["test_first_class_collection_element"],
                "collection:bytes.first_class_bytes": "dGVzdF9maXJzdF9jbGFzc19jb2xsZWN0aW9uX2VsZW1lbnQ=",
                "path.first_class_file": "conftest",
                "callable.first_class_external_function": "conftest.ext_func::param_1:str,param_2:str",
                "datetime.first_class_datetime": "2024-01-01T00:00:00",
            }
        },
        ensure_ascii=False,
        indent=2,
    )

test_pyobjson

Pytest test for Python Object JSON Tool code.

Note

Tests pyobjson.base module.

Classes:

Name Description
TestPythonObjectJson

Pytest class for PythonObjectJson functionality.

TestPythonObjectJson

Pytest class for PythonObjectJson functionality.

Source code in tests/test_pyobjson.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class TestPythonObjectJson:
    """Pytest class for PythonObjectJson functionality."""

    def test_serialization_to_json_string(self, first_class_with_nested_child_classes, first_class_json_str):
        # confirm conftest.FirstClass instance as a JSON string is equal to the conftest FirstClass JSON string
        assert first_class_with_nested_child_classes.to_json_str() == first_class_json_str

    def test_deserialization_from_json_string(
        self, first_class_with_empty_arguments, first_class_with_nested_child_classes, first_class_json_str
    ):
        # create new FirstClass instance with all empty/None arguments
        first_class_instance = first_class_with_empty_arguments

        # confirm new empty FirstClass instance is not equivalent to conftest.FirstClass instance
        assert first_class_instance != first_class_with_nested_child_classes

        # load conftest FirstClass JSON string to new empty FirstClass instance
        first_class_instance.from_json_str(first_class_json_str)

        # confirm newly created FirstClass instance loaded from JSON is equivalent to conftest.FirstClass instance
        assert first_class_instance == first_class_with_nested_child_classes