2016-10-16 12:56:13 +02:00
|
|
|
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)
|
2016-10-16 12:56:13 +02:00
|
|
|
|
|
|
|
with pytest.raises(ValidationError):
|
2019-05-16 16:57:02 +02:00
|
|
|
get_object_attribute([{}, {"name": "test"}], many=True)
|
2016-10-16 12:56:13 +02:00
|
|
|
|
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"
|
2016-10-16 12:56:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
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}
|
2016-10-16 12:56:13 +02:00
|
|
|
found_role = fetch_objects(Role, data)
|
|
|
|
assert found_role == role
|
|
|
|
|
2019-05-16 16:57:02 +02:00
|
|
|
data = {"name": role.name}
|
2016-10-16 12:56:13 +02:00
|
|
|
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}]
|
2016-10-16 12:56:13 +02:00
|
|
|
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}]
|
2016-10-16 12:56:13 +02:00
|
|
|
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}]
|
2016-10-16 12:56:13 +02:00
|
|
|
fetch_objects(Role, data, many=True)
|
|
|
|
|
|
|
|
with pytest.raises(ValidationError):
|
2019-05-16 16:57:02 +02:00
|
|
|
data = {"name": "nah"}
|
2016-10-16 12:56:13 +02:00
|
|
|
fetch_objects(Role, data)
|