test_repository_aggregate()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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