Skip to content

Commit

Permalink
add tests v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vict0rsch committed Dec 7, 2020
1 parent 089b4df commit b8bfd6c
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,3 @@ Out[6]: True
In [7]: args.items()
Out[7]: dict_items([('foo', 'bar'), ('yes', {'no': {'maybe': 'idontknow'}})])
```

40 changes: 40 additions & 0 deletions tests/test_minydict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from minydra.dict import MinyDict


def test_init():
_ = MinyDict()


def test_from_dict():
_ = MinyDict({1: 2, 3: 4})


def test_in():
d = MinyDict({"a": 1, "b": 2, 4: "x"})
assert "a" in d
assert "b" in d
assert "x" not in d
assert d.z is None
assert 4 in d
assert d.a == 1
assert d.b == 2
assert d[4] == "x"


def test_attr():
d = MinyDict({"a": 1, "b": 2, 4: "x"})
d.x = 3
assert d.x == 3
d.y = MinyDict({"w": 4})
assert d.y.w == 4


def test_resolve():
d = MinyDict({"a.b.c": 2, "a.b.d": 3})
d.resolve()
assert d == MinyDict({"a": {"b": {"c": 2, "d": 3}}})


def test_pretty_print():
d = MinyDict({"a": 1, "b": 2, 4: "x", "r": {"t": 5}})
d.pretty_print()
53 changes: 53 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from minydra.dict import MinyDict
import subprocess
from minydra.exceptions import MinydraWrongArgumentException
import io
from contextlib import redirect_stdout

COMMAND = [
"python",
"-c",
"from minydra import Parser; parser = Parser(); parser.args.resolve().pretty_print()",
]


def run(args):
assert isinstance(args, list)
return subprocess.run(COMMAND + args, capture_output=True).stdout.decode()


def capture(minydict):
f = io.StringIO()
with redirect_stdout(f):
minydict.resolve().pretty_print()
return f.getvalue()


def test_empty():
assert run([]) == capture(MinyDict({}))


def test_basic():
assert run(["a=1", "b=1e-3", "c", "-d"]) == capture(
MinyDict({"a": 1, "b": 0.001, "c": True, "d": False})
)


def test_env(monkeypatch):
monkeypatch.setenv("USER", "TestingUser")
assert run(["a=$USER"]) == capture(MinyDict({"a": "TestingUser"}))


def test_dict():
assert run(["a={}".format('{"b": 3}')]) == capture(MinyDict({"a": {"b": 3}}))


def test_list():
assert run(["a=[2, 4, 'x']"]) == capture(MinyDict({"a": [2, 4, "x"]}))


def test_dotted():
assert run(["a.b.x=2"]) == capture(MinyDict({"a": {"b": {"x": 2}}}))


# TODO: add fail tests
127 changes: 127 additions & 0 deletions tests/test_parser_static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import pytest
from minydra import Parser
from minydra.exceptions import MinydraWrongArgumentException


def test_init():
_ = Parser()
_ = Parser(verbose=1)
_ = Parser(allow_overwrites=True)
_ = Parser(allow_overwrites=False)
_ = Parser(warn_overwrites=True)
_ = Parser(warn_overwrites=False)
_ = Parser(parse_env=False)
_ = Parser(parse_env=True)
_ = Parser(warn_env=False)
_ = Parser(warn_env=True)


def test_check_args():
Parser.check_args(["p=2", "x=4", "a", "-t", "r.s=0"])
with pytest.raises(MinydraWrongArgumentException):
Parser.check_args([" ="])
with pytest.raises(MinydraWrongArgumentException):
Parser.check_args(["= "])
with pytest.raises(MinydraWrongArgumentException):
Parser.check_args(["p="])
with pytest.raises(MinydraWrongArgumentException):
Parser.check_args(["-p=2"])
with pytest.raises(MinydraWrongArgumentException):
Parser.check_args([".p=2"])


def test_simple_map_args(capfd):
args = ["a=2"]
assert Parser.map_args(args, True, False) == {"a": "2"}


def test_map_args_overwrite_warn(capfd):
args = ["a=2", "a=3"]
assert Parser.map_args(args, True, True) == {"a": "3"}
out, err = capfd.readouterr()
assert "Repeated argument a" in out


def test_map_args_overwrite_no_warn(capfd):
args = ["a=2", "a=3"]
assert Parser.map_args(args, True, False) == {"a": "3"}
out, err = capfd.readouterr()
assert not out


def test_map_args_no_overwrite(capfd):
args = ["a=2", "a=3"]
with pytest.raises(MinydraWrongArgumentException):
Parser.map_args(args, False, False)


def test_map_args_positive_flag():
args = ["a", "b"]
assert Parser.map_args(args, False, False) == {"a": True, "b": True}


def test_map_args_negative_flag():
args = ["-a", "-b"]
assert Parser.map_args(args, False, False) == {"a": False, "b": False}


def test_map_args_mixed_flags():
args = ["a", "-b"]
assert Parser.map_args(args, False, False) == {"a": True, "b": False}


def test_set_env(monkeypatch, capfd):
monkeypatch.setenv("USER", "TestingUser")
monkeypatch.setenv("HOME", "TestingHome")
monkeypatch.setenv("OTHER_VAR", "OTHER")
monkeypatch.setenv("other_var", "other")
assert Parser.set_env(3) == 3
assert Parser.set_env("$HOME") == "TestingHome"
assert Parser.set_env("$OTHER_VAR") == "OTHER"
assert Parser.set_env("$other_var") == "other"
assert Parser.set_env("$HOME/$USER") == "TestingHome/TestingUser"
assert Parser.set_env("$HOME/$USER/project") == "TestingHome/TestingUser/project"
assert Parser.set_env("$MINY_VAR_DOES_NOT_EXIST") == "$MINY_VAR_DOES_NOT_EXIST"
out, err = capfd.readouterr()
assert "Detected variable $MINY_VAR_DOES_NOT_EXIST" in out


def test_sanitize_args_int():
assert Parser.sanitize_args({"a": "3"}) == {"a": 3}


def test_sanitize_args_float():
assert Parser.sanitize_args({"a": "3.1"}) == {"a": 3.1}
assert Parser.sanitize_args({"a": "1e-4"}) == {"a": 0.0001}


def test_sanitize_args_bool():
assert Parser.sanitize_args({"a": "true"}) == {"a": True}
assert Parser.sanitize_args({"a": "True"}) == {"a": True}
assert Parser.sanitize_args({"a": "false"}) == {"a": False}
assert Parser.sanitize_args({"a": "False"}) == {"a": False}


def test_sanitize_args_list():
assert Parser.sanitize_args({"a": "[]"}) == {"a": []}
assert Parser.sanitize_args({"a": "[1, 2]"}) == {"a": [1, 2]}
assert Parser.sanitize_args({"a": "[1, 2, 'b']"}) == {"a": [1, 2, "b"]}
assert Parser.sanitize_args({"a": "[1, 2, 'b']", "c": 4}) == {
"a": [1, 2, "b"],
"c": 4,
}
assert Parser.sanitize_args({"a": "[1, [2, 3]]"}) == {"a": [1, [2, 3]]}
assert Parser.sanitize_args({"a": "[1, [2, 1e-3]]"}) == {"a": [1, [2, 0.001]]}
assert Parser.sanitize_args({"a": "['false']"}) == {"a": [False]}
assert Parser.sanitize_args({"a": "[False]"}) == {"a": [False]}


def test_sanitize_args_dict(monkeypatch):
assert Parser.sanitize_args({"a": "{}"}) == {"a": {}}
assert Parser.sanitize_args({"a": "{1: 2}"}) == {"a": {1: 2}}
assert Parser.sanitize_args({"a": "{1: 2e-3}"}) == {"a": {1: 0.002}}
assert Parser.sanitize_args({"a": "{1: False}"}) == {"a": {1: False}}
assert Parser.sanitize_args({"a": "{1: [1, 3]}"}) == {"a": {1: [1, 3]}}

monkeypatch.setenv("USER", "TestingUser")
assert Parser.sanitize_args({"a": "{1: '$USER'}"}) == {"a": {1: "TestingUser"}}

0 comments on commit b8bfd6c

Please sign in to comment.