Passed
Pull Request — main (#32)
by Rami
01:09
created

tests.test_base_repository   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 25
eloc 106
dl 0
loc 174
rs 10
c 0
b 0
f 0

19 Functions

Rating   Name   Duplication   Size   Complexity  
A test_repository_get_does_not_exist() 0 3 2
A test_index_creation() 0 8 1
A test_repository_get() 0 5 1
A test_repository_get_with_duplicate() 0 10 2
A test_repository_aggregate() 0 13 1
A test_safe_repository_aggregate_error() 0 9 1
A test_repository_definition_without_model() 0 6 2
A test_repository_aggregate_error() 0 6 2
A test_safe_repository_get() 0 2 1
A test_safe_repository_find_invalid_filter() 0 6 1
A test_repository_save() 0 8 1
A example_user() 0 5 1
A repository() 0 3 1
A test_repository_find_invalid_filter() 0 6 2
A mongodb() 0 3 1
A test_repository_definition_without_collection() 0 6 2
A test_repository_find_nonexistent() 0 5 1
A test_safe_repository_get_with_duplicate() 0 9 1
A test_repository_find() 0 9 1
1
from typing import Generator
2
3
import pytest
4
from mongomantic import BaseRepository
5
from mongomantic.core.database import connect
6
from mongomantic.core.errors import DoesNotExistError, InvalidQueryError, MultipleObjectsReturnedError
7
from mongomantic.core.index import Index
8
9
from .user import User
10
from .user_repository import SafeUserRepository, UserRepository
11
12
13
@pytest.fixture(scope="function")
14
def mongodb():
15
    connect("localhost:27017", "test", mock=True)
16
17
18
@pytest.fixture(scope="function", params=[UserRepository, SafeUserRepository])
19
def repository(request):
20
    return request.param
21
22
23
def test_repository_definition_without_collection():
24
    with pytest.raises(NotImplementedError):
25
26
        class TestRepo(BaseRepository):
27
            class Meta:
28
                model = User
29
30
31
def test_repository_definition_without_model():
32
    with pytest.raises(NotImplementedError):
33
34
        class TestRepo(BaseRepository):
35
            class Meta:
36
                collection = "user"
37
38
39
def test_repository_save(mongodb, repository):
40
    user = User(first_name="John", last_name="Smith", email="[email protected]", age=29)
41
42
    user = repository.save(user)
43
44
    assert user
45
    assert user.id
46
    assert user.first_name == "John"
47
48
49
@pytest.fixture()
50
def example_user(mongodb, repository) -> User:
51
    user = User(first_name="John", last_name="Smith", email="[email protected]", age=29)
52
53
    return repository.save(user)
54
55
56
def test_repository_get(example_user, repository):
57
58
    user = repository.get(age=example_user.age)
59
    assert user
60
    assert user.first_name == example_user.first_name
61
62
63
def test_repository_get_does_not_exist(mongodb):
64
    with pytest.raises(DoesNotExistError):
65
        UserRepository.get(age=1)
66
67
68
def test_safe_repository_get(mongodb):
69
    assert SafeUserRepository.get(age=1) is None
70
71
72
def test_repository_get_with_duplicate(mongodb):
73
74
    user = User(first_name="John", last_name="Smith", email="[email protected]", age=29)
75
    UserRepository.save(user)
76
77
    duplicate = User(first_name="John", last_name="Smith", email="[email protected]", age=29)
78
    UserRepository.save(duplicate)
79
80
    with pytest.raises(MultipleObjectsReturnedError):
81
        UserRepository.get(age=29)
82
83
84
def test_safe_repository_get_with_duplicate(mongodb):
85
86
    user = User(first_name="John", last_name="Smith", email="[email protected]", age=29)
87
    SafeUserRepository.save(user)
88
89
    duplicate = User(first_name="John", last_name="Smith", email="[email protected]", age=29)
90
    SafeUserRepository.save(duplicate)
91
92
    assert SafeUserRepository.get(age=29) is None
93
94
95
def test_repository_find(example_user, repository):
96
    users = repository.find(first_name="John")
97
98
    assert isinstance(users, Generator)
99
    users_list = list(users)
100
101
    assert len(users_list) == 1
102
    assert isinstance(users_list[0], User)
103
    assert users_list[0].first_name == example_user.first_name
104
105
106
def test_repository_find_nonexistent(mongodb, repository):
107
    users = repository.find(first_name="X")
108
109
    assert isinstance(users, Generator)
110
    assert len(list(users)) == 0
111
112
113
def test_repository_find_invalid_filter(mongodb):
114
    users = UserRepository.find(first_name={"$tf": "test"})
115
    assert isinstance(users, Generator)
116
117
    with pytest.raises(InvalidQueryError):
118
        assert len(list(users)) == 0
119
120
121
def test_safe_repository_find_invalid_filter(mongodb):
122
    users = SafeUserRepository.find(first_name={"$tf": "test"})
123
    assert isinstance(users, Generator)
124
125
    # Unaffected by bad filter, but logs error
126
    assert list(users) == []
127
128
129
def test_repository_aggregate(example_user):
130
    johns = list(
131
        UserRepository.aggregate(
132
            [
133
                {"$match": {"first_name": "John"}},
134
            ]
135
        )
136
    )
137
138
    assert len(johns) == 1
139
    assert isinstance(johns[0], User)
140
    assert johns[0].id
141
    assert johns[0].first_name == example_user.first_name
142
143
144
def test_repository_aggregate_error(example_user):
145
    with pytest.raises(InvalidQueryError):
146
        next(
147
            UserRepository.aggregate(
148
                [
149
                    {"$asd": {"first_name": "John"}},
150
                ]
151
            )
152
        )
153
154
155
def test_safe_repository_aggregate_error(example_user):
156
    user = SafeUserRepository.aggregate(
157
        [
158
            {"$asd": {"first_name": "John"}},
159
        ]
160
    )
161
162
    assert isinstance(user, Generator)
163
    assert list(user) == []
164
165
166
def test_index_creation():
167
    class TestRepo(BaseRepository):
168
        class Meta:
169
            model = User
170
            collection = "user"
171
            indexes = [
172
                Index(name="email_index", unique=True, fields=["email"]),
173
                Index(name="fullname_index", unique=True, fields=["+first_name", "-last_name"]),
174
            ]
175