File: test_maybe_equality.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 (101 lines) | stat: -rw-r--r-- 2,981 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
from copy import copy, deepcopy

import pytest

from returns.maybe import Nothing, Some, _Nothing  # noqa: PLC2701
from returns.primitives.exceptions import ImmutableStateError


def test_equals():
    """Ensures that ``.equals`` method works correctly."""
    inner_value = 1

    assert Some(inner_value).equals(Some(inner_value))
    assert Nothing.equals(Nothing)


def test_not_equals():
    """Ensures that ``.equals`` method works correctly."""
    assert not Some(1).equals(Nothing)
    assert not Some(1).equals(Some(0))
    assert not Nothing.equals(Some(1))


def test_equality():
    """Ensures that containers can be compared."""
    assert Nothing is Nothing  # noqa: PLR0124, WPS312
    assert Nothing == _Nothing() == _Nothing(None)
    assert Some(5) == Some(5)
    assert hash(Some(1))
    assert hash(Nothing)


def test_nonequality():
    """Ensures that containers are not compared to regular values."""
    assert Nothing is not None
    assert Nothing != None  # noqa: E711
    assert _Nothing(None) != None  # noqa: E711
    assert Some(5) != 5
    assert Some(3) is not Some(3)


def test_is_compare():
    """Ensures that `is` operator works correctly."""
    some_container = Some(1)

    assert Nothing.bind(lambda state: state) is Nothing
    assert some_container is not Some(1)


def test_immutability_failure():
    """Ensures that Failure container is immutable."""
    with pytest.raises(ImmutableStateError):
        Nothing._inner_state = 1  # noqa: SLF001

    with pytest.raises(ImmutableStateError):
        Nothing.missing = 2

    with pytest.raises(ImmutableStateError):
        del Nothing._inner_state  # type: ignore # noqa: SLF001, WPS420

    with pytest.raises(AttributeError):
        Nothing.missing  # type: ignore # noqa: B018


def test_immutability_success():
    """Ensures that Success container is immutable."""
    with pytest.raises(ImmutableStateError):
        Some(0)._inner_state = 1  # noqa: SLF001

    with pytest.raises(ImmutableStateError):
        Some(1).missing = 2

    with pytest.raises(ImmutableStateError):
        del Some(0)._inner_state  # type: ignore # noqa: SLF001, WPS420

    with pytest.raises(AttributeError):
        Some(1).missing  # type: ignore # noqa: B018


def test_success_immutable_copy():
    """Ensures that Success returns it self when passed to copy function."""
    some = Some(1)  # noqa: WPS110
    assert some is copy(some)


def test_success_immutable_deepcopy():
    """Ensures that Success returns it self when passed to deepcopy function."""
    some = Some(1)  # noqa: WPS110
    assert some is deepcopy(some)


def test_failure_immutable_copy():
    """Ensures that Failure returns it self when passed to copy function."""
    nothing = _Nothing()
    assert nothing is copy(nothing)


def test_failure_immutable_deepcopy():
    """Ensures that Failure returns it self when passed to deepcopy function."""
    nothing = _Nothing()
    assert nothing is deepcopy(nothing)