Debian Codegraph @

tests/test_ignore_uuid_types.py

raw
1 #!/usr/bin/env python
2 import uuid
3 import unittest
4 from deepdiff import DeepDiff
5
6
7 class TestIgnoreUuidTypes(unittest.TestCase):
8 """Test ignore_uuid_types functionality"""
9
10 def test_uuid_vs_string_without_ignore(self):
11 """Test that UUID vs string reports type change by default"""
12 test_uuid = uuid.UUID('12345678-1234-5678-1234-567812345678')
13 uuid_str = '12345678-1234-5678-1234-567812345678'
14
15 result = DeepDiff(test_uuid, uuid_str)
16
17 assert 'type_changes' in result
18 assert result['type_changes']['root']['old_type'] == uuid.UUID
19 assert result['type_changes']['root']['new_type'] == str
20 assert result['type_changes']['root']['old_value'] == test_uuid
21 assert result['type_changes']['root']['new_value'] == uuid_str
22
23 def test_uuid_vs_string_with_ignore(self):
24 """Test that UUID vs string is ignored when ignore_uuid_types=True"""
25 test_uuid = uuid.UUID('12345678-1234-5678-1234-567812345678')
26 uuid_str = '12345678-1234-5678-1234-567812345678'
27
28 result = DeepDiff(test_uuid, uuid_str, ignore_uuid_types=True)
29
30 assert result == {}
31
32 def test_string_vs_uuid_with_ignore(self):
33 """Test that string vs UUID is ignored when ignore_uuid_types=True (reverse order)"""
34 test_uuid = uuid.UUID('12345678-1234-5678-1234-567812345678')
35 uuid_str = '12345678-1234-5678-1234-567812345678'
36
37 result = DeepDiff(uuid_str, test_uuid, ignore_uuid_types=True)
38
39 assert result == {}
40
41 def test_different_uuid_values_with_ignore(self):
42 """Test that different UUID values are still reported"""
43 uuid1 = uuid.UUID('12345678-1234-5678-1234-567812345678')
44 uuid2 = uuid.UUID('87654321-4321-8765-4321-876543218765')
45
46 result = DeepDiff(uuid1, uuid2, ignore_uuid_types=True)
47
48 assert 'values_changed' in result
49 assert result['values_changed']['root']['old_value'] == uuid1
50 assert result['values_changed']['root']['new_value'] == uuid2
51
52 def test_uuid_vs_different_string_with_ignore(self):
53 """Test that UUID vs different UUID string reports value change"""
54 test_uuid = uuid.UUID('12345678-1234-5678-1234-567812345678')
55 different_str = '87654321-4321-8765-4321-876543218765'
56
57 result = DeepDiff(test_uuid, different_str, ignore_uuid_types=True)
58
59 assert 'values_changed' in result
60 assert result['values_changed']['root']['old_value'] == test_uuid
61 assert result['values_changed']['root']['new_value'] == different_str
62
63 def test_uuid_vs_invalid_string_with_ignore(self):
64 """Test that UUID vs invalid UUID string reports value change"""
65 test_uuid = uuid.UUID('12345678-1234-5678-1234-567812345678')
66 invalid_str = 'not-a-uuid'
67
68 result = DeepDiff(test_uuid, invalid_str, ignore_uuid_types=True)
69
70 assert 'values_changed' in result
71 assert result['values_changed']['root']['old_value'] == test_uuid
72 assert result['values_changed']['root']['new_value'] == invalid_str
73
74 def test_uuid_in_dict_with_ignore(self):
75 """Test that UUID vs string in dictionaries works correctly"""
76 test_uuid = uuid.UUID('12345678-1234-5678-1234-567812345678')
77 uuid_str = '12345678-1234-5678-1234-567812345678'
78
79 dict1 = {'id': test_uuid, 'name': 'test', 'count': 42}
80 dict2 = {'id': uuid_str, 'name': 'test', 'count': 42}
81
82 result = DeepDiff(dict1, dict2, ignore_uuid_types=True)
83
84 assert result == {}
85
86 def test_uuid_in_list_with_ignore(self):
87 """Test that UUID vs string in lists works correctly"""
88 test_uuid = uuid.UUID('12345678-1234-5678-1234-567812345678')
89 uuid_str = '12345678-1234-5678-1234-567812345678'
90
91 list1 = [test_uuid, 'test', 42]
92 list2 = [uuid_str, 'test', 42]
93
94 result = DeepDiff(list1, list2, ignore_uuid_types=True)
95
96 assert result == {}
97
98 def test_mixed_uuid_comparisons_with_ignore(self):
99 """Test mixed UUID/string comparisons in nested structures"""
100 uuid1 = uuid.UUID('12345678-1234-5678-1234-567812345678')
101 uuid2 = uuid.UUID('87654321-4321-8765-4321-876543218765')
102
103 data1 = {
104 'uuid_obj': uuid1,
105 'uuid_str': '12345678-1234-5678-1234-567812345678',
106 'nested': {
107 'id': uuid2,
108 'items': [uuid1, 'test']
109 }
110 }
111
112 data2 = {
113 'uuid_obj': '12345678-1234-5678-1234-567812345678', # string version
114 'uuid_str': uuid1, # UUID object version
115 'nested': {
116 'id': '87654321-4321-8765-4321-876543218765', # string version
117 'items': ['12345678-1234-5678-1234-567812345678', 'test'] # string version
118 }
119 }
120
121 result = DeepDiff(data1, data2, ignore_uuid_types=True)
122
123 assert result == {}
124
125 def test_uuid_with_other_ignore_flags(self):
126 """Test that ignore_uuid_types works with other ignore flags"""
127 test_uuid = uuid.UUID('12345678-1234-5678-1234-567812345678')
128
129 data1 = {
130 'id': test_uuid,
131 'name': 'TEST',
132 'count': 42
133 }
134
135 data2 = {
136 'id': '12345678-1234-5678-1234-567812345678',
137 'name': 'test', # different case
138 'count': 42.0 # different numeric type
139 }
140
141 result = DeepDiff(data1, data2,
142 ignore_uuid_types=True,
143 ignore_string_case=True,
144 ignore_numeric_type_changes=True)
145
146 assert result == {}
147
148
149 if __name__ == '__main__':
150 unittest.main()