|
1 | | -import unittest |
| 1 | +import logging |
2 | 2 |
|
| 3 | +import pytest |
3 | 4 | import requests |
4 | 5 |
|
5 | | -from wikibaseintegrator import WikibaseIntegrator |
| 6 | +from wikibaseintegrator import WikibaseIntegrator, wbi_login |
6 | 7 | from wikibaseintegrator.datatypes import BaseDataType, Item |
7 | 8 | from wikibaseintegrator.wbi_config import config as wbi_config |
8 | 9 | from wikibaseintegrator.wbi_exceptions import NonExistentEntityError |
9 | 10 |
|
10 | 11 | wbi_config['USER_AGENT'] = 'WikibaseIntegrator-pytest/1.0 (test_entity_item.py)' |
| 12 | +wbi_config['MEDIAWIKI_API_URL'] = 'http://localhost/w/api.php' |
| 13 | +wbi_config['SPARQL_ENDPOINT_URL'] = 'http://localhost:8834/proxy/wdqs/bigdata/namespace/wdq/sparql' |
| 14 | +wbi_config['WIKIBASE_URL'] = 'http://wikibase.svc' |
11 | 15 |
|
12 | | -wbi = WikibaseIntegrator() |
| 16 | +wbi = WikibaseIntegrator(login=wbi_login.Login(user='admin', password='change-this-password')) |
| 17 | +logging.basicConfig(level=logging.DEBUG) |
13 | 18 |
|
14 | 19 |
|
15 | | -class TestEntityItem(unittest.TestCase): |
| 20 | +@pytest.mark.order('first') |
| 21 | +@pytest.fixture |
| 22 | +def test_item_creation(): |
| 23 | + return wbi.item.new().write().id |
16 | 24 |
|
17 | | - def test_get(self): |
18 | | - # Test with complete id |
19 | | - assert wbi.item.get('Q582').id == 'Q582' |
20 | | - # Test with numeric id as string |
21 | | - assert wbi.item.get('582').id == 'Q582' |
22 | | - # Test with numeric id as int |
23 | | - assert wbi.item.get(582).id == 'Q582' |
24 | 25 |
|
25 | | - # Test with invalid id |
26 | | - with self.assertRaises(ValueError): |
27 | | - wbi.item.get('L5') |
| 26 | +def test_get(test_item_creation): |
| 27 | + entity_id = test_item_creation |
| 28 | + # Test with complete id |
| 29 | + assert wbi.item.get(entity_id).id == entity_id |
| 30 | + # Test with numeric id as string |
| 31 | + assert wbi.item.get(entity_id).id == entity_id |
| 32 | + # Test with numeric id as int |
| 33 | + assert wbi.item.get(entity_id).id == entity_id |
28 | 34 |
|
29 | | - # Test with zero id |
30 | | - with self.assertRaises(ValueError): |
31 | | - wbi.item.get(0) |
| 35 | + # Test with invalid id |
| 36 | + with self.assertRaises(ValueError): |
| 37 | + wbi.item.get('L5') |
32 | 38 |
|
33 | | - # Test with negative id |
34 | | - with self.assertRaises(ValueError): |
35 | | - wbi.item.get(-1) |
| 39 | + # Test with zero id |
| 40 | + with self.assertRaises(ValueError): |
| 41 | + wbi.item.get(0) |
36 | 42 |
|
37 | | - # Test with negative id |
38 | | - with self.assertRaises(NonExistentEntityError): |
39 | | - wbi.item.get("Q99999999999999") |
| 43 | + # Test with negative id |
| 44 | + with self.assertRaises(ValueError): |
| 45 | + wbi.item.get(-1) |
40 | 46 |
|
41 | | - def test_get_json(self): |
42 | | - assert wbi.item.get('Q582').get_json()['labels']['fr']['value'] == 'Villeurbanne' |
| 47 | + # Test with negative id |
| 48 | + with self.assertRaises(NonExistentEntityError): |
| 49 | + wbi.item.get("Q99999999999999") |
43 | 50 |
|
44 | | - def test_write(self): |
45 | | - with self.assertRaises(requests.exceptions.JSONDecodeError): |
46 | | - wbi.item.get('Q582').write(allow_anonymous=True, mediawiki_api_url='https://httpstat.us/200') |
47 | 51 |
|
48 | | - def test_write_not_required(self): |
49 | | - assert not wbi.item.get('Q582').write_required(base_filter=[BaseDataType(prop_nr='P1791')]) |
| 52 | +def test_get_json(): |
| 53 | + assert wbi.item.get('Q582').get_json()['labels']['fr']['value'] == 'Villeurbanne' |
50 | 54 |
|
51 | | - def test_write_required(self): |
52 | | - item = wbi.item.get('Q582') |
53 | | - item.claims.add(Item(prop_nr='P1791', value='Q42')) |
54 | | - assert item.write_required([BaseDataType(prop_nr='P1791')]) |
55 | 55 |
|
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) |
| 56 | +def test_write(): |
| 57 | + with self.assertRaises(requests.exceptions.JSONDecodeError): |
| 58 | + wbi.item.get('Q582').write(allow_anonymous=True, mediawiki_api_url='https://httpstat.us/200') |
58 | 59 |
|
59 | | - def test_write_required_ref(self): |
60 | | - 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) |
63 | 60 |
|
64 | | - def test_long_item_id(self): |
65 | | - assert wbi.item.get('Item:Q582').id == 'Q582' |
| 61 | +def test_write_not_required(): |
| 62 | + assert not wbi.item.get('Q582').write_required(base_filter=[BaseDataType(prop_nr='P1791')]) |
| 63 | + |
| 64 | + |
| 65 | +def test_write_required(): |
| 66 | + item = wbi.item.get('Q582') |
| 67 | + item.claims.add(Item(prop_nr='P1791', value='Q42')) |
| 68 | + assert item.write_required([BaseDataType(prop_nr='P1791')]) |
| 69 | + |
| 70 | + |
| 71 | +def test_write_not_required_ref(): |
| 72 | + assert not wbi.item.get('Q582').write_required(base_filter=[BaseDataType(prop_nr='P2581')], use_refs=True) |
| 73 | + |
| 74 | + |
| 75 | +def test_write_required_ref(): |
| 76 | + item = wbi.item.get('Q582') |
| 77 | + item.claims.get('P2581')[0].references.references.pop() |
| 78 | + assert item.write_required(base_filter=[BaseDataType(prop_nr='P2581')], use_refs=True) |
| 79 | + |
| 80 | + |
| 81 | +def test_long_item_id(): |
| 82 | + assert wbi.item.get('Item:Q582').id == 'Q582' |
0 commit comments