8
8
9
9
def test_run_refresh (monkeypatch ):
10
10
_was_called = False
11
-
11
+
12
12
def mock_get_object (url , parms = None , default = None ):
13
13
nonlocal _was_called
14
14
assert url == "/livequery/v1/orgs/Z100/runs/abcdefg"
15
15
assert parms is None
16
16
assert default is None
17
17
_was_called = True
18
- return {"org_key" :"Z100" , "name" :"FoobieBletch" ,
19
- "id" :"abcdefg" , "status" :"COMPLETE" }
20
-
18
+ return {"org_key" : "Z100" , "name" : "FoobieBletch" ,
19
+ "id" : "abcdefg" , "status" : "COMPLETE" }
20
+
21
21
api = CbLiveQueryAPI (url = "https://example.com" , token = "ABCD/1234" ,
22
22
org_key = "Z100" , ssl_verify = True )
23
- run = Run (api , "abcdefg" , {"org_key" :"Z100" , "name" :"FoobieBletch" ,
24
- "id" :"abcdefg" , "status" :"ACTIVE" })
23
+ run = Run (api , "abcdefg" , {"org_key" : "Z100" , "name" : "FoobieBletch" ,
24
+ "id" : "abcdefg" , "status" : "ACTIVE" })
25
25
monkeypatch .setattr (api , "get_object" , mock_get_object )
26
26
monkeypatch .setattr (api , "post_object" , ConnectionMocks .get ("POST" ))
27
27
monkeypatch .setattr (api , "put_object" , ConnectionMocks .get ("PUT" ))
28
28
monkeypatch .setattr (api , "delete_object" , ConnectionMocks .get ("DELETE" ))
29
29
rc = run .refresh ()
30
30
assert _was_called
31
- assert rc == True
31
+ assert rc
32
32
assert run .org_key == "Z100"
33
33
assert run .name == "FoobieBletch"
34
34
assert run .id == "abcdefg"
35
35
assert run .status == "COMPLETE"
36
-
37
-
36
+
37
+
38
38
def test_run_stop (monkeypatch ):
39
39
_was_called = False
40
-
40
+
41
41
def mock_put_object (url , body , ** kwargs ):
42
42
nonlocal _was_called
43
43
assert url == "/livequery/v1/orgs/Z100/runs/abcdefg/status"
44
44
assert body ["status" ] == "CANCELLED"
45
45
_was_called = True
46
- return MockResponse ({"org_key" :"Z100" , "name" :"FoobieBletch" ,
47
- "id" :"abcdefg" , "status" :"CANCELLED" })
48
-
46
+ return MockResponse ({"org_key" : "Z100" , "name" : "FoobieBletch" ,
47
+ "id" : "abcdefg" , "status" : "CANCELLED" })
48
+
49
49
api = CbLiveQueryAPI (url = "https://example.com" , token = "ABCD/1234" ,
50
50
org_key = "Z100" , ssl_verify = True )
51
- run = Run (api , "abcdefg" , {"org_key" :"Z100" , "name" :"FoobieBletch" ,
52
- "id" :"abcdefg" , "status" :"ACTIVE" })
51
+ run = Run (api , "abcdefg" , {"org_key" : "Z100" , "name" : "FoobieBletch" ,
52
+ "id" : "abcdefg" , "status" : "ACTIVE" })
53
53
monkeypatch .setattr (api , "get_object" , ConnectionMocks .get ("GET" ))
54
54
monkeypatch .setattr (api , "post_object" , ConnectionMocks .get ("POST" ))
55
55
monkeypatch .setattr (api , "put_object" , mock_put_object )
56
56
monkeypatch .setattr (api , "delete_object" , ConnectionMocks .get ("DELETE" ))
57
57
rc = run .stop ()
58
58
assert _was_called
59
- assert rc == True
59
+ assert rc
60
60
assert run .org_key == "Z100"
61
61
assert run .name == "FoobieBletch"
62
62
assert run .id == "abcdefg"
63
63
assert run .status == "CANCELLED"
64
64
65
-
65
+
66
66
def test_run_stop_failed (monkeypatch ):
67
67
_was_called = False
68
-
68
+
69
69
def mock_put_object (url , body , ** kwargs ):
70
70
nonlocal _was_called
71
71
assert url == "/livequery/v1/orgs/Z100/runs/abcdefg/status"
72
72
assert body ["status" ] == "CANCELLED"
73
73
_was_called = True
74
- return MockResponse ({"error_message" :"The query is not presently running." }, 409 )
75
-
74
+ return MockResponse ({"error_message" : "The query is not presently running." }, 409 )
75
+
76
76
api = CbLiveQueryAPI (url = "https://example.com" , token = "ABCD/1234" ,
77
77
org_key = "Z100" , ssl_verify = True )
78
- run = Run (api , "abcdefg" , {"org_key" :"Z100" , "name" :"FoobieBletch" ,
79
- "id" :"abcdefg" , "status" :"CANCELLED" })
78
+ run = Run (api , "abcdefg" , {"org_key" : "Z100" , "name" : "FoobieBletch" ,
79
+ "id" : "abcdefg" , "status" : "CANCELLED" })
80
80
monkeypatch .setattr (api , "get_object" , ConnectionMocks .get ("GET" ))
81
81
monkeypatch .setattr (api , "post_object" , ConnectionMocks .get ("POST" ))
82
82
monkeypatch .setattr (api , "put_object" , mock_put_object )
83
83
monkeypatch .setattr (api , "delete_object" , ConnectionMocks .get ("DELETE" ))
84
84
rc = run .stop ()
85
85
assert _was_called
86
- assert rc == False
87
-
88
-
86
+ assert not rc
87
+
88
+
89
89
def test_run_delete (monkeypatch ):
90
90
_was_called = False
91
-
91
+
92
92
def mock_delete_object (url ):
93
93
nonlocal _was_called
94
94
if _was_called :
95
95
pytest .fail ("delete should not be called twice!" )
96
96
assert url == "/livequery/v1/orgs/Z100/runs/abcdefg"
97
97
_was_called = True
98
98
return MockResponse (None )
99
-
99
+
100
100
api = CbLiveQueryAPI (url = "https://example.com" , token = "ABCD/1234" ,
101
101
org_key = "Z100" , ssl_verify = True )
102
- run = Run (api , "abcdefg" , {"org_key" :"Z100" , "name" :"FoobieBletch" ,
103
- "id" :"abcdefg" , "status" :"ACTIVE" })
102
+ run = Run (api , "abcdefg" , {"org_key" : "Z100" , "name" : "FoobieBletch" ,
103
+ "id" : "abcdefg" , "status" : "ACTIVE" })
104
104
monkeypatch .setattr (api , "get_object" , ConnectionMocks .get ("GET" ))
105
105
monkeypatch .setattr (api , "post_object" , ConnectionMocks .get ("POST" ))
106
106
monkeypatch .setattr (api , "put_object" , ConnectionMocks .get ("PUT" ))
107
107
monkeypatch .setattr (api , "delete_object" , mock_delete_object )
108
108
rc = run .delete ()
109
109
assert _was_called
110
- assert rc == True
110
+ assert rc
111
111
assert run ._is_deleted
112
112
# Now ensure that certain operations that don't make sense on a deleted object raise ApiError
113
113
with pytest .raises (ApiError ):
@@ -116,31 +116,31 @@ def mock_delete_object(url):
116
116
run .stop ()
117
117
# And make sure that deleting a deleted object returns True immediately
118
118
rc = run .delete ()
119
- assert rc == True
120
-
121
-
119
+ assert rc
120
+
121
+
122
122
def test_run_delete_failed (monkeypatch ):
123
123
_was_called = False
124
-
124
+
125
125
def mock_delete_object (url ):
126
126
nonlocal _was_called
127
127
assert url == "/livequery/v1/orgs/Z100/runs/abcdefg"
128
128
_was_called = True
129
129
return MockResponse (None , 403 )
130
-
130
+
131
131
api = CbLiveQueryAPI (url = "https://example.com" , token = "ABCD/1234" ,
132
132
org_key = "Z100" , ssl_verify = True )
133
- run = Run (api , "abcdefg" , {"org_key" :"Z100" , "name" :"FoobieBletch" ,
134
- "id" :"abcdefg" , "status" :"ACTIVE" })
133
+ run = Run (api , "abcdefg" , {"org_key" : "Z100" , "name" : "FoobieBletch" ,
134
+ "id" : "abcdefg" , "status" : "ACTIVE" })
135
135
monkeypatch .setattr (api , "get_object" , ConnectionMocks .get ("GET" ))
136
136
monkeypatch .setattr (api , "post_object" , ConnectionMocks .get ("POST" ))
137
137
monkeypatch .setattr (api , "put_object" , ConnectionMocks .get ("PUT" ))
138
138
monkeypatch .setattr (api , "delete_object" , mock_delete_object )
139
139
rc = run .delete ()
140
140
assert _was_called
141
- assert rc == False
141
+ assert not rc
142
142
assert not run ._is_deleted
143
-
143
+
144
144
145
145
def test_result_device_summaries (monkeypatch ):
146
146
_was_called = False
@@ -155,22 +155,22 @@ def mock_post_object(url, body, **kwargs):
155
155
assert t ["field" ] == "device_name"
156
156
assert t ["order" ] == "ASC"
157
157
_was_called = True
158
- metrics = [{"key" :"aaa" , "value" :0.0 }, {"key" :"bbb" , "value" :0.0 }]
159
- res1 = {"id" :"ghijklm" , "total_results" :2 , "device_id" :314159 , "metrics" :metrics }
160
- res2 = {"id" :"mnopqrs" , "total_results" :3 , "device_id" :271828 , "metrics" :metrics }
161
- return MockResponse ({"org_key" :"Z100" , "num_found" :2 , "results" :[res1 , res2 ]})
162
-
158
+ metrics = [{"key" : "aaa" , "value" : 0.0 }, {"key" : "bbb" , "value" : 0.0 }]
159
+ res1 = {"id" : "ghijklm" , "total_results" : 2 , "device_id" : 314159 , "metrics" : metrics }
160
+ res2 = {"id" : "mnopqrs" , "total_results" : 3 , "device_id" : 271828 , "metrics" : metrics }
161
+ return MockResponse ({"org_key" : "Z100" , "num_found" : 2 , "results" : [res1 , res2 ]})
162
+
163
163
api = CbLiveQueryAPI (url = "https://example.com" , token = "ABCD/1234" ,
164
164
org_key = "Z100" , ssl_verify = True )
165
- tmp_id = {"id" :"abcdefg" }
166
- result = Result (api , {"id" : "abcdefg" , "device" :tmp_id , "fields" :{}, "metrics" :{}})
165
+ tmp_id = {"id" : "abcdefg" }
166
+ result = Result (api , {"id" : "abcdefg" , "device" : tmp_id , "fields" : {}, "metrics" : {}})
167
167
monkeypatch .setattr (api , "get_object" , ConnectionMocks .get ("GET" ))
168
168
monkeypatch .setattr (api , "post_object" , mock_post_object )
169
169
monkeypatch .setattr (api , "put_object" , ConnectionMocks .get ("PUT" ))
170
170
monkeypatch .setattr (api , "delete_object" , ConnectionMocks .get ("DELETE" ))
171
171
query = result .query_device_summaries ().where ("foo" ).criteria (device_name = ["AxCx" , "A7X" ])
172
172
query = query .sort_by ("device_name" )
173
- assert isinstance (query , ResultQuery )
173
+ assert isinstance (query , ResultQuery )
174
174
count = 0
175
175
for item in query .all ():
176
176
if item .id == "ghijklm" :
@@ -184,8 +184,8 @@ def mock_post_object(url, body, **kwargs):
184
184
count = count + 1
185
185
assert _was_called
186
186
assert count == 2
187
-
188
-
187
+
188
+
189
189
def test_result_query_result_facets (monkeypatch ):
190
190
_was_called = False
191
191
@@ -198,51 +198,51 @@ def mock_post_object(url, body, **kwargs):
198
198
t = body ["terms" ]
199
199
assert t ["fields" ] == ["alpha" , "bravo" , "charlie" ]
200
200
_was_called = True
201
- v1 = {"total" :1 , "id" :"alpha1" , "name" :"alpha1" }
202
- v2 = {"total" :2 , "id" :"alpha2" , "name" :"alpha2" }
203
- term1 = {"field" :"alpha" , "values" :[v1 , v2 ]}
204
- v1 = {"total" :1 , "id" :"bravo1" , "name" :"bravo1" }
205
- v2 = {"total" :2 , "id" :"bravo2" , "name" :"bravo2" }
206
- term2 = {"field" :"bravo" , "values" :[v1 , v2 ]}
207
- v1 = {"total" :1 , "id" :"charlie1" , "name" :"charlie1" }
208
- v2 = {"total" :2 , "id" :"charlie2" , "name" :"charlie2" }
209
- term3 = {"field" :"charlie" , "values" :[v1 , v2 ]}
210
- return MockResponse ({"terms" :[term1 , term2 , term3 ]})
211
-
201
+ v1 = {"total" : 1 , "id" : "alpha1" , "name" : "alpha1" }
202
+ v2 = {"total" : 2 , "id" : "alpha2" , "name" : "alpha2" }
203
+ term1 = {"field" : "alpha" , "values" : [v1 , v2 ]}
204
+ v1 = {"total" : 1 , "id" : "bravo1" , "name" : "bravo1" }
205
+ v2 = {"total" : 2 , "id" : "bravo2" , "name" : "bravo2" }
206
+ term2 = {"field" : "bravo" , "values" : [v1 , v2 ]}
207
+ v1 = {"total" : 1 , "id" : "charlie1" , "name" : "charlie1" }
208
+ v2 = {"total" : 2 , "id" : "charlie2" , "name" : "charlie2" }
209
+ term3 = {"field" : "charlie" , "values" : [v1 , v2 ]}
210
+ return MockResponse ({"terms" : [term1 , term2 , term3 ]})
211
+
212
212
api = CbLiveQueryAPI (url = "https://example.com" , token = "ABCD/1234" ,
213
213
org_key = "Z100" , ssl_verify = True )
214
- tmp_id = {"id" :"abcdefg" }
215
- result = Result (api , {"id" : "abcdefg" , "device" :tmp_id , "fields" :{}, "metrics" :{}})
214
+ tmp_id = {"id" : "abcdefg" }
215
+ result = Result (api , {"id" : "abcdefg" , "device" : tmp_id , "fields" : {}, "metrics" : {}})
216
216
monkeypatch .setattr (api , "get_object" , ConnectionMocks .get ("GET" ))
217
217
monkeypatch .setattr (api , "post_object" , mock_post_object )
218
218
monkeypatch .setattr (api , "put_object" , ConnectionMocks .get ("PUT" ))
219
219
monkeypatch .setattr (api , "delete_object" , ConnectionMocks .get ("DELETE" ))
220
220
query = result .query_result_facets ().where ("xyzzy" )
221
221
query = query .facet_field ("alpha" ).facet_field (["bravo" , "charlie" ])
222
- query = query .criteria (device_name = ["AxCx" , "A7X" ])
222
+ query = query .criteria (device_name = ["AxCx" , "A7X" ])
223
223
assert isinstance (query , FacetQuery )
224
224
count = 0
225
225
for item in query .all ():
226
226
vals = item .values
227
227
if item .field == "alpha" :
228
- assert vals [0 ]["id" ] == "alpha1"
229
- assert vals [1 ]["id" ] == "alpha2"
228
+ assert vals [0 ]["id" ] == "alpha1"
229
+ assert vals [1 ]["id" ] == "alpha2"
230
230
elif item .field == "bravo" :
231
- assert vals [0 ]["id" ] == "bravo1"
232
- assert vals [1 ]["id" ] == "bravo2"
231
+ assert vals [0 ]["id" ] == "bravo1"
232
+ assert vals [1 ]["id" ] == "bravo2"
233
233
elif item .field == "charlie" :
234
- assert vals [0 ]["id" ] == "charlie1"
235
- assert vals [1 ]["id" ] == "charlie2"
234
+ assert vals [0 ]["id" ] == "charlie1"
235
+ assert vals [1 ]["id" ] == "charlie2"
236
236
else :
237
- pytest .fail ("Unknown field name %s seen" % item .field )
237
+ pytest .fail ("Unknown field name %s seen" % item .field )
238
238
count = count + 1
239
239
assert _was_called
240
240
assert count == 3
241
-
242
-
241
+
242
+
243
243
def test_result_query_device_summary_facets (monkeypatch ):
244
244
_was_called = False
245
-
245
+
246
246
def mock_post_object (url , body , ** kwargs ):
247
247
nonlocal _was_called
248
248
assert url == "/livequery/v1/orgs/Z100/runs/abcdefg/results/device_summaries/_facet"
@@ -252,43 +252,43 @@ def mock_post_object(url, body, **kwargs):
252
252
t = body ["terms" ]
253
253
assert t ["fields" ] == ["alpha" , "bravo" , "charlie" ]
254
254
_was_called = True
255
- v1 = {"total" :1 , "id" :"alpha1" , "name" :"alpha1" }
256
- v2 = {"total" :2 , "id" :"alpha2" , "name" :"alpha2" }
257
- term1 = {"field" :"alpha" , "values" :[v1 , v2 ]}
258
- v1 = {"total" :1 , "id" :"bravo1" , "name" :"bravo1" }
259
- v2 = {"total" :2 , "id" :"bravo2" , "name" :"bravo2" }
260
- term2 = {"field" :"bravo" , "values" :[v1 , v2 ]}
261
- v1 = {"total" :1 , "id" :"charlie1" , "name" :"charlie1" }
262
- v2 = {"total" :2 , "id" :"charlie2" , "name" :"charlie2" }
263
- term3 = {"field" :"charlie" , "values" :[v1 , v2 ]}
264
- return MockResponse ({"terms" :[term1 , term2 , term3 ]})
265
-
255
+ v1 = {"total" : 1 , "id" : "alpha1" , "name" : "alpha1" }
256
+ v2 = {"total" : 2 , "id" : "alpha2" , "name" : "alpha2" }
257
+ term1 = {"field" : "alpha" , "values" : [v1 , v2 ]}
258
+ v1 = {"total" : 1 , "id" : "bravo1" , "name" : "bravo1" }
259
+ v2 = {"total" : 2 , "id" : "bravo2" , "name" : "bravo2" }
260
+ term2 = {"field" : "bravo" , "values" : [v1 , v2 ]}
261
+ v1 = {"total" : 1 , "id" : "charlie1" , "name" : "charlie1" }
262
+ v2 = {"total" : 2 , "id" : "charlie2" , "name" : "charlie2" }
263
+ term3 = {"field" : "charlie" , "values" : [v1 , v2 ]}
264
+ return MockResponse ({"terms" : [term1 , term2 , term3 ]})
265
+
266
266
api = CbLiveQueryAPI (url = "https://example.com" , token = "ABCD/1234" ,
267
267
org_key = "Z100" , ssl_verify = True )
268
- tmp_id = {"id" :"abcdefg" }
269
- result = Result (api , {"id" : "abcdefg" , "device" :tmp_id , "fields" :{}, "metrics" :{}})
268
+ tmp_id = {"id" : "abcdefg" }
269
+ result = Result (api , {"id" : "abcdefg" , "device" : tmp_id , "fields" : {}, "metrics" : {}})
270
270
monkeypatch .setattr (api , "get_object" , ConnectionMocks .get ("GET" ))
271
271
monkeypatch .setattr (api , "post_object" , mock_post_object )
272
272
monkeypatch .setattr (api , "put_object" , ConnectionMocks .get ("PUT" ))
273
273
monkeypatch .setattr (api , "delete_object" , ConnectionMocks .get ("DELETE" ))
274
274
query = result .query_device_summary_facets ().where ("xyzzy" )
275
275
query = query .facet_field ("alpha" ).facet_field (["bravo" , "charlie" ])
276
- query = query .criteria (device_name = ["AxCx" , "A7X" ])
276
+ query = query .criteria (device_name = ["AxCx" , "A7X" ])
277
277
assert isinstance (query , FacetQuery )
278
278
count = 0
279
279
for item in query .all ():
280
280
vals = item .values
281
281
if item .field == "alpha" :
282
- assert vals [0 ]["id" ] == "alpha1"
283
- assert vals [1 ]["id" ] == "alpha2"
282
+ assert vals [0 ]["id" ] == "alpha1"
283
+ assert vals [1 ]["id" ] == "alpha2"
284
284
elif item .field == "bravo" :
285
- assert vals [0 ]["id" ] == "bravo1"
286
- assert vals [1 ]["id" ] == "bravo2"
285
+ assert vals [0 ]["id" ] == "bravo1"
286
+ assert vals [1 ]["id" ] == "bravo2"
287
287
elif item .field == "charlie" :
288
- assert vals [0 ]["id" ] == "charlie1"
289
- assert vals [1 ]["id" ] == "charlie2"
288
+ assert vals [0 ]["id" ] == "charlie1"
289
+ assert vals [1 ]["id" ] == "charlie2"
290
290
else :
291
- pytest .fail ("Unknown field name %s seen" % item .field )
291
+ pytest .fail ("Unknown field name %s seen" % item .field )
292
292
count = count + 1
293
293
assert _was_called
294
294
assert count == 3
0 commit comments