Esempio n. 1
0
def test_should_return_projects_details_one_streams(
    get_project_details_use_case: GetProjectDetailsUseCase,
    find_project_by_project_id: Mock,
    find_streams_by_project: Mock,
) -> None:
    project_id = uuid4()
    project = Project(
        project_id=project_id,
        title="Test Project Title",
        created_at=datetime.now(),
        status=ProjectStatus.ACTIVE,
    )
    project_maybe: Maybe[Project] = Maybe.from_value(project)
    streams = [
        Stream(
            stream_id=uuid4(),
            name="Test Stream",
            source_type=SourceType.TOPIC,
            source_name="test_one_stream",
        ),
    ]
    find_project_by_project_id.return_value = Success(project_maybe)
    find_streams_by_project.return_value = Success(StreamList(streams))

    actual = get_project_details_use_case(project_id)

    find_project_by_project_id.assert_called_once()
    find_streams_by_project.assert_called_once()
    find_streams_by_project.assert_called_with(project)
    assert isinstance(actual, Result.success_type)
    assert project_id == actual.unwrap().project.project_id
    assert 1 == len(actual.unwrap().streams)
    assert streams == actual.unwrap().streams
Esempio n. 2
0
 def __call__(self,
              stream_id: UUID) -> Result[Maybe[Stream], FailureDetails]:
     stream = StreamModel.query.filter(
         StreamModel.stream_id == stream_id).first()
     maybe_stream: Maybe[Stream] = Maybe.from_value(stream).map(
         lambda _stream: _stream.to_entity()  # type: ignore
     )
     return Success(maybe_stream)
Esempio n. 3
0
 def __call__(self,
              project_id: UUID) -> Result[Maybe[Project], FailureDetails]:
     project = ProjectModel.query.filter(
         ProjectModel.project_id == project_id).first()
     maybe_project: Maybe[Project] = Maybe.from_value(project).map(
         lambda _project: _project.to_entity()  # type: ignore
     )
     return Success(maybe_project)
def test_should_return_stream_details_correctly(
    get_stream_details_use_case: GetStreamDetailsUseCase,
    find_stream_by_stream_id: Mock,
    find_projects_by_stream: Mock,
    get_stream_by_name: Mock,
) -> None:
    stream_id = uuid4()
    stream = Stream(
        stream_id=stream_id,
        name="TEST_STREAM",
        source_type=SourceType.STREAM,
        source_name="OTHER_STREAM_TEST",
    )
    project_list = [
        Project(
            project_id=uuid4(),
            title="Project One",
            created_at=datetime.now(),
            status=ProjectStatus.ACTIVE,
        ),
        Project(
            project_id=uuid4(),
            title="Project Two",
            created_at=datetime.now(),
            status=ProjectStatus.INACTIVE,
        ),
    ]
    ksql_stream_detailed = KSQLStreamDetailed(
        name="TEST_STREAM",
        fields=[
            KSQLStreamDetailed.KSQLStreamField(
                name="field_one",
                schema=KSQLStreamDetailed.KSQLStreamField.
                KSQLStreamFieldSchema(type="INT"),
            )
        ],
        type="STREAM",
        format="JSON",
        topic="SOME_TOPIC",
    )
    stream_details = StreamDetails.build(stream, project_list,
                                         ksql_stream_detailed)

    find_stream_by_stream_id.return_value = Success(Maybe.from_value(stream))
    find_projects_by_stream.return_value = Success(project_list)
    get_stream_by_name.return_value = Success(ksql_stream_detailed)

    actual = get_stream_details_use_case(stream_id)

    get_stream_by_name.assert_called_once()
    get_stream_by_name.assert_called_with(stream.name)
    assert isinstance(actual, Result.success_type)
    assert isinstance(actual.unwrap(), StreamDetails)
    assert stream_details == actual.unwrap()
Esempio n. 5
0
def test_should_return_failure_not_found_when_project_was_not_found(
    get_project_details_use_case: GetProjectDetailsUseCase,
    find_project_by_project_id: Mock,
) -> None:
    project_id = uuid4()
    project: Maybe[None] = Maybe.from_value(None)
    find_project_by_project_id.return_value = Success(project)

    actual = get_project_details_use_case(project_id)

    find_project_by_project_id.assert_called_once()
    assert isinstance(actual, Result.failure_type)
    assert isinstance(actual.failure(), BusinessFailureDetails)
    assert "NOT_FOUND" == actual.failure().reason
Esempio n. 6
0
def result_to_maybe(
    result_container: Result[_ValueType, _ErrorType], ) -> Maybe[_ValueType]:
    """
    Converts ``Result`` container to ``Maybe`` container.

    .. code:: python

      >>> from returns.maybe import Some, Nothing
      >>> from returns.result import Failure, Success

      >>> assert result_to_maybe(Success(1)) == Some(1)
      >>> assert result_to_maybe(Failure(1)) == Nothing
      >>> assert result_to_maybe(Success(None)) == Nothing
      >>> assert result_to_maybe(Failure(None)) == Nothing

    """
    return Maybe.from_value(result_container.value_or(None))
def test_should_return_failure_when_get_stream_by_name_fails(
    get_stream_details_use_case: GetStreamDetailsUseCase,
    find_stream_by_stream_id: Mock,
    find_projects_by_stream: Mock,
    get_stream_by_name: Mock,
) -> None:
    stream_id = uuid4()
    stream = Stream(
        stream_id=stream_id,
        name="TEST_STREAM",
        source_type=SourceType.STREAM,
        source_name="OTHER_STREAM_TEST",
    )
    project_list = [
        Project(
            project_id=uuid4(),
            title="Project One",
            created_at=datetime.now(),
            status=ProjectStatus.ACTIVE,
        ),
        Project(
            project_id=uuid4(),
            title="Project Two",
            created_at=datetime.now(),
            status=ProjectStatus.INACTIVE,
        ),
    ]
    failure = FailureDetails(reason="TEST_FIND_STREAM_BY_NAME_FAILS")

    find_stream_by_stream_id.return_value = Success(Maybe.from_value(stream))
    find_projects_by_stream.return_value = Success(project_list)
    get_stream_by_name.return_value = Failure(failure)

    actual = get_stream_details_use_case(stream_id)

    get_stream_by_name.assert_called_once()
    get_stream_by_name.assert_called_with(stream.name)
    assert isinstance(actual, Result.failure_type)
    assert isinstance(actual.failure(), FailureDetails)
    assert failure == actual.failure()
def test_should_return_failure_when_find_projects_by_stream_fails(
    get_stream_details_use_case: GetStreamDetailsUseCase,
    find_stream_by_stream_id: Mock,
    find_projects_by_stream: Mock,
) -> None:
    stream_id = uuid4()
    stream = Stream(
        stream_id=stream_id,
        name="TEST_STREAM",
        source_type=SourceType.STREAM,
        source_name="OTHER_STREAM_TEST",
    )
    failure = FailureDetails(reason="TEST_FIND_PROJECTS_FAILS")

    find_stream_by_stream_id.return_value = Success(Maybe.from_value(stream))
    find_projects_by_stream.return_value = Failure(failure)

    actual = get_stream_details_use_case(stream_id)

    find_projects_by_stream.assert_called_once()
    find_projects_by_stream.assert_called_with(stream)
    assert isinstance(actual, Result.failure_type)
    assert isinstance(actual.failure(), FailureDetails)
    assert failure == actual.failure()
Esempio n. 9
0
)
from returns.contrib.pytest import ReturnsAsserts
from returns.future import Future, FutureResult
from returns.io import IO, IOResult
from returns.maybe import Maybe
from returns.primitives.asserts import assert_equal
from returns.primitives.container import BaseContainer
from returns.result import Result

_containers: Sequence[BaseContainer] = (
    Result.from_failure(1),
    Result.from_value(1),
    IO(1),
    IOResult.from_failure(1),
    IOResult.from_value(1),
    Maybe.from_value(1),
    Maybe.from_value(None),
    Maybe.from_optional(None),
    Future.from_value(1),
    FutureResult.from_value(1),
    FutureResult.from_failure(1),
    Reader.from_value(1),
    ReaderResult.from_value(1),
    ReaderResult.from_failure(1),
    ReaderIOResult.from_value(1),
    ReaderIOResult.from_failure(1),
    ReaderFutureResult.from_value(1),
    ReaderFutureResult.from_failure(1),
)

Esempio n. 10
0
def test_return_false_with_maybe_container():
    """Ensures `is_io` function will return False for Maybe."""
    assert is_io(Maybe.from_value(None)) is False
Esempio n. 11
0
def test_maybe_new_nothing():
    """Ensures that `new` works for Nothing container."""
    assert Maybe.from_value(None) == Nothing
Esempio n. 12
0
def test_maybe_new_some():
    """Ensures that `new` works for Some container."""
    assert Maybe.from_value(5) == Some(5)
Esempio n. 13
0
 def safe_call(self, func: str, *args, **kwargs) -> Maybe:
     """ Calls mlflow method, if it's enabled and safely does nothing in the opposite case"""
     return Maybe.from_value(self._get_mlflow()).bind(
         lambda obj: Maybe.from_value(getattr(obj, func)(*args, **kwargs)))
Esempio n. 14
0
def test_apply_with_maybe():
    """Ensures that functions can be composed and return type is correct."""
    applied = apply(Maybe.from_value(_function))

    assert applied(Some(1)) == Some('2')
    assert applied(Nothing) == Nothing
Esempio n. 15
0
def maybe_type(obj: Any, expected: Type[T]) -> Maybe[T]:
    return Maybe.from_value(obj) if isinstance(
        obj, not_empty(expected)) else Nothing