Skip to content

Commit

Permalink
chore: Test cases for annotations and annotation layers incorrect cre…
Browse files Browse the repository at this point in the history
…ation through API (#17246)

* Add/Refactor tests

* Add return type

* Update api tests
  • Loading branch information
geido authored and AAfghahi committed Jan 10, 2022
1 parent 3dab33c commit e0aab37
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 34 deletions.
78 changes: 70 additions & 8 deletions tests/integration_tests/annotation_layers/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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
Expand All @@ -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):
"""
Expand All @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 0 additions & 5 deletions tests/integration_tests/annotation_layers/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# under the License.
# isort:skip_file
import pytest
import dateutil.parser
from datetime import datetime
from typing import Optional

Expand All @@ -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:
Expand Down
24 changes: 24 additions & 0 deletions tests/unit_tests/annotation_layers/fixtures.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,52 @@
AnnotationLayerPostSchema,
AnnotationLayerPutSchema,
)
from tests.integration_tests.annotation_layers.fixtures import (
from tests.unit_tests.annotation_layers.fixtures import (
END_DTTM,
END_STR,
START_DTTM,
START_STR,
)


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}
)
Expand All @@ -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",
Expand All @@ -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"})

0 comments on commit e0aab37

Please sign in to comment.