def test_init_params(self): p1 = model.Person(ident="urn:uuid:1234") self.assertEqual(p1.id, "urn:uuid:1234") p2 = model.Person(ident="http://schema.org/Foo") self.assertEqual(p2.id, "schema:Foo") p3 = model.Name(content="Test") self.assertEqual(p3.content, "Test")
def test_int_per_type(self): model.factory.auto_id_type = "int-per-type" p = model.Person() p2 = model.Person() self.assertEqual(int(p.id[-1]), int(p2.id[-1]) - 1) p3 = model.Activity() self.assertEqual(int(p.id[-1]), int(p3.id[-1]))
def test_set_magic_resource_inverse(self): model.factory.materialize_inverses = True artist = model.Person('00001', 'Jane Doe') son = model.Person('00002', 'John Doe') artist._set_magic_resource('parent_of', son) self.assertEqual(son.parent, [artist]) model.factory.materialize_inverses = False
def test_other_uris(self): p1 = model.Person(ident="tag:some-info-about-person") self.assertEqual(p1.id, "tag:some-info-about-person") p2 = model.Person(ident="info:ulan/500012345") self.assertEqual(p2.id, "info:ulan/500012345") p3 = model.Person(ident="some:random:thing:with:colons") self.assertFalse(p3.id == "some:random:thing:with:colons")
def test_set_magic_resource(self): artist = model.Person('00001', 'Jane Doe') son = model.Person('00002', 'John Doe') daughter = model.Person('00002', 'Jenny Doe') son2 = model.Person('00002', 'Jim Doe') artist._set_magic_resource('parent_of', son) self.assertEqual(artist.parent_of, son) artist._set_magic_resource('parent_of', daughter) try: self.assertIn(son, artist.parent_of) self.assertIn(daughter, artist.parent_of) except: # 2.6 doesn't have assertIn self.assertTrue(son in artist.parent_of) self.assertTrue(daughter in artist.parent_of) artist._set_magic_resource('parent_of', son2) try: self.assertIn(son, artist.parent_of) self.assertIn(daughter, artist.parent_of) self.assertIn(son2, artist.parent_of) except: self.assertTrue(son in artist.parent_of) self.assertTrue(daughter in artist.parent_of) self.assertTrue(son2 in artist.parent_of)
def test_set_magic_lang(self): model.factory.default_lang = 'en' artist = model.Person('00001', 'Jane Doe') self.assertEqual(artist.label, {'en': 'Jane Doe'}) artist._set_magic_lang('label', 'Janey') self.assertEqual(artist.label, {'en': ['Jane Doe', 'Janey']}) son = model.Person('00002', 'John Doe') self.assertRaises(model.DataError, artist._set_magic_lang, 'parent_of', son)
def test_in_value(self): people = ( model.Person('00001', 'Jane Doe'), # artist model.Person('00002', 'Jim Doe') # son2 ) self.assertIn(self.artist, people) self.assertNotIn(self.son, people) self.assertNotIn(self.daughter, people) self.assertIn(self.son2, people)
def test_prefixes(self): model.factory.prefixes = {'fish': 'http://example.org/ns/'} p3 = model.Person('fish:3') self.assertEqual(p3.id, 'fish:3') self.assertEqual(p3._full_id, 'http://example.org/ns/3') model.factory.prefixes = {} p4 = model.Person('fish:4') self.assertTrue(p4.id.startswith(model.factory.base_url))
def test_init_params(self): p1 = model.Person(ident="urn:uuid:1234") self.assertEqual(p1.id, "urn:uuid:1234") p2 = model.Person(ident="http://schema.org/Foo") self.assertEqual(p2.id, "schema:Foo") p3 = model.Name(content="Test") self.assertEqual(p3.content, "Test") c = model.MonetaryAmount(value=10) self.assertEqual(c.value, 10) n = model.Name(value="Rob") self.assertEqual(n.content, "Rob") i = model.Identifier(content="xyz123") self.assertEqual(i.content, "xyz123") i2 = model.Identifier(value="abc") self.assertEqual(i2.content, "abc")
def test_boundary_setter(self): vocab.add_linked_art_boundary_check() p = model.Person() p2 = model.Person() n = model.Name() n.content = "Test" p2.identified_by = n p.exact_match = p2 # Now, Test should not appear in the resulting JSON of p factory.linked_art_boundaries = True js = factory.toJSON(p) self.assertTrue(not 'identified_by' in js['exact_match'][0]) factory.linked_art_boundaries = False js = factory.toJSON(p) self.assertTrue('identified_by' in js['exact_match'][0])
def test_list_my_props(self): p1 = model.Person() p1.classified_as = model.Type() props = p1.list_my_props() self.assertEqual(set(props), set(['classified_as', 'id'])) props = p1.list_my_props(filter=model.Type) self.assertEqual(props, ['classified_as'])
def test_prefixes(self): p = model.Person("ulan:1") self.assertEqual(p.id, "ulan:1") self.assertEqual(p._full_id, "http://vocab.getty.edu/ulan/1") p2 = model.Person('http://vocab.getty.edu/ulan/2') self.assertEqual(p2.id, 'ulan:2') self.assertEqual(p2._full_id, 'http://vocab.getty.edu/ulan/2') model.factory.prefixes = {'fish':'http://example.org/ns/'} p3 = model.Person('fish:3') self.assertEqual(p3.id, 'fish:3') self.assertEqual(p3._full_id, 'http://example.org/ns/3') model.factory.prefixes = {} p4 = model.Person('fish:4') self.assertTrue(p4.id.startswith(model.factory.base_url))
def test_validate_profile_off(self): model.factory.validate_profile = False ia = model.IdentifierAssignment() # If it's not turned off this should raise model.factory.validate_profile = True self.assertRaises(model.ProfileError, model.IdentifierAssignment) p1 = model.Person() self.assertRaises(model.ProfileError, p1.__setattr__, 'documented_in', "foo")
def test_int_per_segment(self): model.factory._auto_id_segments = {} model.factory.auto_id_type = "int-per-segment" model.Activity._uri_segment = model.Person._uri_segment p = model.Person() p2 = model.Activity() self.assertEqual(int(p.id[-1]), int(p2.id[-1]) - 1) p3 = model.TimeSpan() self.assertEqual(int(p.id[-1]), int(p3.id[-1]))
def test_neq(self): self.assertNotEqual(self.artist, self.son) self.assertNotEqual(self.artist, model.Person('00001', 'Jane')) # label differs self.assertNotEqual(self.artist, self.daughter) self.assertNotEqual(self.artist, self.son2) self.assertNotEqual(self.son, self.daughter) self.assertNotEqual(self.son, self.son2) self.assertNotEqual(self.daughter, self.son2)
def test_validate_multiplicity(self): model.factory.validate_multiplicity = True who = model.Person() b1 = model.Birth() who.born = b1 b2 = model.Birth() self.assertRaises(model.ProfileError, who.__setattr__, 'born', b2) model.factory.validate_multiplicity = False who.born = b2 self.assertEqual(who.born, [b1, b2])
def test_collapse_json(self): p = model.Person() p.classified_as = model.Type(ident="http://example.org/Type", label="Test") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6)
def test_collapse_json(self): model.factory.auto_id_type = "uuid" model.factory.base_url = "http://lod.example.org/museum/" model.factory.context_uri = "https://linked.art/ns/v1/linked-art.json" p = model.Person() p.classified_as = model.Type(ident="http://example.org/Type", label="Test") res1 = model.factory.toString(p, compact=False, collapse=60) # all new lines res2 = model.factory.toString(p, compact=False, collapse=120) # compact list of type self.assertEqual(len(res1.splitlines()), 12) self.assertEqual(len(res2.splitlines()), 6)
def test_toJSON_full(self): expect = OrderedDict([(u'@context', 'http://lod.getty.edu/context.json'), (u'@id', u'http://lod.example.org/museum/Person/1'), (u'@type', u'crm:E21_Person'), ('rdfs:label', 'Test Person')]) model.factory.context_uri = 'http://lod.getty.edu/context.json' model.factory.full_names = True p = model.Person("1") p.label = "Test Person" outj = model.factory.toJSON(p) self.assertEqual(expect, outj) # reset model.factory.full_names = False model.factory.context_uri = ""
def test_ordering(self): p = model.Person(label="Person") p.classified_as = model.Type(ident="type-uri") p.referred_to_by = model.LinguisticObject(content="text") p.dimension = model.Dimension(value=1) outstr = model.factory.toString(p) lbl = outstr.index("_label") clsf = outstr.index("classified_as") r2b = outstr.index("referred_to_by") dim = outstr.index("dimension") self.assertTrue(lbl < clsf) self.assertTrue(clsf < r2b) self.assertTrue(r2b < dim)
def test_production_mode(self): # model.factory.production_mode() # Can't unset the cached hierarchy # and it causes the test for the hierarchy to fail model.factory.validate_profile = False model.factory.validate_properties = False model.factory.validate_range = False model.factory.validate_multiplicity = False p = model.Person() p.identified_by = model.Name(value="abc") p.part = model.HumanMadeObject() js = model.factory.toJSON(p) model.factory.production_mode(state=False)
def setup_static_instances(self): ''' These are instances that are used statically in the code. For example, when we provide attribution of an identifier to Getty, or use a Lugt number, we need to serialize the related Group or Person record for that attribution, even if it does not appear in the source data. ''' lugt_ulan = 500321736 gri_ulan = 500115990 gci_ulan = 500115991 GETTY_GRI_URI = self.helper.make_proj_uri('ORGANIZATION', 'LOCATION-CODE', 'JPGM') GETTY_GCI_URI = self.helper.make_shared_uri( 'STATIC', 'ORGANIZATION', 'Getty Conservation Institute') LUGT_URI = self.helper.make_proj_uri('PERSON', 'ULAN', lugt_ulan) gci = model.Group(ident=GETTY_GCI_URI, label='Getty Conservation Institute') gci.identified_by = vocab.PrimaryName( ident='', content='Getty Conservation Institute') gci.exact_match = model.BaseResource( ident=f'http://vocab.getty.edu/ulan/{gci_ulan}') gri = model.Group(ident=GETTY_GRI_URI, label='Getty Research Institute') gri.identified_by = vocab.PrimaryName( ident='', content='Getty Research Institute') gri.exact_match = model.BaseResource( ident=f'http://vocab.getty.edu/ulan/{gri_ulan}') lugt = model.Person(ident=LUGT_URI, label='Frits Lugt') lugt.identified_by = vocab.PrimaryName(ident='', content='Frits Lugt') lugt.exact_match = model.BaseResource( ident=f'http://vocab.getty.edu/ulan/{lugt_ulan}') instances = defaultdict(dict) instances.update({ 'Group': { 'gci': gci, 'gri': gri }, 'Person': { 'lugt': lugt } }) return instances
def test_not_multiple_instance(self): who = model.Person() n = model.Name(content="Test") who.identified_by = n model.factory.multiple_instances_per_property = "error" self.assertRaises(model.DataError, who.__setattr__, 'identified_by', n) self.assertEqual(who.identified_by, [n]) model.factory.multiple_instances_per_property = "drop" who.identified_by = n self.assertEqual(who.identified_by, [n, n]) # and check that only serialized once js = model.factory.toJSON(who) self.assertEqual(len(js['identified_by']), 1) model.factory.multiple_instances_per_property = "allow" js = model.factory.toJSON(who) self.assertEqual(len(js['identified_by']), 2)
def test_no_ident(self): model.factory.auto_assign_id = True p1 = model.Person() # auto assigned p2 = model.Person(ident=None) # auto assigned p3 = model.Person(ident="") # bnode explicitly self.assertTrue(p1.id.startswith('http')) self.assertTrue(p2.id.startswith('http')) self.assertEqual(p3.id, '') model.factory.auto_assign_id = False p4 = model.Person() # bnode is default p5 = model.Person(ident=None) # bnode is default p6 = model.Person(ident="") # bnode explicitly self.assertEqual(p4.id, '') self.assertEqual(p5.id, '') self.assertEqual(p6.id, '')
def test_validation_off(self): model.factory.validate_properties = False artist = model.Person('00001', 'Jane Doe') artist.unknown_property = 1 self.assertEqual(artist.unknown_property, 1)
def test_validation_wrong_type(self): model.factory.validate_properties = True artist = model.Person('00001', 'Jane Doe') self.assertRaises(model.DataError, artist.__setattr__, 'parent_of', 'Bad Value')
def test_validation_unknown(self): model.factory.validate_properties = True artist = model.Person('00001', 'Jane Doe') self.assertRaises(model.DataError, artist.__setattr__, 'unknown_property', 1)
def setUp(self): self.artist = model.Person('00001', 'Jane Doe') self.son = model.Person('00002', 'John Doe') model.Person._properties['parent_of']['okayToUse'] = 1
def test_uuid(self): model.factory.auto_id_type = "uuid" p = model.Person() self.assertTrue(p.id.startswith('urn:uuid:'))
def test_int(self): model.factory.auto_id_type = "int" p = model.Person() p2 = model.Activity() self.assertEqual(int(p.id[-1]), int(p2.id[-1]) - 1)