File: test_flatten.py

package info (click to toggle)
python-returns 0.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: python: 11,000; makefile: 18
file content (137 lines) | stat: -rw-r--r-- 4,216 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import pytest

from returns.context import (
    RequiresContext,
    RequiresContextFutureResult,
    RequiresContextIOResult,
    RequiresContextResult,
)
from returns.converters import flatten
from returns.future import Future, FutureResult
from returns.io import IO, IOFailure, IOSuccess
from returns.maybe import Nothing, Some
from returns.result import Failure, Success


@pytest.mark.parametrize(
    ('container', 'merged'),
    [
        # Flattens:
        (IO(IO(1)), IO(1)),
        (Success(Success({})), Success({})),
        (IOSuccess(IOSuccess(1)), IOSuccess(1)),
        (Some(Some(None)), Some(None)),
        (Some(Some([])), Some([])),
        # Nope:
        (Nothing, Nothing),
        (Failure(Failure('a')), Failure(Failure('a'))),
        (Failure(Success('a')), Failure(Success('a'))),
        (IOFailure(IOFailure('a')), IOFailure(IOFailure('a'))),
        (IOFailure(IOSuccess('a')), IOFailure(IOSuccess('a'))),
    ],
)
def test_flatten(container, merged):
    """Ensures that `flatten` is always returning the correct type."""
    assert flatten(container) == merged


@pytest.mark.parametrize(
    ('container', 'merged'),
    [
        (
            RequiresContextResult.from_value(
                RequiresContextResult.from_value(1),
            ),
            RequiresContextResult.from_value(1),
        ),
        (
            RequiresContextIOResult.from_value(
                RequiresContextIOResult.from_value(1),
            ),
            RequiresContextIOResult.from_value(1),
        ),
        (
            RequiresContext.from_value(RequiresContext.from_value(1)),
            RequiresContext.from_value(1),
        ),
    ],
)
def test_flatten_context(container, merged):
    """Ensures that `flatten` is always returning the correct type."""
    assert flatten(container)(...) == merged(...)


@pytest.mark.anyio
async def test_flatten_future(subtests):
    """Ensures that `flatten` is always returning the correct type."""
    futures = [
        # Flattens:
        (Future.from_value(Future.from_value(1)), Future.from_value(1)),
        (
            FutureResult.from_value(FutureResult.from_value(1)),
            FutureResult.from_value(1),
        ),
    ]

    for container, merged in futures:
        with subtests.test(container=container, merged=merged):
            assert await flatten(container) == await merged  # type: ignore


@pytest.mark.anyio
async def test_flatten_context_future_result(subtests):
    """Ensures that `flatten` is always returning the correct type."""
    futures = [
        # Flattens:
        (
            RequiresContextFutureResult.from_value(
                RequiresContextFutureResult.from_value(1),
            ),
            RequiresContextFutureResult.from_value(1),
        ),
    ]

    for container, merged in futures:
        with subtests.test(container=container, merged=merged):
            assert await flatten(
                container,
            )(...) == await merged(...)


@pytest.mark.anyio
async def test_non_flatten_future(subtests):
    """Ensures that `flatten` is always returning the correct type."""
    futures = [
        # Not flattens:
        FutureResult.from_failure(FutureResult.from_failure(1)),
        FutureResult.from_failure(FutureResult.from_value(1)),
    ]

    for cont in futures:
        with subtests.test(container=cont):
            assert isinstance(
                (await flatten(cont)).failure()._inner_value,  # noqa: SLF001
                cont.__class__,
            )


@pytest.mark.anyio
async def test_non_flatten_context_future_result(subtests):
    """Ensures that `flatten` is always returning the correct type."""
    futures = [
        # Not flattens:
        RequiresContextFutureResult.from_failure(
            RequiresContextFutureResult.from_failure(1),
        ),
        RequiresContextFutureResult.from_failure(
            RequiresContextFutureResult.from_value(1),
        ),
    ]

    for cont in futures:
        with subtests.test(container=cont):
            inner = await flatten(cont)(...)
            assert isinstance(
                inner.failure()._inner_value,  # noqa: SLF001
                cont.__class__,
            )