File: test_asserts.py

package info (click to toggle)
python-sqlalchemy-utils 0.41.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,252 kB
  • sloc: python: 13,566; makefile: 141
file content (160 lines) | stat: -rw-r--r-- 4,917 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import pytest
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import ARRAY

from sqlalchemy_utils import (
    assert_max_length,
    assert_max_value,
    assert_min_value,
    assert_non_nullable,
    assert_nullable
)


@pytest.fixture()
def User(Base):
    class User(Base):
        __tablename__ = 'user'
        id = sa.Column('_id', sa.Integer, primary_key=True)
        name = sa.Column('_name', sa.String(20))
        age = sa.Column('_age', sa.Integer, nullable=False)
        email = sa.Column(
            '_email', sa.String(200), nullable=False, unique=True
        )
        fav_numbers = sa.Column('_fav_numbers', ARRAY(sa.Integer))

        __table_args__ = (
            sa.CheckConstraint(sa.and_(age >= 0, age <= 150)),
            sa.CheckConstraint(
                sa.and_(
                    sa.func.array_length(fav_numbers, 1) <= 8
                )
            )
        )
    return User


@pytest.fixture()
def user(User, session):
    user = User(
        name='Someone',
        email='someone@example.com',
        age=15,
        fav_numbers=[1, 2, 3]
    )
    session.add(user)
    session.commit()
    return user


@pytest.mark.usefixtures('postgresql_dsn')
class TestAssertMaxLengthWithArray:

    def test_with_max_length(self, user):
        assert_max_length(user, 'fav_numbers', 8)
        assert_max_length(user, 'fav_numbers', 8)

    def test_smaller_than_max_length(self, user):
        with pytest.raises(AssertionError):
            assert_max_length(user, 'fav_numbers', 7)
        with pytest.raises(AssertionError):
            assert_max_length(user, 'fav_numbers', 7)

    def test_bigger_than_max_length(self, user):
        with pytest.raises(AssertionError):
            assert_max_length(user, 'fav_numbers', 9)
        with pytest.raises(AssertionError):
            assert_max_length(user, 'fav_numbers', 9)


@pytest.mark.usefixtures('postgresql_dsn')
class TestAssertNonNullable:

    def test_non_nullable_column(self, user):
        # Test everything twice so that session gets rolled back properly
        assert_non_nullable(user, 'age')
        assert_non_nullable(user, 'age')

    def test_nullable_column(self, user):
        with pytest.raises(AssertionError):
            assert_non_nullable(user, 'name')
        with pytest.raises(AssertionError):
            assert_non_nullable(user, 'name')


@pytest.mark.usefixtures('postgresql_dsn')
class TestAssertNullable:

    def test_nullable_column(self, user):
        assert_nullable(user, 'name')
        assert_nullable(user, 'name')

    def test_non_nullable_column(self, user):
        with pytest.raises(AssertionError):
            assert_nullable(user, 'age')
        with pytest.raises(AssertionError):
            assert_nullable(user, 'age')


@pytest.mark.usefixtures('postgresql_dsn')
class TestAssertMaxLength:

    def test_with_max_length(self, user):
        assert_max_length(user, 'name', 20)
        assert_max_length(user, 'name', 20)

    def test_with_non_nullable_column(self, user):
        assert_max_length(user, 'email', 200)
        assert_max_length(user, 'email', 200)

    def test_smaller_than_max_length(self, user):
        with pytest.raises(AssertionError):
            assert_max_length(user, 'name', 19)
        with pytest.raises(AssertionError):
            assert_max_length(user, 'name', 19)

    def test_bigger_than_max_length(self, user):
        with pytest.raises(AssertionError):
            assert_max_length(user, 'name', 21)
        with pytest.raises(AssertionError):
            assert_max_length(user, 'name', 21)


@pytest.mark.usefixtures('postgresql_dsn')
class TestAssertMinValue:

    def test_with_min_value(self, user):
        assert_min_value(user, 'age', 0)
        assert_min_value(user, 'age', 0)

    def test_smaller_than_min_value(self, user):
        with pytest.raises(AssertionError):
            assert_min_value(user, 'age', -1)
        with pytest.raises(AssertionError):
            assert_min_value(user, 'age', -1)

    def test_bigger_than_min_value(self, user):
        with pytest.raises(AssertionError):
            assert_min_value(user, 'age', 1)
        with pytest.raises(AssertionError):
            assert_min_value(user, 'age', 1)


@pytest.mark.usefixtures('postgresql_dsn')
class TestAssertMaxValue:

    def test_with_min_value(self, user):
        assert_max_value(user, 'age', 150)
        assert_max_value(user, 'age', 150)

    def test_smaller_than_max_value(self, user):
        with pytest.raises(AssertionError):
            assert_max_value(user, 'age', 149)
        with pytest.raises(AssertionError):
            assert_max_value(user, 'age', 149)

    def test_bigger_than_max_value(self, user):
        with pytest.raises(AssertionError):
            assert_max_value(user, 'age', 151)
        with pytest.raises(AssertionError):
            assert_max_value(user, 'age', 151)