-
Notifications
You must be signed in to change notification settings - Fork 63
/
test_variable_group_repo.py
167 lines (133 loc) · 5.91 KB
/
test_variable_group_repo.py
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
161
162
163
164
165
166
167
import pytest
from unittest.mock import patch
from cartoframes.exceptions import DiscoveryException
from cartoframes.data.observatory.catalog.entity import CatalogList
from cartoframes.data.observatory.catalog.variable_group import VariableGroup
from cartoframes.data.observatory.catalog.repository.variable_group_repo import VariableGroupRepository
from cartoframes.data.observatory.catalog.repository.repo_client import RepoClient
from ..examples import test_variable_group1, test_variables_groups, db_variable_group1, db_variable_group2
class TestVariableGroupRepo(object):
@patch.object(RepoClient, 'get_variables_groups')
def test_get_all(self, mocked_repo):
# Given
mocked_repo.return_value = [db_variable_group1, db_variable_group2]
repo = VariableGroupRepository()
# When
variables_groups = repo.get_all()
# Then
mocked_repo.assert_called_once_with(None)
assert isinstance(variables_groups, CatalogList)
assert variables_groups == test_variables_groups
@patch.object(RepoClient, 'get_variables_groups')
def test_get_all_when_empty(self, mocked_repo):
# Given
mocked_repo.return_value = []
# When
repo = VariableGroupRepository()
variables_groups = repo.get_all()
# Then
mocked_repo.assert_called_once_with(None)
assert variables_groups is None
@patch.object(RepoClient, 'get_variables_groups')
def test_get_all_only_uses_allowed_filters(self, mocked_repo):
# Given
mocked_repo.return_value = [db_variable_group1, db_variable_group2]
repo = VariableGroupRepository()
filters = {
'country_id': 'usa',
'dataset_id': 'carto-do.project.census2011',
'category_id': 'demographics',
'variable_id': 'population',
'geography_id': 'census-geo',
'variable_group_id': 'var-group',
'provider_id': 'open_data',
'fake_field_id': 'fake_value'
}
# When
variables_groups = repo.get_all(filters)
# Then
mocked_repo.assert_called_once_with({
'dataset_id': 'carto-do.project.census2011'
})
assert variables_groups == test_variables_groups
@patch.object(RepoClient, 'get_variables_groups')
def test_get_by_id(self, mocked_repo):
# Given
mocked_repo.return_value = [db_variable_group1, db_variable_group2]
requested_id = db_variable_group1['id']
# When
repo = VariableGroupRepository()
variable_group = repo.get_by_id(requested_id)
# Then
mocked_repo.assert_called_once_with({'id': requested_id})
assert isinstance(variable_group, VariableGroup)
assert variable_group == test_variable_group1
@patch.object(RepoClient, 'get_variables_groups')
def test_get_by_id_unknown_fails(self, mocked_repo):
# Given
mocked_repo.return_value = []
requested_id = 'unknown_id'
repo = VariableGroupRepository()
# Then
with pytest.raises(DiscoveryException):
repo.get_by_id(requested_id)
@patch.object(RepoClient, 'get_variables_groups')
def test_get_by_slug(self, mocked_repo):
# Given
mocked_repo.return_value = [db_variable_group1]
requested_slug = db_variable_group1['slug']
repo = VariableGroupRepository()
# When
variable = repo.get_by_id(requested_slug)
# Then
mocked_repo.assert_called_once_with({'slug': requested_slug})
assert variable == test_variable_group1
@patch.object(RepoClient, 'get_variables_groups')
def test_get_by_id_list(self, mocked_repo):
# Given
mocked_repo.return_value = [db_variable_group1, db_variable_group2]
repo = VariableGroupRepository()
# When
variable_groups = repo.get_by_id_list([db_variable_group1['id'], db_variable_group2['id']])
# Then
mocked_repo.assert_called_once_with({'id': [db_variable_group1['id'], db_variable_group2['id']]})
assert isinstance(variable_groups, CatalogList)
assert variable_groups == test_variables_groups
@patch.object(RepoClient, 'get_variables_groups')
def test_get_by_slug_list(self, mocked_repo):
# Given
mocked_repo.return_value = [db_variable_group1, db_variable_group2]
repo = VariableGroupRepository()
# When
variable_groups = repo.get_by_id_list([db_variable_group1['slug'], db_variable_group2['slug']])
# Then
mocked_repo.assert_called_once_with({'slug': [db_variable_group1['slug'], db_variable_group2['slug']]})
assert isinstance(variable_groups, CatalogList)
assert variable_groups == test_variables_groups
@patch.object(RepoClient, 'get_variables_groups')
def test_get_by_slug_and_id_list(self, mocked_repo):
# Given
mocked_repo.return_value = [db_variable_group1, db_variable_group2]
repo = VariableGroupRepository()
# When
variable_groups = repo.get_by_id_list([db_variable_group1['id'], db_variable_group2['slug']])
# Then
mocked_repo.assert_called_once_with({'id': [db_variable_group1['id']], 'slug': [db_variable_group2['slug']]})
assert isinstance(variable_groups, CatalogList)
assert variable_groups == test_variables_groups
@patch.object(RepoClient, 'get_variables_groups')
def test_missing_fields_are_mapped_as_None(self, mocked_repo):
# Given
mocked_repo.return_value = [{'id': 'variable_group1'}]
repo = VariableGroupRepository()
expected_variables_groups = CatalogList([VariableGroup({
'id': 'variable_group1',
'slug': None,
'name': None,
'dataset_id': None,
'starred': None
})])
# When
variables_groups = repo.get_all()
# Then
assert variables_groups == expected_variables_groups