diff --git a/tests/core/test_artifacts.py b/tests/core/test_artifacts.py new file mode 100644 index 00000000..ed1cd608 --- /dev/null +++ b/tests/core/test_artifacts.py @@ -0,0 +1,79 @@ +from pathlib import Path + +from baby_steps import given, then, when +from pytest import raises + +from vedro.core import FileArtifact, MemoryArtifact + + +def test_memory_artifact(): + with given: + name = "log" + mime_type = "text/plain" + data = b"" + + with when: + artifact = MemoryArtifact(name, mime_type, data) + + with then: + assert artifact.name == name + assert artifact.mime_type == mime_type + assert artifact.data == data + + +def test_memory_artifact_repr(): + with given: + name = "log" + mime_type = "text/plain" + data = b"" + + with when: + artifact = MemoryArtifact(name, mime_type, data) + + with then: + assert repr(artifact) == f"MemoryArtifact<{name!r}, {mime_type!r}, size={len(data)}>" + + +def test_memory_artifact_binary_only(): + with raises(Exception) as exc_info: + MemoryArtifact("log", "text/plain", "text") + + with then: + assert exc_info.type is AssertionError + + +def test_file_artifact(): + with given: + name = "log" + mime_type = "text/plain" + path = Path() + + with when: + artifact = FileArtifact(name, mime_type, path) + + with then: + assert artifact.name == name + assert artifact.mime_type == mime_type + assert artifact.path == path + + +def test_file_artifact_repr(): + with given: + name = "log" + mime_type = "text/plain" + path = Path() + + with when: + artifact = FileArtifact(name, mime_type, path) + + with then: + print(repr(artifact)) + assert repr(artifact) == f"FileArtifact<{name!r}, {mime_type!r}, {path!r}>" + + +def test_file_artifact_path_only(): + with raises(Exception) as exc_info: + FileArtifact("log", "text/plain", "./log.txt") + + with then: + assert exc_info.type is AssertionError diff --git a/tests/core/test_attachment.py b/tests/core/test_attachment.py deleted file mode 100644 index 9b3d8b77..00000000 --- a/tests/core/test_attachment.py +++ /dev/null @@ -1,40 +0,0 @@ -from baby_steps import given, then, when -from pytest import raises - -from vedro.core import Attachment - - -def test_attachment(): - with given: - name = "log" - mime_type = "text/plain" - data = b"" - - with when: - attachment = Attachment(name, mime_type, data) - - with then: - assert attachment.name == name - assert attachment.mime_type == mime_type - assert attachment.data == data - - -def test_attachment_repr(): - with given: - name = "log" - mime_type = "text/plain" - data = b"" - - with when: - attachment = Attachment(name, mime_type, data) - - with then: - assert repr(attachment) == f"Attachment<{name!r}, {mime_type!r}, size={len(data)}>" - - -def test_attachment_binary(): - with raises(Exception) as exc_info: - Attachment("log", "text/plain", "text") - - with then: - assert exc_info.type is AssertionError diff --git a/tests/core/test_scenario_result.py b/tests/core/test_scenario_result.py index 0e48f8f1..51481861 100644 --- a/tests/core/test_scenario_result.py +++ b/tests/core/test_scenario_result.py @@ -9,7 +9,7 @@ from vedro import Scenario from vedro.core import ( - Attachment, + MemoryArtifact, ScenarioResult, ScenarioStatus, StepResult, @@ -224,30 +224,30 @@ def test_scenario_result_not_eq(): assert res is False -def test_scenario_result_attach(*, virtual_scenario: VirtualScenario): +def test_scenario_result_attach_artifact(*, virtual_scenario: VirtualScenario): with given: scenario_result = ScenarioResult(virtual_scenario) - attachment = Attachment("name", "text/plain", b"") + artifact = MemoryArtifact("name", "text/plain", b"") with when: - res = scenario_result.attach(attachment) + res = scenario_result.attach(artifact) with then: assert res is None -def test_scenario_result_get_attachments(*, virtual_scenario: VirtualScenario): +def test_scenario_result_get_artifacts(*, virtual_scenario: VirtualScenario): with given: scenario_result = ScenarioResult(virtual_scenario) - attachment1 = Attachment("name1", "text/plain", b"") - scenario_result.attach(attachment1) + artifact1 = MemoryArtifact("name1", "text/plain", b"") + scenario_result.attach(artifact1) - attachment2 = Attachment("name2", "text/plain", b"") - scenario_result.attach(attachment2) + artifact2 = MemoryArtifact("name2", "text/plain", b"") + scenario_result.attach(artifact2) with when: - attachments = scenario_result.attachments + artifacts = scenario_result.artifacts with then: - assert attachments == [attachment1, attachment2] + assert artifacts == [artifact1, artifact2] diff --git a/tests/core/test_step_result.py b/tests/core/test_step_result.py index 6715a010..41f66d84 100644 --- a/tests/core/test_step_result.py +++ b/tests/core/test_step_result.py @@ -4,7 +4,7 @@ import pytest from baby_steps import given, then, when -from vedro.core import Attachment, ExcInfo, StepResult, StepStatus, VirtualStep +from vedro.core import ExcInfo, MemoryArtifact, StepResult, StepStatus, VirtualStep @pytest.fixture() @@ -141,30 +141,30 @@ def test_step_result_not_eq(): assert res is False -def test_step_result_attach(*, virtual_step: VirtualStep): +def test_step_result_attach_artifact(*, virtual_step: VirtualStep): with given: step_result = StepResult(virtual_step) - attachment = Attachment("name", "text/plain", b"") + artifact = MemoryArtifact("name", "text/plain", b"") with when: - res = step_result.attach(attachment) + res = step_result.attach(artifact) with then: assert res is None -def test_step_result_get_attachments(*, virtual_step: VirtualStep): +def test_step_result_get_artifacts(*, virtual_step: VirtualStep): with given: step_result = StepResult(virtual_step) - attachment1 = Attachment("name1", "text/plain", b"") - step_result.attach(attachment1) + artifact1 = MemoryArtifact("name1", "text/plain", b"") + step_result.attach(artifact1) - attachment2 = Attachment("name2", "text/plain", b"") - step_result.attach(attachment2) + artifact2 = MemoryArtifact("name2", "text/plain", b"") + step_result.attach(artifact2) with when: - attachments = step_result.attachments + artifacts = step_result.artifacts with then: - assert attachments == [attachment1, attachment2] + assert artifacts == [artifact1, artifact2] diff --git a/vedro/core/__init__.py b/vedro/core/__init__.py index 829c990a..43531cea 100644 --- a/vedro/core/__init__.py +++ b/vedro/core/__init__.py @@ -1,5 +1,5 @@ from ._arg_parser import ArgumentParser -from ._attachment import Attachment +from ._artifacts import Artifact, FileArtifact, MemoryArtifact from ._config_loader import Config, ConfigFileLoader, ConfigLoader, ConfigType, Section from ._dispatcher import Dispatcher, Subscriber from ._event import Event @@ -22,4 +22,4 @@ "ScenarioResult", "StepResult", "VirtualScenario", "VirtualStep", "ScenarioStatus", "StepStatus", "ArgumentParser", "ConfigLoader", "ConfigFileLoader", "Config", "Section", "ConfigType", - "ModuleLoader", "ModuleFileLoader", "Attachment",) + "ModuleLoader", "ModuleFileLoader", "Artifact", "MemoryArtifact", "FileArtifact",) diff --git a/vedro/core/_artifacts.py b/vedro/core/_artifacts.py new file mode 100644 index 00000000..95345af9 --- /dev/null +++ b/vedro/core/_artifacts.py @@ -0,0 +1,55 @@ +from abc import ABC +from pathlib import Path + +__all__ = ("Artifact", "MemoryArtifact", "FileArtifact",) + + +class Artifact(ABC): + pass + + +class MemoryArtifact(Artifact): + def __init__(self, name: str, mime_type: str, data: bytes) -> None: + assert isinstance(data, bytes) + self._name = name + self._data = data + self._mime_type = mime_type + + @property + def name(self) -> str: + return self._name + + @property + def mime_type(self) -> str: + return self._mime_type + + @property + def data(self) -> bytes: + return self._data + + def __repr__(self) -> str: + size = len(self._data) + return f"{self.__class__.__name__}<{self._name!r}, {self._mime_type!r}, size={size}>" + + +class FileArtifact(Artifact): + def __init__(self, name: str, mime_type: str, path: Path) -> None: + assert isinstance(path, Path) + self._name = name + self._path = path + self._mime_type = mime_type + + @property + def name(self) -> str: + return self._name + + @property + def mime_type(self) -> str: + return self._mime_type + + @property + def path(self) -> Path: + return self._path + + def __repr__(self) -> str: + return f"{self.__class__.__name__}<{self._name!r}, {self._mime_type!r}, {self._path!r}>" diff --git a/vedro/core/_attachment.py b/vedro/core/_attachment.py deleted file mode 100644 index 50f561c0..00000000 --- a/vedro/core/_attachment.py +++ /dev/null @@ -1,25 +0,0 @@ -__all__ = ("Attachment",) - - -class Attachment: - def __init__(self, name: str, mime_type: str, data: bytes) -> None: - assert isinstance(data, bytes) - self._name = name - self._data = data - self._mime_type = mime_type - - @property - def name(self) -> str: - return self._name - - @property - def data(self) -> bytes: - return self._data - - @property - def mime_type(self) -> str: - return self._mime_type - - def __repr__(self) -> str: - size = len(self._data) - return f"{self.__class__.__name__}<{self._name!r}, {self._mime_type!r}, size={size}>" diff --git a/vedro/core/_scenario_result.py b/vedro/core/_scenario_result.py index 858647be..588b094f 100644 --- a/vedro/core/_scenario_result.py +++ b/vedro/core/_scenario_result.py @@ -2,7 +2,7 @@ from enum import Enum from typing import Any, Dict, List, Union -from ._attachment import Attachment +from ._artifacts import Artifact from ._step_result import StepResult from ._virtual_scenario import VirtualScenario @@ -24,7 +24,7 @@ def __init__(self, scenario: VirtualScenario, *, rerun: int = 0) -> None: self._ended_at: Union[float, None] = None self._step_results: List[StepResult] = [] self._scope: Union[Dict[Any, Any], None] = None - self._attachments: List[Attachment] = [] + self._artifacts: List[Artifact] = [] self._rerun = rerun @property @@ -108,13 +108,13 @@ def scope(self) -> Dict[Any, Any]: return {} return self._scope - def attach(self, attachment: Attachment) -> None: - assert isinstance(attachment, Attachment) - self._attachments.append(attachment) + def attach(self, artifact: Artifact) -> None: + assert isinstance(artifact, Artifact) + self._artifacts.append(artifact) @property - def attachments(self) -> List[Attachment]: - return self._attachments[:] + def artifacts(self) -> List[Artifact]: + return self._artifacts[:] def __repr__(self) -> str: return f"{self.__class__.__name__}<{self._scenario!r}>" diff --git a/vedro/core/_step_result.py b/vedro/core/_step_result.py index cf846932..04f5159f 100644 --- a/vedro/core/_step_result.py +++ b/vedro/core/_step_result.py @@ -1,7 +1,7 @@ from enum import Enum from typing import Any, List, Union -from ._attachment import Attachment +from ._artifacts import Artifact from ._exc_info import ExcInfo from ._virtual_step import VirtualStep @@ -21,7 +21,7 @@ def __init__(self, step: VirtualStep) -> None: self._started_at: Union[float, None] = None self._ended_at: Union[float, None] = None self._exc_info: Union[ExcInfo, None] = None - self._attachments: List[Attachment] = [] + self._artifacts: List[Artifact] = [] @property def step_name(self) -> str: @@ -75,13 +75,13 @@ def set_exc_info(self, exc_info: ExcInfo) -> "StepResult": self._exc_info = exc_info return self - def attach(self, attachment: Attachment) -> None: - assert isinstance(attachment, Attachment) - self._attachments.append(attachment) + def attach(self, artifact: Artifact) -> None: + assert isinstance(artifact, Artifact) + self._artifacts.append(artifact) @property - def attachments(self) -> List[Attachment]: - return self._attachments[:] + def artifacts(self) -> List[Artifact]: + return self._artifacts[:] def __repr__(self) -> str: return f"{self.__class__.__name__}({self._step!r})"