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

Commit

Permalink
Use mixer for reimbursement view tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caiocarrara committed May 26, 2017
1 parent ab26012 commit c756a7f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 114 deletions.
168 changes: 55 additions & 113 deletions jarbas/api/tests/test_reimbursement_view.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,19 @@
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 mixer.backend.django import mixer

from jarbas.core.models import Reimbursement
from jarbas.core.tests import sample_reimbursement_data, suspicions


class TestListApi(TestCase):

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)

mixer.cycle(5).blend(Reimbursement)
self.url = resolve_url('api:reimbursement-list')

def test_status(self):
Expand All @@ -67,38 +27,43 @@ def test_content_general(self):
def test_ordering(self):
resp = self.client.get(self.url)
content = loads(resp.content.decode('utf-8'))
first = content['results'][0]
last = content['results'][-1]
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'
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 = mixer.blend(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'
mixer.blend(Reimbursement, issue_date='1970-01-01')
mixer.blend(Reimbursement, issue_date='1970-02-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)
mixer.cycle(4).blend(Reimbursement,
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 @@ -113,8 +78,9 @@ 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 = mixer.blend(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 @@ -123,76 +89,52 @@ 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,
receipt=dict(fetched=False, url=None)
)
self.assertEqual(expected, contents)
for result_attr, result_value in contents.items():
if not hasattr(self.reimbursement, result_attr):
continue
expected_value = getattr(self.reimbursement, result_attr)
self.assertEqual(str(result_value), str(expected_value))


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 = mixer.blend(
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 = mixer.blend(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()
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)
6 changes: 5 additions & 1 deletion jarbas/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ 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:
# this is far from good
return list()

def __repr__(self):
return 'Reimbursement(document_id={})'.format(self.document_id)
Expand Down
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mixer==5.6.6

0 comments on commit c756a7f

Please sign in to comment.