Repository navigation
Expand file tree
/
Copy pathtest_api_wrappers.py
More file actions
124 lines (102 loc) · 3.58 KB
/
Copy pathtest_api_wrappers.py
File metadata and controls
124 lines (102 loc) · 3.58 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Copyright (C) CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT
import pickle
from copy import deepcopy
from cvat_sdk import models
from deepdiff import DeepDiff
def test_models_do_not_change_input_values():
# Nested containers may be modified during the model input data parsing.
# This can lead to subtle memory errors, which are very hard to find.
original_input_data = {
"name": "test",
"labels": [
{
"name": "cat",
"attributes": [
{
"default_value": "yy",
"input_type": "text",
"mutable": False,
"name": "x",
"values": ["yy"],
},
{
"default_value": "1",
"input_type": "radio",
"mutable": False,
"name": "y",
"values": ["1", "2"],
},
],
}
],
}
input_data = deepcopy(original_input_data)
models.TaskWriteRequest(**input_data)
assert DeepDiff(original_input_data, input_data) == {}
def test_models_do_not_store_input_collections():
# Avoid depending on input data for collection fields after the model is initialized.
# This can lead to subtle memory errors and unexpected behavior
# if the original input data is modified.
input_data = {
"name": "test",
"labels": [
{
"name": "cat1",
"attributes": [
{
"default_value": "yy",
"input_type": "text",
"mutable": False,
"name": "x",
"values": ["yy"],
},
{
"default_value": "1",
"input_type": "radio",
"mutable": False,
"name": "y",
"values": ["1", "2"],
},
],
},
{"name": "cat2", "attributes": []},
],
}
model = models.TaskWriteRequest(**input_data)
model_data1 = model.to_dict()
# Modify input value containers
input_data["labels"][0]["attributes"].clear()
input_data["labels"][1]["attributes"].append(
{
"default_value": "",
"input_type": "text",
"mutable": True,
"name": "z",
}
)
input_data["labels"].append({"name": "dog"})
model_data2 = model.to_dict()
assert DeepDiff(model_data1, model_data2) == {}
def test_models_do_not_return_internal_collections():
# Avoid returning internal data for mutable collection fields.
# This can lead to subtle memory errors and unexpected behavior
# if the returned data is modified.
input_data = {
"name": "test",
"labels": [],
}
model = models.TaskWriteRequest(**input_data)
model_data1 = model.to_dict()
model_data1_original = deepcopy(model_data1)
# Modify an output value container
model_data1["labels"].append({"name": "dog"})
model_data2 = model.to_dict()
assert DeepDiff(model_data1_original, model_data2) == {}
def test_models_are_pickleable():
model = models.PatchedLabelRequest(id=5, name="person")
pickled_model = pickle.dumps(model)
unpickled_model = pickle.loads(pickled_model)
assert unpickled_model.id == model.id
assert unpickled_model.name == model.name