def test_object_ids_for_location(user: User, action: Action): data = {'name': {'_type': 'text', 'text': 'Object'}} object1 = objects.create_object(user_id=user.id, action_id=action.id, data=data) object2 = objects.create_object(user_id=user.id, action_id=action.id, data=data) object3 = objects.create_object(user_id=user.id, action_id=action.id, data=data) location1 = locations.create_location("Location", "This is an example location", None, user.id) location2 = locations.create_location("Location", "This is an example location", None, user.id) assert locations.get_object_ids_at_location(location1.id) == set() assert locations.get_object_ids_at_location(location2.id) == set() locations.assign_location_to_object(object1.id, location1.id, None, user.id, "") assert locations.get_object_ids_at_location(location1.id) == {object1.id} assert locations.get_object_ids_at_location(location2.id) == set() locations.assign_location_to_object(object2.id, location1.id, None, user.id, "") assert locations.get_object_ids_at_location( location1.id) == {object1.id, object2.id} assert locations.get_object_ids_at_location(location2.id) == set() locations.assign_location_to_object(object3.id, location2.id, user.id, user.id, "") assert locations.get_object_ids_at_location( location1.id) == {object1.id, object2.id} assert locations.get_object_ids_at_location(location2.id) == {object3.id}
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_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_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 post(self): if flask.g.user.is_readonly: return {'message': 'user has been marked as read only'}, 400 request_json = flask.request.get_json(force=True) if not isinstance(request_json, dict): return {"message": "JSON object body required"}, 400 for key in request_json: if key not in { 'object_id', 'version_id', 'action_id', 'schema', 'data' }: return {"message": "invalid key '{}'".format(key)}, 400 if 'object_id' in request_json: return {"message": "object_id cannot be set"}, 400 if 'version_id' in request_json: if request_json['version_id'] != 0: return {"message": "version_id must be 0"}, 400 if 'action_id' in request_json: if not isinstance(request_json['action_id'], int): return {"message": "action_id must be an integer"}, 400 action_id = request_json['action_id'] try: action = get_action(action_id=action_id) except errors.ActionDoesNotExistError: return { "message": "action {} does not exist".format(action_id) }, 400 else: return {"message": "action_id must be set"}, 400 if 'schema' in request_json: if request_json['schema'] != action.schema: return { "message": "schema must be:\n{}".format( json.dumps(action.schema, indent=4)) }, 400 schema = action.schema if 'data' not in request_json: return {"message": "data must be set"}, 400 data = request_json['data'] try: object = create_object(action_id=action_id, data=data, user_id=flask.g.user.id, schema=schema) except errors.ValidationError as e: messages = e.message.splitlines() return { "message": "validation failed:\n - {}".format('\n - '.join(messages)) }, 400 except Exception: return {"message": "failed to create object"}, 400 object_version_url = flask.url_for('api.object_version', object_id=object.object_id, version_id=object.version_id) return flask.redirect(object_version_url, code=201)
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)
def object(user: User, action: Action): data = {'name': {'_type': 'text', 'text': 'Object'}} return objects.create_object(user_id=user.id, action_id=action.id, data=data)
def set_up_state(user: User): action = actions.create_action( action_type_id=sampledb.models.ActionType.SAMPLE_CREATION, name='Example Action', schema={ 'title': 'Example Object', 'type': 'object', 'properties': { 'name': { 'title': 'Sample Name', 'type': 'text' } }, 'required': ['name'] }, description='', instrument_id=None ) actions.create_action( action_type_id=sampledb.models.ActionType.SAMPLE_CREATION, name='Irrelevant Action', schema={ 'title': 'Example Object', 'type': 'object', 'properties': { 'name': { 'title': 'Sample Name', 'type': 'text' } }, 'required': ['name'] }, description='', instrument_id=None ) data = {'name': {'_type': 'text', 'text': 'Object'}} object = objects.create_object(user_id=user.id, action_id=action.id, data=data) def save_content(file): file.write("This is a test file.".encode('utf-8')) files.create_local_file(object.id, user.id, "test.txt", save_content) files.create_url_file(object.id, user.id, "https://example.com") instrument = sampledb.logic.instruments.create_instrument( name="Example Instrument", description="Example Instrument Description", users_can_view_log_entries=True ) category = sampledb.logic.instrument_log_entries.create_instrument_log_category( instrument_id=instrument.id, title="Category", theme=sampledb.logic.instrument_log_entries.InstrumentLogCategoryTheme.RED ) log_entry = sampledb.logic.instrument_log_entries.create_instrument_log_entry( instrument_id=instrument.id, user_id=user.id, content="Example Log Entry Text", category_ids=[category.id] ) sampledb.logic.instrument_log_entries.create_instrument_log_file_attachment( instrument_log_entry_id=log_entry.id, file_name="example.txt", content=b'Example Content' ) sampledb.logic.instrument_log_entries.create_instrument_log_object_attachment( instrument_log_entry_id=log_entry.id, object_id=object.id )
def test_generate_rdf(flask_server, user, other_user): action = actions.create_action( action_type=ActionType.SAMPLE_CREATION, name='Example Action', schema={ 'title': 'Example Object', 'type': 'object', 'properties': { 'name': { 'title': 'Sample Name', 'type': 'text' } }, 'required': ['name'] }, description='', instrument_id=None ) data = {'name': {'_type': 'text', 'text': 'Example Object'}} object = objects.create_object(user_id=user.id, action_id=action.id, data=data) comments.create_comment(object.id, other_user.id, 'Example Comment') objects.update_object(object.id, data, user.id) objects.update_object(object.id, data, other_user.id) flask_server.app.config['SERVER_NAME'] = urlparse(flask_server.base_url).netloc with flask_server.app.app_context(): rdf_document = rdf.generate_rdf(user.id, object.id) etree.register_namespace('dcterms', 'http://purl.org/dc/terms/') et = etree.fromstring(rdf_document) assert next(et.iter('{*}title')).text == 'Example Object' assert next(et.iter('{*}identifier')).text == f'{flask_server.base_url}objects/{object.id}' creators = list(et.iter('{*}creator')) assert len(creators) == 2 assert creators[0][0].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about') == f'{flask_server.base_url}users/{user.id}' assert creators[0][0][0].text == user.name assert creators[1][0].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about') == f'{flask_server.base_url}users/{other_user.id}' assert creators[1][0][0].text == other_user.name contributors = list(et.iter('{*}contributor')) assert len(contributors) == 0 versions = list(et.iter('{*}hasVersion')) assert len(versions) == 3 assert versions[0].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource') == f'{flask_server.base_url}objects/{object.id}/versions/0' assert versions[1].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource') == f'{flask_server.base_url}objects/{object.id}/versions/1' assert versions[2].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource') == f'{flask_server.base_url}objects/{object.id}/versions/2' is_version_of = list(et.iter('{*}isVersionOf')) assert len(is_version_of) == 0 flask_server.app.config['SERVER_NAME'] = urlparse(flask_server.base_url).netloc with flask_server.app.app_context(): rdf_document = rdf.generate_rdf(user.id, object.id, 1) etree.register_namespace('dcterms', 'http://purl.org/dc/terms/') et = etree.fromstring(rdf_document) assert next(et.iter('{*}title')).text == 'Example Object' assert next(et.iter('{*}identifier')).text == f'{flask_server.base_url}objects/{object.id}/versions/1' creators = list(et.iter('{*}creator')) assert len(creators) == 1 assert creators[0][0].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about') == f'{flask_server.base_url}users/{user.id}' assert creators[0][0][0].text == user.name contributors = list(et.iter('{*}contributor')) assert len(contributors) == 1 assert contributors[0][0].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about') == f'{flask_server.base_url}users/{other_user.id}' assert contributors[0][0][0].text == other_user.name versions = list(et.iter('{*}hasVersion')) assert len(versions) == 0 is_version_of = list(et.iter('{*}isVersionOf')) assert len(is_version_of) == 1 assert is_version_of[0].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource') == f'{flask_server.base_url}objects/{object.id}'