Skip to content

Commit db42f61

Browse files
committed
add tests
1 parent 1ba35eb commit db42f61

File tree

3 files changed

+66
-30
lines changed

3 files changed

+66
-30
lines changed

dsaps/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def main(ctx, url, email, password):
3636
'w')],
3737
level=logging.INFO)
3838
logger.info('Application start')
39-
client = models.Client(url, email, password)
39+
client = models.Client(url)
40+
client.authenticate(email, password)
4041
start_time = time.time()
4142
ctx.obj['client'] = client
4243
ctx.obj['start_time'] = start_time

dsaps/models.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515

1616
class Client:
17-
def __init__(self, url, email, password):
17+
def __init__(self, url):
1818
self.url = url
1919
logger.info('Initializing client')
20+
21+
def authenticate(self, email, password):
2022
data = {'email': email, 'password': password}
2123
header = {'content-type': 'application/json', 'accept':
2224
'application/json'}
@@ -52,12 +54,16 @@ def filtered_item_search(self, key, string, query_type,
5254
items = ''
5355
item_links = []
5456
while items != []:
55-
endpoint = f'{self.url}/rest/filtered-items?query_field[]='
56-
endpoint += f'{key}&query_op[]={query_type}&query_val[]={string}'
57-
endpoint += f'{selected_collections}&limit=200&offset={offset}'
58-
logger.info(endpoint)
57+
endpoint = f'{self.url}/rest/filtered-items?'
58+
params = {'query_field[]': key, 'query_op[]': query_type,
59+
'query_val[]': string, '&collSel[]':
60+
selected_collections, 'limit': 200, 'offset': offset}
61+
logger.info(params)
62+
print(endpoint)
5963
response = requests.get(endpoint, headers=self.header,
60-
cookies=self.cookies).json()
64+
params=params, cookies=self.cookies)
65+
print(f'Response url: {response.url}')
66+
response = response.json()
6167
items = response['items']
6268
for item in items:
6369
item_links.append(item['link'])
@@ -73,12 +79,12 @@ def _pop_inst(self, class_type, rec_obj):
7379
collections = self._build_uuid_list(rec_obj, kwargs, 'collections')
7480
rec_obj['collections'] = collections
7581
elif class_type == Collection:
76-
items = self._build_uuid_list(rec_obj, kwargs, 'items')
82+
items = self._build_uuid_list(rec_obj, 'items')
7783
rec_obj['items'] = items
7884
rec_obj = class_type(**kwargs)
7985
return rec_obj
8086

81-
def _build_uuid_list(self, rec_obj, kwargs, children):
87+
def _build_uuid_list(self, rec_obj, children):
8288
child_list = []
8389
for child in rec_obj[children]:
8490
child_list.append(child['uuid'])

tests/test_models.py

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,30 @@
77

88
@pytest.fixture
99
def client():
10+
client = models.Client('mock://example.com')
11+
client.header = {}
12+
client.cookies = {}
13+
client.user_full_name = ''
14+
return client
15+
16+
17+
def test_authenticate(client):
18+
"""Test authenticate function."""
1019
with requests_mock.Mocker() as m:
11-
uri1 = 'mock://example.com/rest/login'
12-
uri2 = 'mock://example.com/rest/status'
20+
url1 = '/rest/login'
21+
url2 = '/rest/status'
22+
email = 'test@test.mock'
23+
password = '1234'
24+
header = {'content-type': 'application/json', 'accept':
25+
'application/json'}
1326
cookies = {'JSESSIONID': '11111111'}
1427
json_object = {'fullname': 'User Name'}
15-
m.post(uri1, cookies=cookies)
16-
m.get(uri2, json=json_object)
17-
client = models.Client('mock://example.com', 'test', 'test')
18-
return client
28+
m.post(url1, cookies=cookies)
29+
m.get(url2, json=json_object)
30+
client.authenticate(email, password)
31+
assert client.user_full_name == 'User Name'
32+
assert client.cookies == cookies
33+
assert client.header == header
1934

2035

2136
def test_get_record(client):
@@ -28,18 +43,32 @@ def test_get_record(client):
2843
assert attr.asdict(rec_obj)['metadata'] == json_object['metadata']
2944

3045

31-
# def test_filtered_item_search(client):
32-
# """Test filtered_item_search function."""
33-
# item_links = client.filtered_item_search(key, string, query_type,
34-
# selected_collections='')
35-
# assert False
36-
#
37-
#
38-
# def test__pop_inst(client):
39-
# rec_obj = client._pop_inst(class_type, rec_obj)
40-
# assert False
41-
#
42-
#
43-
# def test__build_uuid_list(client):
44-
# child_list = client._build_uuid_list(self, rec_obj, kwargs, children)
45-
# assert False
46+
def test_filtered_item_search(client):
47+
"""Test filtered_item_search function."""
48+
with requests_mock.Mocker() as m:
49+
key = 'dc.title'
50+
string = 'test'
51+
query_type = 'contains'
52+
endpoint = '/rest/filtered-items?'
53+
json_object_1 = {'items': [{'link': '1234'}]}
54+
json_object_2 = {'items': []}
55+
m.get(endpoint, [{'json': json_object_1}, {'json': json_object_2}])
56+
57+
item_links = client.filtered_item_search(key, string, query_type,
58+
selected_collections='')
59+
assert '1234' in item_links
60+
61+
62+
def test__pop_inst(client):
63+
class_type = models.Collection
64+
rec_obj = {'name': 'Test title', 'type': 'collection', 'items': []}
65+
rec_obj = client._pop_inst(class_type, rec_obj)
66+
assert type(rec_obj) == class_type
67+
assert rec_obj.name == 'Test title'
68+
69+
70+
def test__build_uuid_list(client):
71+
rec_obj = {'items': [{'uuid': '1234'}]}
72+
children = 'items'
73+
child_list = client._build_uuid_list(rec_obj, children)
74+
assert '1234' in child_list

0 commit comments

Comments
 (0)