Skip to content

Commit f6d8b99

Browse files
committed
tests: make tests independent of testing user
1 parent 96a6d34 commit f6d8b99

7 files changed

Lines changed: 87 additions & 69 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
0.16.12
2+
- tests: make tests independent of testing user
13
0.16.11
24
- fix: support CKAN 2.11 (authorization and status_show)
35
- fix: implement non-functional `DownloadQueue.get_status`

tests/common.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@
1313
from dcoraid.upload import UploadQueue
1414

1515

16-
CIRCLE = "dcoraid-circle"
17-
COLLECTION = "dcoraid-collection"
18-
DATASET = "dcoraid-dataset"
1916
SERVER = "dcor-dev.mpl.mpg.de"
20-
USER = "dcoraid"
21-
USER_NAME = "DCOR-Aid tester"
22-
TITLE = "DCOR-Aid test dataset"
23-
2417
dpath = pathlib.Path(__file__).parent / "data" / "calibration_beads_47.rtdc"
2518

2619

@@ -40,14 +33,32 @@ def get_api_key():
4033
return key
4134

4235

36+
@functools.lru_cache()
37+
def get_test_defaults():
38+
api = get_api()
39+
user_dict = api.get("user_show")
40+
name = user_dict["name"]
41+
return {
42+
"circle": f"{name}-circle",
43+
"collection": f"{name}-collection",
44+
"dataset": f"{name}-dataset",
45+
"user": name,
46+
"user_name": f"DCOR-Aid Tester {name.capitalize()}",
47+
"user_dict": user_dict,
48+
"title": "DCOR-Aid test dataset",
49+
"server": SERVER,
50+
}
51+
52+
4353
def make_dataset_dict(hint=""):
54+
defaults = get_test_defaults()
4455
space = " " if hint else ""
4556
dataset_dict = {
4657
"title": f"A test dataset {hint}{space}{time.time()}",
4758
"private": True,
4859
"license_id": "CC0-1.0",
49-
"owner_org": CIRCLE,
50-
"authors": USER_NAME,
60+
"owner_org": defaults["circle"],
61+
"authors": defaults["user_name"],
5162
}
5263
return dataset_dict
5364

tests/conftest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,17 @@ def pytest_sessionstart(session):
8686
before performing collection and entering the run test loop.
8787
"""
8888
api = common.get_api()
89-
api.get("group_show", id=common.COLLECTION)
89+
defaults = common.get_test_defaults()
9090
try:
91-
api.post("group_create", {"name": common.COLLECTION})
91+
api.post("group_create", {"name": defaults["collection"]})
9292
except APIConflictError:
9393
pass
9494
try:
95-
api.post("organization_create", {"name": common.CIRCLE})
95+
api.post("organization_create", {"name": defaults["circle"]})
9696
except APIConflictError:
9797
pass
98-
user_dict = api.get("user_show", id=common.USER)
99-
user_dict["fullname"] = common.USER_NAME
98+
user_dict = api.get("user_show")
99+
user_dict["fullname"] = defaults["user_name"]
100100
api.post("user_update", user_dict)
101101

102102

tests/test_api_dataset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ def test_dataset_creation():
7070
data = dataset_create(dataset_dict=dataset_dict,
7171
api=api,
7272
)
73+
defaults = common.get_test_defaults()
7374
# simple test
7475
assert "authors" in data
75-
assert data["authors"] == common.USER_NAME
76+
assert data["authors"] == defaults["user_name"]
7677
assert data["state"] == "draft"
7778
# remove draft dataset
7879
dataset_draft_remove(dataset_id=data["id"],

tests/test_dbmodel_api.py

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,30 @@
1010

1111
dpath = pathlib.Path(__file__).parent / "data" / "calibration_beads_47.rtdc"
1212

13+
HAS_FIGSHARE_ACCESS = common.get_test_defaults()["user"] == "dcoraid"
1314

15+
16+
@pytest.mark.skipif(not HAS_FIGSHARE_ACCESS,
17+
reason="No access to figshare-import circle")
1418
def test_get_circles():
1519
api = common.get_api()
1620
db = db_api.APIInterrogator(api=api)
1721
circles = db.get_circles()
18-
assert common.CIRCLE in circles
22+
defaults = common.get_test_defaults()
23+
assert defaults["circle"] in circles
1924
# requires that the "dcoraid" user is in the figshare-import circle
2025
assert "figshare-import" in circles
2126

2227

28+
@pytest.mark.skipif(not HAS_FIGSHARE_ACCESS,
29+
reason="No access to figshare-import circle")
2330
def test_get_collections():
2431
api = common.get_api()
2532
db = db_api.APIInterrogator(api=api)
2633
collections = db.get_collections()
27-
assert common.COLLECTION in collections
28-
# requires that the "dcoraid" user is in the figshare-collection collection
34+
defaults = common.get_test_defaults()
35+
assert defaults["collection"] in collections
36+
# requires that the "dcoraid" user is in figshare-collection collection
2937
assert "figshare-collection" in collections
3038

3139

@@ -40,80 +48,84 @@ def test_public_api_interrogator():
4048
"""This test uses the figshare datasets on SERVER"""
4149
api = common.get_api()
4250
db = db_api.APIInterrogator(api=api)
43-
assert common.CIRCLE in db.get_circles()
44-
assert common.COLLECTION in db.get_collections()
45-
assert common.USER in db.get_users()
51+
defaults = common.get_test_defaults()
52+
assert defaults["circle"] in db.get_circles()
53+
assert defaults["collection"] in db.get_collections()
54+
assert defaults["user"] in db.get_users()
4655

4756

4857
def test_user_data():
4958
"""Test the user information"""
5059
api = common.get_api()
5160
db = db_api.APIInterrogator(api=api)
5261
data = db.user_data
53-
assert data["fullname"] == common.USER_NAME, "fullname not correct"
62+
defaults = common.get_test_defaults()
63+
assert data["fullname"] == defaults["user_name"], "fullname not correct"
5464

5565

5666
def test_search_dataset_basic():
5767
api = common.get_api()
5868
ranstr = ''.join(random.choice("0123456789") for _i in range(10))
69+
defaults = common.get_test_defaults()
5970
# Create a test dataset
60-
dataset_create({"title": "{} {}".format(common.TITLE, ranstr),
61-
"owner_org": common.CIRCLE,
62-
"authors": common.USER_NAME,
71+
dataset_create({"title": "{} {}".format(defaults["title"], ranstr),
72+
"owner_org": defaults["circle"],
73+
"authors": defaults["user_name"],
6374
"license_id": "CC0-1.0",
64-
"groups": [{"name": common.COLLECTION}],
75+
"groups": [{"name": defaults["collection"]}],
6576
},
6677
api=api,
6778
resources=[dpath],
6879
activate=True)
6980
db = db_api.APIInterrogator(api=api)
7081
# Positive test
7182
data = db.search_dataset(query="dcoraid",
72-
circles=[common.CIRCLE],
73-
collections=[common.COLLECTION],
83+
circles=[defaults["circle"]],
84+
collections=[defaults["collection"]],
7485
)
7586
assert len(data) >= 1
7687
for dd in data:
7788
if dd["name"].endswith(ranstr):
7889
break
7990
else:
80-
assert False, "{} not found!".format(common.DATASET)
91+
assert False, "{} not found!".format(defaults["dataset"])
8192
# Negative test
8293
data = db.search_dataset(query="cliauwenlc_should_never_exist",
83-
circles=[common.CIRCLE],
84-
collections=[common.COLLECTION],
94+
circles=[defaults["circle"]],
95+
collections=[defaults["collection"]],
8596
)
8697
assert len(data) == 0, "search result for non-existent dataset?"
8798

8899

89100
def test_search_dataset_limit():
90101
api = common.get_api()
91102
ranstr = ''.join(random.choice("0123456789") for _i in range(10))
103+
defaults = common.get_test_defaults()
92104
dataset_ids = []
93105
# Create three test datasets
94106
for _ in range(3):
95107
ds_dict = dataset_create(
96-
{"title": "{} {}".format(common.TITLE, ranstr),
97-
"owner_org": common.CIRCLE,
98-
"authors": common.USER_NAME,
108+
{"title": "{} {}".format(defaults["title"], ranstr),
109+
"owner_org": defaults["circle"],
110+
"authors": defaults["user_name"],
99111
"license_id": "CC0-1.0",
100-
"groups": [{"name": common.COLLECTION}],
112+
"groups": [{"name": defaults["collection"]}],
101113
},
102114
api=api,
103115
resources=[dpath],
104116
activate=True)
105117
dataset_ids.append(ds_dict["id"])
106118
db = db_api.APIInterrogator(api=api)
107119
data_limited = db.search_dataset(query=ranstr,
108-
circles=[common.CIRCLE],
109-
collections=[common.COLLECTION],
120+
circles=[defaults["circle"]],
121+
collections=[defaults["collection"]],
110122
circle_collection_union=True,
111123
limit=2
112124
)
113125
assert len(data_limited) == 2
114126
data_unlimited = db.search_dataset(query=ranstr,
115-
circles=[common.CIRCLE],
116-
collections=[common.COLLECTION],
127+
circles=[defaults["circle"]],
128+
collections=[defaults["collection"]],
117129
circle_collection_union=True,
118130
limit=0
119131
)
@@ -123,28 +135,32 @@ def test_search_dataset_limit():
123135
def test_search_dataset_limit_negative_error():
124136
api = common.get_api()
125137
ranstr = ''.join(random.choice("0123456789") for _i in range(10))
138+
defaults = common.get_test_defaults()
126139
# Create three test datasets
127140
dataset_create(
128-
{"title": "{} {}".format(common.TITLE, ranstr),
129-
"owner_org": common.CIRCLE,
130-
"authors": common.USER_NAME,
141+
{"title": "{} {}".format(defaults["title"], ranstr),
142+
"owner_org": defaults["circle"],
143+
"authors": defaults["user_name"],
131144
"license_id": "CC0-1.0",
132-
"groups": [{"name": common.COLLECTION}],
145+
"groups": [{"name": defaults["collection"]}],
133146
},
134147
api=api,
135148
resources=[dpath],
136149
activate=True)
137150
db = db_api.APIInterrogator(api=api)
138151
with pytest.raises(ValueError, match="must be 0 or >0"):
139152
db.search_dataset(query=ranstr,
140-
circles=[common.CIRCLE],
141-
collections=[common.COLLECTION],
153+
circles=[defaults["circle"]],
154+
collections=[defaults["collection"]],
142155
circle_collection_union=True,
143156
limit=-1
144157
)
145158

146159

160+
@pytest.mark.skipif(not HAS_FIGSHARE_ACCESS,
161+
reason="No access to figshare-import circle")
147162
def test_search_dataset_only_one_filter_query():
163+
# The figshare circle must have the testing user as a member
148164
api = common.get_api()
149165
db = db_api.APIInterrogator(api=api)
150166
ds = db.search_dataset(filter_queries=[f"-creator_user_id:{api.user_id}"])
@@ -155,9 +171,10 @@ def test_search_dataset_only_one_filter_query():
155171
assert False, "Search did not return figshare-7771184-v2!"
156172

157173

174+
@pytest.mark.skipif(not HAS_FIGSHARE_ACCESS,
175+
reason="No access to figshare-import circle")
158176
def test_get_datasets_user_shared_figshare():
159-
"""The figshare circle must have the user "dcoraid" as a member
160-
"""
177+
# The figshare circle must have the testing user as a member
161178
api = common.get_api()
162179
db = db_api.APIInterrogator(api=api)
163180
datasets = db.get_datasets_user_shared()
@@ -166,11 +183,3 @@ def test_get_datasets_user_shared_figshare():
166183
break
167184
else:
168185
assert False, "Search did not return figshare-7771184-v2!"
169-
170-
171-
if __name__ == "__main__":
172-
# Run all tests
173-
loc = locals()
174-
for key in list(loc.keys()):
175-
if key.startswith("test_") and hasattr(loc[key], "_call_"):
176-
loc[key]()

tests/test_gui.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,12 @@ def test_gui_mydata_dataset_add_to_collection(mw, qtbot):
123123
api = get_ckan_api()
124124
grps = api.get("group_list_authz")
125125
grps = sorted(grps, key=lambda x: x["display_name"])
126+
defaults = common.get_test_defaults()
126127
for ii, item in enumerate(grps):
127-
if item["name"] == "dcoraid-collection":
128+
if item["name"] == defaults["collection"]:
128129
break
129130
else:
130-
assert False, "could not find dcoraid-collection"
131+
assert False, f"could not find {defaults['collection']}"
131132
with mock.patch.object(
132133
QInputDialog, "getItem",
133134
return_value=(f"{ii}: {item['display_name']}", True)):
@@ -137,7 +138,7 @@ def test_gui_mydata_dataset_add_to_collection(mw, qtbot):
137138
ds_dict = api.get("package_show", id=ds_id)
138139
assert "groups" in ds_dict
139140
assert len(ds_dict["groups"]) == 1
140-
assert ds_dict["groups"][0]["name"] == "dcoraid-collection"
141+
assert ds_dict["groups"][0]["name"] == defaults["collection"]
141142

142143

143144
def test_gui_start_with_bad_server(qtbot):
@@ -293,11 +294,12 @@ def test_gui_upload_task_missing_circle_multiple(mw, qtbot):
293294
tdir = pathlib.Path(tempfile.mkdtemp(prefix="recursive_task_"))
294295
shutil.copytree(tpath1.parent, tdir / tpath1.parent.name)
295296
shutil.copytree(tpath2.parent, tdir / tpath2.parent.name)
297+
defaults = common.get_test_defaults()
296298
with mock.patch.object(
297299
QtWidgets.QFileDialog, "getExistingDirectory",
298300
return_value=str(tdir)), \
299301
mock.patch.object(QtWidgets.QInputDialog, "getItem",
300-
return_value=(common.CIRCLE, True)), \
302+
return_value=(defaults["circle"], True)), \
301303
mock.patch.object(QMessageBox, "information", return_value=None):
302304
act = QtWidgets.QAction("some unimportant text")
303305
act.setData("bulk")
@@ -348,11 +350,12 @@ def test_gui_upload_task_missing_circle(mw, qtbot):
348350
tpath = common.make_upload_task(task_id=task_id,
349351
dataset_dict=dataset_dict)
350352
QtWidgets.QApplication.processEvents(QtCore.QEventLoop.AllEvents, 300)
353+
defaults = common.get_test_defaults()
351354
with mock.patch.object(
352355
QtWidgets.QFileDialog, "getOpenFileNames",
353356
return_value=([tpath], None)), \
354357
mock.patch.object(QtWidgets.QInputDialog, "getItem",
355-
return_value=(common.CIRCLE, True)), \
358+
return_value=(defaults["circle"], True)), \
356359
mock.patch.object(QMessageBox, "information", return_value=None):
357360
act = QtWidgets.QAction("some unimportant text")
358361
act.setData("single")

tests/test_upload_task.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_create_task():
2929
path=path_task,
3030
dataset_dict={"authors": "Bruce Banner",
3131
"license_id": "CC0-1.0",
32-
"owner_org": common.CIRCLE},
32+
"owner_org": common.get_test_defaults()["circle"]},
3333
resource_dicts=[{"path": path_rtdc,
3434
"name": "hulk.rtdc",
3535
"supplements": {"chip": {"comments": "Loki bash"}}},
@@ -550,11 +550,3 @@ def test_wrong_ids():
550550
match="I got the following IDs: from upload job "
551551
+ "state: hans; from dataset dict: peter"):
552552
task.load_task(task_path, api=api)
553-
554-
555-
if __name__ == "__main__":
556-
# Run all tests
557-
loc = locals()
558-
for key in list(loc.keys()):
559-
if key.startswith("test_") and hasattr(loc[key], "__call__"):
560-
loc[key]()

0 commit comments

Comments
 (0)