|
11 | 11 | from geonature.utils.env import db
|
12 | 12 | from munch import Munch
|
13 | 13 | from pypnusershub.db.models import User
|
| 14 | +import pytest |
14 | 15 |
|
15 | 16 | from .fixtures import *
|
16 | 17 |
|
17 | 18 | # Reuse Lambda function in the following tests
|
18 |
| -true = lambda: True |
19 |
| -false = lambda: False |
20 | 19 | abs_function = lambda *args, **kwargs: None
|
21 | 20 |
|
22 | 21 |
|
@@ -86,123 +85,126 @@ def parents(self):
|
86 | 85 | return SequenceMock()
|
87 | 86 |
|
88 | 87 |
|
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) |
99 | 103 |
|
100 | 104 |
|
| 105 | +@pytest.fixture |
| 106 | +def client_click(): |
| 107 | + return CliRunner() |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.usefixtures() |
101 | 111 | 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() |
113 | 114 |
|
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"]) |
134 | 117 | assert isinstance(result.exception, Exception)
|
135 | 118 |
|
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) |
138 | 121 | 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"]) |
140 | 123 | assert result.exception.code > 0
|
141 | 124 |
|
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) |
144 | 127 | 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/"]) |
146 | 129 | assert result.exit_code == 0
|
147 | 130 |
|
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) |
151 | 133 | module_path = "backend/geonature/core"
|
152 | 134 | monkeypatch.setattr(
|
153 | 135 | install_module, "iter_modules_dist", iter_module_dist_mock("geonature")
|
154 | 136 | )
|
155 |
| - result = cli.invoke(install_module.install_gn_module, [module_path]) |
| 137 | + result = self.cli.invoke(install_module.install_gn_module, [module_path]) |
156 | 138 | assert result.exit_code == 0
|
157 | 139 |
|
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" |
159 | 143 | 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]) |
161 | 145 | assert result.exit_code > 0
|
162 | 146 | monkeypatch.setattr(
|
163 | 147 | install_module, "iter_modules_dist", iter_module_dist_mock("geonature")
|
164 | 148 | )
|
165 | 149 |
|
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"]) |
169 | 157 | assert result.exit_code == 0
|
170 | 158 |
|
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 | + ) |
174 | 169 | assert result.exit_code == 0
|
175 | 170 |
|
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 | + ) |
177 | 177 | 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 | + |
179 | 180 | assert result.exit_code == 0
|
180 | 181 |
|
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" |
182 | 185 | monkeypatch.setattr(install_module.os.path, "exists", lambda x: False)
|
183 | 186 | 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]) |
185 | 188 | assert result.exit_code > 0 # will fail
|
186 | 189 |
|
187 | 190 | def test_upgrade_modules_db(self, monkeypatch):
|
188 |
| - cli = CliRunner() |
189 | 191 | monkeypatch.setattr(
|
190 | 192 | install_module, "iter_modules_dist", iter_module_dist_mock("geonature")
|
191 | 193 | )
|
192 |
| - result = cli.invoke(install_module.upgrade_modules_db, []) |
| 194 | + result = self.cli.invoke(install_module.upgrade_modules_db, []) |
193 | 195 | assert result.exit_code > 0
|
194 | 196 |
|
195 | 197 | with monkeypatch.context() as m:
|
196 | 198 | 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"]) |
198 | 200 | assert result.exit_code == 0
|
199 | 201 |
|
200 | 202 | 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"]) |
202 | 204 | assert result.exit_code == 0
|
203 | 205 |
|
204 | 206 | 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"]) |
206 | 208 | assert result.exit_code == 0
|
207 | 209 |
|
208 | 210 | def test_nvm_available(self, monkeypatch):
|
|
0 commit comments