From 0ee6d6eba80e01b76b42f37549ec4a9569f87b01 Mon Sep 17 00:00:00 2001 From: AlexandreKempf Date: Wed, 28 Feb 2024 10:00:17 +0100 Subject: [PATCH] add more extensive tests --- tests/func/plots/test_collect.py | 36 +++++++++++--------- tests/integration/plots/conftest.py | 48 +++++++++++++++++++++++++-- tests/integration/plots/test_plots.py | 18 ++++++++++ 3 files changed, 83 insertions(+), 19 deletions(-) diff --git a/tests/func/plots/test_collect.py b/tests/func/plots/test_collect.py index 5273ff843..d6e751a0a 100644 --- a/tests/func/plots/test_collect.py +++ b/tests/func/plots/test_collect.py @@ -3,36 +3,36 @@ import dpath from dvc.repo.plots import Plots -def test_plots_definitions_works_with_nested_plots(tmp_dir, scm, dvc): - data_v1 = [ +def test_subdir_config_not_overwritten_by_parents(tmp_dir, scm, dvc): + plot_data = [ {"x": 1, "y": 0.1}, {"x": 2, "y": 0.2}, {"x": 3, "y": 0.3}, ] - data_subdir_v1 = [ + subdir_plot_data = [ {"x": 1, "y": 0.2, "z": 0.1}, {"x": 2, "y": 0.3, "z": 0.2}, {"x": 3, "y": 0.4, "z": 0.3}, ] - plots_dir = tmp_dir / "plots" - subdir = plots_dir / "subdir" + (tmp_dir / "plots").mkdir() + (tmp_dir / "plots" / "subdir").mkdir() - plots_dir.mkdir() - subdir.mkdir() - - (plots_dir / "data_v1.json").dump_json(data_v1) - (subdir / "data_subdir_v1.json").dump_json(data_subdir_v1) + (tmp_dir / "plots" / "plot.json").dump_json(plot_data) + (tmp_dir / "plots" / "subdir" / "plot.json").dump_json(subdir_plot_data) plots_config = [ { "plots/subdir/": { "x": "z", + "y": "x", } }, + {"plots": {"x": "x", "y": "y"}}, { - "plots": { - "x": "x", + "subdir axis defined by filename": { + "x": {"plots/subdir/plot.json": "x"}, + "y": {"plots/subdir/plot.json": "y"}, } }, ] @@ -44,8 +44,8 @@ def test_plots_definitions_works_with_nested_plots(tmp_dir, scm, dvc): scm.add( [ - "plots/data_v1.json", - "plots/subdir/data_subdir_v1.json", + "plots/plot.json", + "plots/subdir/plot.json", "dvc.yaml", ] ) @@ -54,6 +54,10 @@ def test_plots_definitions_works_with_nested_plots(tmp_dir, scm, dvc): plots = next(Plots(dvc).collect()) assert dpath.get(plots, "workspace/definitions/data/dvc.yaml/data") == { - "plots/data_v1.json": {"x": "x"}, - "plots/subdir/data_subdir_v1.json": {"x": "z"}, + "plots/plot.json": {"x": "x", "y": "y"}, + "plots/subdir/plot.json": {"x": "z", "y": "x"}, + "subdir axis defined by filename": { + "x": {"plots/subdir/plot.json": "x"}, + "y": {"plots/subdir/plot.json": "y"}, + }, } diff --git a/tests/integration/plots/conftest.py b/tests/integration/plots/conftest.py index fd5deb58b..8199d80da 100644 --- a/tests/integration/plots/conftest.py +++ b/tests/integration/plots/conftest.py @@ -86,15 +86,27 @@ def repo_with_plots(tmp_dir, scm, dvc, run_copy_metrics): @pytest.fixture def repo_with_config_plots(tmp_dir, scm, dvc, run_copy_metrics): def make(): + # test subdir functionality + linear_subdirA_v1 = [ + {"x": 1, "y": 0.2}, + {"x": 2, "y": 0.3}, + {"x": 3, "y": 0.4}, + ] + # test subdir default values for x = step + linear_subdirB_v1 = [ + {"step": 1, "y": 0.2}, + {"step": 2, "y": 0.3}, + {"step": 3, "y": 0.4}, + ] linear_train_v1 = [ {"x": 1, "y": 0.1}, {"x": 2, "y": 0.2}, {"x": 3, "y": 0.3}, ] linear_test_v1 = [ - {"x": 1, "y": 0.2}, - {"x": 2, "y": 0.3}, - {"x": 3, "y": 0.4}, + {"x": 1, "y": 0.3}, + {"x": 2, "y": 0.4}, + {"x": 3, "y": 0.5}, ] confusion_train_v1 = [ @@ -110,6 +122,10 @@ def repo_with_config_plots(tmp_dir, scm, dvc, run_copy_metrics): {"actual": 1, "predicted": 0}, ] + (tmp_dir / "subdirA").mkdir() + (tmp_dir / "subdirB").mkdir() + (tmp_dir / "subdirA" / "linear_subdir_src.json").dump_json(linear_subdirA_v1) + (tmp_dir / "subdirB" / "linear_subdir_src.json").dump_json(linear_subdirB_v1) (tmp_dir / "linear_train_src.json").dump_json(linear_train_v1) (tmp_dir / "linear_test_src.json").dump_json(linear_test_v1) (tmp_dir / "confusion_train_src.json").dump_json(confusion_train_v1) @@ -117,6 +133,8 @@ def repo_with_config_plots(tmp_dir, scm, dvc, run_copy_metrics): scm.add( [ + "subdirA/linear_subdir_src.json", + "subdirB/linear_subdir_src.json", "linear_train_src.json", "linear_test_src.json", "confusion_train_src.json", @@ -125,6 +143,20 @@ def repo_with_config_plots(tmp_dir, scm, dvc, run_copy_metrics): ) scm.commit("add data sources") + run_copy_metrics( + "subdirA/linear_subdir_src.json", + "subdirA/linear_subdir.json", + name="linear_subdir", + outs=["subdirA/linear_subdir.json"], + commit="linear_subdir", + ) + run_copy_metrics( + "subdirB/linear_subdir_src.json", + "subdirB/linear_subdir.json", + name="linear_subdir", + outs=["subdirB/linear_subdir.json"], + commit="linear_subdir", + ) run_copy_metrics( "linear_train_src.json", "linear_train.json", @@ -154,6 +186,14 @@ def repo_with_config_plots(tmp_dir, scm, dvc, run_copy_metrics): commit="confusion_test", ) plots_config = [ + { + "subdirA": { + "x": "x", + "y": "y", + "title": "subdir plots with x and y defined", + } + }, + {"subdirB": {"title": "subdir plots with default x and y"}}, { "linear_train_vs_test": { "x": "x", @@ -182,6 +222,8 @@ def repo_with_config_plots(tmp_dir, scm, dvc, run_copy_metrics): scm.commit("commit dvc files") yield { "data": { + "subdirA/linear_subdir.json": linear_subdirA_v1, + "subdirB/linear_subdir.json": linear_subdirB_v1, "linear_train.json": linear_train_v1, "linear_test.json": linear_test_v1, "confusion_train.json": confusion_train_v1, diff --git a/tests/integration/plots/test_plots.py b/tests/integration/plots/test_plots.py index e3d5f189d..d53c5a467 100644 --- a/tests/integration/plots/test_plots.py +++ b/tests/integration/plots/test_plots.py @@ -440,6 +440,8 @@ def test_repo_with_config_plots(tmp_dir, capsys, repo_with_config_plots): html_result = extract_vega_specs( html_path, [ + "subdirA/linear_subdir.json", + "subdirB/linear_subdir.json", "linear_train_vs_test", "confusion_train_vs_test", ], @@ -466,6 +468,22 @@ def test_repo_with_config_plots(tmp_dir, capsys, repo_with_config_plots): ] == ble ) + assert ( + html_result["subdirA/linear_subdir.json"]["layer"][1]["encoding"]["x"]["field"] + == "x" + ) + assert ( + html_result["subdirA/linear_subdir.json"]["layer"][1]["encoding"]["y"]["field"] + == "y" + ) + assert ( + html_result["subdirB/linear_subdir.json"]["layer"][1]["encoding"]["x"]["field"] + == "step" + ) + assert ( + html_result["subdirB/linear_subdir.json"]["layer"][1]["encoding"]["y"]["field"] + == "y" + ) @pytest.mark.vscode