|
| 1 | +""" |
| 2 | +Collection of test cases to test the dj cli |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +import subprocess |
| 7 | +import pytest |
| 8 | +import datajoint as dj |
| 9 | +from .schema_simple import * |
| 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 | + |
| 30 | + assert ( |
| 31 | + "usage: datajoint [--help] [-V] [-u USER] [-p PASSWORD] [-h HOST]\n\ |
| 32 | + [-s SCHEMAS [SCHEMAS ...]]" |
| 33 | + in captured_output |
| 34 | + ) |
| 35 | + assert "DataJoint console interface." in captured_output |
| 36 | + assert "optional arguments:" in captured_output |
| 37 | + assert "--help show this help message and exit" in captured_output |
| 38 | + assert ( |
| 39 | + "-V, --version show program's version number and exit" |
| 40 | + in captured_output |
| 41 | + ) |
| 42 | + assert "-u USER, --user USER Datajoint username" in captured_output |
| 43 | + assert ( |
| 44 | + "-p PASSWORD, --password PASSWORD\n\ |
| 45 | + Datajoint password" |
| 46 | + in captured_output |
| 47 | + ) |
| 48 | + assert "-h HOST, --host HOST Datajoint host" in captured_output |
| 49 | + assert ( |
| 50 | + "-s SCHEMAS [SCHEMAS ...], --schemas SCHEMAS [SCHEMAS ...]\n\ |
| 51 | + A list of virtual module mappings in `db:schema ...`\n\ |
| 52 | + format" |
| 53 | + in captured_output |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def test_cli_config(): |
| 58 | + process = subprocess.Popen( |
| 59 | + ["dj"], |
| 60 | + stdin=subprocess.PIPE, |
| 61 | + stdout=subprocess.PIPE, |
| 62 | + stderr=subprocess.PIPE, |
| 63 | + text=True, |
| 64 | + ) |
| 65 | + |
| 66 | + process.stdin.write("dj.config\n") |
| 67 | + process.stdin.flush() |
| 68 | + |
| 69 | + stdout, stderr = process.communicate() |
| 70 | + |
| 71 | + assert f"{dj.config}" in stdout |
| 72 | + |
| 73 | + |
| 74 | +def test_cli_args(): |
| 75 | + process = subprocess.Popen( |
| 76 | + ["dj", "-utest_user", "-ptest_pass", "-htest_host"], |
| 77 | + stdin=subprocess.PIPE, |
| 78 | + stdout=subprocess.PIPE, |
| 79 | + stderr=subprocess.PIPE, |
| 80 | + text=True, |
| 81 | + ) |
| 82 | + |
| 83 | + process.stdin.write("dj.config['database.user']\n") |
| 84 | + process.stdin.write("dj.config['database.password']\n") |
| 85 | + process.stdin.write("dj.config['database.host']\n") |
| 86 | + process.stdin.flush() |
| 87 | + |
| 88 | + stdout, stderr = process.communicate() |
| 89 | + assert "test_user" in stdout |
| 90 | + assert "test_pass" in stdout |
| 91 | + assert "test_host" in stdout |
| 92 | + |
| 93 | + |
| 94 | +def test_cli_schemas(): |
| 95 | + process = subprocess.Popen( |
| 96 | + ["dj", "-s", "djtest_test1:test_schema1", "djtest_relational:test_schema2"], |
| 97 | + stdin=subprocess.PIPE, |
| 98 | + stdout=subprocess.PIPE, |
| 99 | + stderr=subprocess.PIPE, |
| 100 | + text=True, |
| 101 | + ) |
| 102 | + |
| 103 | + process.stdin.write("test_schema1.__dict__['__name__']\n") |
| 104 | + process.stdin.write("test_schema1.__dict__['schema']\n") |
| 105 | + process.stdin.write("test_schema1.TTest.fetch(as_dict=True)\n") |
| 106 | + process.stdin.flush() |
| 107 | + |
| 108 | + stdout, stderr = process.communicate() |
| 109 | + fetch_res = [ |
| 110 | + {"key": 0, "value": 0}, |
| 111 | + {"key": 1, "value": 2}, |
| 112 | + {"key": 2, "value": 4}, |
| 113 | + {"key": 3, "value": 6}, |
| 114 | + {"key": 4, "value": 8}, |
| 115 | + {"key": 5, "value": 10}, |
| 116 | + {"key": 6, "value": 12}, |
| 117 | + {"key": 7, "value": 14}, |
| 118 | + {"key": 8, "value": 16}, |
| 119 | + {"key": 9, "value": 18}, |
| 120 | + ] |
| 121 | + assert "dj repl\n\nschema modules:\n\n - test_schema1\n - test_schema2" in stderr |
| 122 | + assert "'test_schema1'" == stdout[4:18] |
| 123 | + assert "Schema `djtest_test1`" == stdout[23:44] |
| 124 | + assert fetch_res == json.loads(stdout[50:295].replace("'", '"')) |
0 commit comments