openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/test_deepcopy.py
58lines · modeblame
08b8179aDavid Schnurr2 years ago | 1 | from openai._utils import deepcopy_minimal |
| 2 | | |
| 3 | | |
| 4 | def assert_different_identities(obj1: object, obj2: object) -> None: | |
| 5 | assert obj1 == obj2 | |
| 6 | assert id(obj1) != id(obj2) | |
| 7 | | |
| 8 | | |
| 9 | def test_simple_dict() -> None: | |
| 10 | obj1 = {"foo": "bar"} | |
| 11 | obj2 = deepcopy_minimal(obj1) | |
| 12 | assert_different_identities(obj1, obj2) | |
| 13 | | |
| 14 | | |
| 15 | def test_nested_dict() -> None: | |
| 16 | obj1 = {"foo": {"bar": True}} | |
| 17 | obj2 = deepcopy_minimal(obj1) | |
| 18 | assert_different_identities(obj1, obj2) | |
| 19 | assert_different_identities(obj1["foo"], obj2["foo"]) | |
| 20 | | |
| 21 | | |
| 22 | def test_complex_nested_dict() -> None: | |
| 23 | obj1 = {"foo": {"bar": [{"hello": "world"}]}} | |
| 24 | obj2 = deepcopy_minimal(obj1) | |
| 25 | assert_different_identities(obj1, obj2) | |
| 26 | assert_different_identities(obj1["foo"], obj2["foo"]) | |
| 27 | assert_different_identities(obj1["foo"]["bar"], obj2["foo"]["bar"]) | |
| 28 | assert_different_identities(obj1["foo"]["bar"][0], obj2["foo"]["bar"][0]) | |
| 29 | | |
| 30 | | |
| 31 | def test_simple_list() -> None: | |
| 32 | obj1 = ["a", "b", "c"] | |
| 33 | obj2 = deepcopy_minimal(obj1) | |
| 34 | assert_different_identities(obj1, obj2) | |
| 35 | | |
| 36 | | |
| 37 | def test_nested_list() -> None: | |
| 38 | obj1 = ["a", [1, 2, 3]] | |
| 39 | obj2 = deepcopy_minimal(obj1) | |
| 40 | assert_different_identities(obj1, obj2) | |
| 41 | assert_different_identities(obj1[1], obj2[1]) | |
| 42 | | |
| 43 | | |
1d8a28e3Stainless Bot1 years ago | 44 | class MyObject: ... |
08b8179aDavid Schnurr2 years ago | 45 | |
| 46 | | |
| 47 | def test_ignores_other_types() -> None: | |
| 48 | # custom classes | |
| 49 | my_obj = MyObject() | |
| 50 | obj1 = {"foo": my_obj} | |
| 51 | obj2 = deepcopy_minimal(obj1) | |
| 52 | assert_different_identities(obj1, obj2) | |
| 53 | assert obj1["foo"] is my_obj | |
| 54 | | |
| 55 | # tuples | |
| 56 | obj3 = ("a", "b") | |
| 57 | obj4 = deepcopy_minimal(obj3) | |
| 58 | assert obj3 is obj4 |