/
test_variable_group_repo.py
104 lines (82 loc) · 3.42 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
import unittest
from cartoframes.exceptions import DiscoveryException
from cartoframes.data.observatory.entity import CatalogList
from cartoframes.data.observatory.variable_group import VariableGroup
from cartoframes.data.observatory.repository.variable_group_repo import VariableGroupRepository
from cartoframes.data.observatory.repository.repo_client import RepoClient
from ..examples import test_variable_group1, test_variables_groups, db_variable_group1, db_variable_group2
try:
from unittest.mock import Mock, patch
except ImportError:
from mock import Mock, patch
class TestVariableGroupRepo(unittest.TestCase):
@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_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 self.assertRaises(DiscoveryException):
repo.get_by_id(requested_id)
@patch.object(RepoClient, 'get_variables_groups')
def test_get_by_dataset(self, mocked_repo):
# Given
mocked_repo.return_value = [db_variable_group1, db_variable_group2]
dataset_id = 'dataset1'
repo = VariableGroupRepository()
# When
variables_groups = repo.get_by_dataset(dataset_id)
# Then
mocked_repo.assert_called_once_with({'dataset_id': dataset_id})
assert isinstance(variables_groups, CatalogList)
assert variables_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