def init_mf(): mf = miniflask.init( module_dirs=str(Path(__file__).parent / "modules"), debug=True ) mf.load("all") return mf
def test_equal(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.load("module1") mf.parse_args([ "--int1=1337", "--int2=-1337", "--float1=1.234", "--float2=-1.234", "--float3=-0.0", "--float4=0.0", "--float5=3e5", "--float6=-3e5", "--bool1=False", "--bool2=True", "--enum1=small", "--str1=abcd1234", "--str2=αβγδ∀⇐Γ∂", "--str3=" ]) captured = capsys.readouterr() mf.event.print_all() captured = capsys.readouterr() assert captured.out == """ modules.module1.int1: 1337 modules.module1.int2: -1337 modules.module1.float1: 1.234 modules.module1.float2: -1.234 modules.module1.float3: -0.0 modules.module1.float4: 0.0 modules.module1.float5: 300000.0 modules.module1.float6: -300000.0 modules.module1.bool1: False modules.module1.bool2: True modules.module1.enum1: SIZE.SMALL modules.module1.str1: abcd1234 modules.module1.str2: αβγδ∀⇐Γ∂ modules.module1.str3: \n""".lstrip()
def test_none(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.load("module1") mf.parse_args() captured = capsys.readouterr() mf.event.print_all() captured = capsys.readouterr() assert captured.out == """ modules.module1.int1: None modules.module1.int2: [] modules.module1.float1: None modules.module1.float2: None modules.module1.float3: None modules.module1.float4: None modules.module1.float5: None modules.module1.float6: [] modules.module1.bool1: None modules.module1.bool2: [] modules.module1.enum1: None modules.module1.enum2: [] modules.module1.str1: None modules.module1.str2: [] modules.module1.str3: None\n""".lstrip()
def test_override_scoped_relative(): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.load(["defaults", "defaults2", "defaults_override_scoped_relative"]) mf.parse_args([]) mf.event.print_all()
def test_space(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.load("module1") mf.parse_args([ "--int1", "1337", "--int2", "-1337", "--float1", "1.234", "--float2", "-1.234", "--float3", "-0.0", "--float4", "0.0", "--float5", "3e5", "--float6", "-3e5", "--bool1", "False", "--bool2", "True", "--enum1", "small", "--str1", "abcd1234", "--str2", "αβγδ∀⇐Γ∂", "--str3", "" ]) captured = capsys.readouterr() mf.event.print_all() captured = capsys.readouterr() assert captured.out == """ modules.module1.int1: [1337] modules.module1.int2: [-1337] modules.module1.float1: [1.234] modules.module1.float2: [-1.234] modules.module1.float3: [-0.0] modules.module1.float4: [0.0] modules.module1.float5: [300000.0] modules.module1.float6: [-300000.0] modules.module1.bool1: [False] modules.module1.bool2: [True] modules.module1.enum1: [<SIZE.SMALL: 0>] modules.module1.str1: ['abcd1234'] modules.module1.str2: ['αβγδ∀⇐Γ∂'] modules.module1.str3: [''] """.lstrip()
def test_equal(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.load("module1") mf.parse_args([ "--int1=1337", "--int2=-1337", "--float1=1.234", "--float2=-1.234", "--float3=-0.0", "--float4=0.0", "--float5=3e5", "--float6=-3e5", "--bool1=False", "--bool2=True", "--enum1=small", "--str1=abcd1234", "--str2=αβγδ∀⇐Γ∂", "--str3=" ]) captured = capsys.readouterr() mf.event.print_all() captured = capsys.readouterr() assert captured.out == """ modules.module1.int1: [1337] modules.module1.int2: [-1337] modules.module1.float1: [1.234] modules.module1.float2: [-1.234] modules.module1.float3: [-0.0] modules.module1.float4: [0.0] modules.module1.float5: [300000.0] modules.module1.float6: [-300000.0] modules.module1.bool1: [False] modules.module1.bool2: [True] modules.module1.enum1: [<SIZE.SMALL: 0>] modules.module1.str1: ['abcd1234'] modules.module1.str2: ['αβγδ∀⇐Γ∂'] modules.module1.str3: [''] """.lstrip()
def test_space(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.load("module1") mf.parse_args([ "--int1", "1337", "--int2", "-1337", "--float1", "1.234", "--float2", "-1.234", "--float3", "-0.0", "--float4", "0.0", "--float5", "3e5", "--float6", "-3e5", "--bool1", "False", "--bool2", "True", "--enum1", "small", "--str1", "abcd1234", "--str2", "αβγδ∀⇐Γ∂", "--str3", "" ]) captured = capsys.readouterr() mf.event.print_all() captured = capsys.readouterr() assert captured.out == """ modules.module1.int1: 1337 modules.module1.int2: -1337 modules.module1.float1: 1.234 modules.module1.float2: -1.234 modules.module1.float3: -0.0 modules.module1.float4: 0.0 modules.module1.float5: 300000.0 modules.module1.float6: -300000.0 modules.module1.bool1: False modules.module1.bool2: True modules.module1.enum1: SIZE.SMALL modules.module1.str1: abcd1234 modules.module1.str2: αβγδ∀⇐Γ∂ modules.module1.str3: \n""".lstrip()
def test_unregister(): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=False) mf.load(["module1", "main_unregister"]) mf.parse_args([]) with pytest.raises(AttributeError): mf.event.main()
def test_exception_event(): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=False) mf.load("module1") mf.parse_args([]) with pytest.raises(ValueError): mf.event.main()
def test_enum(): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.run(modules=["module1"], argv=[ "--module1.sizerequired", "SMALL", "--module1.sizelist", "MEDIUM", "SMALL" ])
def test_override_conflict(): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.load(["defaults", "defaults2", "defaults_override"]) with pytest.raises(miniflask.exceptions.RegisterError): mf.parse_args([]) mf.event.print_all()
def test_outervar(): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) event = mf.event mf.load("module1") var_a = 42 mf.event.main() del event, var_a # now unused
def test_overwrite_with_attached(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=False) mf.load(["module1", "main_overwrite_with_attached"]) mf.parse_args([]) captured = capsys.readouterr() mf.event.main() captured = capsys.readouterr() assert captured.out == """ overwritten main event and removed attached as well """.lstrip()
def test_beforeafter_setup(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.load("setup") mf.parse_args([]) captured = capsys.readouterr() mf.event.main() captured = capsys.readouterr() assert captured.out == """ event called with value: 42 event returned value: 42 """.lstrip()
def test_global_setup(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.load("globalvar") mf.parse_args([]) captured = capsys.readouterr() mf.event.print_all() captured = capsys.readouterr() assert captured.out == """ globalvar: 1 """.lstrip()
def test_helper_cli_after_overwrite(capsys): mf = miniflask.init( module_dirs=str(Path(__file__).parent / "modules"), debug=True ) mf.load(["helpervar", "helpervar_override"]) with pytest.raises(ValueError): mf.parse_args([ "--helpervar", "11" ]) mf.event.print_all()
def test_overwrite_setup(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=False) mf.load(["module1"]) mf.parse_args([]) captured = capsys.readouterr() mf.event.main() captured = capsys.readouterr() assert captured.out == """ before main event main event after main event """.lstrip()
def test_helper_overwrite(capsys): mf = miniflask.init( module_dirs=str(Path(__file__).parent / "modules"), debug=True ) mf.load(["helpervar", "helpervar_override"]) mf.parse_args([]) captured = capsys.readouterr() mf.event.print_all() captured = capsys.readouterr() assert captured.out == """ modules.helpervar.helpervar: 2 """.lstrip()
def test_beforeafter_after_otherorder(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.load(["setup", "afterevent2", "afterevent"]) mf.parse_args([]) captured = capsys.readouterr() mf.event.main() captured = capsys.readouterr() assert captured.out == """ event called with value: 42 after_-event (2) called after_-event called event returned value: 86 """.lstrip()
def test_named_call(capsys): mf = miniflask.init( module_dirs=str(Path(__file__).parent / "modules"), debug=False ) mf.load(["module1", "module2"]) mf.parse_args([]) for module, result in mf.event.named_call("main").items(): print(module, result) captured = capsys.readouterr() assert "\n".join(captured.out.split("\n")[2:]) == """ modules.module1 1337 modules.module2 2345 """.lstrip()
def test_setup(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.load(["defaults"]) mf.parse_args(["--var_default_override_twice_and_cli", "1114"]) captured = capsys.readouterr() mf.event.print_all() captured = capsys.readouterr() assert captured.out == """ modules.defaults.var_default: 1 modules.defaults.var_default_override: 2 modules.defaults.var_default_override_twice: 3 modules.defaults.var_default_override_twice_and_cli: 1114 """.lstrip()
def test_override_cli(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.load(["globalvar", "globalvar_override"]) mf.parse_args([ "--globalvar", "111", ]) captured = capsys.readouterr() mf.event.print_all() captured = capsys.readouterr() assert captured.out == """ globalvar: 111 """.lstrip()
def test_beforeafter_before(capsys): mf = miniflask.init( module_dirs=str(Path(__file__).parent / "modules"), debug=True ) mf.load(["setup_none_return", "beforeevent", "beforeevent2"]) mf.parse_args([]) captured = capsys.readouterr() mf.event.main() captured = capsys.readouterr() assert captured.out == """ before_-event called before_-event (2) called event called with value: 85 event returned value: None """.lstrip()
def test_tuple_len_error(capsys): mf = miniflask.init( module_dirs=str(Path(__file__).parent / "modules"), debug=True ) mf.load("module1") with pytest.raises(SystemExit): mf.parse_args([ "--float4", "0.0" ]) captured = capsys.readouterr() with open("/tmp/test", "w") as f: f.write(captured.err) assert captured.err.split("\n", 1)[1] == """ pytest: error: argument --modules.module1.float4: expected 4 arguments """.lstrip()
def test_bool_yesno(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.load("module1") mf.parse_args([ "--bool1=no", "--bool2=yes", ]) captured = capsys.readouterr() mf.event.print_bool() captured = capsys.readouterr() assert captured.out == """ modules.module1.bool1: False modules.module1.bool2: True """.lstrip()
def test_outervar_with_before_event(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) event = mf.event mf.load("module2") var_a = 42 mf.event.main() del event, var_a # now unused captured = capsys.readouterr() out = """ before_main outervar: 42 main """ assert out in captured.out
def test_space(capsys): mf = miniflask.init( module_dirs=str(Path(__file__).parent / "modules"), debug=True ) mf.load("module1") mf.parse_args([ "--int1", "--int2", "-1337", "1234", "--float1", "--float2", "-1.234", "--float3", "-0.0", "-0.0", "--float4", "0.0", "0.0", "0.0", "--float5", "3e5", "4e4", "5e3", "6e2", "--float6", "-3e5", "-4e4", "-5e3", "-6e2", "--bool1", "--bool2", "True", "False", "--enum1", "large", "medium", "small", "--str1", "abcd", "1234", "--str2", "αβγδ", "∀⇐", "Γ∂", "--str3", "" ]) captured = capsys.readouterr() mf.event.print_all() captured = capsys.readouterr() assert captured.out == """ modules.module1.int1: [] modules.module1.int2: [-1337, 1234] modules.module1.float1: [] modules.module1.float2: [-1.234] modules.module1.float3: [-0.0, -0.0] modules.module1.float4: [0.0, 0.0, 0.0] modules.module1.float5: [300000.0, 40000.0, 5000.0, 600.0] modules.module1.float6: [-300000.0, -40000.0, -5000.0, -600.0] modules.module1.bool1: [] modules.module1.bool2: [True, False] modules.module1.enum1: [<SIZE.LARGE: 2>, <SIZE.MEDIUM: 1>, <SIZE.SMALL: 0>] modules.module1.str1: ['abcd', '1234'] modules.module1.str2: ['αβγδ', '∀⇐', 'Γ∂'] modules.module1.str3: [''] """.lstrip()
def test_beforeafter_before_and_after(capsys): mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True) mf.load([ "setup_multiple", "beforeevent", "beforeevent2", "afterevent_multiple", "afterevent2_multiple" ]) mf.parse_args([]) captured = capsys.readouterr() mf.event.main() captured = capsys.readouterr() assert captured.out == """ before_-event called before_-event (2) called event called with value: 85 event called with value: 85 event called with value: 85 event called with value: 85 after_-event called after_-event (2) called event returned value: [1, 87, 173, 259] """.lstrip()
from pathlib import Path import miniflask # noqa: E402 mf = miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), ) def test_exception_event(): mf.load("module1") mf.register_event("main", lambda: print("Main.")) mf.run(argv=[])
def init_mf(): return miniflask.init(module_dirs=str(Path(__file__).parent / "modules"), debug=True)