Exemplo n.º 1
0
    def test_object_key_unqiueness(self):
        er = ExternalResources('terms')
        data = Data(name='data_name',
                    data=np.array([('Mus musculus', 9, 81.0),
                                   ('H**o sapien', 3, 27.0)],
                                  dtype=[('species', 'U14'), ('age', 'i4'),
                                         ('weight', 'f4')]))

        er.add_ref(
            container=data,
            key='Mus musculus',
            resource_name='NCBI_Taxonomy',
            resource_uri='https://www.ncbi.nlm.nih.gov/taxonomy',
            entity_id='NCBI:txid10090',
            entity_uri=
            'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=10090'
        )
        existing_key = er.get_key('Mus musculus')
        er.add_ref(container=data,
                   key=existing_key,
                   resource_name='resource2',
                   resource_uri='resource_uri2',
                   entity_id='entity2',
                   entity_uri='entity_uri2')

        self.assertEqual(er.object_keys.data, [(0, 0)])
Exemplo n.º 2
0
    def test_get_keys_subset(self):
        er = ExternalResources('terms')
        er.add_ref(container='uuid1',
                   key='key1',
                   resource_name='resource1',
                   resource_uri='resource_uri1',
                   entity_id="id11",
                   entity_uri='url11')
        er.add_ref(container='uuid2',
                   key='key2',
                   resource_name='resource2',
                   resource_uri='resource_uri2',
                   entity_id="id12",
                   entity_uri='url21')
        er.add_ref(container='uuid1',
                   key=er.get_key(key_name='key1'),
                   resource_name='resource3',
                   resource_uri='resource_uri3',
                   entity_id="id13",
                   entity_uri='url31')
        key = er.keys.row[0]
        received = er.get_keys(keys=key)

        expected = pd.DataFrame(
            data=[['key1', 0, 'id11', 'url11'], ['key1', 2, 'id13', 'url31']],
            columns=['key_name', 'resources_idx', 'entity_id', 'entity_uri'])
        pd.testing.assert_frame_equal(received, expected)
Exemplo n.º 3
0
class TestExternalResourcesGetKey(TestCase):

    def setUp(self):
        self.er = ExternalResources('terms')

    def test_get_key(self):
        self.er.add_ref('uuid1', 'field1', 'key1', 'resource1', 'resource_id1', 'url1')
        self.er.add_ref('uuid2', 'field2', 'key1', 'resource2', 'resource_id2', 'url2')
        keys = self.er.get_key('key1')
        self.assertIsInstance(keys, Key)
        self.assertEqual(keys.key_name, 'key1')

    def test_get_key_w_object_info(self):
        self.er.add_ref('uuid1', 'field1', 'key1', 'resource1', 'resource_id1', 'url1')
        self.er.add_ref('uuid2', 'field2', 'key1', 'resource2', 'resource_id2', 'url2')
        keys = self.er.get_key('key1', 'uuid1', 'field1')
        self.assertIsInstance(keys, Key)
        self.assertEqual(keys.key_name, 'key1')

    def test_get_key_w_bad_object_info(self):
        self.er.add_ref('uuid1', 'field1', 'key1', 'resource1', 'resource_id1', 'url1')
        self.er.add_ref('uuid2', 'field2', 'key2', 'resource2', 'resource_id2', 'url2')
        with self.assertRaisesRegex(ValueError, "No key with name 'key2' for container 'uuid1' and field 'field1'"):
            self.er.get_key('key2', 'uuid1', 'field1')

    def test_get_key_doesnt_exist(self):
        self.er.add_ref('uuid1', 'field1', 'key1', 'resource1', 'resource_id1', 'url1')
        self.er.add_ref('uuid2', 'field2', 'key1', 'resource2', 'resource_id2', 'url2')
        with self.assertRaisesRegex(ValueError, "key 'bad_key' does not exist"):
            self.er.get_key('bad_key')

    def test_get_key_same_keyname_all(self):
        self.er = ExternalResources('terms')
        key1 = self.er.add_key('key1')
        key2 = self.er.add_key('key1')
        self.er.add_ref('uuid1', 'field1', key1, 'resource11', 'resource_id11', 'url11')
        self.er.add_ref('uuid2', 'field2', key2, 'resource21', 'resource_id21', 'url21')
        self.er.add_ref('uuid1', 'field1', 'key1', 'resource12', 'resource_id12', 'url12')

        keys = self.er.get_key('key1')

        self.assertIsInstance(keys, list)
        self.assertEqual(keys[0].key_name, 'key1')
        self.assertEqual(keys[1].key_name, 'key1')

    def test_get_key_same_keyname_specific(self):
        self.er = ExternalResources('terms')
        key1 = self.er.add_key('key1')
        key2 = self.er.add_key('key1')
        self.er.add_ref('uuid1', 'field1', key1, 'resource11', 'resource_id11', 'url11')
        self.er.add_ref('uuid2', 'field2', key2, 'resource21', 'resource_id21', 'url21')
        self.er.add_ref('uuid1', 'field1', 'key1', 'resource12', 'resource_id12', 'url12')

        keys = self.er.get_key('key1', 'uuid1', 'field1')
        self.assertIsInstance(keys, Key)
        self.assertEqual(keys.key_name, 'key1')
Exemplo n.º 4
0
 def test_add_ref_two_resources(self):
     er = ExternalResources('terms')
     er.add_ref(container='uuid1',
                key='key1',
                resource_name='resource1',
                resource_uri='resource_uri1',
                entity_id="id11",
                entity_uri='url11')
     er.add_ref(container='uuid1',
                key=er.get_key(key_name='key1'),
                resource_name='resource2',
                resource_uri='resource_uri2',
                entity_id="id12",
                entity_uri='url21')
     self.assertEqual(er.keys.data, [('key1', )])
     self.assertEqual(er.resources.data, [('resource1', 'resource_uri1'),
                                          ('resource2', 'resource_uri2')])
     self.assertEqual(er.objects.data, [('uuid1', '', '')])
     self.assertEqual(er.entities.data, [(0, 0, 'id11', 'url11'),
                                         (0, 1, 'id12', 'url21')])
Exemplo n.º 5
0
class TestExternalResourcesGetKey(TestCase):
    def setUp(self):
        self.er = ExternalResources('terms')

    def test_get_key(self):
        self.er.add_ref('uuid1',
                        key='key1',
                        resource_name='resource1',
                        resource_uri='resource_uri1',
                        entity_id="id11",
                        entity_uri='url11')
        self.er.add_ref('uuid2',
                        key='key1',
                        resource_name='resource2',
                        resource_uri='resource_uri2',
                        entity_id="id12",
                        entity_uri='url21')

        keys = self.er.get_key('key1', 'uuid2', '')
        self.assertIsInstance(keys, Key)
        self.assertEqual(keys.idx, 1)

    def test_get_key_bad_arg(self):
        self.er._add_key('key2')
        self.er.add_ref('uuid1',
                        key='key1',
                        resource_name='resource1',
                        resource_uri='resource_uri1',
                        entity_id="id11",
                        entity_uri='url11')
        with self.assertRaises(ValueError):
            self.er.get_key('key2', 'uuid1', '')

    @unittest.skip('Outdated do to privatization')
    def test_get_key_without_container(self):
        self.er = ExternalResources('terms')
        self.er._add_key('key1')
        keys = self.er.get_key('key1')
        self.assertIsInstance(keys, Key)

    def test_get_key_w_object_info(self):
        self.er.add_ref('uuid1',
                        key='key1',
                        resource_name='resource1',
                        resource_uri='resource_uri1',
                        entity_id="id11",
                        entity_uri='url11')
        self.er.add_ref('uuid2',
                        key='key1',
                        resource_name='resource2',
                        resource_uri='resource_uri2',
                        entity_id="id12",
                        entity_uri='url21')
        keys = self.er.get_key('key1', 'uuid1', '')
        self.assertIsInstance(keys, Key)
        self.assertEqual(keys.key, 'key1')

    def test_get_key_w_bad_object_info(self):
        self.er.add_ref('uuid1',
                        key='key1',
                        resource_name='resource1',
                        resource_uri='resource_uri1',
                        entity_id="id11",
                        entity_uri='url11')
        self.er.add_ref('uuid2',
                        key='key1',
                        resource_name='resource2',
                        resource_uri='resource_uri2',
                        entity_id="id12",
                        entity_uri='url21')

        with self.assertRaisesRegex(ValueError, "No key 'key2'"):
            self.er.get_key('key2', 'uuid1', '')

    def test_get_key_doesnt_exist(self):
        self.er.add_ref('uuid1',
                        key='key1',
                        resource_name='resource1',
                        resource_uri='resource_uri1',
                        entity_id="id11",
                        entity_uri='url11')
        self.er.add_ref('uuid2',
                        key='key1',
                        resource_name='resource2',
                        resource_uri='resource_uri2',
                        entity_id="id12",
                        entity_uri='url21')
        with self.assertRaisesRegex(ValueError,
                                    "key 'bad_key' does not exist"):
            self.er.get_key('bad_key')

    @unittest.skip('Outdated do to privatization')
    def test_get_key_same_keyname_all(self):
        self.er = ExternalResources('terms')
        key1 = self.er._add_key('key1')
        key2 = self.er._add_key('key1')
        self.er.add_ref('uuid1',
                        key=key1,
                        resource_name='resource1',
                        resource_uri='resource_uri1',
                        entity_id="id11",
                        entity_uri='url11')
        self.er.add_ref('uuid2',
                        key=key2,
                        resource_name='resource2',
                        resource_uri='resource_uri2',
                        entity_id="id12",
                        entity_uri='url12')
        self.er.add_ref('uuid1',
                        key=self.er.get_key('key1', 'uuid1', ''),
                        resource_name='resource3',
                        resource_uri='resource_uri3',
                        entity_id="id13",
                        entity_uri='url13')

        keys = self.er.get_key('key1')

        self.assertIsInstance(keys, Key)
        self.assertEqual(keys[0].key, 'key1')
        self.assertEqual(keys[1].key, 'key1')

    def test_get_key_same_keyname_specific(self):
        self.er = ExternalResources('terms')

        self.er.add_ref('uuid1',
                        key='key1',
                        resource_name='resource1',
                        resource_uri='resource_uri1',
                        entity_id="id11",
                        entity_uri='url11')
        self.er.add_ref('uuid2',
                        key='key2',
                        resource_name='resource2',
                        resource_uri='resource_uri2',
                        entity_id="id12",
                        entity_uri='url12')
        self.er.add_ref('uuid1',
                        key=self.er.get_key('key1', 'uuid1', ''),
                        resource_name='resource3',
                        resource_uri='resource_uri3',
                        entity_id="id13",
                        entity_uri='url13')

        keys = self.er.get_key('key1', 'uuid1', '')
        self.assertIsInstance(keys, Key)
        self.assertEqual(keys.key, 'key1')
        self.assertEqual(self.er.keys.data, [('key1', ), ('key2', )])