From 13b90ecccd5fbae03ef7b8e2b0c6a970db0b9cca Mon Sep 17 00:00:00 2001 From: Bernardo Henrique Date: Thu, 20 Sep 2018 17:45:18 -0300 Subject: [PATCH 1/9] Adds Continous Integration Adds tavis file which runs all tests and projs builds. Adds Build Flag. --- .travis.yml | 16 ++++++++++++++++ README.md | 3 +++ 2 files changed, 19 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..dde723d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,16 @@ +sudo: required +services: + - docker +env: + DOCKER_COMPOSE_VERSION: 1.21.1 +before_install: + - sudo rm /usr/local/bin/docker-compose + - curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose + - chmod +x docker-compose + - sudo mv docker-compose /usr/local/bin +before_script: + - docker-compose -f docker-compose-dev.yml up --build -d +script: + - docker-compose -f docker-compose-dev.yml run base python manage.py test +after_script: + - docker-compose -f docker-compose-dev.yml down \ No newline at end of file diff --git a/README.md b/README.md index b941766..5c609e2 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +[![Build Status](https://travis-ci.org/Kalkuli/2018.2-Kalkuli_Reports.svg?branch=master +)](https://travis-ci.com/Kalkuli/2018.2-Kalkuli_Reports) + # Configurando o ambiente Para instruções de como instalar o Docker e o Docker-compose clique [aqui](https://github.com/Kalkuli/2018.2-Kalkuli_Front-End/blob/master/README.md). From 919215238239a1388fd7e8210e38b0d23ef8734e Mon Sep 17 00:00:00 2001 From: Felipe Hargreaves Date: Thu, 20 Sep 2018 22:59:01 -0300 Subject: [PATCH 2/9] Add tests coverage. --- .gitignore | 3 ++- manage.py | 30 ++++++++++++++++++++++++++++-- requirements.txt | 3 ++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index bc15f85..6c46732 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__ env -.vscode/ \ No newline at end of file +.vscode/ +htmlcov/ diff --git a/manage.py b/manage.py index d1e6e33..cfddda2 100644 --- a/manage.py +++ b/manage.py @@ -2,6 +2,17 @@ from project import app, db from project.api.models import Report, TagUseReport import unittest +import coverage + +COV = coverage.coverage( + branch=True, + include='project/*', + omit=[ + 'project/tests/*', + 'project/config.py', + ] +) +COV.start() cli = FlaskGroup(app) @@ -23,7 +34,22 @@ def test(): return 0 return 1 - + +@cli.command() +def cov(): + """Runs the unit tests with coverage.""" + tests = unittest.TestLoader().discover('project/tests') + result = unittest.TextTestRunner(verbosity=2).run(tests) + if result.wasSuccessful(): + COV.stop() + COV.save() + print('Coverage Summary:') + COV.report() + COV.html_report() + COV.erase() + return 0 + return 1 + if __name__ == '__main__': - cli() \ No newline at end of file + cli() diff --git a/requirements.txt b/requirements.txt index dc55b61..de8c41e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ Flask==1.0.2 Flask-SQLAlchemy==2.3.2 psycopg2==2.7.4 -Flask-Testing==0.6.2 \ No newline at end of file +Flask-Testing==0.6.2 +coverage==4.5.1 \ No newline at end of file From 29ab9a912b257b08c85f34b1cc97062a90bf58f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariana=20P=C3=ADcolo?= Date: Mon, 24 Sep 2018 21:01:23 -0300 Subject: [PATCH 3/9] Add readme --- README.md | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5c609e2..fc1a6c5 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,52 @@ -[![Build Status](https://travis-ci.org/Kalkuli/2018.2-Kalkuli_Reports.svg?branch=master -)](https://travis-ci.com/Kalkuli/2018.2-Kalkuli_Reports) +# Serviço de Geração de Relatórios + +
+ + + + + + +
# Configurando o ambiente Para instruções de como instalar o Docker e o Docker-compose clique [aqui](https://github.com/Kalkuli/2018.2-Kalkuli_Front-End/blob/master/README.md). +*** -
+### Colocando no ar +Com o _Docker_ e _Docker-Compose_ instalados, basta apenas utilizar os comandos: -## Colocando no ar -Com o Docker e Docker-Compose instalados, basta apenas utilizar os comandos: +``` +chmod +x entrypoint.sh -```chmod +x entrypoint.sh``` +docker-compose -f docker-compose-dev.yml build -```docker-compose -f docker-compose-dev.yml build``` +docker-compose -f docker-compose-dev.yml up +``` -e +Abra outro terminal, e execute o comando: -```docker-compose -f docker-compose-dev.yml up``` -Acesse o servidor local no endereço apresentado abaixo: +``` +docker-compose -f docker-compose-dev.yml run base python manage.py recreate_db +``` -http://localhost:5004/ +Acesse o servidor local no endereço apresentado abaixo: +[localhost:5004](http://localhost:5004/) Agora você já pode começar a contribuir! ## Testando -```docker-compose -f docker-compose-dev.yml run base python manage.py test``` \ No newline at end of file +``` +docker-compose -f docker-compose-dev.yml run base python manage.py test +``` + +Execute o comando abaixo para checar a cobertura: + +``` +docker-compose -f docker-compose-dev.yml run base python manage.py cov +``` \ No newline at end of file From ed6f56a6ef801dff90b303e0f3d614252515c2b7 Mon Sep 17 00:00:00 2001 From: Bernardo Henrique Date: Thu, 27 Sep 2018 22:22:13 -0300 Subject: [PATCH 4/9] Hotfix Minor changes on reacreatedb command, whitch was not beeing recognized. --- README.md | 34 ++++++++++++++++++++++++---------- manage.py | 2 +- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5c609e2..980e809 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,45 @@ [![Build Status](https://travis-ci.org/Kalkuli/2018.2-Kalkuli_Reports.svg?branch=master )](https://travis-ci.com/Kalkuli/2018.2-Kalkuli_Reports) + # Configurando o ambiente Para instruções de como instalar o Docker e o Docker-compose clique [aqui](https://github.com/Kalkuli/2018.2-Kalkuli_Front-End/blob/master/README.md). - -
+*** ## Colocando no ar -Com o Docker e Docker-Compose instalados, basta apenas utilizar os comandos: +Com o _Docker_ e _Docker-Compose_ instalados, basta apenas utilizar os comandos: -```chmod +x entrypoint.sh``` +``` +chmod +x entrypoint.sh -```docker-compose -f docker-compose-dev.yml build``` +docker-compose -f docker-compose-dev.yml build -e +docker-compose -f docker-compose-dev.yml up +``` -```docker-compose -f docker-compose-dev.yml up``` +Abra outro terminal, e execute o comando: -Acesse o servidor local no endereço apresentado abaixo: -http://localhost:5004/ +``` +docker-compose -f docker-compose-dev.yml run base python manage.py recreatedb +``` +Acesse o servidor local no endereço apresentado abaixo: + +[localhost:5004](http://localhost:5004/) Agora você já pode começar a contribuir! ## Testando -```docker-compose -f docker-compose-dev.yml run base python manage.py test``` \ No newline at end of file +``` +docker-compose -f docker-compose-dev.yml run base python manage.py test +``` + +Execute o comando abaixo para checar a cobertura: + +``` +docker-compose -f docker-compose-dev.yml run base python manage.py cov +``` \ No newline at end of file diff --git a/manage.py b/manage.py index cfddda2..2903851 100644 --- a/manage.py +++ b/manage.py @@ -19,7 +19,7 @@ # Create command @cli.command() -def recreate_db(): +def recreatedb(): db.drop_all() db.create_all() db.session.commit() From 0bbacc0208dc968818e68adb1261d6efdeabeee4 Mon Sep 17 00:00:00 2001 From: devsalula Date: Thu, 27 Sep 2018 23:47:14 -0300 Subject: [PATCH 5/9] Create report method Co-authored-by: Saleh Nazih Co-authored-by: Pedro Feo --- project/__init__.py | 8 +++----- project/api/views.py | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 project/api/views.py diff --git a/project/__init__.py b/project/__init__.py index f8299c3..5f7a6a1 100644 --- a/project/__init__.py +++ b/project/__init__.py @@ -14,9 +14,7 @@ # Instanciate Database db = SQLAlchemy(app) +from project.api.views import reports_blueprint -@app.route('/', methods=['GET']) -def ping_pong(): - return jsonify({ - 'data': 'Welcome to Kalkuli Reports Service!' - }) \ No newline at end of file + +app.register_blueprint(reports_blueprint) diff --git a/project/api/views.py b/project/api/views.py new file mode 100644 index 0000000..4dd3edf --- /dev/null +++ b/project/api/views.py @@ -0,0 +1,26 @@ +from flask import Blueprint, request, jsonify + +reports_blueprint = Blueprint('/report', __name__) + + +@reports_blueprint.route('/report', methods=['POST']) +def reports(): + + data = request.get_json() + + if not data: + return jsonify({ + 'error': 'empty json' + }), 400 + + reports = data.get('receipts') + + sum = 0 + + for report in reports: + sum += report.get('total_price') + + return jsonify({ + 'receipts': reports, + 'total_sum': sum + }), 200 From 57bcd5939c750087a7b0c8d4d446ad522828967a Mon Sep 17 00:00:00 2001 From: devsalula Date: Fri, 28 Sep 2018 13:28:04 -0300 Subject: [PATCH 6/9] Add report data base Co-authored-by: Pedro Feo Co-authored-by: Saleh Nazih --- project/api/models.py | 2 +- project/api/views.py | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/project/api/models.py b/project/api/models.py index 8ddc4e9..d7ed1ad 100644 --- a/project/api/models.py +++ b/project/api/models.py @@ -9,7 +9,7 @@ class Report(db.Model): date_to = db.Column(db.DateTime, nullable=False) total_cost = db.Column(db.Float, nullable=False) total_tax_cost = db.Column(db.Float, nullable=False) - tagUseReports = db.relationship('tagUseReport', backref='report', lazy=True) +# tagUseReports = db.relationship('tagUseReport', backref='report', lazy=True) def __init__(self, company_id, date_from, date_to, total_cost, total_tax_cost): self.company_id = company_id diff --git a/project/api/views.py b/project/api/views.py index 4dd3edf..3a2b64b 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -1,4 +1,9 @@ from flask import Blueprint, request, jsonify +from project.api.models import Report + +from sqlalchemy import exc +from project import db + reports_blueprint = Blueprint('/report', __name__) @@ -22,5 +27,40 @@ def reports(): return jsonify({ 'receipts': reports, - 'total_sum': sum + 'total_cost': sum }), 200 + + +@reports_blueprint.route('/reports', methods=['POST']) +def add_report(): + data = request.get_json() + + if not data: + return jsonify({ + 'error': 'Report can not be saved' + }), 400 + + reports = data.get('receipts') + + company_id = data.get('company_id') + data_from = data.get('date_from') + data_to = data.get('date_to') + total_cost = sum + total_tax_cost = data.get('total_tax_cost') + + try: + report = Report(company_id, data_from, data_to, total_cost, total_tax_cost) + db.session.add(report) + db.session.commit() + + return jsonify({ + 'status': 'success', + 'data': { + 'message': 'Report was created!' + } + }), 200 + except exc.IntegrityError: + db.session.rollback() + return jsonify({ + 'error': 'Report can not be saved' + }), 400 From 141a6810ca0e71aece93edcacda32e270cbc2197 Mon Sep 17 00:00:00 2001 From: Phe0 Date: Fri, 28 Sep 2018 15:09:34 -0300 Subject: [PATCH 7/9] Add tests to Report and Fix model and add_report MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Pedro Féo Co-authored-by: Saleh Nazih --- project/api/models.py | 8 +++--- project/api/views.py | 17 +++++++---- project/tests/test_reports.py | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/project/api/models.py b/project/api/models.py index d7ed1ad..a100e56 100644 --- a/project/api/models.py +++ b/project/api/models.py @@ -4,12 +4,12 @@ class Report(db.Model): __tablename__ = 'report' id = db.Column(db.Integer, primary_key=True, autoincrement=True) - company_id = db.Column(db.Integer, nullable=False) + company_id = db.Column(db.Integer, nullable=True) date_from = db.Column(db.DateTime, nullable=False) date_to = db.Column(db.DateTime, nullable=False) - total_cost = db.Column(db.Float, nullable=False) - total_tax_cost = db.Column(db.Float, nullable=False) -# tagUseReports = db.relationship('tagUseReport', backref='report', lazy=True) + total_cost = db.Column(db.String, nullable=False) + total_tax_cost = db.Column(db.Float, nullable=True) + #tagUseReports = db.relationship('tagUseReport', backref='report', lazy=True) def __init__(self, company_id, date_from, date_to, total_cost, total_tax_cost): self.company_id = company_id diff --git a/project/api/views.py b/project/api/views.py index 3a2b64b..52837d2 100644 --- a/project/api/views.py +++ b/project/api/views.py @@ -23,15 +23,21 @@ def reports(): sum = 0 for report in reports: + if not report.get('total_price'): + return jsonify({ + 'error': 'empty total_price' + }), 400 sum += report.get('total_price') + sum = str(sum) + return jsonify({ 'receipts': reports, - 'total_cost': sum + 'total_cost': sum }), 200 -@reports_blueprint.route('/reports', methods=['POST']) +@reports_blueprint.route('/add_report', methods=['POST']) def add_report(): data = request.get_json() @@ -42,11 +48,12 @@ def add_report(): reports = data.get('receipts') - company_id = data.get('company_id') + company_id = None data_from = data.get('date_from') data_to = data.get('date_to') - total_cost = sum - total_tax_cost = data.get('total_tax_cost') + total_cost = data.get('total_cost') + total_tax_cost = None + try: report = Report(company_id, data_from, data_to, total_cost, total_tax_cost) diff --git a/project/tests/test_reports.py b/project/tests/test_reports.py index 3d0e0b5..70cbc8c 100644 --- a/project/tests/test_reports.py +++ b/project/tests/test_reports.py @@ -11,7 +11,61 @@ def test_reports(self): self.assertEqual(response.status_code, 200) self.assertIn('Welcome to Kalkuli Reports Service!', data['data']) + def test_reports(self): + with self.client: + response = self.client.post( + '/report', + data = json.dumps({ + 'receipts': [ + { + 'total_price': 12 + }, + { + 'total_price': 13 + } + ] + }), + content_type = 'application/json', + ) + + data = json.loads(response.data.decode()) + + self.assertEqual(response.status_code, 200) + self.assertIn( '25', data['total_cost']) + + def test_empty_json(self): + with self.client: + response = self.client.post( + '/report', + data = json.dumps({ + }), + content_type = 'application/json', + ) + + data = json.loads(response.data.decode()) + + self.assertEqual(response.status_code, 400) + + def test_missing_total_price(self): + with self.client: + response = self.client.post( + '/report', + data = json.dumps({ + 'receipts': [ + { + + }, + { + 'total_price': 12 + } + ] + }), + content_type = 'application/json', + ) + + data = json.loads(response.data.decode()) + self.assertEqual(response.status_code, 400) if __name__ == '__main__': unittest.main() \ No newline at end of file From 0a52fc419285f162792970fded76b554d60c2c46 Mon Sep 17 00:00:00 2001 From: Phe0 Date: Fri, 28 Sep 2018 15:51:14 -0300 Subject: [PATCH 8/9] Add tests to add_reports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Pedro Féo Co-authored-by: Saleh Nazih --- project/tests/test_reports.py | 80 +++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/project/tests/test_reports.py b/project/tests/test_reports.py index 70cbc8c..de845e2 100644 --- a/project/tests/test_reports.py +++ b/project/tests/test_reports.py @@ -33,12 +33,11 @@ def test_reports(self): self.assertEqual(response.status_code, 200) self.assertIn( '25', data['total_cost']) - def test_empty_json(self): + def test_empty_json_report(self): with self.client: response = self.client.post( '/report', - data = json.dumps({ - }), + data = json.dumps({}), content_type = 'application/json', ) @@ -66,6 +65,81 @@ def test_missing_total_price(self): data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 400) + + def test_add_report(self): + with self.client: + response = self.client.post( + '/add_report', + data = json.dumps({ + "date_to": "2018-02-23", + "date_from": "2018-02-12", + "total_cost": "123" + }), + content_type = 'application/json', + ) + + data = json.loads(response.data.decode()) + + self.assertEqual(response.status_code, 200) + self.assertIn('success', data['status']) + self.assertIn('Report was created!', data['data']['message']) + + def test_empty_json_add_report(self): + with self.client: + response = self.client.post( + '/add_report', + data = json.dumps({}), + content_type = 'application/json', + ) + + data = json.loads(response.data.decode()) + + self.assertEqual(response.status_code, 400) + + def test_missing_date_to(self): + with self.client: + response = self.client.post( + '/add_report', + data = json.dumps({ + "date_from": "2018-02-12", + "total_cost": "123" + }), + content_type = 'application/json', + ) + + data = json.loads(response.data.decode()) + + self.assertEqual(response.status_code, 400) + + def test_missing_date_from(self): + with self.client: + response = self.client.post( + '/add_report', + data = json.dumps({ + "date_to": "2018-02-12", + "total_cost": "123" + }), + content_type = 'application/json', + ) + + data = json.loads(response.data.decode()) + + self.assertEqual(response.status_code, 400) + + def test_missing_total_cost(self): + with self.client: + response = self.client.post( + '/add_report', + data = json.dumps({ + "date_to": "2018-02-12", + "date_from": "2018-02-22" + }), + content_type = 'application/json', + ) + + data = json.loads(response.data.decode()) + + self.assertEqual(response.status_code, 400) if __name__ == '__main__': unittest.main() \ No newline at end of file From ff867b7d60ac79d7308ecdbe4b1df5a7e1d003ea Mon Sep 17 00:00:00 2001 From: Bernardo Henrique Date: Tue, 2 Oct 2018 20:15:30 -0300 Subject: [PATCH 9/9] Config Deploy Co-authored-by: Felipe Hargreaves --- Procfile | 2 ++ README.md | 9 ++++++++- requirements.txt | 3 ++- 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 Procfile diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..b569f39 --- /dev/null +++ b/Procfile @@ -0,0 +1,2 @@ +web: gunicorn -b 0.0.0.0:$PORT manage:app +release: python manage.py recreatedb \ No newline at end of file diff --git a/README.md b/README.md index 83ce644..3f992f9 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,13 @@ -# Configurando o ambiente +## Deploy + +A ultima versão do app pode ser encontrada em: https://kalkuli-reports.herokuapp.com + +*** + +## Configurando o ambiente Para instruções de como instalar o Docker e o Docker-compose clique [aqui](https://github.com/Kalkuli/2018.2-Kalkuli_Front-End/blob/master/README.md). *** @@ -40,6 +46,7 @@ Acesse o servidor local no endereço apresentado abaixo: Agora você já pode começar a contribuir! +*** ## Testando diff --git a/requirements.txt b/requirements.txt index de8c41e..d5d77a8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ Flask==1.0.2 Flask-SQLAlchemy==2.3.2 psycopg2==2.7.4 Flask-Testing==0.6.2 -coverage==4.5.1 \ No newline at end of file +coverage==4.5.1 +gunicorn==19.8.1 \ No newline at end of file