tests: tests for dvcfile (#4032)

This commit is contained in:
Saugat Pachhai
2020-06-12 15:54:05 +03:00
committed by GitHub
parent 9ead641c95
commit b94a18cfb9
2 changed files with 64 additions and 17 deletions
+8 -16
View File
@@ -2,7 +2,7 @@ import textwrap
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,
@@ -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):
@@ -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):
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
+56 -1
View File
@@ -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(
@@ -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]