From 8f6ee7fa82ce897d8b2d031c20f71190662ce857 Mon Sep 17 00:00:00 2001 From: devsalula Date: Thu, 25 Oct 2018 00:29:14 -0300 Subject: [PATCH 1/7] Create Basic Structure for Export --- project/__init__.py | 7 ++----- project/api/views.py | 4 ++++ 2 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 project/api/views.py diff --git a/project/__init__.py b/project/__init__.py index 50bd7a7..2c1c60f 100644 --- a/project/__init__.py +++ b/project/__init__.py @@ -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!' - }) \ No newline at end of file +from project.api.views import export_blueprint +app.register_blueprint(export_blueprint) \ No newline at end of file diff --git a/project/api/views.py b/project/api/views.py new file mode 100644 index 0000000..eb45d22 --- /dev/null +++ b/project/api/views.py @@ -0,0 +1,4 @@ +from flask import Flask, jsonify, Blueprint, request + +export_blueprint = Blueprint('export', __name__) + From cc75eca7897fbb527c9f44786293dc1dbbcb355f Mon Sep 17 00:00:00 2001 From: devsalula Date: Fri, 26 Oct 2018 15:38:46 -0300 Subject: [PATCH 2/7] create method that converts json in csv --- project/api/views.py | 33 ++++++++++++++++++++++++++++++++- project/assets/.placeholder | 0 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 project/assets/.placeholder diff --git a/project/api/views.py b/project/api/views.py index eb45d22..4140322 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -1,4 +1,35 @@ -from flask import Flask, jsonify, Blueprint, request +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') + + count = 0 + for emp in receipts: + if count == 0: + header = emp.keys() + csvwriter.writerow(header) + count += 1 + csvwriter.writerow(emp.values()) + # csvwriter.writerows(total_cost) + employ_data.close() + + return send_file('./assets/arquivo.csv') \ No newline at end of file diff --git a/project/assets/.placeholder b/project/assets/.placeholder new file mode 100644 index 0000000..e69de29 From 65157d034fe1c7b8b0e96cbeab406837f58ff57e Mon Sep 17 00:00:00 2001 From: devsalula Date: Fri, 26 Oct 2018 19:50:10 -0300 Subject: [PATCH 3/7] Add total cost in csv and add arquivo.csv in gitignore --- .gitignore | 1 + project/api/views.py | 31 +++++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 6c46732..6d5d56e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__ env .vscode/ htmlcov/ +arquivo.csv diff --git a/project/api/views.py b/project/api/views.py index 4140322..32c273b 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -16,20 +16,39 @@ def exports(): 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','company_id','emission_date','emission_place','id','tax_value','total_price', 'total_cost']) + count = 0 + for emp in receipts: - if count == 0: - header = emp.keys() - csvwriter.writerow(header) + if not count: + csvwriter.writerow([emp['cnpj'], + emp['company_id'], + emp['emission_date'], + emp['emission_place'], + emp['id'], + emp['tax_value'], + emp['total_price'], + total_cost]) count += 1 - csvwriter.writerow(emp.values()) - # csvwriter.writerows(total_cost) + else: + csvwriter.writerow([emp['cnpj'], + emp['company_id'], + emp['emission_date'], + emp['emission_place'], + emp['id'], + emp['tax_value'], + emp['total_price']]) + employ_data.close() - return send_file('./assets/arquivo.csv') \ No newline at end of file + return send_file('./assets/arquivo.csv', mimetype='text/csv', + attachment_filename='arquivo.csv', + as_attachment=True) \ No newline at end of file From aea0badcdb40ba3cefdfb24e28f39ff2abc0cd43 Mon Sep 17 00:00:00 2001 From: devsalula Date: Sat, 27 Oct 2018 01:26:26 -0300 Subject: [PATCH 4/7] Add Request status --- project/api/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/api/views.py b/project/api/views.py index 32c273b..1bcd1ca 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -51,4 +51,4 @@ def exports(): return send_file('./assets/arquivo.csv', mimetype='text/csv', attachment_filename='arquivo.csv', - as_attachment=True) \ No newline at end of file + as_attachment=True), 200 \ No newline at end of file From fdaddbdd3106e3c5ba9b23c2b16c64e31b91f7c2 Mon Sep 17 00:00:00 2001 From: devsalula Date: Sat, 27 Oct 2018 12:49:01 -0300 Subject: [PATCH 5/7] Change Header in CSV and add tests --- project/api/views.py | 3 +- project/tests/base.py | 2 +- project/tests/test_exports.py | 68 +++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 project/tests/test_exports.py diff --git a/project/api/views.py b/project/api/views.py index 1bcd1ca..65dbfa1 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -23,7 +23,7 @@ def exports(): receipts = post_data.get('receipts') total_cost = post_data.get('total_cost') - csvwriter.writerow(['cnpj','company_id','emission_date','emission_place','id','tax_value','total_price', 'total_cost']) + csvwriter.writerow(['CNPJ','ID da Empresa','Data de Emissão','Local','Imposto','Valor', 'Valor total']) count = 0 @@ -33,7 +33,6 @@ def exports(): emp['company_id'], emp['emission_date'], emp['emission_place'], - emp['id'], emp['tax_value'], emp['total_price'], total_cost]) diff --git a/project/tests/base.py b/project/tests/base.py index b8f83e9..4c8622c 100644 --- a/project/tests/base.py +++ b/project/tests/base.py @@ -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') diff --git a/project/tests/test_exports.py b/project/tests/test_exports.py new file mode 100644 index 0000000..85f8f2d --- /dev/null +++ b/project/tests/test_exports.py @@ -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() \ No newline at end of file From 77de7d43e3dcded5e7dd9ad09194e0ea56821a0f Mon Sep 17 00:00:00 2001 From: devsalula Date: Sat, 27 Oct 2018 16:06:20 -0300 Subject: [PATCH 6/7] remove id of export --- project/api/views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project/api/views.py b/project/api/views.py index 65dbfa1..6afe753 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -42,7 +42,6 @@ def exports(): emp['company_id'], emp['emission_date'], emp['emission_place'], - emp['id'], emp['tax_value'], emp['total_price']]) From 4cfcdcf0ca497cd07365a59b18e6807be58f3bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariana=20P=C3=ADcolo?= Date: Sat, 27 Oct 2018 23:08:07 -0300 Subject: [PATCH 7/7] Update travis to send test coverage to codeclimate --- .gitignore | 2 ++ .travis.yml | 5 +++++ README.md | 2 +- manage.py | 2 +- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6d5d56e..3a7a386 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ env .vscode/ htmlcov/ arquivo.csv +.coverage +coverage.xml diff --git a/.travis.yml b/.travis.yml index 856b7e4..31d88b0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index e50f30a..6ef5822 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ - + diff --git a/manage.py b/manage.py index aa035cc..c8a25de 100644 --- a/manage.py +++ b/manage.py @@ -37,7 +37,7 @@ def cov(): print('Coverage Summary:') COV.report() COV.html_report() - COV.erase() + COV.xml_report() return 0 return 1