def test_entity_label_description_three_chars_lang_codes(fx_client: Client): """ Ensure that three-character language codes are handled """ cbk_zam = fx_client.get(EntityId('Q33281'), load=True) assert isinstance(cbk_zam.label, MultilingualText) assert cbk_zam.label[Locale('ko')] == '차바카노어' assert Locale('cbk-zam') in cbk_zam.label assert isinstance(cbk_zam.description, MultilingualText) assert cbk_zam.description[Locale('en')] == \ 'Spanish-based creole language spoken in the Philippines' assert Locale('cbk-zam') not in cbk_zam.description
def test_decoder_globecoordinate(fx_client: Client): d = Decoder() decoded = d(fx_client, 'globe-coordinate', { 'value': { "latitude": 70.1525, "longitude": 70.1525, "precision": 0.0002777777777777778, "globe": "http://www.wikidata.org/entity/Q111" }, 'type': 'globecoordinate' }) gold = GlobeCoordinate(70.1525, 70.1525, fx_client.get(EntityId("Q111")), 0.0002777777777777778,) assert decoded == gold
def test_client_guess_entity_type( fx_client_opener: urllib.request.OpenerDirector): guess_client = Client(opener=fx_client_opener, entity_type_guess=True) assert guess_client.guess_entity_type(EntityId('Q1299')) is EntityType.item assert (guess_client.guess_entity_type(EntityId('P434')) is EntityType.property) assert guess_client.guess_entity_type(EntityId('NotApplicable')) is None noguess_client = Client(opener=fx_client_opener, entity_type_guess=False) assert noguess_client.guess_entity_type(EntityId('Q1299')) is None assert noguess_client.guess_entity_type(EntityId('P434')) is None assert noguess_client.guess_entity_type(EntityId('NotApplicable')) is None
def test_entity_label_description_three_chars_lang_codes(fx_client: Client): """As a short-term workaround, we currently ignore language codes ISO 639-1 doesn't cover. See also: https://github.com/dahlia/wikidata/issues/2 """ cbk_zam = fx_client.get(EntityId('Q33281'), load=True) assert isinstance(cbk_zam.label, MultilingualText) assert cbk_zam.label[Locale.parse('ko')] == '차바카노어' assert 'cbk-zam' not in cbk_zam.label assert 'cbk_zam' not in cbk_zam.label assert isinstance(cbk_zam.description, MultilingualText) assert cbk_zam.description[Locale.parse('en')] == \ 'Spanish-based creole language spoken in the Philippines' assert 'cbk-zam' not in cbk_zam.description assert 'cbk_zam' not in cbk_zam.description
def test_decoder_quantity_with_unit(fx_client: Client): d = Decoder() decoded = d( fx_client, 'quantity', { 'value': { "amount": "+610.13", "lower_bound": "+610.12", "upper_bound": "+610.14", "unit": "http://www.wikidata.org/entity/Q828224" }, 'type': 'quantity' }) gold = Quantity(amount=610.13, lower_bound=610.12, upper_bound=610.14, unit=fx_client.get(EntityId("Q828224"))) assert decoded == gold
def test_entity_mapping(fx_client: Client, fx_loaded_entity: Entity): occupation = fx_client.get(EntityId('P106')) musicbrainz_id = fx_client.get(EntityId('P434')) singer = fx_client.get(EntityId('Q177220')) instagram_username = fx_client.get(EntityId('P2003')) assert len(fx_loaded_entity) == 13 expected_ids = { 'P19', 'P21', 'P27', 'P31', 'P106', 'P136', 'P345', 'P434', 'P569', 'P646', 'P1303', 'P1728', 'P1953' } expected = {fx_client.get(EntityId(pid)) for pid in expected_ids} assert set(fx_loaded_entity) == expected assert musicbrainz_id in fx_loaded_entity assert (fx_loaded_entity[musicbrainz_id] == fx_loaded_entity.get(musicbrainz_id) == fx_loaded_entity.get(musicbrainz_id, ...) == '3eb63662-a02c-4d2d-9544-845cd92fd4e7') assert (fx_loaded_entity.getlist(musicbrainz_id) == ['3eb63662-a02c-4d2d-9544-845cd92fd4e7']) assert occupation in fx_loaded_entity assert (fx_loaded_entity[occupation] == fx_loaded_entity.get(occupation) == fx_loaded_entity.get(occupation, ...) == singer) assert (fx_loaded_entity.getlist(occupation) == [singer, fx_client.get(EntityId('Q753110'))]) assert instagram_username not in fx_loaded_entity with raises(KeyError): fx_loaded_entity[instagram_username] assert fx_loaded_entity.get(instagram_username) is None assert fx_loaded_entity.get(instagram_username, ...) is ... assert fx_loaded_entity.getlist(instagram_username) == [] assert (dict(fx_loaded_entity.iterlists()) == dict(fx_loaded_entity.lists()) == {p: fx_loaded_entity.getlist(p) for p in expected}) def sorted_list(v: Iterable) -> list: return list(sorted(v, key=str)) assert (sorted_list(fx_loaded_entity.iterlistvalues()) == sorted_list(fx_loaded_entity.listvalues()) == sorted_list(fx_loaded_entity.getlist(p) for p in expected))
def test_entity_getlist_novalue(fx_client: Client): hong_kong = fx_client.get(EntityId('Q8646')) locator_map_image = fx_client.get(EntityId('P242')) # There are 3 snaks for this property, but one has no associated value assert len(hong_kong.getlist(locator_map_image)) == 2
def fx_property(fx_client: Client) -> Entity: return fx_client.get(EntityId('P2003'))
def fx_redirected_entity(fx_client: Client) -> Entity: return fx_client.get(EntityId('Q16231742'))
def fx_loaded_entity(fx_client: Client) -> Entity: entity = fx_client.get(EntityId('Q494290')) entity.load() return entity
def fx_unloaded_entity(fx_client: Client) -> Entity: return fx_client.get(EntityId('Q1299'))
def fx_globecoordinate() -> GlobeCoordinate: client = Client() return GlobeCoordinate(latitude=70.1525, longitude=70.1525, precision=0.0002777777777777778, globe=client.get(EntityId("Q111")))
def test_client_pickle(fx_client: Client): dumped = pickle.dumps(fx_client) c = pickle.loads(dumped) entity = c.get(EntityId('Q1299'), load=True) assert isinstance(entity, Entity) assert entity.label[Locale('en')] == 'The Beatles'
#!/usr/bin/env python3 import sys from wikidata.entity import EntityId from wikidata_tree_generator.generate_from_yaml import generate_from_yaml if __name__ == '__main__': generate_from_yaml(sys.argv[1], EntityId(sys.argv[2]), sys.argv[3])