lemur/lemur/tests/test_schemas.py

59 lines
1.7 KiB
Python
Raw Normal View History

import pytest
from marshmallow.exceptions import ValidationError
from lemur.tests.factories import RoleFactory
def test_get_object_attribute():
from lemur.schemas import get_object_attribute
with pytest.raises(ValidationError):
get_object_attribute({})
with pytest.raises(ValidationError):
get_object_attribute([{}], many=True)
with pytest.raises(ValidationError):
2019-05-16 16:57:02 +02:00
get_object_attribute([{}, {"id": 1}], many=True)
with pytest.raises(ValidationError):
2019-05-16 16:57:02 +02:00
get_object_attribute([{}, {"name": "test"}], many=True)
2019-05-16 16:57:02 +02:00
assert get_object_attribute({"name": "test"}) == "name"
assert get_object_attribute({"id": 1}) == "id"
assert get_object_attribute([{"name": "test"}], many=True) == "name"
assert get_object_attribute([{"id": 1}], many=True) == "id"
def test_fetch_objects(session):
from lemur.roles.models import Role
from lemur.schemas import fetch_objects
role = RoleFactory()
role1 = RoleFactory()
session.commit()
2019-05-16 16:57:02 +02:00
data = {"id": role.id}
found_role = fetch_objects(Role, data)
assert found_role == role
2019-05-16 16:57:02 +02:00
data = {"name": role.name}
found_role = fetch_objects(Role, data)
assert found_role == role
2019-05-16 16:57:02 +02:00
data = [{"id": role.id}, {"id": role1.id}]
found_roles = fetch_objects(Role, data, many=True)
assert found_roles == [role, role1]
2019-05-16 16:57:02 +02:00
data = [{"name": role.name}, {"name": role1.name}]
found_roles = fetch_objects(Role, data, many=True)
assert found_roles == [role, role1]
with pytest.raises(ValidationError):
2019-05-16 16:57:02 +02:00
data = [{"name": "blah"}, {"name": role1.name}]
fetch_objects(Role, data, many=True)
with pytest.raises(ValidationError):
2019-05-16 16:57:02 +02:00
data = {"name": "nah"}
fetch_objects(Role, data)