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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
# isort:skip_file
"""Unit tests for Superset"""
import pytest
import prison
from sqlalchemy.sql import func
import tests.integration_tests.test_app # noqa: F401
from superset import db
from superset.utils import json
from superset.models.annotations import Annotation, AnnotationLayer
from tests.integration_tests.base_tests import SupersetTestCase
from tests.integration_tests.annotation_layers.fixtures import (
create_annotation_layers, # noqa: F401
get_end_dttm,
get_start_dttm,
)
from tests.unit_tests.annotation_layers.fixtures import (
START_STR,
END_STR,
)
from tests.integration_tests.constants import ADMIN_USERNAME
ANNOTATION_LAYERS_COUNT = 10
ANNOTATIONS_COUNT = 5
class TestAnnotationLayerApi(SupersetTestCase):
@staticmethod
def get_layer_with_annotation() -> AnnotationLayer:
return (
db.session.query(AnnotationLayer)
.filter(AnnotationLayer.name == "layer_with_annotations")
.one_or_none()
)
@pytest.mark.usefixtures("create_annotation_layers")
def test_get_annotation_layer(self):
"""
Annotation Api: Test get annotation layer
"""
annotation_layer = (
db.session.query(AnnotationLayer)
.filter(AnnotationLayer.name == "name1")
.first()
)
self.login(ADMIN_USERNAME)
uri = f"api/v1/annotation_layer/{annotation_layer.id}"
rv = self.get_assert_metric(uri, "get")
assert rv.status_code == 200
expected_result = {
"id": annotation_layer.id,
"name": "name1",
"descr": "descr1",
}
data = json.loads(rv.data.decode("utf-8"))
assert data["result"] == expected_result
def test_info_annotation(self):
"""
Annotation API: Test info
"""
self.login(ADMIN_USERNAME)
uri = "api/v1/annotation_layer/_info"
rv = self.get_assert_metric(uri, "info")
assert rv.status_code == 200
def test_info_security_query(self):
"""
Annotation API: Test info security
"""
self.login(ADMIN_USERNAME)
params = {"keys": ["permissions"]}
uri = f"api/v1/annotation_layer/_info?q={prison.dumps(params)}"
rv = self.get_assert_metric(uri, "info")
data = json.loads(rv.data.decode("utf-8"))
assert rv.status_code == 200
assert "can_read" in data["permissions"]
assert "can_write" in data["permissions"]
assert len(data["permissions"]) == 2
@pytest.mark.usefixtures("create_annotation_layers")
def test_get_annotation_layer_not_found(self):
"""
Annotation Api: Test get annotation layer not found
"""
max_id = db.session.query(func.max(AnnotationLayer.id)).scalar()
self.login(ADMIN_USERNAME)
uri = f"api/v1/annotation_layer/{max_id + 1}"
rv = self.get_assert_metric(uri, "get")
assert rv.status_code == 404
@pytest.mark.usefixtures("create_annotation_layers")
def test_get_list_annotation_layer(self):
"""
Annotation Api: Test get list annotation layers
"""
self.login(ADMIN_USERNAME)
uri = "api/v1/annotation_layer/"
rv = self.get_assert_metric(uri, "get_list")
expected_fields = [
"name",
"descr",
"created_by",
"created_on",
"changed_by",
"changed_on_delta_humanized",
"changed_on",
]
assert rv.status_code == 200
data = json.loads(rv.data.decode("utf-8"))
assert data["count"] == ANNOTATION_LAYERS_COUNT
for expected_field in expected_fields:
assert expected_field in data["result"][0]
@pytest.mark.usefixtures("create_annotation_layers")
def test_get_list_annotation_layer_sorting(self):
"""
Annotation Api: Test sorting on get list annotation layers
"""
self.login(ADMIN_USERNAME)
uri = "api/v1/annotation_layer/"
order_columns = [
"name",
"descr",
"created_by.first_name",
"changed_by.first_name",
"changed_on",
"changed_on_delta_humanized",
"created_on",
]
for order_column in order_columns:
arguments = {"order_column": order_column, "order_direction": "asc"}
uri = f"api/v1/annotation_layer/?q={prison.dumps(arguments)}"
rv = self.get_assert_metric(uri, "get_list")
assert rv.status_code == 200
@pytest.mark.usefixtures("create_annotation_layers")
def test_get_list_annotation_layer_filter(self):
"""
Annotation Api: Test filters on get list annotation layers
"""
self.login(ADMIN_USERNAME)
arguments = {
"columns": ["name", "descr"],
"filters": [
{"col": "name", "opr": "annotation_layer_all_text", "value": "2"}
],
}
uri = f"api/v1/annotation_layer/?q={prison.dumps(arguments)}"
rv = self.get_assert_metric(uri, "get_list")
expected_result = {
"name": "name2",
"descr": "descr2",
}
assert rv.status_code == 200
data = json.loads(rv.data.decode("utf-8"))
assert data["count"] == 1
assert data["result"][0] == expected_result
arguments = {
"columns": ["name", "descr"],
"filters": [
{"col": "name", "opr": "annotation_layer_all_text", "value": "descr3"}
],
}
uri = f"api/v1/annotation_layer/?q={prison.dumps(arguments)}"
rv = self.get_assert_metric(uri, "get_list")
expected_result = {
"name": "name3",
"descr": "descr3",
}
assert rv.status_code == 200
data = json.loads(rv.data.decode("utf-8"))
assert data["count"] == 1
assert data["result"][0] == expected_result
def test_create_annotation_layer(self):
"""
Annotation Api: Test create annotation layer
"""
self.login(ADMIN_USERNAME)
annotation_layer_data = {
"name": "new3",
"descr": "description",
}
uri = "api/v1/annotation_layer/"
rv = self.client.post(uri, json=annotation_layer_data)
assert rv.status_code == 201
data = json.loads(rv.data.decode("utf-8"))
created_model = db.session.query(AnnotationLayer).get(data.get("id"))
assert created_model is not None
assert created_model.name == annotation_layer_data["name"]
assert created_model.descr == annotation_layer_data["descr"]
# Rollback changes
db.session.delete(created_model)
db.session.commit()
def test_create_incorrect_annotation_layer(self):
"""
Annotation Api: Test create incorrect annotation layer
"""
self.login(ADMIN_USERNAME)
annotation_layer_data = {}
uri = "api/v1/annotation_layer/"
rv = self.client.post(uri, json=annotation_layer_data)
assert rv.status_code == 400
data = json.loads(rv.data.decode("utf-8"))
assert data == {"message": {"name": ["Missing data for required field."]}}
@pytest.mark.usefixtures("create_annotation_layers")
def test_create_annotation_layer_uniqueness(self):
"""
Annotation Api: Test create annotation layer uniqueness
"""
self.login(ADMIN_USERNAME)
annotation_layer_data = {"name": "name3", "descr": "description"}
uri = "api/v1/annotation_layer/"
rv = self.client.post(uri, json=annotation_layer_data)
assert rv.status_code == 422
data = json.loads(rv.data.decode("utf-8"))
assert data == {"message": {"name": ["Name must be unique"]}}
@pytest.mark.usefixtures("create_annotation_layers")
def test_update_annotation_layer(self):
"""
Annotation Api: Test update annotation layer
"""
annotation_layer = (
db.session.query(AnnotationLayer)
.filter(AnnotationLayer.name == "name2")
.one_or_none()
)
self.login(ADMIN_USERNAME)
annotation_layer_data = {"name": "changed_name"}
uri = f"api/v1/annotation_layer/{annotation_layer.id}"
rv = self.client.put(uri, json=annotation_layer_data)
assert rv.status_code == 200
updated_model = db.session.query(AnnotationLayer).get(annotation_layer.id)
assert updated_model is not None
assert updated_model.name == annotation_layer_data["name"]
# make sure the descr hasn't updated
assert updated_model.descr == annotation_layer.descr
@pytest.mark.usefixtures("create_annotation_layers")
def test_update_annotation_layer_uniqueness(self):
"""
Annotation Api: Test update annotation layer uniqueness
"""
annotation_layer = (
db.session.query(AnnotationLayer)
.filter(AnnotationLayer.name == "name2")
.one_or_none()
)
self.login(ADMIN_USERNAME)
annotation_layer_data = {"name": "name3", "descr": "changed_description"}
uri = f"api/v1/annotation_layer/{annotation_layer.id}"
rv = self.client.put(uri, json=annotation_layer_data)
data = json.loads(rv.data.decode("utf-8"))
assert rv.status_code == 422
assert data == {"message": {"name": ["Name must be unique"]}}
@pytest.mark.usefixtures("create_annotation_layers")
def test_update_annotation_layer_not_found(self):
"""
Annotation Api: Test update annotation layer not found
"""
max_id = db.session.query(func.max(AnnotationLayer.id)).scalar()
self.login(ADMIN_USERNAME)
annotation_layer_data = {"name": "changed_name", "descr": "changed_description"}
uri = f"api/v1/annotation_layer/{max_id + 1}"
rv = self.client.put(uri, json=annotation_layer_data)
assert rv.status_code == 404
@pytest.mark.usefixtures("create_annotation_layers")
def test_delete_annotation_layer(self):
"""
Annotation Api: Test update annotation layer
"""
annotation_layer = (
db.session.query(AnnotationLayer)
.filter(AnnotationLayer.name == "name1")
.one_or_none()
)
self.login(ADMIN_USERNAME)
uri = f"api/v1/annotation_layer/{annotation_layer.id}"
rv = self.client.delete(uri)
assert rv.status_code == 200
updated_model = db.session.query(AnnotationLayer).get(annotation_layer.id)
assert updated_model is None
@pytest.mark.usefixtures("create_annotation_layers")
def test_delete_annotation_layer_not_found(self):
"""
Annotation Api: Test delete annotation layer not found
"""
max_id = db.session.query(func.max(AnnotationLayer.id)).scalar()
self.login(ADMIN_USERNAME)
uri = f"api/v1/annotation_layer/{max_id + 1}"
rv = self.client.delete(uri)
assert rv.status_code == 404
@pytest.mark.usefixtures("create_annotation_layers")
def test_delete_annotation_layer_integrity(self):
"""
Annotation Api: Test delete annotation layer integrity error
"""
query_child_layer = db.session.query(AnnotationLayer).filter(
AnnotationLayer.name == "layer_with_annotations"
)
child_layer = query_child_layer.one_or_none()
self.login(ADMIN_USERNAME)
uri = f"api/v1/annotation_layer/{child_layer.id}"
rv = self.client.delete(uri)
assert rv.status_code == 422
@pytest.mark.usefixtures("create_annotation_layers")
def test_bulk_delete_annotation_layer(self):
"""
Annotation Api: Test bulk delete annotation layers
"""
query_no_child_layers = db.session.query(AnnotationLayer).filter(
AnnotationLayer.name.like("name%")
)
no_child_layers = query_no_child_layers.all()
no_child_layers_ids = [
annotation_layer.id for annotation_layer in no_child_layers
]
self.login(ADMIN_USERNAME)
uri = f"api/v1/annotation_layer/?q={prison.dumps(no_child_layers_ids)}"
rv = self.client.delete(uri)
assert rv.status_code == 200
deleted_annotation_layers = query_no_child_layers.all()
assert deleted_annotation_layers == []
response = json.loads(rv.data.decode("utf-8"))
expected_response = {
"message": f"Deleted {len(no_child_layers_ids)} annotation layers"
}
assert response == expected_response
@pytest.mark.usefixtures("create_annotation_layers")
def test_bulk_delete_annotation_layer_not_found(self):
"""
Annotation Api: Test bulk delete annotation layers not found
"""
all_annotation_layers = (
db.session.query(AnnotationLayer)
.filter(AnnotationLayer.name.like("name%"))
.all()
)
all_annotation_layers_ids = [
annotation_layer.id for annotation_layer in all_annotation_layers
]
max_id = db.session.query(func.max(AnnotationLayer.id)).scalar()
all_annotation_layers_ids.append(max_id + 1)
self.login(ADMIN_USERNAME)
uri = f"api/v1/annotation_layer/?q={prison.dumps(all_annotation_layers_ids)}"
rv = self.client.delete(uri)
assert rv.status_code == 404
@pytest.mark.usefixtures("create_annotation_layers")
def test_get_annotation(self):
"""
Annotation API: Test get annotation
"""
annotation_id = 1
annotation = (
db.session.query(Annotation)
.filter(Annotation.short_descr == f"short_descr{annotation_id}")
.one_or_none()
)
self.login(ADMIN_USERNAME)
uri = (
f"api/v1/annotation_layer/{annotation.layer_id}/annotation/{annotation.id}"
)
rv = self.get_assert_metric(uri, "get")
assert rv.status_code == 200
expected_result = {
"id": annotation.id,
"end_dttm": get_end_dttm(annotation_id).isoformat(),
"json_metadata": "",
"layer": {"id": annotation.layer_id, "name": "layer_with_annotations"},
"long_descr": annotation.long_descr,
"short_descr": annotation.short_descr,
"start_dttm": get_start_dttm(annotation_id).isoformat(),
}
data = json.loads(rv.data.decode("utf-8"))
assert data["result"] == expected_result
@pytest.mark.usefixtures("create_annotation_layers")
def test_get_annotation_not_found(self):
"""
Annotation API: Test get annotation not found
"""
layer = self.get_layer_with_annotation()
max_id = db.session.query(func.max(Annotation.id)).scalar()
self.login(ADMIN_USERNAME)
uri = f"api/v1/annotation_layer/{layer.id}/annotation/{max_id + 1}"
rv = self.get_assert_metric(uri, "get")
assert rv.status_code == 404
@pytest.mark.usefixtures("create_annotation_layers")
def test_get_list_annotation(self):
"""
Annotation Api: Test get list of annotations
"""
layer = self.get_layer_with_annotation()
self.login(ADMIN_USERNAME)
uri = f"api/v1/annotation_layer/{layer.id}/annotation/"
rv = self.get_assert_metric(uri, "get_list")
expected_fields = [
"short_descr",
"created_by",
"changed_by",
"start_dttm",
"end_dttm",
]
assert rv.status_code == 200
data = json.loads(rv.data.decode("utf-8"))
assert data["count"] == ANNOTATIONS_COUNT
for expected_field in expected_fields:
assert expected_field in data["result"][0]
@pytest.mark.usefixtures("create_annotation_layers")
def test_get_list_annotation_sorting(self):
"""
Annotation Api: Test sorting on get list of annotations
"""
layer = self.get_layer_with_annotation()
self.login(ADMIN_USERNAME)
order_columns = [
"short_descr",
"created_by.first_name",
"changed_by.first_name",
"changed_on_delta_humanized",
"start_dttm",
"end_dttm",
]
for order_column in order_columns:
arguments = {"order_column": order_column, "order_direction": "asc"}
uri = f"api/v1/annotation_layer/{layer.id}/annotation/?q={prison.dumps(arguments)}" # noqa: E501
rv = self.get_assert_metric(uri, "get_list")
assert rv.status_code == 200
@pytest.mark.usefixtures("create_annotation_layers")
def test_get_list_annotation_filter(self):
"""
Annotation Api: Test filters on get list annotation layers
"""
layer = self.get_layer_with_annotation()
self.login(ADMIN_USERNAME)
arguments = {
"filters": [
{"col": "short_descr", "opr": "annotation_all_text", "value": "2"}
]
}
uri = f"api/v1/annotation_layer/{layer.id}/annotation/?q={prison.dumps(arguments)}" # noqa: E501
rv = self.get_assert_metric(uri, "get_list")
assert rv.status_code == 200
data = json.loads(rv.data.decode("utf-8"))
assert data["count"] == 1
arguments = {
"filters": [
{"col": "short_descr", "opr": "annotation_all_text", "value": "descr3"}
]
}
uri = f"api/v1/annotation_layer/{layer.id}/annotation/?q={prison.dumps(arguments)}" # noqa: E501
rv = self.get_assert_metric(uri, "get_list")
assert rv.status_code == 200
data = json.loads(rv.data.decode("utf-8"))
assert data["count"] == 1
@pytest.mark.usefixtures("create_annotation_layers")
def test_create_annotation(self):
"""
Annotation Api: Test create annotation
"""
layer = self.get_layer_with_annotation()
self.login(ADMIN_USERNAME)
annotation_data = {
"short_descr": "new",
"long_descr": "description",
"start_dttm": START_STR,
"end_dttm": END_STR,
}
uri = f"api/v1/annotation_layer/{layer.id}/annotation/"
rv = self.client.post(uri, json=annotation_data)
assert rv.status_code == 201
data = json.loads(rv.data.decode("utf-8"))
created_model: Annotation = db.session.query(Annotation).get(data.get("id"))
assert created_model is not None
assert created_model.short_descr == annotation_data["short_descr"]
assert created_model.long_descr == annotation_data["long_descr"]
# Rollback changes
db.session.delete(created_model)
db.session.commit()
@pytest.mark.usefixtures("create_annotation_layers")
def test_create_incorrect_annotation(self):
"""
Annotation Api: Test create incorrect annotation
"""
layer = self.get_layer_with_annotation()
self.login(ADMIN_USERNAME)
annotation_data = {
"long_descr": "description",
}
uri = f"api/v1/annotation_layer/{layer.id}/annotation/"
rv = self.client.post(uri, json=annotation_data)
data = json.loads(rv.data.decode("utf-8"))
assert rv.status_code == 400
assert data == {
"message": {
"end_dttm": ["Missing data for required field."],
"short_descr": ["Missing data for required field."],
"start_dttm": ["Missing data for required field."],
}
}
@pytest.mark.usefixtures("create_annotation_layers")
def test_create_annotation_uniqueness(self):
"""
Annotation Api: Test create annotation uniqueness
"""
layer = self.get_layer_with_annotation()
self.login(ADMIN_USERNAME)
annotation_data = {
"short_descr": "short_descr2",
"long_descr": "description",
"start_dttm": START_STR,
"end_dttm": END_STR,
}
uri = f"api/v1/annotation_layer/{layer.id}/annotation/"
rv = self.client.post(uri, json=annotation_data)
assert rv.status_code == 422
data = json.loads(rv.data.decode("utf-8"))
assert data == {
"message": {
"short_descr": ["Short description must be unique for this layer"]
}
}
@pytest.mark.usefixtures("create_annotation_layers")
def test_update_annotation(self):
"""
Annotation Api: Test update annotation
"""
layer = self.get_layer_with_annotation()
annotation = (
db.session.query(Annotation)
.filter(Annotation.short_descr == "short_descr2")
.one_or_none()
)
self.login(ADMIN_USERNAME)
annotation_data = {
"short_descr": "changed_name",
}
uri = f"api/v1/annotation_layer/{layer.id}/annotation/{annotation.id}"
rv = self.client.put(uri, json=annotation_data)
assert rv.status_code == 200
updated_model: Annotation = db.session.query(Annotation).get(annotation.id)
assert updated_model is not None
assert updated_model.short_descr == annotation_data["short_descr"]
# make sure long_descr hasn't updated
assert updated_model.long_descr == annotation.long_descr
@pytest.mark.usefixtures("create_annotation_layers")
def test_update_annotation_null_datetime(self):
"""
Annotation Api: Test update annotation null datetime
"""
layer = self.get_layer_with_annotation()
annotation = (
db.session.query(Annotation)
.filter(Annotation.short_descr == "short_descr2")
.one_or_none()
)
self.login(ADMIN_USERNAME)
annotation_data = {"start_dttm": None, "end_dttm": None}
uri = f"api/v1/annotation_layer/{layer.id}/annotation/{annotation.id}"
rv = self.client.put(uri, json=annotation_data)
assert rv.status_code == 400
data = json.loads(rv.data.decode("utf-8"))
assert data == {
"message": {
"end_dttm": ["Field may not be null."],
"start_dttm": ["Field may not be null."],
}
}
@pytest.mark.usefixtures("create_annotation_layers")
def test_update_annotation_uniqueness(self):
"""
Annotation Api: Test update annotation uniqueness
"""
layer = self.get_layer_with_annotation()
annotation = (
db.session.query(Annotation)
.filter(Annotation.short_descr == "short_descr2")
.one_or_none()
)
self.login(ADMIN_USERNAME)
annotation_layer_data = {
"short_descr": "short_descr3",
"long_descr": "changed_description",
}
uri = f"api/v1/annotation_layer/{layer.id}/annotation/{annotation.id}"
rv = self.client.put(uri, json=annotation_layer_data)
assert rv.status_code == 422
data = json.loads(rv.data.decode("utf-8"))
assert data == {
"message": {
"short_descr": ["Short description must be unique for this layer"]
}
}
@pytest.mark.usefixtures("create_annotation_layers")
def test_update_annotation_not_found(self):
"""
Annotation Api: Test update annotation not found
"""
layer = self.get_layer_with_annotation()
max_id = db.session.query(func.max(Annotation.id)).scalar()
self.login(ADMIN_USERNAME)
annotation_layer_data = {
"short_descr": "changed_name",
}
uri = f"api/v1/annotation_layer/{layer.id}/annotation/{max_id + 1}"
rv = self.client.put(uri, json=annotation_layer_data)
assert rv.status_code == 404
@pytest.mark.usefixtures("create_annotation_layers")
def test_delete_annotation(self):
"""
Annotation Api: Test update annotation
"""
layer = self.get_layer_with_annotation()
annotation = (
db.session.query(Annotation)
.filter(Annotation.short_descr == "short_descr1")
.one_or_none()
)
self.login(ADMIN_USERNAME)
uri = f"api/v1/annotation_layer/{layer.id}/annotation/{annotation.id}"
rv = self.client.delete(uri)
assert rv.status_code == 200
updated_model = db.session.query(Annotation).get(annotation.id)
assert updated_model is None
@pytest.mark.usefixtures("create_annotation_layers")
def test_delete_annotation_not_found(self):
"""
Annotation Api: Test delete annotation not found
"""
layer = self.get_layer_with_annotation()
max_id = db.session.query(func.max(Annotation.id)).scalar()
self.login(ADMIN_USERNAME)
uri = f"api/v1/annotation_layer/{layer.id}/annotation{max_id + 1}"
rv = self.client.delete(uri)
assert rv.status_code == 404
@pytest.mark.usefixtures("create_annotation_layers")
def test_bulk_delete_annotation(self):
"""
Annotation Api: Test bulk delete annotation
"""
layer = self.get_layer_with_annotation()
query_annotations = db.session.query(Annotation).filter(
Annotation.layer == layer
)
annotations = query_annotations.all()
annotations_ids = [annotation.id for annotation in annotations]
self.login(ADMIN_USERNAME)
uri = f"api/v1/annotation_layer/{layer.id}/annotation/?q={prison.dumps(annotations_ids)}" # noqa: E501
rv = self.client.delete(uri)
assert rv.status_code == 200
deleted_annotations = query_annotations.all()
assert deleted_annotations == []
response = json.loads(rv.data.decode("utf-8"))
expected_response = {"message": f"Deleted {len(annotations_ids)} annotations"}
assert response == expected_response
@pytest.mark.usefixtures("create_annotation_layers")
def test_bulk_delete_annotation_not_found(self):
"""
Annotation Api: Test bulk delete annotation not found
"""
layer = self.get_layer_with_annotation()
query_annotations = db.session.query(Annotation).filter(
Annotation.layer == layer
)
annotations = query_annotations.all()
annotations_ids = [annotation.id for annotation in annotations]
max_id = db.session.query(func.max(Annotation.id)).scalar()
annotations_ids.append(max_id + 1)
self.login(ADMIN_USERNAME)
uri = f"api/v1/annotation_layer/{layer.id}/annotation/?q={prison.dumps(annotations_ids)}" # noqa: E501
rv = self.client.delete(uri)
assert rv.status_code == 404