File tree Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -25,8 +25,7 @@ def get_return(lookup, return_fields=None):
25
25
"""Returns simple representations for items passed to lookup.
26
26
27
27
Used to return a "simple" representation of objects and collections
28
- sent to it via lookup. If lookup is an IPNetwork object immediately
29
- return the string representation. Otherwise, we look to see if
28
+ sent to it via lookup. Otherwise, we look to see if
30
29
lookup is a "choices" field (dict with only 'id' and 'value')
31
30
or a nested_return. Finally, we check if it's a Record, if
32
31
so simply return a string. Order is important due to nested_return
@@ -41,6 +40,10 @@ def get_return(lookup, return_fields=None):
41
40
return lookup [i ]
42
41
else :
43
42
if hasattr (lookup , i ):
43
+ # check if this is a "choices" field record
44
+ # from a NetBox 2.7 server.
45
+ if sorted (dict (lookup )) == sorted (["id" , "value" , "label" ]):
46
+ return getattr (lookup , "value" )
44
47
return getattr (lookup , i )
45
48
46
49
if isinstance (lookup , Record ):
Original file line number Diff line number Diff line change @@ -112,6 +112,43 @@ def test_dict(self):
112
112
test = Record (test_values , None , None )
113
113
self .assertEqual (dict (test ), test_values )
114
114
115
+ def test_choices_idempotence_prev27 (self ):
116
+ test_values = {
117
+ "id" : 123 ,
118
+ "choices_test" : {
119
+ "value" : 1 ,
120
+ "label" : "test" ,
121
+ },
122
+ }
123
+ test = Record (test_values , None , None )
124
+ test .choices_test = 1
125
+ self .assertFalse (test ._diff ())
126
+
127
+ def test_choices_idempotence_v27 (self ):
128
+ test_values = {
129
+ "id" : 123 ,
130
+ "choices_test" : {
131
+ "value" : "test" ,
132
+ "label" : "test" ,
133
+ "id" : 1 ,
134
+ },
135
+ }
136
+ test = Record (test_values , None , None )
137
+ test .choices_test = "test"
138
+ self .assertFalse (test ._diff ())
139
+
140
+ def test_choices_idempotence_v28 (self ):
141
+ test_values = {
142
+ "id" : 123 ,
143
+ "choices_test" : {
144
+ "value" : "test" ,
145
+ "label" : "test" ,
146
+ },
147
+ }
148
+ test = Record (test_values , None , None )
149
+ test .choices_test = "test"
150
+ self .assertFalse (test ._diff ())
151
+
115
152
def test_hash (self ):
116
153
endpoint = Mock ()
117
154
endpoint .name = "test-endpoint"
You can’t perform that action at this time.
0 commit comments