Skip to content

Commit 05c977e

Browse files
committed
conftest
1 parent 85a27ca commit 05c977e

File tree

3 files changed

+122
-132
lines changed

3 files changed

+122
-132
lines changed

dsaps/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def build_file_dict_remote(directory_url, file_type, file_dict):
215215

216216
def create_csv_from_list(list_name, output):
217217
"""Creates CSV file from list content."""
218-
with open(output, 'w') as f:
218+
with open(f'{output}.csv', 'w') as f:
219219
writer = csv.writer(f)
220220
writer.writerow(['id'])
221221
for item in list_name:

tests/conftest.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from click.testing import CliRunner
2+
import pytest
3+
import requests_mock
4+
5+
from dsaps import models
6+
7+
8+
@pytest.fixture(autouse=True)
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(autouse=True)
18+
def ds_mock():
19+
with requests_mock.Mocker() as m:
20+
cookies = {'JSESSIONID': '11111111'}
21+
m.post('mock://example.com/login', cookies=cookies)
22+
user_json = {'fullname': 'User Name'}
23+
m.get('mock://example.com/status', json=user_json)
24+
rec_json = {'metadata': {'title': 'Sample title'}, 'type': 'item'}
25+
m.get('mock://example.com/items/123?expand=all', json=rec_json)
26+
results_json1 = {'items': [{'link': '1234'}]}
27+
results_json2 = {'items': []}
28+
m.get('mock://example.com/filtered-items?', [{'json': results_json1},
29+
{'json': results_json2}])
30+
rec_json = {'uuid': '123'}
31+
m.get('mock://example.com/handle/111.1111', json=rec_json)
32+
comm_json = {'uuid': 'a1b2'}
33+
m.get('mock://example.com/handle/1234', json=comm_json)
34+
coll_json = {'uuid': '5678'}
35+
m.post('mock://example.com/communities/a1b2/collections',
36+
json=coll_json)
37+
item_json = {'uuid': 'a1b2', 'handle': '1111.1/1111'}
38+
m.post('mock://example.com/collections/789/items', json=item_json)
39+
b_json_1 = {'uuid': 'c3d4'}
40+
url_1 = 'mock://example.com/items/a1b2/bitstreams?name=123_1.pdf'
41+
m.post(url_1, json=b_json_1)
42+
b_json_2 = {'uuid': 'e5f6'}
43+
url_2 = 'mock://example.com/items/a1b2/bitstreams?name=123_2.pdf'
44+
m.post(url_2, json=b_json_2)
45+
yield m
46+
47+
48+
@pytest.fixture(autouse=True)
49+
def runner():
50+
return CliRunner()
51+
52+
53+
@pytest.fixture(autouse=True)
54+
def sample_content_1(tmp_path):
55+
content = 'test'
56+
dir = tmp_path / 'sub'
57+
dir.mkdir()
58+
sample_content = dir / '123_1.pdf'
59+
sample_content.write_text(content)
60+
return sample_content
61+
62+
63+
@pytest.fixture(autouse=True)
64+
def sample_content_2(tmp_path):
65+
content = 'test'
66+
dir = tmp_path / 'sub'
67+
sample_content = dir / '123_2.pdf'
68+
sample_content.write_text(content)
69+
return sample_content

tests/test_models.py

Lines changed: 52 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,162 +1,78 @@
1+
import csv
2+
13
import attr
2-
import pytest
34
import requests_mock
45

56
from dsaps import models
67

78

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-
369
def test_authenticate(client):
3710
"""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'}
4816

4917

5018
def test_get_record(client):
5119
"""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'}
5822

5923

6024
def test_filtered_item_search(client):
6125
"""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
7432

7533

7634
def test_get_id_from_handle(client):
7735
"""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'
8438

8539

8640
def test_post_coll_to_comm(client):
8741
"""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'
9846

9947

10048
def test_post_items_to_coll(client, sample_content_1):
10149
"""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
12365

12466

12567
def test_post_bitstreams_to_item(client, sample_content_1, sample_content_2):
12668
"""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:
16076
assert 'c3d4' == bit_id
16177

16278

@@ -194,10 +110,15 @@ def test_build_file_dict_remote():
194110
assert '999' in file_list
195111

196112

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'
201122

202123

203124
def test_metadata_elems_from_row():

0 commit comments

Comments
 (0)