openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.21.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/test_deepcopy.py

59lines · modeblame

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