-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest_query_api.py
More file actions
642 lines (527 loc) · 19.3 KB
/
Copy pathtest_query_api.py
File metadata and controls
642 lines (527 loc) · 19.3 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#
# Copyright (c) 2015-2026 LabKey Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import json
import pytest
from labkey.query import (
delete_rows,
update_rows,
insert_rows,
save_rows,
select_rows,
execute_sql,
get_queries,
QueryFilter,
)
from labkey.exceptions import (
RequestError,
QueryNotFoundError,
ServerNotFoundError,
RequestAuthorizationError,
)
from labkey.utils import waf_encode
from .utilities import MockLabKey, mock_server_context, success_test, throws_error_test
class MockSelectRows(MockLabKey):
api = "getQuery.api"
default_success_body = '{"columnModel": [{"align": "right", "dataIndex": "Participant ID", "editable": true , "header": "Participant ID", "hidden": false , "required": false , "scale": 10 , "sortable": true , "width": 60 }] , "formatVersion": 8.3 , "metaData": {"description": null , "fields": [{"autoIncrement": false , "calculated": false , "caption": "Participant ID", "conceptURI": null , "defaultScale": "LINEAR", "defaultValue": null , "dimension": false , "excludeFromShifting": false , "ext": {} , "facetingBehaviorType": "AUTOMATIC", "fieldKey": "Participant ID", "fieldKeyArray": ["Participant ID"] , "fieldKeyPath": "Participant ID", "friendlyType": "Integer", "hidden": false , "inputType": "text", "isAutoIncrement": false , "isHidden": false , "isKeyField": false , "isMvEnabled": false , "isNullable": true , "isReadOnly": false , "isSelectable": true , "isUserEditable": true , "isVersionField": false , "jsonType": "int", "keyField": false , "measure": false , "mvEnabled": false , "name": "Participant ID", "nullable": true , "protected": false , "rangeURI": "http://www.w3.org/2001/XMLSchema#int", "readOnly": false , "recommendedVariable": false , "required": false , "selectable": true , "shortCaption": "Participant ID", "shownInDetailsView": true , "shownInInsertView": true , "shownInUpdateView": true , "sqlType": "int", "type": "int", "userEditable": true , "versionField": false }] , "id": "Key", "importMessage": null , "importTemplates": [{"label": "Download Template", "url": ""}] , "root": "rows", "title": "Demographics", "totalProperty": "rowCount"} , "queryName": "Demographics", "rowCount": 224 , "rows": [{ "Participant ID": 133428 } , { "Participant ID": 138488 } , { "Participant ID": 140163 } , { "Participant ID": 144740 } , { "Participant ID": 150489 } ] , "schemaName": "lists"}'
class MockInsertRows(MockLabKey):
api = "insertRows.api"
class MockDeleteRows(MockLabKey):
api = "deleteRows.api"
class MockExecuteSQL(MockLabKey):
api = "executeSql.api"
class MockUpdateRows(MockLabKey):
api = "updateRows.api"
class MockSaveRows(MockLabKey):
api = "saveRows.api"
class MockGetQueries(MockLabKey):
api = "getQueries.api"
schema = "testSchema"
query = "testQuery"
@pytest.fixture
def delete_rows_setup():
service = MockDeleteRows()
rows = "{id:1234}"
expected_kwargs = {
"expected_args": [service.get_server_url()],
"data": '{"queryName": "'
+ query
+ '", "rows": "'
+ rows
+ '", "schemaName": "'
+ schema
+ '"}',
"headers": {"Content-Type": "application/json"},
"timeout": 300,
"allow_redirects": False,
}
args = [mock_server_context(service), schema, query, rows]
return service, args, expected_kwargs
def test_delete_rows_success(delete_rows_setup):
service, args, expected_kwargs = delete_rows_setup
success_test(service.get_successful_response(), delete_rows, True, *args, **expected_kwargs)
def test_delete_rows_unauthorized(delete_rows_setup):
service, args, expected_kwargs = delete_rows_setup
throws_error_test(
RequestAuthorizationError,
service.get_unauthorized_response(),
delete_rows,
*args,
**expected_kwargs,
)
def test_delete_rows_query_not_found(delete_rows_setup):
service, args, expected_kwargs = delete_rows_setup
throws_error_test(
QueryNotFoundError,
service.get_query_not_found_response(),
delete_rows,
*args,
**expected_kwargs,
)
def test_delete_rows_server_not_found(delete_rows_setup):
service, args, expected_kwargs = delete_rows_setup
throws_error_test(
ServerNotFoundError,
service.get_server_not_found_response(),
delete_rows,
*args,
**expected_kwargs,
)
def test_delete_rows_general_error(delete_rows_setup):
service, args, expected_kwargs = delete_rows_setup
throws_error_test(
RequestError, service.get_general_error_response(), delete_rows, *args, **expected_kwargs
)
@pytest.fixture
def update_rows_setup():
service = MockUpdateRows()
rows = "{id:1234}"
expected_kwargs = {
"expected_args": [service.get_server_url()],
"data": '{"queryName": "'
+ query
+ '", "rows": "'
+ rows
+ '", "schemaName": "'
+ schema
+ '"}',
"headers": {"Content-Type": "application/json"},
"timeout": 300,
"allow_redirects": False,
}
args = [mock_server_context(service), schema, query, rows]
return service, args, expected_kwargs
def test_update_rows_success(update_rows_setup):
service, args, expected_kwargs = update_rows_setup
success_test(service.get_successful_response(), update_rows, True, *args, **expected_kwargs)
def test_update_rows_unauthorized(update_rows_setup):
service, args, expected_kwargs = update_rows_setup
throws_error_test(
RequestAuthorizationError,
service.get_unauthorized_response(),
update_rows,
*args,
**expected_kwargs,
)
def test_update_rows_query_not_found(update_rows_setup):
service, args, expected_kwargs = update_rows_setup
throws_error_test(
QueryNotFoundError,
service.get_query_not_found_response(),
update_rows,
*args,
**expected_kwargs,
)
def test_update_rows_server_not_found(update_rows_setup):
service, args, expected_kwargs = update_rows_setup
throws_error_test(
ServerNotFoundError,
service.get_server_not_found_response(),
update_rows,
*args,
**expected_kwargs,
)
def test_update_rows_general_error(update_rows_setup):
service, args, expected_kwargs = update_rows_setup
throws_error_test(
RequestError, service.get_general_error_response(), update_rows, *args, **expected_kwargs
)
@pytest.fixture
def insert_rows_setup():
service = MockInsertRows()
rows = "{id:1234}"
expected_kwargs = {
"expected_args": [service.get_server_url()],
"data": '{"queryName": "'
+ query
+ '", "rows": "'
+ rows
+ '", "schemaName": "'
+ schema
+ '"}',
"headers": {"Content-Type": "application/json"},
"timeout": 300,
"allow_redirects": False,
}
args = [mock_server_context(service), schema, query, rows]
return service, args, expected_kwargs
def test_insert_rows_success(insert_rows_setup):
service, args, expected_kwargs = insert_rows_setup
success_test(service.get_successful_response(), insert_rows, True, *args, **expected_kwargs)
def test_insert_rows_unauthorized(insert_rows_setup):
service, args, expected_kwargs = insert_rows_setup
throws_error_test(
RequestAuthorizationError,
service.get_unauthorized_response(),
insert_rows,
*args,
**expected_kwargs,
)
def test_insert_rows_query_not_found(insert_rows_setup):
service, args, expected_kwargs = insert_rows_setup
throws_error_test(
QueryNotFoundError,
service.get_query_not_found_response(),
insert_rows,
*args,
**expected_kwargs,
)
def test_insert_rows_server_not_found(insert_rows_setup):
service, args, expected_kwargs = insert_rows_setup
throws_error_test(
ServerNotFoundError,
service.get_server_not_found_response(),
insert_rows,
*args,
**expected_kwargs,
)
def test_insert_rows_general_error(insert_rows_setup):
service, args, expected_kwargs = insert_rows_setup
throws_error_test(
RequestError, service.get_general_error_response(), insert_rows, *args, **expected_kwargs
)
@pytest.fixture
def execute_sql_setup():
service = MockExecuteSQL()
sql = "select * from " + schema + "." + query
expected_kwargs = {
"expected_args": [service.get_server_url()],
"data": {"sql": waf_encode(sql), "schemaName": schema},
"headers": None,
"timeout": 300,
"allow_redirects": False,
}
args = [mock_server_context(service), schema, sql]
return service, args, expected_kwargs
def test_execute_sql_success(execute_sql_setup):
service, args, expected_kwargs = execute_sql_setup
success_test(service.get_successful_response(), execute_sql, True, *args, **expected_kwargs)
def test_execute_sql_unauthorized(execute_sql_setup):
service, args, expected_kwargs = execute_sql_setup
throws_error_test(
RequestAuthorizationError,
service.get_unauthorized_response(),
execute_sql,
*args,
**expected_kwargs,
)
def test_execute_sql_query_not_found(execute_sql_setup):
service, args, expected_kwargs = execute_sql_setup
throws_error_test(
QueryNotFoundError,
service.get_query_not_found_response(),
execute_sql,
*args,
**expected_kwargs,
)
def test_execute_sql_server_not_found(execute_sql_setup):
service, args, expected_kwargs = execute_sql_setup
throws_error_test(
ServerNotFoundError,
service.get_server_not_found_response(),
execute_sql,
*args,
**expected_kwargs,
)
def test_execute_sql_general_error(execute_sql_setup):
service, args, expected_kwargs = execute_sql_setup
throws_error_test(
RequestError, service.get_general_error_response(), execute_sql, *args, **expected_kwargs
)
@pytest.fixture
def select_rows_setup():
service = MockSelectRows()
expected_kwargs = {
"expected_args": [service.get_server_url()],
"data": {"schemaName": schema, "query.queryName": query, "query.maxRows": -1},
"headers": None,
"timeout": 300,
"allow_redirects": False,
}
args = [mock_server_context(service), schema, query]
return service, args, expected_kwargs
def test_select_rows_success(select_rows_setup):
service, args, expected_kwargs = select_rows_setup
success_test(service.get_successful_response(), select_rows, True, *args, **expected_kwargs)
def test_select_rows_query_filter(select_rows_setup):
service, args, expected_kwargs = select_rows_setup
view_name = None
filter_array = [
QueryFilter("Field1", "value", "eq"),
QueryFilter("Field2", "value1", "contains"),
QueryFilter("Field2", "value2", "contains"),
]
test_args = list(args) + [view_name, filter_array]
# Expected query field values in post request body
query_field = {
"query.Field1~eq": ["value"],
"query.Field2~contains": ["value1", "value2"],
}
# Update post request body with expected query field values
expected_kwargs["data"].update(query_field)
success_test(
service.get_successful_response(),
select_rows,
True,
*test_args,
**expected_kwargs,
)
def test_select_rows_unauthorized(select_rows_setup):
service, args, expected_kwargs = select_rows_setup
throws_error_test(
RequestAuthorizationError,
service.get_unauthorized_response(),
select_rows,
*args,
**expected_kwargs,
)
def test_select_rows_query_not_found(select_rows_setup):
service, args, expected_kwargs = select_rows_setup
throws_error_test(
QueryNotFoundError,
service.get_query_not_found_response(),
select_rows,
*args,
**expected_kwargs,
)
def test_select_rows_server_not_found(select_rows_setup):
service, args, expected_kwargs = select_rows_setup
throws_error_test(
ServerNotFoundError,
service.get_server_not_found_response(),
select_rows,
*args,
**expected_kwargs,
)
def test_select_rows_general_error(select_rows_setup):
service, args, expected_kwargs = select_rows_setup
throws_error_test(
RequestError, service.get_general_error_response(), select_rows, *args, **expected_kwargs
)
@pytest.fixture
def save_rows_setup():
service = MockSaveRows()
commands = [
{
"command": "insert",
"schema_name": "exp",
"query_name": "materials",
"rows": [{"name": "New Sample 1"}],
}
]
expected_payload = {
"commands": [
{
"command": "insert",
"schemaName": "exp",
"queryName": "materials",
"rows": [{"name": "New Sample 1"}],
}
]
}
expected_kwargs = {
"expected_args": [service.get_server_url()],
"data": json.dumps(expected_payload, sort_keys=True),
"headers": {"Content-Type": "application/json"},
"timeout": 300,
"allow_redirects": False,
}
args = [mock_server_context(service), commands]
return service, args, expected_kwargs
def test_save_rows_success(save_rows_setup):
service, args, expected_kwargs = save_rows_setup
success_test(service.get_successful_response(), save_rows, True, *args, **expected_kwargs)
def test_save_rows_unauthorized(save_rows_setup):
service, args, expected_kwargs = save_rows_setup
throws_error_test(
RequestAuthorizationError,
service.get_unauthorized_response(),
save_rows,
*args,
**expected_kwargs,
)
def test_save_rows_query_not_found(save_rows_setup):
service, args, expected_kwargs = save_rows_setup
throws_error_test(
QueryNotFoundError,
service.get_query_not_found_response(),
save_rows,
*args,
**expected_kwargs,
)
def test_save_rows_server_not_found(save_rows_setup):
service, args, expected_kwargs = save_rows_setup
throws_error_test(
ServerNotFoundError,
service.get_server_not_found_response(),
save_rows,
*args,
**expected_kwargs,
)
def test_save_rows_general_error(save_rows_setup):
service, args, expected_kwargs = save_rows_setup
throws_error_test(
RequestError, service.get_general_error_response(), save_rows, *args, **expected_kwargs
)
def test_save_rows_with_optional_command_fields(save_rows_setup):
service, _, _ = save_rows_setup
"""Test save_rows with all optional fields populated in command"""
commands = [
{
"command": "update",
"schema_name": "exp",
"query_name": "materials",
"rows": [{"rowId": 1, "name": "Updated Sample"}],
"audit_behavior": "DETAILED",
"audit_user_comment": "Test update comment",
"container_path": "/custom/path",
"extra_context": {"custom": "data"},
"skip_reselect_rows": True,
}
]
expected_payload = {
"commands": [
{
"command": "update",
"schemaName": "exp",
"queryName": "materials",
"rows": [{"rowId": 1, "name": "Updated Sample"}],
"auditBehavior": "DETAILED",
"auditUserComment": "Test update comment",
"containerPath": "/custom/path",
"extraContext": {"custom": "data"},
"skipReselectRows": True,
}
]
}
expected_kwargs = {
"expected_args": [service.get_server_url()],
"data": json.dumps(expected_payload, sort_keys=True),
"headers": {"Content-Type": "application/json"},
"timeout": 300,
"allow_redirects": False,
}
args = [mock_server_context(service), commands]
success_test(service.get_successful_response(), save_rows, True, *args, **expected_kwargs)
def test_save_rows_with_optional_parameters(save_rows_setup):
service, _, _ = save_rows_setup
api_version = 18.21
commands = [
{
"command": "delete",
"schema_name": "exp",
"query_name": "materials",
"rows": [{"rowId": 1}],
}
]
extra_context = {"global": "context"}
transacted = False
timeout = 600
validate_only = True
expected_payload = {
"commands": [
{
"command": "delete",
"schemaName": "exp",
"queryName": "materials",
"rows": [{"rowId": 1}],
}
],
"apiVersion": api_version,
"extraContext": extra_context,
"transacted": transacted,
"validateOnly": True,
}
expected_kwargs = {
"expected_args": [service.get_server_url()],
"data": json.dumps(expected_payload, sort_keys=True),
"headers": {"Content-Type": "application/json"},
"timeout": timeout,
"allow_redirects": False,
}
args = [
mock_server_context(service),
commands,
api_version,
None, # container_path
extra_context,
timeout,
transacted,
validate_only,
]
success_test(service.get_successful_response(), save_rows, True, *args, **expected_kwargs)
@pytest.fixture
def get_queries_setup():
service = MockGetQueries()
expected_kwargs = {
"expected_args": [service.get_server_url()],
"data": {"schemaName": schema},
"headers": None,
"timeout": 300,
"allow_redirects": False,
}
args = [mock_server_context(service), schema]
return service, args, expected_kwargs
def test_get_queries_success(get_queries_setup):
service, args, expected_kwargs = get_queries_setup
success_test(service.get_successful_response(), get_queries, True, *args, **expected_kwargs)
def test_get_queries_camel_case(get_queries_setup):
service, args, expected_kwargs = get_queries_setup
options = {
"include_columns": True,
"include_system_queries": False,
"include_title": True,
"include_user_queries": False,
"include_view_data_url": True,
"query_detail_columns": False,
}
expected_kwargs["data"].update(
{
"includeColumns": True,
"includeSystemQueries": False,
"includeTitle": True,
"includeUserQueries": False,
"includeViewDataUrl": True,
"queryDetailColumns": False,
}
)
# We use a lambda below because get_queries uses kwargs, but success_test doesn't have a way to pass kwargs
success_test(
service.get_successful_response(),
lambda *a: get_queries(*a, **options),
True,
*args,
**expected_kwargs,
)