|
| 1 | +import json |
1 | 2 | import os
|
2 | 3 | import re
|
3 | 4 | from pathlib import Path
|
|
7 | 8 | from diffpy.labpdfproc.labpdfprocapp import get_args
|
8 | 9 | from diffpy.labpdfproc.tools import (
|
9 | 10 | known_sources,
|
| 11 | + load_user_info, |
10 | 12 | load_user_metadata,
|
11 | 13 | set_input_lists,
|
12 | 14 | set_output_directory,
|
@@ -241,3 +243,65 @@ def test_load_user_metadata_bad(inputs, msg):
|
241 | 243 | actual_args = get_args(cli_inputs)
|
242 | 244 | with pytest.raises(ValueError, match=msg[0]):
|
243 | 245 | actual_args = load_user_metadata(actual_args)
|
| 246 | + |
| 247 | + |
| 248 | +# For each test case, check username and email are properly loaded and written into config file |
| 249 | +params_user_info = [ |
| 250 | + # No existing config file, user enters valid username and email |
| 251 | + ([ "good_username", "[email protected]", "non_existing_file.json"], [ "good_username", "[email protected]"]), |
| 252 | + # There exists a config file, so we read it and fetch info, no user input is fine |
| 253 | + ([ "", "", "test.json"], [ "good_username", "[email protected]"]), |
| 254 | + # There exists a config file, user input username/email overwrites the username/email in the file |
| 255 | + ([ "new_username", "", "test.json"], [ "new_username", "[email protected]"]), |
| 256 | + ([ "", "[email protected]", "test.json"], [ "good_username", "[email protected]"]), |
| 257 | + ([ "new_username", "[email protected]", "test.json"], [ "new_username", "[email protected]"]), |
| 258 | + # There exists a config file that does not contain username/email, then we write user inputs into config file |
| 259 | + ([ "good_username", "[email protected]", "test2.json"], [ "good_username", "[email protected]"]), |
| 260 | +] |
| 261 | + |
| 262 | + |
| 263 | +@pytest.mark.parametrize("inputs, expected", params_user_info) |
| 264 | +def test_load_user_info(monkeypatch, inputs, expected, user_filesystem): |
| 265 | + os.chdir(user_filesystem / "conf_dir") |
| 266 | + expected_username, expected_email = expected |
| 267 | + input_username, input_email, input_config_file = inputs |
| 268 | + mock_prompt_user_info = iter([input_username, input_email]) |
| 269 | + monkeypatch.setattr("builtins.input", lambda _: next(mock_prompt_user_info)) |
| 270 | + |
| 271 | + cli_inputs = ["2.5", "data.xy"] |
| 272 | + actual_args = get_args(cli_inputs) |
| 273 | + actual_args = load_user_info(actual_args, input_config_file) |
| 274 | + |
| 275 | + assert actual_args.username == expected_username |
| 276 | + assert actual_args.email == expected_email |
| 277 | + with open(input_config_file, "r") as f: |
| 278 | + config_data = json.load(f) |
| 279 | + assert config_data == {"username": expected_username, "email": expected_email} |
| 280 | + |
| 281 | + |
| 282 | +params_user_info_bad = [ |
| 283 | + # No valid username/email in config file (or no config file), |
| 284 | + # and user didn't enter username/email the first time they were asked |
| 285 | + (["", "", "non_existing_file.json"], "Please rerun the program and provide a username and email."), |
| 286 | + ([ "", "[email protected]", "non_existing_file.json"], "Please rerun the program and provide a username."), |
| 287 | + (["good_username", "", "non_existing_file.json"], "Please rerun the program and provide an email."), |
| 288 | + # User entered an invalid email |
| 289 | + ( |
| 290 | + ["good_username", "bad_email", "non_existing_file.json"], |
| 291 | + "Please rerun the program and provide a valid email.", |
| 292 | + ), |
| 293 | + (["good_username", "bad_email", "test.json"], "Please rerun the program and provide a valid email."), |
| 294 | +] |
| 295 | + |
| 296 | + |
| 297 | +@pytest.mark.parametrize("inputs, msg", params_user_info_bad) |
| 298 | +def test_load_user_info_bad(monkeypatch, inputs, msg, user_filesystem): |
| 299 | + os.chdir(user_filesystem) |
| 300 | + input_username, input_email, input_config_file = inputs |
| 301 | + mock_prompt_user_info = iter([input_username, input_email]) |
| 302 | + monkeypatch.setattr("builtins.input", lambda _: next(mock_prompt_user_info)) |
| 303 | + |
| 304 | + cli_inputs = ["2.5", "data.xy"] |
| 305 | + actual_args = get_args(cli_inputs) |
| 306 | + with pytest.raises(ValueError, match=msg[0]): |
| 307 | + actual_args = load_user_info(actual_args, config_file=input_config_file) |
0 commit comments