def test_replace_skolem_ids_when_adding_items(self):
        with open(filepath('test-data.json')) as f:
            data = json.load(f)
        with open(filepath('test-patch-adds-items.json')) as f:
            original_patch = JsonPatch(json.load(f))
        applied_patch, id_map = identifier.replace_skolem_ids(
            original_patch, data, [])
        self.assertRegex(
            applied_patch.patch[0]['path'],
            r'^/periodCollections/p0trgkv/definitions/p0trgkv[%s]{4}$'
            % identifier.XDIGITS)
        self.assertRegex(
            applied_patch.patch[0]['value']['id'],
            r'^p0trgkv[%s]{4}$' % identifier.XDIGITS)
        identifier.check(applied_patch.patch[0]['value']['id'])
        self.assertTrue(
            applied_patch.patch[0]['value']['id'] in id_map.values())

        self.assertRegex(
            applied_patch.patch[1]['path'],
            r'^/periodCollections/p0[%s]{5}$' % identifier.XDIGITS)
        self.assertRegex(
            applied_patch.patch[1]['value']['id'],
            r'^p0[%s]{5}$' % identifier.XDIGITS)
        collection_id = applied_patch.patch[1]['value']['id']
        identifier.check(collection_id)
        self.assertTrue(collection_id in id_map.values())
        defs = applied_patch.patch[1]['value']['definitions']
        self.assertRegex(
            list(defs.keys())[0],
            r'^%s[%s]{4}$' % (collection_id, identifier.XDIGITS))
        self.assertEqual(
            list(defs.values())[0]['id'],
            list(defs.keys())[0])
        identifier.check(list(defs.keys())[0])
    def test_replace_skolem_ids_when_replacing_collections(self):
        with open(filepath('test-data.json')) as f:
            data = json.load(f)
        with open(filepath('test-patch-replaces-collections.json')) as f:
            original_patch = JsonPatch(json.load(f))
        applied_patch, id_map = identifier.replace_skolem_ids(
            original_patch, data, [])
        self.assertEqual(
            applied_patch.patch[0]['path'],
            original_patch.patch[0]['path'])

        collection_id, collection = list(
            applied_patch.patch[0]['value'].items())[0]
        self.assertTrue(collection_id in id_map.values())
        self.assertRegex(
            collection_id,
            r'^p0[%s]{5}$' % identifier.XDIGITS)
        self.assertEqual(collection_id, collection['id'])
        identifier.check(collection_id)

        definition_id, definition = list(
            applied_patch.patch[0]['value'][collection_id]['definitions']
            .items())[0]
        self.assertTrue(definition_id in id_map.values())
        self.assertRegex(
            definition_id,
            r'^%s[%s]{4}$' % (collection_id, identifier.XDIGITS))
        self.assertEqual(definition_id, definition['id'])
        identifier.check(definition_id)
Esempio n. 3
0
    def test_replace_skolem_ids_when_replacing_authorities(self):
        with open(filepath('test-data.json')) as f:
            data = json.load(f)
        with open(filepath('test-patch-replaces-authorities.json')) as f:
            original_patch = JsonPatch(json.load(f))
        applied_patch, id_map = identifier.replace_skolem_ids(
            original_patch, data, [])
        self.assertEqual(
            applied_patch.patch[0]['path'],
            original_patch.patch[0]['path'])

        authority_id, authority = list(
            applied_patch.patch[0]['value'].items())[0]
        self.assertTrue(authority_id in id_map.values())
        self.assertRegex(
            authority_id,
            r'^p0[%s]{5}$' % identifier.XDIGITS)
        self.assertEqual(authority_id, authority['id'])
        identifier.check(authority_id)

        period_id, period = list(
            applied_patch.patch[0]['value'][authority_id]['periods']
            .items())[0]
        self.assertTrue(period_id in id_map.values())
        self.assertRegex(
            period_id,
            r'^%s[%s]{4}$' % (authority_id, identifier.XDIGITS))
        self.assertEqual(period_id, period['id'])
        identifier.check(period_id)
Esempio n. 4
0
def merge(patch_id, user_id):
    row = database.query_db(
        'SELECT * FROM patch_request WHERE id = ?', (patch_id,), one=True)

    if not row:
        raise MergeError('No patch with ID {}.'.format(patch_id))
    if row['merged']:
        raise MergeError('Patch is already merged.')
    if not row['open']:
        raise MergeError('Closed patches cannot be merged.')

    dataset = database.get_dataset()
    mergeable = is_mergeable(row['original_patch'], dataset)

    if not mergeable:
        raise UnmergeablePatchError('Patch is not mergeable.')

    data = json.loads(dataset['data'])
    original_patch = from_text(row['original_patch'])
    applied_patch, id_map = replace_skolem_ids(
        original_patch, data, database.get_removed_entity_keys())
    created_entities = set(id_map.values())

    # Should this be ordered?
    new_data = applied_patch.apply(data)

    db = database.get_db()
    curs = db.cursor()
    curs.execute(
        '''
        UPDATE patch_request
        SET merged = 1,
            open = 0,
            merged_at = strftime('%s', 'now'),
            merged_by = ?,
            applied_to = ?,
            created_entities = ?,
            identifier_map = ?,
            applied_patch = ?
        WHERE id = ?;
        ''',
        (user_id,
         dataset['id'],
         json.dumps(sorted(created_entities)),
         json.dumps(id_map),
         applied_patch.to_string(),
         row['id'])
    )
    version_id = add_new_version_of_dataset(new_data)
    curs.execute(
        '''
        UPDATE patch_request
        SET resulted_in = ?
        WHERE id = ?;
        ''',
        (version_id, row['id'])
    )
Esempio n. 5
0
    def test_replace_skolem_ids_when_adding_items(self):
        with open(filepath('test-data.json')) as f:
            data = json.load(f)
        with open(filepath('test-patch-adds-items.json')) as f:
            original_patch = JsonPatch(json.load(f))
        applied_patch, id_map = identifier.replace_skolem_ids(
            original_patch, data, [])
        self.assertRegex(
            applied_patch.patch[0]['path'],
            r'^/authorities/p0trgkv/periods/p0trgkv[%s]{4}$'
            % identifier.XDIGITS)
        self.assertRegex(
            applied_patch.patch[0]['value']['id'],
            r'^p0trgkv[%s]{4}$' % identifier.XDIGITS)
        identifier.check(applied_patch.patch[0]['value']['id'])
        self.assertTrue(
            applied_patch.patch[0]['value']['id'] in id_map.values())

        self.assertRegex(
            applied_patch.patch[1]['path'],
            r'^/authorities/p0[%s]{5}$' % identifier.XDIGITS)
        self.assertRegex(
            applied_patch.patch[1]['value']['id'],
            r'^p0[%s]{5}$' % identifier.XDIGITS)
        authority_id = applied_patch.patch[1]['value']['id']
        identifier.check(authority_id)
        self.assertTrue(authority_id in id_map.values())
        periods = applied_patch.patch[1]['value']['periods']
        for key in periods.keys():
            self.assertRegex(
                key,
                r'^%s[%s]{4}$' % (authority_id, identifier.XDIGITS))
            self.assertEqual(
                key,
                periods[key]['id'])
            identifier.check(key)
            # check that skolem IDs in prop values get replaced too
            prop = 'broader' if 'broader' in periods[key] else 'narrower'
            self.assertRegex(
                periods[key][prop],
                r'^%s[%s]{4}$' % (authority_id, identifier.XDIGITS))
            identifier.check(periods[key][prop])
            self.assertTrue(periods[key][prop] in id_map.values())