Skip to content

Commit 8058867

Browse files
Merge branch 'master' into pyqt6_migration
2 parents 46868a3 + b13a179 commit 8058867

9 files changed

Lines changed: 98 additions & 77 deletions

File tree

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
0.17.0
22
- BREAKING CHANGE: migrate codebase from PyQt5 to PyQt6
3+
0.16.12
4+
- tests: allow to use different DCOR instance for testing
5+
- tests: make tests independent of testing user
36
0.16.11
47
- fix: support CKAN 2.11 (authorization and status_show)
58
- fix: implement non-functional `DownloadQueue.get_status`

tests/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
api_key
1+
api_key*

tests/common.py

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

1515

16-
CIRCLE = "dcoraid-circle"
17-
COLLECTION = "dcoraid-collection"
18-
DATASET = "dcoraid-dataset"
19-
SERVER = "dcor-dev.mpl.mpg.de"
20-
USER = "dcoraid"
21-
USER_NAME = "DCOR-Aid tester"
22-
TITLE = "DCOR-Aid test dataset"
16+
# You can run e.g. tests on a local DCOR instance with
17+
# export DCORAID_TEST_SERVER="http://192.168.122.143"
18+
SERVER = os.environ.get("DCORAID_TEST_SERVER", "dcor-dev.mpl.mpg.de")
2319

2420
dpath = pathlib.Path(__file__).parent / "data" / "calibration_beads_47.rtdc"
2521

@@ -40,14 +36,32 @@ def get_api_key():
4036
return key
4137

4238

39+
@functools.lru_cache()
40+
def get_test_defaults():
41+
api = get_api()
42+
user_dict = api.get("user_show")
43+
name = user_dict["name"]
44+
return {
45+
"circle": f"{name}-circle",
46+
"collection": f"{name}-collection",
47+
"dataset": f"{name}-dataset",
48+
"user": name,
49+
"user_name": f"DCOR-Aid Tester {name.capitalize()}",
50+
"user_dict": user_dict,
51+
"title": "DCOR-Aid test dataset",
52+
"server": SERVER,
53+
}
54+
55+
4356
def make_dataset_dict(hint=""):
57+
defaults = get_test_defaults()
4458
space = " " if hint else ""
4559
dataset_dict = {
4660
"title": f"A test dataset {hint}{space}{time.time()}",
4761
"private": True,
4862
"license_id": "CC0-1.0",
49-
"owner_org": CIRCLE,
50-
"authors": USER_NAME,
63+
"owner_org": defaults["circle"],
64+
"authors": defaults["user_name"],
5165
}
5266
return dataset_dict
5367

tests/conftest.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def pytest_configure(config):
5151
settings = QtCore.QSettings()
5252
settings.setValue("check for updates", "0")
5353
settings.setValue("user scenario", "dcor-dev")
54-
settings.setValue("auth/server", "dcor-dev.mpl.mpg.de")
54+
settings.setValue("auth/server", common.SERVER)
5555
settings.setValue("auth/api key", common.get_api_key())
5656
settings.setValue("debug/without timers", "1")
5757
settings.sync()
@@ -84,17 +84,17 @@ def pytest_sessionstart(session):
8484
before performing collection and entering the run test loop.
8585
"""
8686
api = common.get_api()
87-
api.get("group_show", id=common.COLLECTION)
87+
defaults = common.get_test_defaults()
8888
try:
89-
api.post("group_create", {"name": common.COLLECTION})
89+
api.post("group_create", {"name": defaults["collection"]})
9090
except APIConflictError:
9191
pass
9292
try:
93-
api.post("organization_create", {"name": common.CIRCLE})
93+
api.post("organization_create", {"name": defaults["circle"]})
9494
except APIConflictError:
9595
pass
96-
user_dict = api.get("user_show", id=common.USER)
97-
user_dict["fullname"] = common.USER_NAME
96+
user_dict = api.get("user_show")
97+
user_dict["fullname"] = defaults["user_name"]
9898
api.post("user_update", user_dict)
9999

100100

tests/test_api_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_api_requests_cache_no_parameters():
1313
t0 = time.perf_counter()
1414
api.get("status_show")
1515
t1 = time.perf_counter()
16-
for ii in range(50):
16+
for ii in range(20):
1717
api.get("status_show")
1818
t2 = time.perf_counter()
1919
assert (t1 - t0) > (t2 - t1)
@@ -25,7 +25,7 @@ def test_api_requests_cache_with_parameters():
2525
t0 = time.perf_counter()
2626
api.get("organization_list_for_user", permission="create_dataset")
2727
t1 = time.perf_counter()
28-
for ii in range(50):
28+
for ii in range(20):
2929
api.get("organization_list_for_user", permission="create_dataset")
3030
t2 = time.perf_counter()
3131
assert (t1 - t0) > (t2 - t1)

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: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,110 +10,121 @@
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

3240
def test_get_users_anonymous():
33-
api = CKANAPI(server="dcor-dev.mpl.mpg.de")
41+
api = CKANAPI(server=common.SERVER) # anonymous access
3442
db = db_api.APIInterrogator(api=api)
3543
with pytest.raises(errors.APIAuthorizationError, match="Access denied"):
3644
db.get_users()
3745

3846

3947
def test_public_api_interrogator():
40-
"""This test uses the figshare datasets on SERVER"""
4148
api = common.get_api()
4249
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()
50+
defaults = common.get_test_defaults()
51+
assert defaults["circle"] in db.get_circles()
52+
assert defaults["collection"] in db.get_collections()
53+
assert defaults["user"] in db.get_users()
4654

4755

4856
def test_user_data():
4957
"""Test the user information"""
5058
api = common.get_api()
5159
db = db_api.APIInterrogator(api=api)
5260
data = db.user_data
53-
assert data["fullname"] == common.USER_NAME, "fullname not correct"
61+
defaults = common.get_test_defaults()
62+
assert data["fullname"] == defaults["user_name"], "fullname not correct"
5463

5564

5665
def test_search_dataset_basic():
5766
api = common.get_api()
5867
ranstr = ''.join(random.choice("0123456789") for _i in range(10))
68+
defaults = common.get_test_defaults()
5969
# Create a test dataset
60-
dataset_create({"title": "{} {}".format(common.TITLE, ranstr),
61-
"owner_org": common.CIRCLE,
62-
"authors": common.USER_NAME,
70+
dataset_create({"title": "{} {}".format(defaults["title"], ranstr),
71+
"owner_org": defaults["circle"],
72+
"authors": defaults["user_name"],
6373
"license_id": "CC0-1.0",
64-
"groups": [{"name": common.COLLECTION}],
74+
"groups": [{"name": defaults["collection"]}],
6575
},
6676
api=api,
6777
resources=[dpath],
6878
activate=True)
6979
db = db_api.APIInterrogator(api=api)
7080
# Positive test
7181
data = db.search_dataset(query="dcoraid",
72-
circles=[common.CIRCLE],
73-
collections=[common.COLLECTION],
82+
circles=[defaults["circle"]],
83+
collections=[defaults["collection"]],
7484
)
7585
assert len(data) >= 1
7686
for dd in data:
7787
if dd["name"].endswith(ranstr):
7888
break
7989
else:
80-
assert False, "{} not found!".format(common.DATASET)
90+
assert False, "{} not found!".format(defaults["dataset"])
8191
# Negative test
8292
data = db.search_dataset(query="cliauwenlc_should_never_exist",
83-
circles=[common.CIRCLE],
84-
collections=[common.COLLECTION],
93+
circles=[defaults["circle"]],
94+
collections=[defaults["collection"]],
8595
)
8696
assert len(data) == 0, "search result for non-existent dataset?"
8797

8898

8999
def test_search_dataset_limit():
90100
api = common.get_api()
91101
ranstr = ''.join(random.choice("0123456789") for _i in range(10))
102+
defaults = common.get_test_defaults()
92103
dataset_ids = []
93104
# Create three test datasets
94105
for _ in range(3):
95106
ds_dict = dataset_create(
96-
{"title": "{} {}".format(common.TITLE, ranstr),
97-
"owner_org": common.CIRCLE,
98-
"authors": common.USER_NAME,
107+
{"title": "{} {}".format(defaults["title"], ranstr),
108+
"owner_org": defaults["circle"],
109+
"authors": defaults["user_name"],
99110
"license_id": "CC0-1.0",
100-
"groups": [{"name": common.COLLECTION}],
111+
"groups": [{"name": defaults["collection"]}],
101112
},
102113
api=api,
103114
resources=[dpath],
104115
activate=True)
105116
dataset_ids.append(ds_dict["id"])
106117
db = db_api.APIInterrogator(api=api)
107118
data_limited = db.search_dataset(query=ranstr,
108-
circles=[common.CIRCLE],
109-
collections=[common.COLLECTION],
119+
circles=[defaults["circle"]],
120+
collections=[defaults["collection"]],
110121
circle_collection_union=True,
111122
limit=2
112123
)
113124
assert len(data_limited) == 2
114125
data_unlimited = db.search_dataset(query=ranstr,
115-
circles=[common.CIRCLE],
116-
collections=[common.COLLECTION],
126+
circles=[defaults["circle"]],
127+
collections=[defaults["collection"]],
117128
circle_collection_union=True,
118129
limit=0
119130
)
@@ -123,28 +134,32 @@ def test_search_dataset_limit():
123134
def test_search_dataset_limit_negative_error():
124135
api = common.get_api()
125136
ranstr = ''.join(random.choice("0123456789") for _i in range(10))
137+
defaults = common.get_test_defaults()
126138
# Create three test datasets
127139
dataset_create(
128-
{"title": "{} {}".format(common.TITLE, ranstr),
129-
"owner_org": common.CIRCLE,
130-
"authors": common.USER_NAME,
140+
{"title": "{} {}".format(defaults["title"], ranstr),
141+
"owner_org": defaults["circle"],
142+
"authors": defaults["user_name"],
131143
"license_id": "CC0-1.0",
132-
"groups": [{"name": common.COLLECTION}],
144+
"groups": [{"name": defaults["collection"]}],
133145
},
134146
api=api,
135147
resources=[dpath],
136148
activate=True)
137149
db = db_api.APIInterrogator(api=api)
138150
with pytest.raises(ValueError, match="must be 0 or >0"):
139151
db.search_dataset(query=ranstr,
140-
circles=[common.CIRCLE],
141-
collections=[common.COLLECTION],
152+
circles=[defaults["circle"]],
153+
collections=[defaults["collection"]],
142154
circle_collection_union=True,
143155
limit=-1
144156
)
145157

146158

159+
@pytest.mark.skipif(not HAS_FIGSHARE_ACCESS,
160+
reason="No access to figshare-import circle")
147161
def test_search_dataset_only_one_filter_query():
162+
# The figshare circle must have the testing user as a member
148163
api = common.get_api()
149164
db = db_api.APIInterrogator(api=api)
150165
ds = db.search_dataset(filter_queries=[f"-creator_user_id:{api.user_id}"])
@@ -155,9 +170,10 @@ def test_search_dataset_only_one_filter_query():
155170
assert False, "Search did not return figshare-7771184-v2!"
156171

157172

173+
@pytest.mark.skipif(not HAS_FIGSHARE_ACCESS,
174+
reason="No access to figshare-import circle")
158175
def test_get_datasets_user_shared_figshare():
159-
"""The figshare circle must have the user "dcoraid" as a member
160-
"""
176+
# The figshare circle must have the testing user as a member
161177
api = common.get_api()
162178
db = db_api.APIInterrogator(api=api)
163179
datasets = db.get_datasets_user_shared()
@@ -166,11 +182,3 @@ def test_get_datasets_user_shared_figshare():
166182
break
167183
else:
168184
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]()

0 commit comments

Comments
 (0)