|
| 1 | +import csv |
| 2 | + |
1 | 3 | import attr |
2 | | -import pytest |
3 | 4 | import requests_mock |
4 | 5 |
|
5 | 6 | from dsaps import models |
6 | 7 |
|
7 | 8 |
|
8 | | -@pytest.fixture |
9 | | -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 | | -@pytest.fixture |
18 | | -def sample_content_1(tmp_path): |
19 | | - content = 'test' |
20 | | - dir = tmp_path / 'sub' |
21 | | - dir.mkdir() |
22 | | - sample_content = dir / '123_1.pdf' |
23 | | - sample_content.write_text(content) |
24 | | - return sample_content |
25 | | - |
26 | | - |
27 | | -@pytest.fixture |
28 | | -def sample_content_2(tmp_path): |
29 | | - content = 'test' |
30 | | - dir = tmp_path / 'sub' |
31 | | - sample_content = dir / '123_2.pdf' |
32 | | - sample_content.write_text(content) |
33 | | - return sample_content |
34 | | - |
35 | | - |
36 | 9 | def test_authenticate(client): |
37 | 10 | """Test authenticate method.""" |
38 | | - with requests_mock.Mocker() as m: |
39 | | - email = 'test@test.mock' |
40 | | - password = '1234' |
41 | | - cookies = {'JSESSIONID': '11111111'} |
42 | | - user_json = {'fullname': 'User Name'} |
43 | | - m.post('mock://example.com/login', cookies=cookies) |
44 | | - m.get('mock://example.com/status', json=user_json) |
45 | | - client.authenticate(email, password) |
46 | | - assert client.user_full_name == 'User Name' |
47 | | - assert client.cookies == cookies |
| 11 | + email = 'test@test.mock' |
| 12 | + password = '1234' |
| 13 | + client.authenticate(email, password) |
| 14 | + assert client.user_full_name == 'User Name' |
| 15 | + assert client.cookies == {'JSESSIONID': '11111111'} |
48 | 16 |
|
49 | 17 |
|
50 | 18 | def test_get_record(client): |
51 | 19 | """Test get_record method.""" |
52 | | - with requests_mock.Mocker() as m: |
53 | | - uri = 'mock://example.com/items/123?expand=all' |
54 | | - rec_json = {'metadata': {'title': 'Sample title'}, 'type': 'item'} |
55 | | - m.get(uri, json=rec_json) |
56 | | - rec_obj = client.get_record('123', 'items') |
57 | | - assert attr.asdict(rec_obj)['metadata'] == rec_json['metadata'] |
| 20 | + rec_obj = client.get_record('123', 'items') |
| 21 | + assert attr.asdict(rec_obj)['metadata'] == {'title': 'Sample title'} |
58 | 22 |
|
59 | 23 |
|
60 | 24 | def test_filtered_item_search(client): |
61 | 25 | """Test filtered_item_search method.""" |
62 | | - with requests_mock.Mocker() as m: |
63 | | - key = 'dc.title' |
64 | | - string = 'test' |
65 | | - query_type = 'contains' |
66 | | - endpoint = 'mock://example.com/filtered-items?' |
67 | | - results_json1 = {'items': [{'link': '1234'}]} |
68 | | - results_json2 = {'items': []} |
69 | | - m.get(endpoint, [{'json': results_json1}, {'json': results_json2}]) |
70 | | - |
71 | | - item_links = client.filtered_item_search(key, string, query_type, |
72 | | - selected_collections='') |
73 | | - assert '1234' in item_links |
| 26 | + key = 'dc.title' |
| 27 | + string = 'test' |
| 28 | + query_type = 'contains' |
| 29 | + item_links = client.filtered_item_search(key, string, query_type, |
| 30 | + selected_collections='') |
| 31 | + assert '1234' in item_links |
74 | 32 |
|
75 | 33 |
|
76 | 34 | def test_get_id_from_handle(client): |
77 | 35 | """Test get_id_from_handle method.""" |
78 | | - with requests_mock.Mocker() as m: |
79 | | - handle = 'mock://example.com/handle/111.1111' |
80 | | - rec_json = {'uuid': '123'} |
81 | | - m.get(handle, json=rec_json) |
82 | | - id = client.get_id_from_handle('111.1111') |
83 | | - assert id == '123' |
| 36 | + id = client.get_id_from_handle('111.1111') |
| 37 | + assert id == '123' |
84 | 38 |
|
85 | 39 |
|
86 | 40 | def test_post_coll_to_comm(client): |
87 | 41 | """Test post_coll_to_comm method.""" |
88 | | - with requests_mock.Mocker() as m: |
89 | | - comm_handle = '1234' |
90 | | - coll_name = 'Test Collection' |
91 | | - comm_json = {'uuid': 'a1b2'} |
92 | | - coll_json = {'uuid': '5678'} |
93 | | - m.get('mock://example.com/handle/1234', json=comm_json) |
94 | | - m.post('mock://example.com/communities/a1b2/collections', |
95 | | - json=coll_json) |
96 | | - coll_id = client.post_coll_to_comm(comm_handle, coll_name) |
97 | | - assert coll_id == '5678' |
| 42 | + comm_handle = '1234' |
| 43 | + coll_name = 'Test Collection' |
| 44 | + coll_id = client.post_coll_to_comm(comm_handle, coll_name) |
| 45 | + assert coll_id == '5678' |
98 | 46 |
|
99 | 47 |
|
100 | 48 | def test_post_items_to_coll(client, sample_content_1): |
101 | 49 | """Test post_items_to_coll method.""" |
102 | | - with requests_mock.Mocker() as m: |
103 | | - coll_metadata = [{"metadata": [ |
104 | | - {"key": "file_identifier", |
105 | | - "value": "123"}, |
106 | | - {"key": "dc.title", "value": |
107 | | - "Monitoring Works: Getting Teachers", |
108 | | - "language": "en_US"}, |
109 | | - {"key": "dc.relation.isversionof", |
110 | | - "value": "repo/0/ao/123"}]}] |
111 | | - coll_id = '789' |
112 | | - ingest_type = 'local' |
113 | | - file_dict = {'123': sample_content_1} |
114 | | - item_json = {'uuid': 'a1b2', 'handle': '1111.1/1111'} |
115 | | - m.post('mock://example.com/collections/789/items', json=item_json) |
116 | | - url = 'mock://example.com/items/a1b2/bitstreams?name=123_1.pdf' |
117 | | - b_json = {'uuid': 'c3d4'} |
118 | | - m.post(url, json=b_json) |
119 | | - item_ids = client.post_items_to_coll(coll_id, coll_metadata, file_dict, |
120 | | - ingest_type) |
121 | | - for item_id in item_ids: |
122 | | - assert 'a1b2' == item_id |
| 50 | + coll_metadata = [{"metadata": [ |
| 51 | + {"key": "file_identifier", |
| 52 | + "value": "123"}, |
| 53 | + {"key": "dc.title", "value": |
| 54 | + "Monitoring Works: Getting Teachers", |
| 55 | + "language": "en_US"}, |
| 56 | + {"key": "dc.relation.isversionof", |
| 57 | + "value": "repo/0/ao/123"}]}] |
| 58 | + coll_id = '789' |
| 59 | + ingest_type = 'local' |
| 60 | + file_dict = {'123': sample_content_1} |
| 61 | + item_ids = client.post_items_to_coll(coll_id, coll_metadata, file_dict, |
| 62 | + ingest_type) |
| 63 | + for item_id in item_ids: |
| 64 | + assert 'a1b2' == item_id |
123 | 65 |
|
124 | 66 |
|
125 | 67 | def test_post_bitstreams_to_item(client, sample_content_1, sample_content_2): |
126 | 68 | """Test post_bitstreams_to_item method.""" |
127 | | - with requests_mock.Mocker() as m: |
128 | | - item_id = 'a1b2' |
129 | | - ingest_type = 'local' |
130 | | - file_identifier = '123' |
131 | | - file_dict = {'123_2': sample_content_2, '123_1': sample_content_1} |
132 | | - b_json_1 = {'uuid': 'c3d4'} |
133 | | - url_1 = 'mock://example.com/items/a1b2/bitstreams?name=123_1.pdf' |
134 | | - m.post(url_1, json=b_json_1) |
135 | | - b_json_2 = {'uuid': 'e5f6'} |
136 | | - url_2 = 'mock://example.com/items/a1b2/bitstreams?name=123_2.pdf' |
137 | | - m.post(url_2, json=b_json_2) |
138 | | - bit_ids = client.post_bitstreams_to_item(item_id, file_identifier, |
139 | | - file_dict, ingest_type) |
140 | | - bit_ids_output = [] |
141 | | - for bit_id in bit_ids: |
142 | | - bit_ids_output.append(bit_id) |
143 | | - assert bit_ids_output[0] == 'c3d4' |
144 | | - assert bit_ids_output[1] == 'e5f6' |
145 | | - |
146 | | - |
147 | | -def test_post_bitstream(client, sample_content_1): |
148 | | - """Test post_bitstream method.""" |
149 | | - with requests_mock.Mocker() as m: |
150 | | - item_id = 'a1b2' |
151 | | - ingest_type = 'local' |
152 | | - file_identifier = '123' |
153 | | - file_dict = {'123': sample_content_1} |
154 | | - b_json = {'uuid': 'c3d4'} |
155 | | - url = 'mock://example.com/items/a1b2/bitstreams?name=123_1.pdf' |
156 | | - bitstream = '123' |
157 | | - m.post(url, json=b_json) |
158 | | - bit_id = client.post_bitstream(item_id, file_identifier, file_dict, |
159 | | - ingest_type, bitstream) |
| 69 | + item_id = 'a1b2' |
| 70 | + ingest_type = 'local' |
| 71 | + file_identifier = '123' |
| 72 | + file_dict = {'123': sample_content_1} |
| 73 | + bit_ids = client.post_bitstreams_to_item(item_id, file_identifier, |
| 74 | + file_dict, ingest_type) |
| 75 | + for bit_id in bit_ids: |
160 | 76 | assert 'c3d4' == bit_id |
161 | 77 |
|
162 | 78 |
|
@@ -194,10 +110,15 @@ def test_build_file_dict_remote(): |
194 | 110 | assert '999' in file_list |
195 | 111 |
|
196 | 112 |
|
197 | | -# # How to test this? Applies to asaps as well |
198 | | -# def test_create_csv_from_list(): |
199 | | -# """Test create_csv_from_list function.""" |
200 | | -# assert False |
| 113 | +def test_create_csv_from_list(runner): |
| 114 | + """Test create_csv_from_list function.""" |
| 115 | + with runner.isolated_filesystem(): |
| 116 | + list_name = ['123'] |
| 117 | + models.create_csv_from_list(list_name, 'output') |
| 118 | + with open('output.csv') as csvfile: |
| 119 | + reader = csv.DictReader(csvfile) |
| 120 | + for row in reader: |
| 121 | + assert row['id'] == '123' |
201 | 122 |
|
202 | 123 |
|
203 | 124 | def test_metadata_elems_from_row(): |
|
0 commit comments