def nested_pids_and_relations(app, db): """Fixture for a nested PIDs and the expected serialized relations.""" # Create some PIDs and connect them into different nested PID relations pids = {} for idx in range(1, 12): pid_value = str(idx) p = PersistentIdentifier.create('recid', pid_value, object_type='rec', status=PIDStatus.REGISTERED) pids[idx] = p VERSION = resolve_relation_type_config('version').id # 1 (Version) # / | \ # 2 3 4 PIDRelation.create(pids[1], pids[2], VERSION, 0) PIDRelation.create(pids[1], pids[3], VERSION, 1) PIDRelation.create(pids[1], pids[4], VERSION, 2) # Define the expected PID relation tree for of the PIDs expected_relations = {} expected_relations[4] = { u'relations': { 'version': [{ u'children': [{ u'pid_type': u'recid', u'pid_value': u'2' }, { u'pid_type': u'recid', u'pid_value': u'3' }, { u'pid_type': u'recid', u'pid_value': u'4' }], u'index': 2, u'is_child': True, u'previous': { 'pid_type': 'recid', 'pid_value': '3' }, u'next': None, u'is_last': True, u'is_parent': False, u'parent': { u'pid_type': u'recid', u'pid_value': u'1' }, u'type': 'version' }], } } return pids, expected_relations
def test_repr(db, version_relation): pid = PersistentIdentifier.create( 'recid', 'barfoo', object_type='rec', status=PIDStatus.REGISTERED ) pid_v1 = PersistentIdentifier.create( 'recid', 'barfoo.v1', object_type='rec', status=PIDStatus.REGISTERED ) pid_relation = PIDRelation.create(pid, pid_v1, version_relation.id, 0) assert ( repr(pid_relation) == "<PIDRelation: (recid:barfoo) -> (recid:barfoo.v1) (Type: 0, Idx: 0)>" )
def get_record_without_version(pid): """Get PID of record without version ID.""" recid_without_ver = None parent_relations = PIDRelation.get_child_relations(pid).one_or_none() if parent_relations: parent_pid = PersistentIdentifier.query. \ filter_by(id=parent_relations.parent_id).one_or_none() if parent_pid: parent_pid_value = parent_pid.pid_value.split(':')[-1] recid_without_ver = PersistentIdentifier.get( pid_type='recid', pid_value=parent_pid_value) return recid_without_ver
def nested_pids_and_relations(app, db): """Fixture for a nested PIDs and the expected serialized relations.""" # Create some PIDs and connect them into different nested PID relations pids = {} for idx in range(1, 12): pid_value = str(idx) p = PersistentIdentifier.create('recid', pid_value, object_type='rec', status=PIDStatus.REGISTERED) pids[idx] = p ORDERED = resolve_relation_type_config('ordered').id UNORDERED = resolve_relation_type_config('unordered').id VERSION = resolve_relation_type_config('version').id # 1 (Version) # / | \ # 2 3 4 PIDRelation.create(pids[1], pids[2], VERSION, 0) PIDRelation.create(pids[1], pids[3], VERSION, 1) PIDRelation.create(pids[1], pids[4], VERSION, 2) # 5 (Ordered) # / | \ # 6 4 7 PIDRelation.create(pids[5], pids[6], ORDERED, 0) PIDRelation.create(pids[5], pids[4], ORDERED, 1) PIDRelation.create(pids[5], pids[7], ORDERED, 2) # 4 (Ordered) # / | # 8 9 PIDRelation.create(pids[4], pids[8], ORDERED, 0) PIDRelation.create(pids[4], pids[9], ORDERED, 1) # 10 (Unordered) # / | # 4 11 PIDRelation.create(pids[10], pids[4], UNORDERED, None) PIDRelation.create(pids[10], pids[11], UNORDERED, None) # Define the expected PID relation tree for of the PIDs expected_relations = {} expected_relations[4] = { 'relations': { 'version': [{ 'children': [{ 'pid_type': 'recid', 'pid_value': '2' }, { 'pid_type': 'recid', 'pid_value': '3' }, { 'pid_type': 'recid', 'pid_value': '4' }], 'is_child': True, 'index': 2, 'previous': { 'pid_type': 'recid', 'pid_value': '3' }, 'next': None, 'is_last': True, 'is_ordered': True, 'is_parent': False, 'parent': { 'pid_type': 'recid', 'pid_value': '1' }, 'type': 'version' }], 'ordered': [{ 'children': [{ 'pid_type': 'recid', 'pid_value': '6' }, { 'pid_type': 'recid', 'pid_value': '4' }, { 'pid_type': 'recid', 'pid_value': '7' }], 'is_child': True, 'index': 1, 'previous': { 'pid_type': 'recid', 'pid_value': '6' }, 'next': { 'pid_type': 'recid', 'pid_value': '7' }, 'is_last': False, 'is_ordered': True, 'is_parent': False, 'parent': { 'pid_type': 'recid', 'pid_value': '5' }, 'type': 'ordered' }, { 'children': [{ 'pid_type': 'recid', 'pid_value': '8' }, { 'pid_type': 'recid', 'pid_value': '9' }], 'is_child': False, 'index': None, 'previous': None, 'next': None, 'is_last': None, 'is_ordered': True, 'is_parent': True, 'parent': { 'pid_type': 'recid', 'pid_value': '4' }, 'type': 'ordered' }], 'unordered': [ { 'children': [{ 'pid_type': 'recid', 'pid_value': '4' }, { 'pid_type': 'recid', 'pid_value': '11' }], 'is_child': True, 'index': None, 'previous': None, 'next': None, 'is_last': False, 'is_ordered': True, 'is_parent': False, 'parent': { 'pid_type': 'recid', 'pid_value': '10' }, 'type': 'unordered' }, ] } } return pids, expected_relations
def pids(app, db): """Test PIDs fixture.""" # TODO: Head PIDs do not have redirects as they are created outside API h1 = PersistentIdentifier.create('recid', 'foobar', object_type='rec', status=PIDStatus.REGISTERED) h1v1 = PersistentIdentifier.create('recid', 'foobar.v1', object_type='rec') h1v2 = PersistentIdentifier.create('recid', 'foobar.v2', object_type='rec') h1v3 = PersistentIdentifier.create('recid', 'foobar.v3', object_type='rec') ORDERED = resolve_relation_type_config('ordered').id UNORDERED = resolve_relation_type_config('unordered').id PIDRelation.create(h1, h1v1, ORDERED, 0) PIDRelation.create(h1, h1v2, ORDERED, 1) PIDRelation.create(h1, h1v3, ORDERED, 2) h1.redirect(h1v3) h2 = PersistentIdentifier.create('recid', 'spam', object_type='rec', status=PIDStatus.REGISTERED) h2v1 = PersistentIdentifier.create('recid', 'spam.v1') PIDRelation.create(h2, h2v1, ORDERED, 0) h2.redirect(h2v1) c1 = PersistentIdentifier.create('recid', 'bazbar') c1r1 = PersistentIdentifier.create('recid', 'resource1') c1r2 = PersistentIdentifier.create('recid', 'resource2') pid1 = PersistentIdentifier.create('recid', 'eggs') PIDRelation.create(c1, c1r1, UNORDERED, None) PIDRelation.create(c1, c1r2, UNORDERED, None) return { 'h1': h1, 'h1v1': h1v1, 'h1v2': h1v2, 'h1v3': h1v3, 'h2': h2, 'h2v1': h2v1, 'c1': c1, 'c1r1': c1r1, 'c1r2': c1r2, 'pid1': pid1, }
def version_pids(app, db, version_relation, draft_relation): """Create versionned PIDs.""" h1 = PersistentIdentifier.create('recid', 'foobar', object_type='rec', status=PIDStatus.REGISTERED) h1v1 = PersistentIdentifier.create('recid', 'foobar.v1', object_type='rec', status=PIDStatus.REGISTERED) h1v2 = PersistentIdentifier.create('recid', 'foobar.v2', object_type='rec', status=PIDStatus.REGISTERED) h1v3 = PersistentIdentifier.create('recid', 'foobar.v3', object_type='rec', status=PIDStatus.REGISTERED) h1del1 = PersistentIdentifier.create('recid', 'foobar.del1', object_type='rec', status=PIDStatus.DELETED) h1del2 = PersistentIdentifier.create('recid', 'foobar.del2', object_type='rec', status=PIDStatus.DELETED) h1draft1 = PersistentIdentifier.create('recid', 'foobar.draft', object_type='rec', status=PIDStatus.RESERVED) h1deposit1 = PersistentIdentifier.create('recid', 'foobar.deposit', object_type='rec') VERSION = version_relation.id DRAFT = draft_relation.id PIDRelation.create(h1, h1v1, VERSION, 0) PIDRelation.create(h1, h1v2, VERSION, 1) PIDRelation.create(h1, h1v3, VERSION, 2) PIDRelation.create(h1, h1del1, VERSION, 3) PIDRelation.create(h1, h1del2, VERSION, 4) PIDRelation.create(h1, h1draft1, VERSION, 5) PIDRelation.create(h1draft1, h1deposit1, DRAFT, 0) h1.redirect(h1v3) h2 = PersistentIdentifier.create('recid', 'spam', object_type='rec', status=PIDStatus.REGISTERED) h2v1 = PersistentIdentifier.create('recid', 'spam.v1') PIDRelation.create(h2, h2v1, VERSION, 0) h2.redirect(h2v1) return [ { 'parent': h1, 'children': [ h1v1, h1v2, h1v3, h1del1, h1del2, h1draft1, ], 'deposit': h1deposit1, }, { 'parent': h2, 'children': [ h2v1, ], }, ]