Skip to content

Commit f385ea5

Browse files
committed
Implement new version of FastRun
1 parent e236674 commit f385ea5

30 files changed

+598
-906
lines changed

.idea/inspectionProfiles/WikibaseIntegrator.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ disable = [
3434

3535
[tool.pytest.ini_options]
3636
log_cli = true
37+
log_cli_level = 'DEBUG'

test/test_all.py

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import copy
2+
import logging
23
import unittest
34

45
from wikibaseintegrator import WikibaseIntegrator, datatypes, wbi_fastrun
56
from wikibaseintegrator.datatypes import BaseDataType, Item
6-
from wikibaseintegrator.entities import ItemEntity
7+
from wikibaseintegrator.entities import BaseEntity, ItemEntity
78
from wikibaseintegrator.wbi_config import config as wbi_config
8-
from wikibaseintegrator.wbi_enums import ActionIfExists
9-
from wikibaseintegrator.wbi_fastrun import get_fastrun_container
109

1110
wbi_config['USER_AGENT'] = 'WikibaseIntegrator-pytest/1.0 (test_all.py)'
1211

@@ -53,66 +52,24 @@ class TestFastRun(unittest.TestCase):
5352
"""
5453
some basic tests for fastrun mode
5554
"""
55+
logging.basicConfig(level=logging.DEBUG)
5656

5757
def test_fastrun(self):
5858
statements = [
59-
datatypes.ExternalID(value='P40095', prop_nr='P352'),
59+
datatypes.ExternalID(value='A0A023PZB3', prop_nr='P352'),
6060
datatypes.ExternalID(value='YER158C', prop_nr='P705')
6161
]
6262

6363
frc = wbi_fastrun.FastRunContainer(base_filter=[BaseDataType(prop_nr='P352'), datatypes.Item(prop_nr='P703', value='Q27510868')], base_data_type=datatypes.BaseDataType)
6464

65-
fastrun_result = frc.write_required(data=statements)
65+
entity = BaseEntity().add_claims(statements)
6666

67-
if fastrun_result:
68-
message = 'fastrun failed'
69-
else:
70-
message = 'successful fastrun'
67+
fastrun_result = frc.write_required(entity=entity)
7168

7269
# here, fastrun should succeed, if not, test failed
7370
if fastrun_result:
7471
raise ValueError
7572

76-
def test_fastrun_label(self):
77-
# tests fastrun label, description and aliases, and label in another language
78-
frc = get_fastrun_container(base_filter=[datatypes.ExternalID(value='/m/02j71', prop_nr='P646')])
79-
item = WikibaseIntegrator().item.get('Q2')
80-
81-
assert item.labels.get(language='en') == "Earth"
82-
descr = item.descriptions.get(language='en')
83-
assert len(descr) > 3
84-
assert "the Earth" in item.aliases.get()
85-
86-
assert list(frc.get_language_data("Q2", 'en', 'label'))[0] == item.labels.get(language='en')
87-
assert frc.check_language_data("Q2", ['not the Earth'], 'en', 'label')
88-
assert "the Earth" in item.aliases.get()
89-
assert "planet" in item.descriptions.get()
90-
91-
assert item.labels.get('es') == "Tierra"
92-
93-
item.descriptions.set(value=descr)
94-
item.descriptions.set(value="fghjkl")
95-
assert item.get_json()['descriptions']['en'] == {'language': 'en', 'value': 'fghjkl'}
96-
item.labels.set(value="Earth")
97-
item.labels.set(value="xfgfdsg")
98-
assert item.get_json()['labels']['en'] == {'language': 'en', 'value': 'xfgfdsg'}
99-
item.aliases.set(values=["fake alias"], action_if_exists=ActionIfExists.APPEND)
100-
assert {'language': 'en', 'value': 'fake alias'} in item.get_json()['aliases']['en']
101-
102-
# something thats empty (for now.., can change, so this just makes sure no exception is thrown)
103-
frc.check_language_data("Q2", ['Ewiase'], 'ak', 'label')
104-
frc.check_language_data("Q2", ['not Ewiase'], 'ak', 'label')
105-
frc.check_language_data("Q2", [''], 'ak', 'description')
106-
frc.check_language_data("Q2", [], 'ak', 'aliases')
107-
frc.check_language_data("Q2", ['sdf', 'sdd'], 'ak', 'aliases')
108-
109-
item.labels.get(language="ak")
110-
item.descriptions.get(language='ak')
111-
item.aliases.get(language="ak")
112-
item.labels.set(value="label", language="ak")
113-
item.descriptions.set(value="d", language="ak")
114-
item.aliases.set(values=["a"], language="ak", action_if_exists=ActionIfExists.APPEND)
115-
11673

11774
def test_sitelinks():
11875
item = wbi.item.get('Q622901')

test/test_entity_item.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ def test_write(self):
4848
def test_write_not_required(self):
4949
assert not wbi.item.get('Q582').write_required(base_filter=[BaseDataType(prop_nr='P1791')])
5050

51+
def test_write_not_required_ref(self):
52+
assert not wbi.item.get('Q582').write_required(base_filter=[BaseDataType(prop_nr='P1464')], use_refs=True)
53+
5154
def test_write_required(self):
5255
item = wbi.item.get('Q582')
5356
item.claims.add(Item(prop_nr='P1791', value='Q42'))
5457
assert item.write_required([BaseDataType(prop_nr='P1791')])
5558

56-
def test_write_not_required_ref(self):
57-
assert not wbi.item.get('Q582').write_required(base_filter=[BaseDataType(prop_nr='P2581')], use_refs=True)
58-
5959
def test_write_required_ref(self):
6060
item = wbi.item.get('Q582')
61-
item.claims.get('P2581')[0].references.references.pop()
62-
assert item.write_required(base_filter=[BaseDataType(prop_nr='P2581')], use_refs=True)
61+
item.claims.get('P1464')[0].references.references.pop()
62+
assert item.write_required(base_filter=[BaseDataType(prop_nr='P1464')], use_refs=True)

test/test_wbi_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def test_new_item_creation(self):
231231
MonolingualText(text="xxx", language="fr", prop_nr="P7"),
232232
Quantity(amount=-5.04, prop_nr="P8"),
233233
Quantity(amount=5.06, upper_bound=9.99, lower_bound=-2.22, unit="Q11573", prop_nr="P8"),
234-
CommonsMedia(value="xxx", prop_nr="P9"),
234+
CommonsMedia(value="Place lazare goujon.jpg", prop_nr="P9"),
235235
GlobeCoordinate(latitude=1.2345, longitude=-1.2345, precision=12, prop_nr="P10"),
236236
GeoShape(value="Data:xxx.map", prop_nr="P11"),
237237
Property(value="P123", prop_nr="P12"),

test/test_wbi_fastrun.py

Lines changed: 0 additions & 211 deletions
This file was deleted.

wikibaseintegrator/datatypes/basedatatype.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import re
4-
from typing import Any, List, Type, Union
4+
from typing import Any, Dict, List, Optional, Type, Union
55

66
from wikibaseintegrator.models import Claim
77

@@ -11,6 +11,7 @@ class BaseDataType(Claim):
1111
The base class for all Wikibase data types, they inherit from it
1212
"""
1313
DTYPE = 'base-data-type'
14+
PTYPE = 'property-data-type'
1415
subclasses: List[Type[BaseDataType]] = []
1516
sparql_query: str = '''
1617
SELECT * WHERE {{
@@ -28,7 +29,14 @@ def __init__(self, prop_nr: Union[int, str] = None, **kwargs: Any):
2829

2930
super().__init__(**kwargs)
3031

31-
self.mainsnak.property_number = prop_nr or None
32+
if isinstance(prop_nr, str):
33+
pattern = re.compile(r'^([a-z][a-z\d+.-]*):([^][<>\"\x00-\x20\x7F])+$')
34+
matches = pattern.match(str(prop_nr))
35+
36+
if matches:
37+
prop_nr = prop_nr.rsplit('/', 1)[-1]
38+
39+
self.mainsnak.property_number = prop_nr
3240
# self.subclasses.append(self)
3341

3442
# Allow registration of subclasses of BaseDataType into BaseDataType.subclasses
@@ -39,7 +47,7 @@ def __init_subclass__(cls, **kwargs):
3947
def set_value(self, value: Any = None):
4048
pass
4149

42-
def get_sparql_value(self) -> str:
50+
def get_sparql_value(self, **kwargs) -> Optional[str]:
4351
return '"' + self.mainsnak.datavalue['value'] + '"'
4452

4553
def parse_sparql_value(self, value, type='literal', unit='1') -> bool:
@@ -61,3 +69,6 @@ def parse_sparql_value(self, value, type='literal', unit='1') -> bool:
6169
raise ValueError
6270

6371
return True
72+
73+
def from_sparql_value(self, sparql_value: Dict) -> BaseDataType:
74+
pass

0 commit comments

Comments
 (0)