|
12 | 12 |
|
13 | 13 | import pysd |
14 | 14 | from pysd.tools.benchmarking import load_outputs, assert_frames_close |
| 15 | +from pysd.cli.parser import parser |
15 | 16 |
|
16 | 17 | test_model_ven = "test-models/samples/teacup/teacup.mdl" |
17 | 18 |
|
@@ -254,6 +255,30 @@ def test_read_vensim_split_model_subviews(self, test_copy): |
254 | 255 | assert (modules_dirname / "view_1" / "submodule_2.py").exists() |
255 | 256 | assert (modules_dirname / "view_2.py").exists() |
256 | 257 |
|
| 258 | + @pytest.mark.parametrize( |
| 259 | + "model", ["more-tests/split_model/test_split_model_subviews.mdl"] |
| 260 | + ) |
| 261 | + @pytest.mark.parametrize( |
| 262 | + "separators,expected", |
| 263 | + [ |
| 264 | + ([], []), |
| 265 | + (['--subview-sep', '-'], ["-"]), |
| 266 | + (['--subview-sep', '-', ","], ["-", ","]), |
| 267 | + (['--subview-sep', '.', "-"], [".", "-"]) |
| 268 | +
|
| 269 | + ]) |
| 270 | + def test_read_vensim_split_model_subviews_hyphen(self, test_copy, |
| 271 | + separators, expected): |
| 272 | + """ |
| 273 | + Test that several arguments to the subview-sep option are correctly |
| 274 | + parsed, and particularly when passing a hyphen. |
| 275 | + """ |
| 276 | + mdl_path = str(test_copy) |
| 277 | + args = [mdl_path, '--translate', '--split-views'] + separators |
| 278 | + |
| 279 | + options = parser.parse_args(args) |
| 280 | + options.subview_sep = expected |
| 281 | + |
257 | 282 | @pytest.mark.parametrize("model", [test_model_ven]) |
258 | 283 | def test_run_return_timestamps(self, out_csv, out_tab, test_copy): |
259 | 284 |
|
@@ -538,3 +563,71 @@ def test_save_without_name(self, out_tab, test_copy, tmp_path): |
538 | 563 | out = load_outputs(tmp_path / outputs) |
539 | 564 | out2 = pysd.read_vensim(test_copy).run() |
540 | 565 | assert_frames_close(out, out2) |
| 566 | + |
| 567 | + |
| 568 | +@pytest.mark.parametrize( |
| 569 | + "model", ["more-tests/split_model/test_split_model_sub_subviews.mdl"] |
| 570 | + ) |
| 571 | +class TestCLI(): |
| 572 | + """To test the CLI, ordering arguments differently""" |
| 573 | + |
| 574 | + @pytest.mark.parametrize( |
| 575 | + "cli_args,expected", |
| 576 | + [(['-I=1.0', '--final-time=101', |
| 577 | + '--split-views', '--subview-sep', '-', '.'], |
| 578 | + {"new_values": {"param": {"another var": 2.0}, |
| 579 | + "initial": {"Stock": -1.1}}, |
| 580 | + "initial_time": 1.0, |
| 581 | + "final_time": 101, |
| 582 | + "split_views": True, |
| 583 | + "subview_sep": ["-", "."] |
| 584 | + } |
| 585 | + ), |
| 586 | + (['--subview-sep', '-', '.', '-I=1.0', '--final-time=101', |
| 587 | + '--split-views'], |
| 588 | + {"new_values": {"param": {"another var": 2.0}, |
| 589 | + "initial": {"Stock": -1.1}}, |
| 590 | + "initial_time": 1.0, |
| 591 | + "final_time": 101, |
| 592 | + "split_views": True, |
| 593 | + "subview_sep": ["-", "."] |
| 594 | + } |
| 595 | + ) |
| 596 | + ], |
| 597 | + ids=["subview_sep_last", "subview_sep_first"] |
| 598 | + ) |
| 599 | + def test_cli_arguments_parsing(self, test_copy, cli_args, expected): |
| 600 | + |
| 601 | + mdl_path = str(test_copy) |
| 602 | + |
| 603 | + positional_args = [mdl_path, 'another var=2', 'Stock:-1.1'] |
| 604 | + |
| 605 | + # putting positional arguments at the begining |
| 606 | + positionals_first = positional_args + cli_args |
| 607 | + parsed = parser.parse_args(positionals_first) |
| 608 | + |
| 609 | + for arg in expected: |
| 610 | + assert expected[arg] == getattr(parsed, arg) |
| 611 | + |
| 612 | + @pytest.mark.xfail( |
| 613 | + reason="Passing positional arguments after nargs argument") |
| 614 | + @pytest.mark.parametrize( |
| 615 | + "cli_args,expected", |
| 616 | + [(['--split-views', '--subview-sep', '-', '.'], |
| 617 | + {"split_views": True, "subview_sep": ["-", "."]} |
| 618 | + ), |
| 619 | + (['another var=2', 'Stock:-1.1'], |
| 620 | + {"new_values": {"param": {"another var": 2.0}, |
| 621 | + "initial": {"Stock": -1.1}}} |
| 622 | + ) |
| 623 | + ] |
| 624 | + ) |
| 625 | + def test_positional_arguments_after_subview_sep(elf, test_copy, cli_args, |
| 626 | + expected): |
| 627 | + |
| 628 | + mdl_path = str(test_copy) |
| 629 | + |
| 630 | + # putting positional arguments after the subview_sep argument |
| 631 | + positionals_last = cli_args + [mdl_path] |
| 632 | + |
| 633 | + parsed = parser.parse_args(positionals_last) |
0 commit comments