Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavomm19 committed Dec 1, 2023
1 parent 65f4746 commit 22f35df
Showing 1 changed file with 191 additions and 2 deletions.
193 changes: 191 additions & 2 deletions breathecode/activity/tests/urls/v2/tests_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,49 @@ def bigquery_client_mock_group(self, n=1, user_id=1, kind=None, fields=[], by=''
return (client_mock, result_mock, project_id, dataset, grouped_data)


def bigquery_client_mock_filters(self, n=1, user_id=1, kind=None, filters={}):
rows_to_insert = [{
'id': uuid4().hex,
'user_id': user_id,
'kind': kind if kind else self.bc.fake.slug(),
'related': {
'type': f'{self.bc.fake.slug()}.{self.bc.fake.slug()}',
'id': random.randint(1, 100),
'slug': self.bc.fake.slug(),
},
'meta': {
self.bc.fake.slug().replace('-', '_'): self.bc.fake.slug(),
self.bc.fake.slug().replace('-', '_'): self.bc.fake.slug(),
self.bc.fake.slug().replace('-', '_'): self.bc.fake.slug(),
},
'timestamp': timezone.now().isoformat(),
} for _ in range(n)]

for key in filters:
literal = filters[key]
if key.endswith('__lte'):
rows_to_insert = list(filter(lambda x: x[key[:-5]] <= literal, rows_to_insert))
elif key.endswith('__lt'):
rows_to_insert = list(filter(lambda x: x[key[:-4]] < literal, rows_to_insert))
elif key.endswith('__gte'):
rows_to_insert = list(filter(lambda x: x[key[:-5]] >= literal, rows_to_insert))
elif key.endswith('__gt'):
rows_to_insert = list(filter(lambda x: x[key[:-4]] > literal, rows_to_insert))
else:
rows_to_insert = list(filter(lambda x: x[key] == literal, rows_to_insert))

result_mock = MagicMock()
result_mock.result.return_value = [AttrDict(**kwargs) for kwargs in rows_to_insert]

client_mock = MagicMock()
client_mock.query.return_value = result_mock

project_id = 'test'
dataset = '4geeks'

return (client_mock, result_mock, project_id, dataset, rows_to_insert)


class MediaTestSuite(MediaTestCase):

def test_no_auth(self):
Expand Down Expand Up @@ -118,7 +161,7 @@ def test_no_query(self):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

@patch('django.utils.timezone.now', MagicMock(return_value=UTC_NOW))
def test_get_group_fields(self):
def test_get_all_fields(self):
json_query = '{}'
expected_query = 'SELECT * FROM `test.4geeks.activity` '
url = reverse_lazy('v2:activity:report') + f'?query={json_query}'
Expand Down Expand Up @@ -147,7 +190,37 @@ def test_get_group_fields(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)

@patch('django.utils.timezone.now', MagicMock(return_value=UTC_NOW))
def test_get_used_fields(self):
def test_get_all_fields_limit(self):
json_query = '{ "limit": 5 }'
expected_query = 'SELECT * FROM `test.4geeks.activity` LIMIT 5'
url = reverse_lazy('v2:activity:report') + f'?query={json_query}'
model = self.bc.database.create(user=1,
academy=1,
profile_academy=1,
capability='read_activity',
role=1)

self.bc.request.authenticate(model.user)
self.bc.request.set_headers(academy=1)

val = bigquery_client_mock(self)
(client_mock, result_mock, project_id, dataset, expected) = val
expected = expected[0:5]

with patch('breathecode.services.google_cloud.big_query.BigQuery.client') as mock:
mock.return_value = (client_mock, project_id, dataset)
response = self.client.get(url)
json = response.json()

self.bc.check.calls(BigQuery.client.call_args_list, [call()])
assert client_mock.query.call_args[0][0] == expected_query
self.bc.check.calls(result_mock.result.call_args_list, [call()])

self.assertEqual(json, expected)
self.assertEqual(response.status_code, status.HTTP_200_OK)

@patch('django.utils.timezone.now', MagicMock(return_value=UTC_NOW))
def test_get_group(self):
json_query = '{ "by": ["kind"], "fields": ["kind"] }'
expected_query = 'SELECT kind FROM `test.4geeks.activity` GROUP BY kind'
url = reverse_lazy('v2:activity:report') + f'?query={json_query}'
Expand All @@ -174,3 +247,119 @@ def test_get_used_fields(self):

self.assertEqual(json, expected)
self.assertEqual(response.status_code, status.HTTP_200_OK)

@patch('django.utils.timezone.now', MagicMock(return_value=UTC_NOW))
def test_get_filters_lte(self):
json_query = '{ "filter": { "user_id__lte": 5 } }'
expected_query = 'SELECT * FROM `test.4geeks.activity` WHERE user_id <= @x__user_id'
url = reverse_lazy('v2:activity:report') + f'?query={json_query}'
model = self.bc.database.create(user=1,
academy=1,
profile_academy=1,
capability='read_activity',
role=1)

self.bc.request.authenticate(model.user)
self.bc.request.set_headers(academy=1)

val = bigquery_client_mock_filters(self, filters={'user_id__lte': 5})
(client_mock, result_mock, project_id, dataset, expected) = val

with patch('breathecode.services.google_cloud.big_query.BigQuery.client') as mock:
mock.return_value = (client_mock, project_id, dataset)
response = self.client.get(url)
json = response.json()

self.bc.check.calls(BigQuery.client.call_args_list, [call()])
assert client_mock.query.call_args[0][0] == expected_query
self.bc.check.calls(result_mock.result.call_args_list, [call()])

self.assertEqual(json, expected)
self.assertEqual(response.status_code, status.HTTP_200_OK)

@patch('django.utils.timezone.now', MagicMock(return_value=UTC_NOW))
def test_get_filters_lt(self):
json_query = '{ "filter": { "user_id__lt": 5 } }'
expected_query = 'SELECT * FROM `test.4geeks.activity` WHERE user_id < @x__user_id'
url = reverse_lazy('v2:activity:report') + f'?query={json_query}'
model = self.bc.database.create(user=1,
academy=1,
profile_academy=1,
capability='read_activity',
role=1)

self.bc.request.authenticate(model.user)
self.bc.request.set_headers(academy=1)

val = bigquery_client_mock_filters(self, filters={'user_id__lt': 5})
(client_mock, result_mock, project_id, dataset, expected) = val

with patch('breathecode.services.google_cloud.big_query.BigQuery.client') as mock:
mock.return_value = (client_mock, project_id, dataset)
response = self.client.get(url)
json = response.json()

self.bc.check.calls(BigQuery.client.call_args_list, [call()])
assert client_mock.query.call_args[0][0] == expected_query
self.bc.check.calls(result_mock.result.call_args_list, [call()])

self.assertEqual(json, expected)
self.assertEqual(response.status_code, status.HTTP_200_OK)

@patch('django.utils.timezone.now', MagicMock(return_value=UTC_NOW))
def test_get_filters_gte(self):
json_query = '{ "filter": { "user_id__gte": 5 } }'
expected_query = 'SELECT * FROM `test.4geeks.activity` WHERE user_id >= @x__user_id'
url = reverse_lazy('v2:activity:report') + f'?query={json_query}'
model = self.bc.database.create(user=1,
academy=1,
profile_academy=1,
capability='read_activity',
role=1)

self.bc.request.authenticate(model.user)
self.bc.request.set_headers(academy=1)

val = bigquery_client_mock_filters(self, filters={'user_id__gte': 5})
(client_mock, result_mock, project_id, dataset, expected) = val

with patch('breathecode.services.google_cloud.big_query.BigQuery.client') as mock:
mock.return_value = (client_mock, project_id, dataset)
response = self.client.get(url)
json = response.json()

self.bc.check.calls(BigQuery.client.call_args_list, [call()])
assert client_mock.query.call_args[0][0] == expected_query
self.bc.check.calls(result_mock.result.call_args_list, [call()])

self.assertEqual(json, expected)
self.assertEqual(response.status_code, status.HTTP_200_OK)

@patch('django.utils.timezone.now', MagicMock(return_value=UTC_NOW))
def test_get_filters_gt(self):
json_query = '{ "filter": { "user_id__gt": 5 } }'
expected_query = 'SELECT * FROM `test.4geeks.activity` WHERE user_id > @x__user_id'
url = reverse_lazy('v2:activity:report') + f'?query={json_query}'
model = self.bc.database.create(user=1,
academy=1,
profile_academy=1,
capability='read_activity',
role=1)

self.bc.request.authenticate(model.user)
self.bc.request.set_headers(academy=1)

val = bigquery_client_mock_filters(self, filters={'user_id__gt': 5})
(client_mock, result_mock, project_id, dataset, expected) = val

with patch('breathecode.services.google_cloud.big_query.BigQuery.client') as mock:
mock.return_value = (client_mock, project_id, dataset)
response = self.client.get(url)
json = response.json()

self.bc.check.calls(BigQuery.client.call_args_list, [call()])
assert client_mock.query.call_args[0][0] == expected_query
self.bc.check.calls(result_mock.result.call_args_list, [call()])

self.assertEqual(json, expected)
self.assertEqual(response.status_code, status.HTTP_200_OK)

0 comments on commit 22f35df

Please sign in to comment.