Skip to content
This repository was archived by the owner on Feb 28, 2018. It is now read-only.

Use Mixer for reimbursement view tests #171

Merged
merged 6 commits into from
May 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ before_install:
install:
- psql -U postgres -c 'create database "jarbas";'
- yarn install
- python -m pip install -r requirements.txt coveralls
- python -m pip install -r requirements-dev.txt coveralls

before_script:
- $TRAVIS_BUILD_DIR/sysconfcpus/bin/sysconfcpus -n 2 yarn assets
Expand Down
39 changes: 39 additions & 0 deletions jarbas/api/tests/__init__.py
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)
)
180 changes: 62 additions & 118 deletions jarbas/api/tests/test_reimbursement_view.py
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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

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'))
Expand All @@ -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

Expand All @@ -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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason to remove that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
2 changes: 1 addition & 1 deletion jarbas/api/tests/test_same_day_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestSameDay(TestCase):

def setUp(self):
self.maxDiff = 2 ** 10
cnpj = '12345678901234'
cnpj = '12345678901234'

company1 = sample_company_data.copy()
company1['cnpj'] = format_cnpj(cnpj)
Expand Down
5 changes: 4 additions & 1 deletion jarbas/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ def as_list(content, cast=None):
return None

parts = list(content.split(','))
return list(map(lambda x: cast(x), parts)) if cast else parts
try:
return list(map(lambda x: cast(x), parts)) if cast else parts
except ValueError as exc:
raise exc

def __repr__(self):
return 'Reimbursement(document_id={})'.format(self.document_id)
Expand Down
5 changes: 3 additions & 2 deletions jarbas/core/tests/test_irregularities_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def test_serializer_without_suspicions(self):
}
self.assertEqual(self.command.serialize(input), expected)


class TestCustomMethods(TestCommand):

@patch('jarbas.core.management.commands.irregularities.Command.irregularities')
Expand All @@ -83,7 +84,7 @@ def test_schedule_update_existing_record(self, get):
reimbursement = Reimbursement()
get.return_value = reimbursement
content = {
'document_id' : 42,
'document_id': 42,
'probability': 0.618,
'suspicions': {'answer': 42}
}
Expand All @@ -97,7 +98,7 @@ def test_schedule_update_existing_record(self, get):
@patch.object(Reimbursement.objects, 'get')
def test_schedule_update_non_existing_record(self, get):
get.side_effect = Reimbursement.DoesNotExist
content = {'document_id' : 42}
content = {'document_id': 42}
self.command.queue = []
self.command.schedule_update(content)
get.assert_called_once_with(document_id=42)
Expand Down
2 changes: 1 addition & 1 deletion jarbas/core/tests/test_load_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ def test_add_arguments(self):
def test_add_arguments_without_drop_all(self):
mock = Mock()
LoadCommand().add_arguments(mock, add_drop_all=False)
self.assertEqual(1, mock.add_argument.call_count)
self.assertEqual(1, mock.add_argument.call_count)
1 change: 0 additions & 1 deletion jarbas/core/tests/test_receipt_class.py
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
Expand Down
3 changes: 3 additions & 0 deletions requirements-dev.txt
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
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ dj-database-url==0.4.2
django-assets==0.12
django-bulk-update==2.0.0
django-cors-middleware==1.3.1
django-test-without-migrations==0.6
djangorestframework==3.6.3
freezegun==0.3.9
gunicorn==19.7.1
Expand Down