Skip to content

Commit

Permalink
Merge pull request #4 from Kalkuli/hotfix_170_atualizar_coberturas_co…
Browse files Browse the repository at this point in the history
…deClimate

Solve #127 Hotfix Atualizar Cobertura Code Climate
  • Loading branch information
Hargre authored Oct 28, 2018
2 parents e5f54bd + 4cfcdcf commit cc49d72
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ __pycache__
env
.vscode/
htmlcov/
arquivo.csv
.coverage
coverage.xml
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ before_install:
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin
before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build
- docker-compose -f docker-compose-dev.yml up --build -d
script:
- docker-compose -f docker-compose-dev.yml run base python manage.py test
- docker-compose -f docker-compose-dev.yml run base python manage.py cov
after_script:
- docker-compose -f docker-compose-dev.yml down
after_success:
- ./cc-test-reporter after-build -t coverage.py --exit-code $TRAVIS_TEST_RESULT
- chmod +x ./deploy.sh
- ./deploy.sh
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<a href="https://travis-ci.com/Kalkuli/2018.2-Kalkuli_Export"><img src="https://travis-ci.org/Kalkuli/2018.2-Kalkuli_Export.svg?branch=master" /></a>
<a href="https://codeclimate.com/github/Kalkuli/2018.2-Kalkuli_Export/maintainability"><img src="https://api.codeclimate.com/v1/badges/b1f0c20fae43ac3f8c59/maintainability" /></a>
<a href="https://codeclimate.com/github/Kalkuli/2018.2-Kalkuli_Export/maintainability"><img src="https://api.codeclimate.com/v1/badges/b1f0c20fae43ac3f8c59/maintainability" /></a>
<a href="https://codeclimate.com/github/Kalkuli/2018.2-Kalkuli_Export/test_coverage"><img src="https://api.codeclimate.com/v1/badges/b1f0c20fae43ac3f8c59/test_coverage" /></a>
<a href="https://opensource.org/licenses/GPL-3.0"><img src="https://img.shields.io/badge/license-GPL-%235DA8C1.svg"/></a>

</div>
Expand Down
2 changes: 1 addition & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def cov():
print('Coverage Summary:')
COV.report()
COV.html_report()
COV.erase()
COV.xml_report()
return 0
return 1

Expand Down
7 changes: 2 additions & 5 deletions project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
app_settings = os.getenv('APP_SETTINGS')
app.config.from_object(app_settings)

@app.route('/', methods=['GET'])
def ping_pong():
return jsonify({
'data': 'Welcome to Kalkuli Exporter!'
})
from project.api.views import export_blueprint
app.register_blueprint(export_blueprint)
52 changes: 52 additions & 0 deletions project/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import csv
from flask import Flask, jsonify, Blueprint, request, send_file


export_blueprint = Blueprint('export', __name__)

@export_blueprint.route('/export', methods=['POST'])
def exports():
post_data = request.get_json()

error_response = {
'status': 'fail',
'message': 'wrong json'
}

if not post_data:
return jsonify(error_response), 400


employ_data = open('./project/assets/arquivo.csv', 'w')
csvwriter = csv.writer(employ_data)

receipts = post_data.get('receipts')
total_cost = post_data.get('total_cost')

csvwriter.writerow(['CNPJ','ID da Empresa','Data de Emissão','Local','Imposto','Valor', 'Valor total'])

count = 0

for emp in receipts:
if not count:
csvwriter.writerow([emp['cnpj'],
emp['company_id'],
emp['emission_date'],
emp['emission_place'],
emp['tax_value'],
emp['total_price'],
total_cost])
count += 1
else:
csvwriter.writerow([emp['cnpj'],
emp['company_id'],
emp['emission_date'],
emp['emission_place'],
emp['tax_value'],
emp['total_price']])

employ_data.close()

return send_file('./assets/arquivo.csv', mimetype='text/csv',
attachment_filename='arquivo.csv',
as_attachment=True), 200
Empty file added project/assets/.placeholder
Empty file.
2 changes: 1 addition & 1 deletion project/tests/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask_testing import TestCase
from project import app

class BaseTestCase(TestCase):
class BaseTestCase(TestCase):

def create_app(self):
app.config.from_object('project.config.TestingConfig')
Expand Down
68 changes: 68 additions & 0 deletions project/tests/test_exports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import json
from project.tests.base import BaseTestCase
import unittest


class TestExportService(BaseTestCase):

def test_exports(self):

with self.client:

response = self.client.post(
'/export',
data=json.dumps({
"receipts": [
{
"cnpj": "000.000.000/0000-00",
"company_id": 1234,
"emission_date": "2018-11-10",
"emission_place": "place",
"id": 1,
"tax_value": 123.12,
"total_price": 456.45
},
{
"cnpj": "000.000.000/0000-00",
"company_id": 1234,
"emission_date": "2018-10-10",
"emission_place": "place",
"id": 2,
"tax_value": 123.12,
"total_price": 456.45
},
{
"cnpj": "000.000.000/0000-00",
"company_id": 1234,
"emission_date": "2018-10-12",
"emission_place": "place",
"id": 3,
"tax_value": 123.12,
"total_price": 456.45
}
],
"total_cost": "1369.35"
}),
content_type='application/json',
)

self.assertEqual(response.status_code, 200)

def test_not_json(self):

with self.client:

response = self.client.post(
'/export',
data=json.dumps({}),
content_type='application/json',
)

data = json.loads(response.data.decode())

self.assertEqual(response.status_code, 400)
self.assertIn('wrong json', data['message'])


if __name__ == '__main__':
unittest.main()

0 comments on commit cc49d72

Please sign in to comment.