Skip to content

Commit

Permalink
Merge pull request #5 from Kalkuli/develop
Browse files Browse the repository at this point in the history
 Solve 125 Criar Release do Projeto
  • Loading branch information
MarianaPicolo authored Oct 3, 2018
2 parents 5a13f46 + a7c1437 commit 8596a70
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__
env
.vscode/
.vscode/
htmlcov/
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web: gunicorn -b 0.0.0.0:$PORT manage:app
release: python manage.py recreatedb
53 changes: 43 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,61 @@
# 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).
# Serviço de Geração de Relatórios

<div style="text-align: center">

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

</div>


<br>
## 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).

***

## 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```
docker-compose -f docker-compose-dev.yml up
```

e
Abra outro terminal, e execute o comando:

```docker-compose -f docker-compose-dev.yml up```

```
docker-compose -f docker-compose-dev.yml run base python manage.py recreatedb
```

Acesse o servidor local no endereço apresentado abaixo:

http://localhost:5004/

[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```
```
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
```
32 changes: 29 additions & 3 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@
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)


# Create command
@cli.command()
def recreate_db():
def recreatedb():
db.drop_all()
db.create_all()
db.session.commit()
Expand All @@ -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()
cli()
8 changes: 3 additions & 5 deletions project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
})

app.register_blueprint(reports_blueprint)
8 changes: 4 additions & 4 deletions project/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 73 additions & 0 deletions project/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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__)


@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:
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
}), 200


@reports_blueprint.route('/add_report', 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 = None
data_from = data.get('date_from')
data_to = data.get('date_to')
total_cost = data.get('total_cost')
total_tax_cost = None


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
128 changes: 128 additions & 0 deletions project/tests/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,135 @@ 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_report(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)

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()
Loading

0 comments on commit 8596a70

Please sign in to comment.