def test_validate_measurement_wrong_object_id_type(): from sampledb.models.users import User, UserType from sampledb.models.actions import Action schema = {'title': 'Example', 'type': 'measurement'} user = User("User", "*****@*****.**", UserType.OTHER) action = Action(sampledb.models.ActionType.MEASUREMENT, "Example Action", schema={ "title": "Measurement Information", "type": "object", "properties": { "name": { "title": "Measurement Name", "type": "text" } }, "required": ["name"] }) sampledb.db.session.add(user) sampledb.db.session.add(action) sampledb.db.session.commit() object = create_object(data={'name': { '_type': 'text', 'text': 'example' }}, user_id=user.id, action_id=action.id) instance = {'_type': 'measurement', 'object_id': object} with pytest.raises(ValidationError): validate(instance, schema)
def test_validate_object_reference_invalid_type(): from sampledb.models.users import User, UserType from sampledb.models.actions import Action schema = {'title': 'Example', 'type': 'object_reference'} user = User("User", "*****@*****.**", UserType.OTHER) action = Action(sampledb.models.ActionType.SAMPLE_CREATION, "Example Action", schema={ "title": "Sample Information", "type": "object", "properties": { "name": { "title": "Sample Name", "type": "text" } }, 'required': ['name'] }) sampledb.db.session.add(user) sampledb.db.session.add(action) sampledb.db.session.commit() object_id = create_object( data={'name': { '_type': 'text', 'text': 'example' }}, user_id=user.id, action_id=action.id) instance = object_id with pytest.raises(ValidationError): validate(instance, schema)
def test_validate_sample(): from sampledb.models.users import User, UserType from sampledb.models.actions import Action schema = { 'title': 'Example', 'type': 'sample' } user = User("User", "*****@*****.**", UserType.OTHER) action = Action(sampledb.models.ActionType.SAMPLE_CREATION, "Example Action", schema={ "title": "Sample Information", "type": "object", "properties": { "name": { "title": "Sample Name", "type": "text" } }, 'required': ['name'] }) sampledb.db.session.add(user) sampledb.db.session.add(action) sampledb.db.session.commit() object = create_object(data={'name': {'_type': 'text', 'text': 'example'}}, user_id=user.id, action_id=action.id) instance = { '_type': 'sample', 'object_id': object.id } validate(instance, schema)
def test_validate_user_wrong_type(): from sampledb.models.users import User, UserType user = User("User", "*****@*****.**", UserType.OTHER) sampledb.db.session.add(user) sampledb.db.session.commit() schema = {'title': 'Example User', 'type': 'user'} instance = {'_type': 'user_id', 'user_id': user.id} with pytest.raises(ValidationError): validate(instance, schema)
def users(): users = [ User(name, '*****@*****.**', UserType.PERSON) for name in ['User 1', 'User 2', 'User 3'] ] for user in users: db.session.add(user) db.session.commit() assert user.id is not None db.session.expunge(user) return users
def test_validate_user(): from sampledb.models.users import User, UserType user = User("User", "*****@*****.**", UserType.OTHER) sampledb.db.session.add(user) sampledb.db.session.commit() schema = { 'title': 'Example User', 'type': 'user' } instance = { '_type': 'user', 'user_id': user.id } validate(instance, schema)
def test_validate_user_unexpected_keys(): from sampledb.models.users import User, UserType user = User("User", "*****@*****.**", UserType.OTHER) sampledb.db.session.add(user) sampledb.db.session.commit() schema = { 'title': 'Example User', 'type': 'user' } instance = { '_type': 'user', 'user_id': user.id, 'user_role': 'example' } with pytest.raises(ValidationError): validate(instance, schema)
def test_validate_object_reference_wrong_action(): from sampledb.models.users import User, UserType from sampledb.models.actions import Action user = User("User", "*****@*****.**", UserType.OTHER) action = Action(sampledb.models.ActionType.SAMPLE_CREATION, "Example Action", schema={ "title": "Sample Information", "type": "object", "properties": { "name": { "title": "Sample Name", "type": "text" } }, "required": ["name"] }) sampledb.db.session.add(user) sampledb.db.session.add(action) sampledb.db.session.commit() object = create_object(data={'name': {'_type': 'text', 'text': 'example'}}, user_id=user.id, action_id=action.id) instance = { '_type': 'object_reference', 'object_id': object.id } schema = { 'title': 'Example', 'type': 'object_reference', 'action_id': action.id } validate(instance, schema) schema = { 'title': 'Example', 'type': 'object_reference', 'action_id': action.id + 1 } with pytest.raises(ValidationError): validate(instance, schema)