From 2199ef2dd7c0093bab08b9b9042c7ff63750ff3a Mon Sep 17 00:00:00 2001 From: Geido <60598000+geido@users.noreply.github.com> Date: Wed, 3 Nov 2021 11:11:41 +0200 Subject: [PATCH] chore: Test cases for annotations and annotation layers incorrect creation through API (#17246) * Add/Refactor tests * Add return type * Update api tests --- .../annotation_layers/api_tests.py | 78 +++++++++++++++++-- .../annotation_layers/fixtures.py | 5 -- .../unit_tests/annotation_layers/fixtures.py | 24 ++++++ .../annotation_layers/schema_tests.py | 42 +++++----- 4 files changed, 115 insertions(+), 34 deletions(-) create mode 100644 tests/unit_tests/annotation_layers/fixtures.py rename tests/{integration_tests => unit_tests}/annotation_layers/schema_tests.py (76%) diff --git a/tests/integration_tests/annotation_layers/api_tests.py b/tests/integration_tests/annotation_layers/api_tests.py index 466bf7ec3f9b8..61f6b2ff67829 100644 --- a/tests/integration_tests/annotation_layers/api_tests.py +++ b/tests/integration_tests/annotation_layers/api_tests.py @@ -31,6 +31,8 @@ create_annotation_layers, get_end_dttm, get_start_dttm, +) +from tests.unit_tests.annotation_layers.fixtures import ( START_STR, END_STR, ) @@ -197,7 +199,6 @@ def test_get_list_annotation_layer_filter(self): assert data["count"] == 1 assert data["result"][0] == expected_result - @pytest.mark.usefixtures("create_annotation_layers") def test_create_annotation_layer(self): """ Annotation Api: Test create annotation layer @@ -220,6 +221,18 @@ def test_create_annotation_layer(self): db.session.delete(created_model) db.session.commit() + def test_create_incorrect_annotation_layer(self): + """ + Annotation Api: Test create incorrect annotation layer + """ + self.login(username="admin") + 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): """ @@ -245,14 +258,15 @@ def test_update_annotation_layer(self): ) self.login(username="admin") - annotation_layer_data = {"name": "changed_name", "descr": "changed_description"} + 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"] - assert updated_model.descr == annotation_layer_data["descr"] + # 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): @@ -521,6 +535,29 @@ def test_create_annotation(self): 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(username="admin") + 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): """ @@ -558,17 +595,42 @@ def test_update_annotation(self): ) self.login(username="admin") - annotation_layer_data = { + annotation_data = { "short_descr": "changed_name", - "long_descr": "changed_description", } uri = f"api/v1/annotation_layer/{layer.id}/annotation/{annotation.id}" - rv = self.client.put(uri, json=annotation_layer_data) + 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_layer_data["short_descr"] - assert updated_model.long_descr == annotation_layer_data["long_descr"] + 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(username="admin") + 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): diff --git a/tests/integration_tests/annotation_layers/fixtures.py b/tests/integration_tests/annotation_layers/fixtures.py index 2576cf4eda154..0b9e19e21fb70 100644 --- a/tests/integration_tests/annotation_layers/fixtures.py +++ b/tests/integration_tests/annotation_layers/fixtures.py @@ -16,7 +16,6 @@ # under the License. # isort:skip_file import pytest -import dateutil.parser from datetime import datetime from typing import Optional @@ -29,10 +28,6 @@ ANNOTATION_LAYERS_COUNT = 10 ANNOTATIONS_COUNT = 5 -START_STR = "2019-01-02T03:04:05.678900" -END_STR = "2020-01-02T03:04:05.678900" -START_DTTM = dateutil.parser.parse(START_STR) -END_DTTM = dateutil.parser.parse(END_STR) def get_start_dttm(annotation_id: int) -> datetime: diff --git a/tests/unit_tests/annotation_layers/fixtures.py b/tests/unit_tests/annotation_layers/fixtures.py new file mode 100644 index 0000000000000..fd07774fd7f9a --- /dev/null +++ b/tests/unit_tests/annotation_layers/fixtures.py @@ -0,0 +1,24 @@ +# 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 +import dateutil.parser + + +START_STR = "2019-01-02T03:04:05.678900" +END_STR = "2020-01-02T03:04:05.678900" +START_DTTM = dateutil.parser.parse(START_STR) +END_DTTM = dateutil.parser.parse(END_STR) diff --git a/tests/integration_tests/annotation_layers/schema_tests.py b/tests/unit_tests/annotation_layers/schema_tests.py similarity index 76% rename from tests/integration_tests/annotation_layers/schema_tests.py rename to tests/unit_tests/annotation_layers/schema_tests.py index ff7fc309dc9ac..b8efcbb0033f9 100644 --- a/tests/integration_tests/annotation_layers/schema_tests.py +++ b/tests/unit_tests/annotation_layers/schema_tests.py @@ -25,7 +25,7 @@ AnnotationLayerPostSchema, AnnotationLayerPutSchema, ) -from tests.integration_tests.annotation_layers.fixtures import ( +from tests.unit_tests.annotation_layers.fixtures import ( END_DTTM, END_STR, START_DTTM, @@ -33,44 +33,44 @@ ) -def test_annotation_layer_post_schema_with_name(): +def test_annotation_layer_post_schema_with_name() -> None: result = AnnotationLayerPostSchema().load({"name": "foo"}) assert result["name"] == "foo" assert "descr" not in result -def test_annotation_layer_post_schema_with_name_and_descr(): +def test_annotation_layer_post_schema_with_name_and_descr() -> None: result = AnnotationLayerPostSchema().load({"name": "foo", "descr": "bar"}) assert result["name"] == "foo" assert result["descr"] == "bar" -def test_annotation_layer_post_schema_with_null_name(): +def test_annotation_layer_post_schema_with_null_name() -> None: with pytest.raises(ValidationError): AnnotationLayerPostSchema().load({"name": None}) -def test_annotation_layer_post_schema_empty(): +def test_annotation_layer_post_schema_empty() -> None: with pytest.raises(ValidationError): AnnotationLayerPostSchema().load({}) -def test_annotation_layer_put_schema_empty(): +def test_annotation_layer_put_schema_empty() -> None: result = AnnotationLayerPutSchema().load({}) assert result == {} -def test_annotation_layer_put_schema_with_null_name(): +def test_annotation_layer_put_schema_with_null_name() -> None: with pytest.raises(ValidationError): AnnotationLayerPutSchema().load({"name": None}) -def test_annotation_layer_put_schema_with_null_descr(): +def test_annotation_layer_put_schema_with_null_descr() -> None: with pytest.raises(ValidationError): AnnotationLayerPutSchema().load({"descr": None}) -def test_annotation_post_schema_basic(): +def test_annotation_post_schema_basic() -> None: result = AnnotationPostSchema().load( {"short_descr": "foo", "start_dttm": START_STR, "end_dttm": END_STR} ) @@ -79,7 +79,7 @@ def test_annotation_post_schema_basic(): assert result["end_dttm"] == END_DTTM -def test_annotation_post_schema_full(): +def test_annotation_post_schema_full() -> None: result = AnnotationPostSchema().load( { "short_descr": "foo", @@ -96,62 +96,62 @@ def test_annotation_post_schema_full(): assert result["json_metadata"] == '{"abc": 123}' -def test_annotation_post_schema_short_descr_null(): +def test_annotation_post_schema_short_descr_null() -> None: with pytest.raises(ValidationError): AnnotationPostSchema().load( {"short_descr": None, "start_dttm": START_STR, "end_dttm": END_STR} ) -def test_annotation_post_schema_start_dttm_null(): +def test_annotation_post_schema_start_dttm_null() -> None: with pytest.raises(ValidationError): result = AnnotationPostSchema().load( {"short_descr": "foo", "start_dttm": None, "end_dttm": END_STR} ) -def test_annotation_post_schema_end_dttm_null(): +def test_annotation_post_schema_end_dttm_null() -> None: with pytest.raises(ValidationError): AnnotationPostSchema().load( {"short_descr": "foo", "start_dttm": START_STR, "end_dttm": None} ) -def test_annotation_put_schema_empty(): +def test_annotation_put_schema_empty() -> None: result = AnnotationPutSchema().load({}) assert result == {} -def test_annotation_put_schema_short_descr_null(): +def test_annotation_put_schema_short_descr_null() -> None: with pytest.raises(ValidationError): AnnotationPutSchema().load({"short_descr": None}) -def test_annotation_put_schema_start_dttm_null(): +def test_annotation_put_schema_start_dttm_null() -> None: with pytest.raises(ValidationError): AnnotationPutSchema().load({"start_dttm": None}) -def test_annotation_put_schema_end_dttm_null(): +def test_annotation_put_schema_end_dttm_null() -> None: with pytest.raises(ValidationError): AnnotationPutSchema().load({"end_dttm": None}) -def test_annotation_put_schema_json_metadata(): +def test_annotation_put_schema_json_metadata() -> None: result = AnnotationPutSchema().load({"json_metadata": '{"abc": 123}'}) assert result["json_metadata"] == '{"abc": 123}' -def test_annotation_put_schema_json_metadata_null(): +def test_annotation_put_schema_json_metadata_null() -> None: result = AnnotationPutSchema().load({"json_metadata": None}) assert result["json_metadata"] is None -def test_annotation_put_schema_json_metadata_empty(): +def test_annotation_put_schema_json_metadata_empty() -> None: result = AnnotationPutSchema().load({"json_metadata": ""}) assert result["json_metadata"] == "" -def test_annotation_put_schema_json_metadata_invalid(): +def test_annotation_put_schema_json_metadata_invalid() -> None: with pytest.raises(ValidationError): AnnotationPutSchema().load({"json_metadata": "foo bar"})