chore: Test cases for annotations and annotation layers incorrect creation through API (#17246)

* Add/Refactor tests

* Add return type

* Update api tests
This commit is contained in:
Geido 2021-11-03 11:11:41 +02:00 committed by GitHub
parent 93bafa0e6a
commit 2199ef2dd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 115 additions and 34 deletions

View File

@ -31,6 +31,8 @@ from tests.integration_tests.annotation_layers.fixtures import (
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 @@ class TestAnnotationLayerApi(SupersetTestCase):
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 @@ class TestAnnotationLayerApi(SupersetTestCase):
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 @@ class TestAnnotationLayerApi(SupersetTestCase):
)
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 @@ class TestAnnotationLayerApi(SupersetTestCase):
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 @@ class TestAnnotationLayerApi(SupersetTestCase):
)
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):

View File

@ -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 @@ from tests.integration_tests.test_app import app
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:

View File

@ -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)

View File

@ -25,7 +25,7 @@ from superset.annotation_layers.schemas import (
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 @@ from tests.integration_tests.annotation_layers.fixtures import (
)
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"})