Skip to content

Commit

Permalink
add artifacts (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsv1 authored Jun 5, 2022
1 parent 4c501f3 commit 7ee77ef
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 103 deletions.
79 changes: 79 additions & 0 deletions tests/core/test_artifacts.py
Original file line number Diff line number Diff line change
@@ -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
40 changes: 0 additions & 40 deletions tests/core/test_attachment.py

This file was deleted.

22 changes: 11 additions & 11 deletions tests/core/test_scenario_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from vedro import Scenario
from vedro.core import (
Attachment,
MemoryArtifact,
ScenarioResult,
ScenarioStatus,
StepResult,
Expand Down Expand Up @@ -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]
22 changes: 11 additions & 11 deletions tests/core/test_step_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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]
4 changes: 2 additions & 2 deletions vedro/core/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,4 +22,4 @@
"ScenarioResult", "StepResult", "VirtualScenario", "VirtualStep",
"ScenarioStatus", "StepStatus", "ArgumentParser",
"ConfigLoader", "ConfigFileLoader", "Config", "Section", "ConfigType",
"ModuleLoader", "ModuleFileLoader", "Attachment",)
"ModuleLoader", "ModuleFileLoader", "Artifact", "MemoryArtifact", "FileArtifact",)
55 changes: 55 additions & 0 deletions vedro/core/_artifacts.py
Original file line number Diff line number Diff line change
@@ -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}>"
25 changes: 0 additions & 25 deletions vedro/core/_attachment.py

This file was deleted.

14 changes: 7 additions & 7 deletions vedro/core/_scenario_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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}>"
Expand Down
14 changes: 7 additions & 7 deletions vedro/core/_step_result.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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})"
Expand Down

0 comments on commit 7ee77ef

Please sign in to comment.