-
Notifications
You must be signed in to change notification settings - Fork 668
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from datasciencebr/cuducos-same-day-data
Add a “same day” API endpoint
- Loading branch information
Showing
8 changed files
with
222 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from json import loads | ||
|
||
from django.shortcuts import resolve_url | ||
from django.test import TestCase | ||
|
||
from jarbas.core.models import Company, Reimbursement | ||
from jarbas.core.tests import sample_company_data, sample_reimbursement_data | ||
from jarbas.api.serializers import format_cnpj | ||
|
||
|
||
class TestSameDay(TestCase): | ||
|
||
def setUp(self): | ||
self.maxDiff = 2 ** 10 | ||
cnpj = '12345678901234' | ||
|
||
company1 = sample_company_data.copy() | ||
company1['cnpj'] = format_cnpj(cnpj) | ||
company1['city'] = 'Atlantis' | ||
company1['state'] = 'WE' | ||
Company.objects.create(**company1) | ||
|
||
company2 = sample_company_data.copy() | ||
company2['cnpj'] = format_cnpj(cnpj[::-1]) | ||
Company.objects.create(**company2) | ||
|
||
data1 = sample_reimbursement_data.copy() | ||
data1['document_id'] = 42 * 2 | ||
data1['cnpj_cpf'] = cnpj | ||
Reimbursement.objects.create(**data1) | ||
|
||
data2 = sample_reimbursement_data.copy() | ||
data2['document_id'] = 42 * 3 | ||
data2['cnpj_cpf'] = cnpj | ||
Reimbursement.objects.create(**data2) | ||
|
||
data3 = sample_reimbursement_data.copy() | ||
data3['document_id'] = 42 * 4 | ||
data3['cnpj_cpf'] = cnpj[::-1] | ||
Reimbursement.objects.create(**data3) | ||
|
||
data4 = sample_reimbursement_data.copy() | ||
data4['document_id'] = 42 * 5 | ||
data4['cnpj_cpf'] = '11111111111111' | ||
Reimbursement.objects.create(**data4) | ||
|
||
unique_id = dict( | ||
year=1970, | ||
applicant_id=13, | ||
document_id=84 | ||
) | ||
url = resolve_url('api:reimbursement-same-day', **unique_id) | ||
self.resp = self.client.get(url) | ||
|
||
def test_status_code(self): | ||
self.assertEqual(200, self.resp.status_code) | ||
|
||
def test_contents(self): | ||
expected = ( | ||
{ | ||
'applicant_id': 13, | ||
'city': 'Atlantis - WE', | ||
'document_id': 126, | ||
'subquota_description': 'Subquota description', | ||
'supplier': 'Acme', | ||
'total_net_value': '4.560', | ||
'year': 1970 | ||
}, | ||
{ | ||
'applicant_id': 13, | ||
'city': None, | ||
'document_id': 168, | ||
'subquota_description': 'Subquota description', | ||
'supplier': 'Acme', | ||
'total_net_value': '4.560', | ||
'year': 1970 | ||
}, | ||
{ | ||
'applicant_id': 13, | ||
'city': None, | ||
'document_id': 210, | ||
'subquota_description': 'Subquota description', | ||
'supplier': 'Acme', | ||
'total_net_value': '4.560', | ||
'year': 1970 | ||
} | ||
) | ||
content = loads(self.resp.content.decode('utf-8')) | ||
self.assertEqual(3, int(content['count'])) | ||
for reimbursement in expected: | ||
with self.subTest(): | ||
self.assertIn(reimbursement, content['results']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
jarbas/core/migrations/0021_make_reciept_fetched_a_db_index.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.10.4 on 2016-12-22 18:22 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0020_rename_supplier_to_company'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='reimbursement', | ||
name='receipt_fetched', | ||
field=models.BooleanField(db_index=True, default=False, verbose_name='Was the receipt URL fetched?'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.db import models | ||
|
||
|
||
class SameDayQuerySet(models.QuerySet): | ||
|
||
def same_day(self, **kwargs): | ||
keys = ('year', 'applicant_id', 'document_id') | ||
unique_id = {k: v for k, v in kwargs.items()} | ||
if not all(map(unique_id.get, keys)): | ||
msg = 'A same_day queryset requires the kwargs: ' + ', '.join(keys) | ||
raise TypeError(msg) | ||
|
||
reference = self.get(**unique_id) | ||
return self.exclude(**unique_id).filter( | ||
issue_date=reference.issue_date, | ||
applicant_id=reference.applicant_id | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters