Skip to content

Commit dba964e

Browse files
authored
refactoring tests(part1) (#101)
Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
1 parent 0a64a2d commit dba964e

22 files changed

+537
-648
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ filterwarnings = [
134134
]
135135
log_cli = true
136136
addopts = "-rs --color=yes"
137+
markers = [
138+
"require_nc: marks a test that requires a minimum version of Nextcloud.",
139+
]
137140

138141
[tool.coverage.run]
139142
cover_pylib = true

tests/_tests_at_the_end.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22
import sys
33
from subprocess import Popen
44

5-
import pytest
65
from _install_wait import check_heartbeat
7-
from gfixture import NC, NC_APP
86

97
# These tests will be run separate, and at the end of all other tests.
108

119

12-
@pytest.mark.skipif(NC_APP is None or NC is None, reason="Requires both Nextcloud Client and App modes.")
13-
def test_ex_app_enable_disable():
10+
def test_ex_app_enable_disable(nc_client, nc_app):
1411
child_environment = os.environ.copy()
1512
child_environment["APP_PORT"] = os.environ.get("APP_PORT", "9009")
1613
r = Popen(
@@ -22,12 +19,12 @@ def test_ex_app_enable_disable():
2219
try:
2320
if check_heartbeat(url, '"status":"ok"', 15, 0.3):
2421
raise RuntimeError("`_install_only_enabled_handler` can not start.")
25-
if NC.apps.ex_app_is_enabled("nc_py_api"):
26-
NC.apps.ex_app_disable("nc_py_api")
27-
assert NC.apps.ex_app_is_disabled("nc_py_api") is True
28-
assert NC.apps.ex_app_is_enabled("nc_py_api") is False
29-
NC.apps.ex_app_enable("nc_py_api")
30-
assert NC.apps.ex_app_is_disabled("nc_py_api") is False
31-
assert NC.apps.ex_app_is_enabled("nc_py_api") is True
22+
if nc_client.apps.ex_app_is_enabled("nc_py_api"):
23+
nc_client.apps.ex_app_disable("nc_py_api")
24+
assert nc_client.apps.ex_app_is_disabled("nc_py_api") is True
25+
assert nc_client.apps.ex_app_is_enabled("nc_py_api") is False
26+
nc_client.apps.ex_app_enable("nc_py_api")
27+
assert nc_client.apps.ex_app_is_disabled("nc_py_api") is False
28+
assert nc_client.apps.ex_app_is_enabled("nc_py_api") is True
3229
finally:
3330
r.terminate()

tests/appcfg_prefs_ex_test.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import pytest
2-
from gfixture import NC_APP
2+
from conftest import NC_APP
33

44
from nc_py_api import NextcloudExceptionNotFound
55

6-
if NC_APP is None or "app_ecosystem_v2" not in NC_APP.capabilities:
7-
pytest.skip("app_ecosystem_v2 is not installed.", allow_module_level=True)
6+
if NC_APP is None:
7+
pytest.skip("Need App mode", allow_module_level=True)
88

99

1010
@pytest.mark.parametrize("class_to_test", (NC_APP.appconfig_ex, NC_APP.preferences_ex))
@@ -114,30 +114,30 @@ def test_cfg_ex_get_typing(class_to_test):
114114
assert r[1].value == "321"
115115

116116

117-
def test_appcfg_sensitive():
118-
appcfg = NC_APP.appconfig_ex
117+
def test_appcfg_sensitive(nc_app):
118+
appcfg = nc_app.appconfig_ex
119119
appcfg.delete("test_key")
120120
appcfg.set_value("test_key", "123", sensitive=True)
121121
assert appcfg.get_value("test_key") == "123"
122122
assert appcfg.get_values(["test_key"])[0].value == "123"
123123
appcfg.delete("test_key")
124124
# next code tests `sensitive` value from the `AppEcosystem`
125125
params = {"configKey": "test_key", "configValue": "123"}
126-
result = NC_APP._session.ocs(method="POST", path=f"{NC_APP._session.ae_url}/{appcfg._url_suffix}", json=params)
126+
result = nc_app._session.ocs(method="POST", path=f"{nc_app._session.ae_url}/{appcfg._url_suffix}", json=params)
127127
assert not result["sensitive"] # by default if sensitive value is unspecified it is False
128128
appcfg.delete("test_key")
129129
params = {"configKey": "test_key", "configValue": "123", "sensitive": True}
130-
result = NC_APP._session.ocs(method="POST", path=f"{NC_APP._session.ae_url}/{appcfg._url_suffix}", json=params)
130+
result = nc_app._session.ocs(method="POST", path=f"{nc_app._session.ae_url}/{appcfg._url_suffix}", json=params)
131131
assert result["configkey"] == "test_key"
132132
assert result["configvalue"] == "123"
133133
assert bool(result["sensitive"]) is True
134134
params.pop("sensitive") # if we not specify value, AppEcosystem should not change it.
135-
result = NC_APP._session.ocs(method="POST", path=f"{NC_APP._session.ae_url}/{appcfg._url_suffix}", json=params)
135+
result = nc_app._session.ocs(method="POST", path=f"{nc_app._session.ae_url}/{appcfg._url_suffix}", json=params)
136136
assert result["configkey"] == "test_key"
137137
assert result["configvalue"] == "123"
138138
assert bool(result["sensitive"]) is True
139139
params["sensitive"] = False
140-
result = NC_APP._session.ocs(method="POST", path=f"{NC_APP._session.ae_url}/{appcfg._url_suffix}", json=params)
140+
result = nc_app._session.ocs(method="POST", path=f"{nc_app._session.ae_url}/{appcfg._url_suffix}", json=params)
141141
assert result["configkey"] == "test_key"
142142
assert result["configvalue"] == "123"
143143
assert bool(result["sensitive"]) is False

tests/apps_test.py

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,59 @@
11
import pytest
2-
from gfixture import NC_TO_TEST
3-
4-
from nc_py_api import Nextcloud
52

63
APP_NAME = "files_trashbin"
74

85

9-
@pytest.mark.parametrize("nc", NC_TO_TEST)
106
def test_list_apps_types(nc):
117
assert isinstance(nc.apps.get_list(), list)
128
assert isinstance(nc.apps.get_list(enabled=True), list)
139
assert isinstance(nc.apps.get_list(enabled=False), list)
1410

1511

16-
@pytest.mark.parametrize("nc", NC_TO_TEST)
1712
def test_list_apps(nc):
1813
apps = nc.apps.get_list()
1914
assert apps
2015
assert APP_NAME in apps
2116

2217

23-
@pytest.mark.skipif(not isinstance(NC_TO_TEST[:1][0], Nextcloud), reason="Not available for NextcloudApp.")
24-
@pytest.mark.parametrize("nc", NC_TO_TEST[:1])
25-
def test_app_enable_disable(nc):
26-
assert nc.apps.is_installed(APP_NAME) is True
27-
if nc.apps.is_enabled(APP_NAME):
28-
nc.apps.disable(APP_NAME)
29-
assert nc.apps.is_disabled(APP_NAME) is True
30-
assert nc.apps.is_enabled(APP_NAME) is False
31-
assert nc.apps.is_installed(APP_NAME) is True
32-
nc.apps.enable(APP_NAME)
33-
assert nc.apps.is_enabled(APP_NAME) is True
34-
assert nc.apps.is_installed(APP_NAME) is True
18+
def test_app_enable_disable(nc_client):
19+
assert nc_client.apps.is_installed(APP_NAME) is True
20+
if nc_client.apps.is_enabled(APP_NAME):
21+
nc_client.apps.disable(APP_NAME)
22+
assert nc_client.apps.is_disabled(APP_NAME) is True
23+
assert nc_client.apps.is_enabled(APP_NAME) is False
24+
assert nc_client.apps.is_installed(APP_NAME) is True
25+
nc_client.apps.enable(APP_NAME)
26+
assert nc_client.apps.is_enabled(APP_NAME) is True
27+
assert nc_client.apps.is_installed(APP_NAME) is True
3528

3629

37-
@pytest.mark.parametrize("nc", NC_TO_TEST)
3830
def test_is_installed_enabled(nc):
3931
assert nc.apps.is_enabled(APP_NAME) != nc.apps.is_disabled(APP_NAME)
4032
assert nc.apps.is_installed(APP_NAME)
4133

4234

43-
@pytest.mark.parametrize("nc", NC_TO_TEST[:1])
44-
def test_invalid_param(nc):
35+
def test_invalid_param(nc_any):
4536
with pytest.raises(ValueError):
46-
nc.apps.is_enabled("")
37+
nc_any.apps.is_enabled("")
4738
with pytest.raises(ValueError):
48-
nc.apps.is_installed("")
39+
nc_any.apps.is_installed("")
4940
with pytest.raises(ValueError):
50-
nc.apps.is_disabled("")
41+
nc_any.apps.is_disabled("")
5142
with pytest.raises(ValueError):
52-
nc.apps.enable("")
43+
nc_any.apps.enable("")
5344
with pytest.raises(ValueError):
54-
nc.apps.disable("")
45+
nc_any.apps.disable("")
5546
with pytest.raises(ValueError):
56-
nc.apps.ex_app_is_enabled("")
47+
nc_any.apps.ex_app_is_enabled("")
5748
with pytest.raises(ValueError):
58-
nc.apps.ex_app_is_disabled("")
49+
nc_any.apps.ex_app_is_disabled("")
5950
with pytest.raises(ValueError):
60-
nc.apps.ex_app_disable("")
51+
nc_any.apps.ex_app_disable("")
6152
with pytest.raises(ValueError):
62-
nc.apps.ex_app_enable("")
53+
nc_any.apps.ex_app_enable("")
6354

6455

65-
@pytest.mark.parametrize("nc", NC_TO_TEST)
66-
def test_ex_app_get_list(nc):
67-
if "app_ecosystem_v2" not in nc.capabilities:
68-
pytest.skip("app_ecosystem_v2 is not installed.")
56+
def test_ex_app_get_list(nc, nc_app):
6957
enabled_ex_apps = nc.apps.ex_app_get_list(enabled=True)
7058
assert isinstance(enabled_ex_apps, list)
7159
for i in enabled_ex_apps:

tests/conftest.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from os import environ
2+
from typing import Optional, Union
3+
4+
import gfixture_set_env # noqa
5+
import pytest
6+
7+
from nc_py_api import Nextcloud, NextcloudApp, _session # noqa
8+
9+
NC_CLIENT = None if environ.get("SKIP_NC_CLIENT_TESTS", False) else Nextcloud()
10+
if environ.get("SKIP_AE_TESTS", False):
11+
NC_APP = None
12+
else:
13+
NC_APP = NextcloudApp(user="admin")
14+
if "app_ecosystem_v2" not in NC_APP.capabilities:
15+
NC_APP = None
16+
if NC_CLIENT is None and NC_APP is None:
17+
raise EnvironmentError("Tests require at least Nextcloud or NextcloudApp.")
18+
19+
20+
@pytest.fixture(scope="session")
21+
def nc_version() -> _session.ServerVersion:
22+
return NC_APP.srv_version if NC_APP else NC_CLIENT.srv_version
23+
24+
25+
@pytest.fixture(scope="session")
26+
def nc_client() -> Optional[Nextcloud]:
27+
if NC_CLIENT is None:
28+
pytest.skip("Need Client mode")
29+
return NC_CLIENT
30+
31+
32+
@pytest.fixture(scope="session")
33+
def nc_app() -> Optional[NextcloudApp]:
34+
if NC_APP is None:
35+
pytest.skip("Need App mode")
36+
return NC_APP
37+
38+
39+
@pytest.fixture(scope="session")
40+
def nc_any() -> Union[Nextcloud, NextcloudApp]:
41+
"""Marks a test to run once for any of the modes."""
42+
return NC_APP if NC_APP else NC_CLIENT
43+
44+
45+
@pytest.fixture(scope="session")
46+
def nc(request):
47+
"""Marks a test to run for both modes if possible."""
48+
return request.param
49+
50+
51+
def pytest_generate_tests(metafunc):
52+
if "nc" in metafunc.fixturenames:
53+
values_ids = []
54+
values = []
55+
if NC_CLIENT is not None:
56+
values.append(NC_CLIENT)
57+
values_ids.append("client")
58+
if NC_APP is not None:
59+
values.append(NC_APP)
60+
values_ids.append("app")
61+
metafunc.parametrize("nc", values, ids=values_ids)
62+
63+
64+
def pytest_collection_modifyitems(items):
65+
for item in items:
66+
require_nc = [i for i in item.own_markers if i.name == "require_nc"]
67+
if require_nc:
68+
min_major = require_nc[0].kwargs["major"]
69+
min_minor = require_nc[0].kwargs.get("minor", 0)
70+
srv_ver = NC_APP.srv_version if NC_APP else NC_CLIENT.srv_version
71+
if srv_ver["major"] < min_major:
72+
item.add_marker(pytest.mark.skip(reason=f"Need NC>={min_major}"))
73+
elif srv_ver["major"] == min_major and srv_ver["minor"] < min_minor:
74+
item.add_marker(pytest.mark.skip(reason=f"Need NC>={min_major}.{min_minor}"))

0 commit comments

Comments
 (0)