-
Notifications
You must be signed in to change notification settings - Fork 61
Use Mixer for reimbursement view tests #171
Changes from all commits
5454222
7b8a5fa
4a86986
8028754
11560ff
5268291
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
def get_sample_reimbursement_api_response(obj): | ||
return dict( | ||
applicant_id=obj.applicant_id, | ||
batch_number=obj.batch_number, | ||
cnpj_cpf=obj.cnpj_cpf, | ||
congressperson_document=obj.congressperson_document, | ||
congressperson_id=obj.congressperson_id, | ||
congressperson_name=obj.congressperson_name, | ||
document_id=obj.document_id, | ||
document_number=obj.document_number, | ||
document_type=obj.document_type, | ||
document_value=float(obj.document_value), | ||
installment=obj.installment, | ||
issue_date=obj.issue_date.strftime('%Y-%m-%d'), | ||
leg_of_the_trip=obj.leg_of_the_trip, | ||
month=obj.month, | ||
party=obj.party, | ||
passenger=obj.passenger, | ||
all_reimbursement_numbers=obj.all_reimbursement_numbers, | ||
all_reimbursement_values=obj.all_reimbursement_values, | ||
all_net_values=obj.all_net_values, | ||
remark_value=obj.remark_value, | ||
state=obj.state, | ||
subquota_description=obj.subquota_description, | ||
subquota_group_description=obj.subquota_group_description, | ||
subquota_group_id=obj.subquota_group_id, | ||
subquota_id=obj.subquota_id, | ||
supplier=obj.supplier, | ||
term=obj.term, | ||
term_id=obj.term_id, | ||
total_net_value=float(obj.total_net_value), | ||
total_reimbursement_value=obj.total_reimbursement_value, | ||
year=obj.year, | ||
probability=obj.probability, | ||
suspicions=obj.suspicions, | ||
last_update=obj.last_update.strftime('%Y-%m-%dT%H:%M:%SZ'), | ||
available_in_latest_dataset=obj.available_in_latest_dataset, | ||
receipt=dict(fetched=obj.receipt_fetched, url=obj.receipt_url) | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,76 @@ | ||
from datetime import date | ||
from json import loads | ||
from unittest.mock import patch | ||
from urllib.parse import urlencode | ||
|
||
from django.core.cache import cache | ||
from django.shortcuts import resolve_url | ||
from django.test import TestCase | ||
from freezegun import freeze_time | ||
from mixer.backend.django import mixer | ||
|
||
from jarbas.core.models import Reimbursement | ||
from jarbas.core.tests import sample_reimbursement_data, suspicions | ||
from jarbas.api.tests import get_sample_reimbursement_api_response | ||
|
||
|
||
class TestListApi(TestCase): | ||
def get_reimbursement(**kwargs): | ||
quantity = kwargs.pop('quantity', 1) | ||
kwargs['net_values'] = '1.99,2.99' | ||
kwargs['reimbursement_values'] = '200.00,500.00' | ||
kwargs['reimbursement_numbers'] = '2,3' | ||
if quantity == 1: | ||
return mixer.blend(Reimbursement, **kwargs) | ||
return mixer.cycle(quantity).blend(Reimbursement, **kwargs) | ||
|
||
def setUp(self): | ||
|
||
data = [ | ||
sample_reimbursement_data.copy(), | ||
sample_reimbursement_data.copy(), | ||
sample_reimbursement_data.copy(), | ||
sample_reimbursement_data.copy(), | ||
sample_reimbursement_data.copy() | ||
] | ||
|
||
data[1]['cnpj_cpf'] = '22222222222' | ||
data[1]['document_id'] = 42 * 2 | ||
data[1]['issue_date'] = date(1969, 12, 31) | ||
data[1]['probability'] = None | ||
data[1]['subquota_id'] = 22 | ||
|
||
data[2]['applicant_id'] = 13 * 3 | ||
data[2]['cnpj_cpf'] = '22222222222' | ||
data[2]['document_id'] = 42 * 3 | ||
data[2]['probability'] = 0.1 | ||
data[2]['subquota_id'] = 22 | ||
|
||
data[3]['applicant_id'] = 13 * 4 | ||
data[3]['cnpj_cpf'] = '22222222222' | ||
data[3]['document_id'] = 42 * 4 | ||
data[3]['probability'] = 0.9 | ||
data[3]['subquota_id'] = 22 | ||
data[3]['year'] = 1983 | ||
data[3]['issue_date'] = '1970-02-01' | ||
|
||
data[4]['applicant_id'] = 13 * 5 | ||
data[4]['cnpj_cpf'] = '22222222222' | ||
data[4]['document_id'] = 42 * 5 | ||
data[4]['subquota_id'] = 22 | ||
data[4]['year'] = 1983 | ||
data[4]['issue_date'] = '1960-02-01' | ||
del data[4]['probability'] | ||
del data[4]['suspicions'] | ||
|
||
for fixture in data: | ||
Reimbursement.objects.create(**fixture) | ||
class TestListApi(TestCase): | ||
|
||
def setUp(self): | ||
get_reimbursement(quantity=3) | ||
self.url = resolve_url('api:reimbursement-list') | ||
|
||
def test_status(self): | ||
resp = self.client.get(self.url) | ||
self.assertEqual(200, resp.status_code) | ||
|
||
def test_content_general(self): | ||
self.assertEqual(5, Reimbursement.objects.count()) | ||
self.assertEqual(5, self._count_results(self.url)) | ||
self.assertEqual(3, Reimbursement.objects.count()) | ||
self.assertEqual(3, self._count_results(self.url)) | ||
|
||
def test_ordering(self): | ||
resp = self.client.get(self.url) | ||
content = loads(resp.content.decode('utf-8')) | ||
self.assertEqual(5, len(content['results'])) | ||
self.assertEqual('1969-12-31', content['results'][3]['issue_date']) | ||
|
||
def test_content_with_filters(self): | ||
url = self.url + ( | ||
'?cnpj_cpf=22222222222' | ||
'&subquota_id=22' | ||
'&order_by=probability' | ||
'&suspicions=1' | ||
first = content['results'][0] | ||
last = content['results'][-1] | ||
self.assertEqual(3, len(content['results'])) | ||
self.assertTrue(first['issue_date'] > last['issue_date']) | ||
|
||
def test_content_with_cnpj_cpf_filter(self): | ||
search_data = ( | ||
('cnpj_cpf', '12345678901'), | ||
('subquota_id', '22'), | ||
('order_by', 'probability'), | ||
('suspicious', '1'), | ||
) | ||
url = '{}?{}'.format(self.url, urlencode(search_data)) | ||
target_result = get_reimbursement(cnpj_cpf='12345678901', subquota_id=22, suspicious=1) | ||
resp = self.client.get(url) | ||
content = loads(resp.content.decode('utf-8')) | ||
self.assertEqual(3, len(content['results'])) | ||
self.assertEqual(0.9, content['results'][0]['probability']) | ||
self.assertEqual(0.1, content['results'][1]['probability']) | ||
self.assertEqual(None, content['results'][2]['probability']) | ||
self.assertEqual(1, len(content['results'])) | ||
self.assertEqual(target_result.cnpj_cpf, content['results'][0]['cnpj_cpf']) | ||
|
||
def test_content_with_date_filters(self): | ||
url = self.url + ( | ||
'?issue_date_start=1970-01-01' | ||
'&issue_date_end=1970-02-01' | ||
get_reimbursement(issue_date='1970-01-01') | ||
get_reimbursement(issue_date='1970-01-01') | ||
search_data = ( | ||
('issue_date_start', '1970-01-01'), | ||
('issue_date_end', '1970-02-02'), | ||
) | ||
url = '{}?{}'.format(self.url, urlencode(search_data)) | ||
resp = self.client.get(url) | ||
content = loads(resp.content.decode('utf-8')) | ||
self.assertEqual(2, len(content['results'])) | ||
self.assertEqual(0.5, content['results'][0]['probability']) | ||
self.assertEqual(0.1, content['results'][1]['probability']) | ||
|
||
def test_more_than_one_document_query(self): | ||
extra = sample_reimbursement_data.copy() | ||
extra['document_id'] = 0 | ||
Reimbursement.objects.create(**extra) | ||
get_reimbursement(quantity=4, document_id=(id for id in (42, 84, 126, 168))) | ||
url = self.url + '?document_id=42,84+126,+168' | ||
resp = self.client.get(url) | ||
content = loads(resp.content.decode('utf-8')) | ||
|
@@ -115,8 +86,12 @@ def _count_results(self, url): | |
class TestRetrieveApi(TestCase): | ||
|
||
def setUp(self): | ||
Reimbursement.objects.create(**sample_reimbursement_data) | ||
url = resolve_url('api:reimbursement-detail', document_id=42) | ||
self.reimbursement = get_reimbursement() | ||
self.sample_response = get_sample_reimbursement_api_response( | ||
self.reimbursement | ||
) | ||
url = resolve_url('api:reimbursement-detail', | ||
document_id=self.reimbursement.document_id) | ||
self.resp = self.client.get(url) | ||
self.maxDiff = 2 ** 11 | ||
|
||
|
@@ -125,78 +100,47 @@ def test_status(self): | |
|
||
def test_contents(self): | ||
contents = loads(self.resp.content.decode('utf-8')) | ||
expected = dict( | ||
applicant_id=13, | ||
batch_number=9, | ||
cnpj_cpf='11111111111111', | ||
congressperson_document=2, | ||
congressperson_id=1, | ||
congressperson_name='Roger That', | ||
document_id=42, | ||
document_number='6', | ||
document_type=7, | ||
document_value=8.90, | ||
installment=7, | ||
issue_date='1970-01-01', | ||
leg_of_the_trip='8', | ||
month=1, | ||
party='Partido', | ||
passenger='John Doe', | ||
all_reimbursement_numbers=[10, 11], | ||
all_reimbursement_values=[12.13, 14.15], | ||
all_net_values=[1.99, 2.99], | ||
remark_value=1.23, | ||
state='UF', | ||
subquota_description='Subquota description', | ||
subquota_group_description='Subquota group desc', | ||
subquota_group_id=5, | ||
subquota_id=4, | ||
supplier='Acme', | ||
term=1970, | ||
term_id=3, | ||
total_net_value=4.56, | ||
total_reimbursement_value=None, | ||
year=1970, | ||
probability=0.5, | ||
suspicions=suspicions, | ||
last_update='1970-01-01T00:00:00Z', | ||
available_in_latest_dataset=True, | ||
receipt=dict(fetched=False, url=None) | ||
) | ||
self.assertEqual(expected, contents) | ||
self.assertEqual(self.sample_response, contents) | ||
|
||
|
||
class TestReceiptApi(TestCase): | ||
|
||
def setUp(self): | ||
self.obj = Reimbursement.objects.create(**sample_reimbursement_data) | ||
self.url = resolve_url('api:reimbursement-receipt', document_id=42) | ||
self.expected_receipt_url = 'http://www.camara.gov.br/cota-parlamentar/documentos/publ/13/1970/42.pdf' | ||
self.reimbursement = get_reimbursement( | ||
year=2017, | ||
applicant_id=1, | ||
document_id=20, | ||
receipt_url='http://www.camara.gov.br/cota-parlamentar/documentos/publ/1/2017/20.pdf' | ||
) | ||
self.reimbursement_no_receipt = get_reimbursement(receipt_url=None) | ||
self.url = resolve_url( | ||
'api:reimbursement-receipt', document_id=self.reimbursement.document_id) | ||
self.url_no_receipt = resolve_url( | ||
'api:reimbursement-receipt', document_id=self.reimbursement_no_receipt.document_id) | ||
|
||
@patch('jarbas.core.models.head') | ||
def test_fetch_existing_receipt(self, mocked_head): | ||
mocked_head.return_value.status_code = 200 | ||
resp = self.client.get(self.url) | ||
expected = dict(url=self.expected_receipt_url) | ||
expected = dict(url=self.reimbursement.receipt_url) | ||
content = loads(resp.content.decode('utf-8')) | ||
self.assertEqual(expected, content) | ||
|
||
@patch('jarbas.core.models.head') | ||
def test_fetch_non_existing_receipt(self, mocked_head): | ||
mocked_head.return_value.status_code = 404 | ||
cache.clear() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any specific reason to remove that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had understood it was only preparing test to fetch an exiting receipt from a reimbursement without a receipt. So I did the proper preparation instead of clearing cache, what seemed to be an workaround. Since now is easy and simple create new objects I did it. Was there any other reason to clear the cache? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hum… if for some reason the URL was accessed before the cache might fools the test. As we have no control of tests ordering, this might happen… but it's not a sure thing. Let's remove it and if this become a problem we put it back ; ) |
||
resp = self.client.get(self.url) | ||
resp = self.client.get(self.url_no_receipt) | ||
expected = dict(url=None) | ||
content = loads(resp.content.decode('utf-8')) | ||
self.assertEqual(expected, content) | ||
|
||
@patch('jarbas.core.models.head') | ||
def test_refetch_existing_receipt(self, mocked_head): | ||
self.obj.receipt_fetched = True | ||
self.obj.receipt_url = None | ||
self.obj.save() | ||
expected = dict(url=self.reimbursement.receipt_url) | ||
self.reimbursement.receipt_fetched = True | ||
self.reimbursement.receipt_url = None | ||
self.reimbursement.save() | ||
mocked_head.return_value.status_code = 200 | ||
resp = self.client.get(self.url + '?force') | ||
expected = dict(url=self.expected_receipt_url) | ||
content = loads(resp.content.decode('utf-8')) | ||
self.assertEqual(expected, content) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
from json import loads | ||
from unittest.mock import patch | ||
|
||
from django.test import TestCase | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-r requirements.txt | ||
django-test-without-migrations==0.6 | ||
mixer==5.6.6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️