Skip to content

tests: unittests for dvcfile and some small fixes #4032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions tests/func/test_dvcfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from dvc.dvcfile import PIPELINE_FILE, PIPELINE_LOCK, Dvcfile
from dvc.dvcfile import PIPELINE_FILE, PIPELINE_LOCK, Dvcfile, SingleStageFile
from dvc.stage.exceptions import (
StageFileDoesNotExistError,
StageFileFormatError,
Expand Down Expand Up @@ -59,8 +59,9 @@ def test_run_load_one_on_single_stage(tmp_dir, dvc):
always_changed=True,
single_stage=True,
)
assert Dvcfile(dvc, stage.path).stages.get("random-name")
assert Dvcfile(dvc, stage.path).stage
assert isinstance(Dvcfile(dvc, stage.path), SingleStageFile)
assert Dvcfile(dvc, stage.path).stages.get("random-name") == stage
assert Dvcfile(dvc, stage.path).stage == stage


def test_has_stage_with_name(tmp_dir, dvc):
Expand Down Expand Up @@ -110,23 +111,14 @@ def test_load_all_singlestage(tmp_dir, dvc):
always_changed=True,
single_stage=True,
)
stages = Dvcfile(dvc, "foo2.dvc").stages.values()
dvcfile = Dvcfile(dvc, "foo2.dvc")
assert isinstance(dvcfile, SingleStageFile)
assert len(dvcfile.stages) == 1
stages = dvcfile.stages.values()
assert len(stages) == 1
assert list(stages) == [stage1]


def test_load_singlestage(tmp_dir, dvc):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test was similar to what we have above test_run_load_one_on_single_stage.

tmp_dir.gen("foo", "foo")
stage1 = dvc.run(
cmd="cp foo foo2",
deps=["foo"],
metrics=["foo2"],
always_changed=True,
single_stage=True,
)
assert Dvcfile(dvc, "foo2.dvc").stage == stage1


def test_try_get_single_stage_from_pipeline_file(tmp_dir, dvc):
from dvc.dvcfile import DvcException

Expand Down
57 changes: 56 additions & 1 deletion tests/unit/test_dvcfile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import pytest

from dvc.dvcfile import Dvcfile, PipelineFile, SingleStageFile
from dvc.dvcfile import (
PIPELINE_FILE,
PIPELINE_LOCK,
Dvcfile,
PipelineFile,
SingleStageFile,
)
from dvc.stage import PipelineStage
from dvc.stage.exceptions import (
StageFileDoesNotExistError,
StageFileFormatError,
StageFileIsNotDvcFileError,
)
from dvc.utils.yaml import dump_yaml


@pytest.mark.parametrize(
Expand All @@ -24,3 +37,45 @@ def test_pipelines_file(path):
def test_pipelines_single_stage_file(path):
file_obj = Dvcfile(object(), path)
assert isinstance(file_obj, SingleStageFile)


@pytest.mark.parametrize("file", ["stage.dvc", "dvc.yaml"])
def test_stage_load_on_not_existing_file(tmp_dir, dvc, file):
dvcfile = Dvcfile(dvc, file)
assert not dvcfile.exists()
with pytest.raises(StageFileDoesNotExistError):
assert dvcfile.stages.values()
(tmp_dir / file).mkdir()
with pytest.raises(StageFileIsNotDvcFileError):
assert dvcfile.stages.values()


@pytest.mark.parametrize("file", ["stage.dvc", "dvc.yaml"])
def test_stage_load_on_invalid_data(tmp_dir, dvc, file):
data = {"is_this_a_valid_dvcfile": False}
dump_yaml(file, data)
dvcfile = Dvcfile(dvc, file)
with pytest.raises(StageFileFormatError):
assert dvcfile.stages
with pytest.raises(StageFileFormatError):
assert dvcfile.validate(data, file)


def test_dump_stage(tmp_dir, dvc):
stage = PipelineStage(
dvc, cmd="command", name="stage_name", path="dvc.yaml"
)
dvcfile = Dvcfile(dvc, "dvc.yaml")

dvcfile.dump(stage, no_lock=True)
assert not (tmp_dir / PIPELINE_FILE).exists()
assert not (tmp_dir / PIPELINE_LOCK).exists()

dvcfile.dump(stage, no_lock=False)
assert not (tmp_dir / PIPELINE_FILE).exists()
assert dvcfile._lockfile.load()

dvcfile.dump(stage, update_pipeline=True, no_lock=False)
assert (tmp_dir / PIPELINE_FILE).exists()
assert (tmp_dir / PIPELINE_LOCK).exists()
assert list(dvcfile.stages.values()) == [stage]