|
| 1 | +""" |
| 2 | +Collection of test cases to test the dj cli |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +import ast |
| 7 | +import subprocess |
| 8 | +import pytest |
| 9 | +import datajoint as dj |
| 10 | + |
| 11 | + |
| 12 | +def test_cli_version(capsys): |
| 13 | + with pytest.raises(SystemExit) as pytest_wrapped_e: |
| 14 | + dj.cli(args=["-V"]) |
| 15 | + assert pytest_wrapped_e.type == SystemExit |
| 16 | + assert pytest_wrapped_e.value.code == 0 |
| 17 | + |
| 18 | + captured_output = capsys.readouterr().out |
| 19 | + assert captured_output == f"{dj.__name__} {dj.__version__}\n" |
| 20 | + |
| 21 | + |
| 22 | +def test_cli_help(capsys): |
| 23 | + with pytest.raises(SystemExit) as pytest_wrapped_e: |
| 24 | + dj.cli(args=["--help"]) |
| 25 | + assert pytest_wrapped_e.type == SystemExit |
| 26 | + assert pytest_wrapped_e.value.code == 0 |
| 27 | + |
| 28 | + captured_output = capsys.readouterr().out |
| 29 | + assert captured_output.strip() |
| 30 | + |
| 31 | + |
| 32 | +def test_cli_config(): |
| 33 | + process = subprocess.Popen( |
| 34 | + ["dj"], |
| 35 | + stdin=subprocess.PIPE, |
| 36 | + stdout=subprocess.PIPE, |
| 37 | + stderr=subprocess.PIPE, |
| 38 | + text=True, |
| 39 | + ) |
| 40 | + |
| 41 | + process.stdin.write("dj.config\n") |
| 42 | + process.stdin.flush() |
| 43 | + |
| 44 | + stdout, stderr = process.communicate() |
| 45 | + cleaned = stdout.strip(" >\t\n\r") |
| 46 | + for key in ("database.user", "database.password", "database.host"): |
| 47 | + assert key in cleaned, f"Key {key} not found in config from stdout: {cleaned}" |
| 48 | + |
| 49 | + |
| 50 | +def test_cli_args(): |
| 51 | + process = subprocess.Popen( |
| 52 | + ["dj", "-utest_user", "-ptest_pass", "-htest_host"], |
| 53 | + stdin=subprocess.PIPE, |
| 54 | + stdout=subprocess.PIPE, |
| 55 | + stderr=subprocess.PIPE, |
| 56 | + text=True, |
| 57 | + ) |
| 58 | + |
| 59 | + process.stdin.write("dj.config['database.user']\n") |
| 60 | + process.stdin.write("dj.config['database.password']\n") |
| 61 | + process.stdin.write("dj.config['database.host']\n") |
| 62 | + process.stdin.flush() |
| 63 | + |
| 64 | + stdout, stderr = process.communicate() |
| 65 | + assert "test_user" == stdout[5:14] |
| 66 | + assert "test_pass" == stdout[21:30] |
| 67 | + assert "test_host" == stdout[37:46] |
| 68 | + |
| 69 | + |
| 70 | +def test_cli_schemas(prefix, connection_root): |
| 71 | + schema = dj.Schema(prefix + "_cli", locals(), connection=connection_root) |
| 72 | + |
| 73 | + @schema |
| 74 | + class IJ(dj.Lookup): |
| 75 | + definition = """ # tests restrictions |
| 76 | + i : int |
| 77 | + j : int |
| 78 | + """ |
| 79 | + contents = list(dict(i=i, j=j + 2) for i in range(3) for j in range(3)) |
| 80 | + |
| 81 | + process = subprocess.Popen( |
| 82 | + ["dj", "-s", "djtest_cli:test_schema"], |
| 83 | + stdin=subprocess.PIPE, |
| 84 | + stdout=subprocess.PIPE, |
| 85 | + stderr=subprocess.PIPE, |
| 86 | + text=True, |
| 87 | + ) |
| 88 | + |
| 89 | + process.stdin.write("test_schema.__dict__['__name__']\n") |
| 90 | + process.stdin.write("test_schema.__dict__['schema']\n") |
| 91 | + process.stdin.write("test_schema.IJ.fetch(as_dict=True)\n") |
| 92 | + process.stdin.flush() |
| 93 | + |
| 94 | + stdout, stderr = process.communicate() |
| 95 | + fetch_res = [ |
| 96 | + {"i": 0, "j": 2}, |
| 97 | + {"i": 0, "j": 3}, |
| 98 | + {"i": 0, "j": 4}, |
| 99 | + {"i": 1, "j": 2}, |
| 100 | + {"i": 1, "j": 3}, |
| 101 | + {"i": 1, "j": 4}, |
| 102 | + {"i": 2, "j": 2}, |
| 103 | + {"i": 2, "j": 3}, |
| 104 | + {"i": 2, "j": 4}, |
| 105 | + ] |
| 106 | + assert ( |
| 107 | + "\ |
| 108 | +dj repl\n\n\ |
| 109 | +\ |
| 110 | +schema modules:\n\n\ |
| 111 | + - test_schema" |
| 112 | + == stderr[159:200] |
| 113 | + ) |
| 114 | + assert "'test_schema'" == stdout[4:17] |
| 115 | + assert "Schema `djtest_cli`" == stdout[22:41] |
| 116 | + assert fetch_res == json.loads(stdout[47:209].replace("'", '"')) |
0 commit comments