Skip to content

Commit 685fa90

Browse files
Merge branch 'feat/update-dependencies' of github.com:PnX-SI/GeoNature into feat/update-dependencies
2 parents 1f919c9 + 8bf303b commit 685fa90

File tree

1 file changed

+72
-70
lines changed

1 file changed

+72
-70
lines changed

backend/geonature/tests/test_commands.py

+72-70
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
from geonature.utils.env import db
1212
from munch import Munch
1313
from pypnusershub.db.models import User
14+
import pytest
1415

1516
from .fixtures import *
1617

1718
# Reuse Lambda function in the following tests
18-
true = lambda: True
19-
false = lambda: False
2019
abs_function = lambda *args, **kwargs: None
2120

2221

@@ -86,123 +85,126 @@ def parents(self):
8685
return SequenceMock()
8786

8887

89-
def print_result(result):
90-
"""
91-
Only for DEBUG test
92-
"""
93-
print("---------")
94-
print("Output")
95-
print(result.output)
96-
print("Exception")
97-
print(result.exception)
98-
print("---------")
88+
def patch_monkeypatch(monkeypatch):
89+
monkeypatch.setattr(command_utils, "run", run_success_mock)
90+
monkeypatch.setattr(install_module.subprocess, "run", run_success_mock)
91+
monkeypatch.setattr(install_module, "Path", PathMock)
92+
93+
for (
94+
method
95+
) in "module_db_upgrade build_frontend create_frontend_module_config install_frontend_dependencies".split():
96+
monkeypatch.setattr(install_module, method, abs_function)
97+
# Redefine os
98+
monkeypatch.setattr(install_module.os.path, "exists", lambda x: True)
99+
monkeypatch.setattr(install_module.os, "symlink", lambda x, y: None)
100+
monkeypatch.setattr(install_module.os, "unlink", lambda x: None)
101+
monkeypatch.setattr(install_module.os, "readlink", lambda x: None)
102+
monkeypatch.setattr(install_module.importlib, "reload", abs_function)
99103

100104

105+
@pytest.fixture
106+
def client_click():
107+
return CliRunner()
108+
109+
110+
@pytest.mark.usefixtures()
101111
class TestCommands:
102-
def test_install_gn_module(self, monkeypatch):
103-
"""
104-
Function to redefine
105-
106-
os.path.exists
107-
subprocess.run
108-
Path.is_file --> strict is always True
109-
module_db_upgrade --> do nothing
110-
"""
111-
logging.info("\nTEST INSTALL GN MODULE")
112-
cli = CliRunner()
112+
# Avoid redefine at each test
113+
cli = CliRunner()
113114

114-
monkeypatch.setattr(command_utils, "run", run_success_mock)
115-
monkeypatch.setattr(install_module.subprocess, "run", run_success_mock)
116-
monkeypatch.setattr(install_module, "Path", PathMock)
117-
118-
for (
119-
method
120-
) in "module_db_upgrade build_frontend create_frontend_module_config install_frontend_dependencies".split():
121-
monkeypatch.setattr(install_module, method, abs_function)
122-
# Redefine os
123-
monkeypatch.setattr(install_module.os.path, "exists", lambda x: True)
124-
monkeypatch.setattr(install_module.os, "symlink", lambda x, y: None)
125-
monkeypatch.setattr(install_module.os, "unlink", lambda x: None)
126-
monkeypatch.setattr(install_module.os, "readlink", lambda x: None)
127-
monkeypatch.setattr(install_module.importlib, "reload", abs_function)
128-
129-
# module code
130-
# 1. If module code
131-
# 1.1 check that if module do not exist works
132-
logging.info("Test: if module code not exists")
133-
result = cli.invoke(install_module.install_gn_module, ["test/", "TEST"])
115+
def test_install_gn_module_no_modulecode(self):
116+
result = self.cli.invoke(install_module.install_gn_module, ["test/", "TEST"])
134117
assert isinstance(result.exception, Exception)
135118

136-
# 1.2 if get_dist_from_code is None
137-
logging.info("Test : if get_dist_from_code() returns None")
119+
def test_install_gn_module_dist_code_is_none(self, monkeypatch):
120+
patch_monkeypatch(monkeypatch)
138121
monkeypatch.setattr(install_module, "get_dist_from_code", lambda x: None)
139-
result = cli.invoke(install_module.install_gn_module, ["test/", "TEST"])
122+
result = self.cli.invoke(install_module.install_gn_module, ["test/", "TEST"])
140123
assert result.exception.code > 0
141124

142-
# 1.2 if get_dist_from_code is GEONATURE
143-
logging.info("Test : if get_dist_from_code() returns GEONATURE")
125+
def test_install_gn_module_dist_code_is_GEONATURE(self, monkeypatch):
126+
patch_monkeypatch(monkeypatch)
144127
monkeypatch.setattr(install_module, "get_dist_from_code", lambda x: "GEONATURE")
145-
result = cli.invoke(install_module.install_gn_module, ["test/"])
128+
result = self.cli.invoke(install_module.install_gn_module, ["test/"])
146129
assert result.exit_code == 0
147130

148-
# 2. If not module code given
149-
150-
logging.info("Test : no module code given")
131+
def test_install_gn_module_no_module_code(self, monkeypatch):
132+
patch_monkeypatch(monkeypatch)
151133
module_path = "backend/geonature/core"
152134
monkeypatch.setattr(
153135
install_module, "iter_modules_dist", iter_module_dist_mock("geonature")
154136
)
155-
result = cli.invoke(install_module.install_gn_module, [module_path])
137+
result = self.cli.invoke(install_module.install_gn_module, [module_path])
156138
assert result.exit_code == 0
157139

158-
logging.info("Test: if iter_modules_dist return an empty iterator")
140+
def test_install_gn_module_empty_iter_module_dist(self, monkeypatch):
141+
patch_monkeypatch(monkeypatch)
142+
module_path = "backend/geonature/core"
159143
monkeypatch.setattr(install_module, "iter_modules_dist", lambda: [])
160-
result = cli.invoke(install_module.install_gn_module, [module_path])
144+
result = self.cli.invoke(install_module.install_gn_module, [module_path])
161145
assert result.exit_code > 0
162146
monkeypatch.setattr(
163147
install_module, "iter_modules_dist", iter_module_dist_mock("geonature")
164148
)
165149

166-
# 3. build parameter set to false
167-
logging.info("Test : build parameter set to false")
168-
result = cli.invoke(install_module.install_gn_module, [module_path, "--build=false"])
150+
def test_install_gn_module_nomodule_code(self, monkeypatch):
151+
patch_monkeypatch(monkeypatch)
152+
module_path = "backend/geonature/core"
153+
monkeypatch.setattr(
154+
install_module, "iter_modules_dist", iter_module_dist_mock("geonature")
155+
)
156+
result = self.cli.invoke(install_module.install_gn_module, [module_path, "--build=false"])
169157
assert result.exit_code == 0
170158

171-
# 4. upgrade_db parameter set to false
172-
logging.info("Test : upgrade_db parameter set to false")
173-
result = cli.invoke(install_module.install_gn_module, [module_path, "--upgrade-db=false"])
159+
def test_install_gn_module_false_upgrade_db(self, monkeypatch):
160+
patch_monkeypatch(monkeypatch)
161+
module_path = "backend/geonature/core"
162+
monkeypatch.setattr(
163+
install_module, "iter_modules_dist", iter_module_dist_mock("geonature")
164+
)
165+
166+
result = self.cli.invoke(
167+
install_module.install_gn_module, [module_path, "--upgrade-db=false"]
168+
)
174169
assert result.exit_code == 0
175170

176-
logging.info("Test : if symlink not exists")
171+
def test_install_gn_module_symlink_not_exists(self, monkeypatch):
172+
patch_monkeypatch(monkeypatch)
173+
module_path = "backend/geonature/core"
174+
monkeypatch.setattr(
175+
install_module, "iter_modules_dist", iter_module_dist_mock("geonature")
176+
)
177177
monkeypatch.setattr(install_module.os.path, "exists", lambda x: False)
178-
result = cli.invoke(install_module.install_gn_module, [module_path])
178+
result = self.cli.invoke(install_module.install_gn_module, [module_path])
179+
179180
assert result.exit_code == 0
180181

181-
logging.info("Test : if module not in sys.module")
182+
def test_install_gn_module_module_notin_sysmodule(self, monkeypatch):
183+
patch_monkeypatch(monkeypatch)
184+
module_path = "backend/geonature/core"
182185
monkeypatch.setattr(install_module.os.path, "exists", lambda x: False)
183186
monkeypatch.setattr(install_module, "iter_modules_dist", iter_module_dist_mock("pouet"))
184-
result = cli.invoke(install_module.install_gn_module, [module_path])
187+
result = self.cli.invoke(install_module.install_gn_module, [module_path])
185188
assert result.exit_code > 0 # will fail
186189

187190
def test_upgrade_modules_db(self, monkeypatch):
188-
cli = CliRunner()
189191
monkeypatch.setattr(
190192
install_module, "iter_modules_dist", iter_module_dist_mock("geonature")
191193
)
192-
result = cli.invoke(install_module.upgrade_modules_db, [])
194+
result = self.cli.invoke(install_module.upgrade_modules_db, [])
193195
assert result.exit_code > 0
194196

195197
with monkeypatch.context() as m:
196198
m.setitem(config, "DISABLED_MODULES", ["test"])
197-
result = cli.invoke(install_module.upgrade_modules_db, ["test"])
199+
result = self.cli.invoke(install_module.upgrade_modules_db, ["test"])
198200
assert result.exit_code == 0
199201

200202
monkeypatch.setattr(install_module, "module_db_upgrade", lambda *args, **kwargs: True)
201-
result = cli.invoke(install_module.upgrade_modules_db, ["test"])
203+
result = self.cli.invoke(install_module.upgrade_modules_db, ["test"])
202204
assert result.exit_code == 0
203205

204206
monkeypatch.setattr(install_module, "module_db_upgrade", lambda *args, **kwargs: False)
205-
result = cli.invoke(install_module.upgrade_modules_db, ["test"])
207+
result = self.cli.invoke(install_module.upgrade_modules_db, ["test"])
206208
assert result.exit_code == 0
207209

208210
def test_nvm_available(self, monkeypatch):

0 commit comments

Comments
 (0)