Skip to content

Commit 2645a37

Browse files
committed
cli tests
1 parent 088c52e commit 2645a37

File tree

4 files changed

+130
-4
lines changed

4 files changed

+130
-4
lines changed

datajoint/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"key",
5252
"key_hash",
5353
"logger",
54+
"cli",
5455
]
5556

5657
from .logging import logger
@@ -70,6 +71,7 @@
7071
from .attribute_adapter import AttributeAdapter
7172
from . import errors
7273
from .errors import DataJointError
74+
from .cli import cli
7375

7476
ERD = Di = Diagram # Aliases for Diagram
7577
schema = Schema # Aliases for Schema

datajoint/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import datajoint as dj
55

66

7-
def dj_cli(args: list = None):
7+
def cli(args: list = None):
88
"""
99
Console interface for DataJoint Python
1010
@@ -17,7 +17,7 @@ def dj_cli(args: list = None):
1717
conflict_handler="resolve",
1818
)
1919
parser.add_argument(
20-
"-V", "--version", action="version", version=f"datajoint {dj.__version__}"
20+
"-V", "--version", action="version", version=f"{dj.__name__} {dj.__version__}"
2121
)
2222
parser.add_argument(
2323
"-u",
@@ -74,4 +74,4 @@ def dj_cli(args: list = None):
7474

7575

7676
if __name__ == "__main__":
77-
dj_cli()
77+
cli()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
],
4141
packages=find_packages(exclude=["contrib", "docs", "tests*"]),
4242
entry_points={
43-
"console_scripts": ["dj=datajoint.cli:dj_cli"],
43+
"console_scripts": ["dj=datajoint.cli:cli", "datajoint=datajoint.cli:cli"],
4444
},
4545
install_requires=requirements,
4646
python_requires="~={}.{}".format(*min_py_version),

tests_old/test_cli.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

Comments
 (0)