From db42f61cfb29c762a17b5e41069bd9dac194c2fb Mon Sep 17 00:00:00 2001 From: Eric Hanson Date: Tue, 26 Nov 2019 13:33:57 -0500 Subject: [PATCH] add tests --- dsaps/cli.py | 3 +- dsaps/models.py | 22 +++++++++----- tests/test_models.py | 71 +++++++++++++++++++++++++++++++------------- 3 files changed, 66 insertions(+), 30 deletions(-) diff --git a/dsaps/cli.py b/dsaps/cli.py index af68ffc..e209cc1 100644 --- a/dsaps/cli.py +++ b/dsaps/cli.py @@ -36,7 +36,8 @@ def main(ctx, url, email, password): 'w')], level=logging.INFO) logger.info('Application start') - client = models.Client(url, email, password) + client = models.Client(url) + client.authenticate(email, password) start_time = time.time() ctx.obj['client'] = client ctx.obj['start_time'] = start_time diff --git a/dsaps/models.py b/dsaps/models.py index 66a906b..c6c8538 100644 --- a/dsaps/models.py +++ b/dsaps/models.py @@ -14,9 +14,11 @@ class Client: - def __init__(self, url, email, password): + def __init__(self, url): self.url = url logger.info('Initializing client') + + def authenticate(self, email, password): data = {'email': email, 'password': password} header = {'content-type': 'application/json', 'accept': 'application/json'} @@ -52,12 +54,16 @@ def filtered_item_search(self, key, string, query_type, items = '' item_links = [] while items != []: - endpoint = f'{self.url}/rest/filtered-items?query_field[]=' - endpoint += f'{key}&query_op[]={query_type}&query_val[]={string}' - endpoint += f'{selected_collections}&limit=200&offset={offset}' - logger.info(endpoint) + endpoint = f'{self.url}/rest/filtered-items?' + params = {'query_field[]': key, 'query_op[]': query_type, + 'query_val[]': string, '&collSel[]': + selected_collections, 'limit': 200, 'offset': offset} + logger.info(params) + print(endpoint) response = requests.get(endpoint, headers=self.header, - cookies=self.cookies).json() + params=params, cookies=self.cookies) + print(f'Response url: {response.url}') + response = response.json() items = response['items'] for item in items: item_links.append(item['link']) @@ -73,12 +79,12 @@ def _pop_inst(self, class_type, rec_obj): collections = self._build_uuid_list(rec_obj, kwargs, 'collections') rec_obj['collections'] = collections elif class_type == Collection: - items = self._build_uuid_list(rec_obj, kwargs, 'items') + items = self._build_uuid_list(rec_obj, 'items') rec_obj['items'] = items rec_obj = class_type(**kwargs) return rec_obj - def _build_uuid_list(self, rec_obj, kwargs, children): + def _build_uuid_list(self, rec_obj, children): child_list = [] for child in rec_obj[children]: child_list.append(child['uuid']) diff --git a/tests/test_models.py b/tests/test_models.py index 7b6cbaf..cb68397 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -7,15 +7,30 @@ @pytest.fixture def client(): + client = models.Client('mock://example.com') + client.header = {} + client.cookies = {} + client.user_full_name = '' + return client + + +def test_authenticate(client): + """Test authenticate function.""" with requests_mock.Mocker() as m: - uri1 = 'mock://example.com/rest/login' - uri2 = 'mock://example.com/rest/status' + url1 = '/rest/login' + url2 = '/rest/status' + email = 'test@test.mock' + password = '1234' + header = {'content-type': 'application/json', 'accept': + 'application/json'} cookies = {'JSESSIONID': '11111111'} json_object = {'fullname': 'User Name'} - m.post(uri1, cookies=cookies) - m.get(uri2, json=json_object) - client = models.Client('mock://example.com', 'test', 'test') - return client + m.post(url1, cookies=cookies) + m.get(url2, json=json_object) + client.authenticate(email, password) + assert client.user_full_name == 'User Name' + assert client.cookies == cookies + assert client.header == header def test_get_record(client): @@ -28,18 +43,32 @@ def test_get_record(client): assert attr.asdict(rec_obj)['metadata'] == json_object['metadata'] -# def test_filtered_item_search(client): -# """Test filtered_item_search function.""" -# item_links = client.filtered_item_search(key, string, query_type, -# selected_collections='') -# assert False -# -# -# def test__pop_inst(client): -# rec_obj = client._pop_inst(class_type, rec_obj) -# assert False -# -# -# def test__build_uuid_list(client): -# child_list = client._build_uuid_list(self, rec_obj, kwargs, children) -# assert False +def test_filtered_item_search(client): + """Test filtered_item_search function.""" + with requests_mock.Mocker() as m: + key = 'dc.title' + string = 'test' + query_type = 'contains' + endpoint = '/rest/filtered-items?' + json_object_1 = {'items': [{'link': '1234'}]} + json_object_2 = {'items': []} + m.get(endpoint, [{'json': json_object_1}, {'json': json_object_2}]) + + item_links = client.filtered_item_search(key, string, query_type, + selected_collections='') + assert '1234' in item_links + + +def test__pop_inst(client): + class_type = models.Collection + rec_obj = {'name': 'Test title', 'type': 'collection', 'items': []} + rec_obj = client._pop_inst(class_type, rec_obj) + assert type(rec_obj) == class_type + assert rec_obj.name == 'Test title' + + +def test__build_uuid_list(client): + rec_obj = {'items': [{'uuid': '1234'}]} + children = 'items' + child_list = client._build_uuid_list(rec_obj, children) + assert '1234' in child_list