Skip to content

Commit

Permalink
[@adrilene/@denisousa] issue #73 WIP testes do post
Browse files Browse the repository at this point in the history
  • Loading branch information
denisousa committed Jun 22, 2020
1 parent 13e61ee commit c9900f6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
8 changes: 4 additions & 4 deletions api/v2/controllers/service_orders_many_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from http import HTTPStatus
from flask import request
from api.v2.models.schemas.service_order_schema import ServiceOrderSchema

from api.v2.utils.util_response import error_response

class ServiceOrdersManyController(Resource):
def get(self):
Expand All @@ -31,12 +31,12 @@ def post(self):

errors = []
for index, service_order in enumerate(body["content"]):
validate, message = ServiceOrderSchema().validate_posts(service_order)
validate, message = ServiceOrderSchema().validate_post(service_order)
if not validate:
errors.append({index: message})
continue

if errors:
return errors
return error_response(errors)

return ""
return ''
10 changes: 5 additions & 5 deletions api/v2/models/schemas/schema_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@


class SchemaBase():
def validate_posts(self, item):
if '_id' in item:
def validate_post(self, body):
if '_id' in body:
return False, 'Id must not be sent'

if 'updated_at' in item:
if 'updated_at' in body:
return False, 'Updated must not be sent'

if 'deleted_at' in item:
if 'deleted_at' in body:
return False, 'Deleted must not be sent'

try:
self.load(item)
self.load(body)
except ValidationError as err:
return False, err.messages

Expand Down
22 changes: 22 additions & 0 deletions api/v2/utils/util_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from flask import make_response, jsonify
from http import HTTPStatus

def error_response(message, status=400):
return make_response(
jsonify({
"error": message,
}),
status
)


def get_response(content, deleted):
response = {'content': content}

response['deleted'] = True if deleted else False

return make_response(jsonify(response), HTTPStatus.OK)


def post_response(content):
return make_response(jsonify({'content': content}), 201)
5 changes: 5 additions & 0 deletions tests/v2/mocks/mock_service_orders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mock_service_orders = {
'service_order_with_id': {
'_id': '123'
}
}
32 changes: 32 additions & 0 deletions tests/v2/models/schemas/test_service_orders_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import json
from run import app
from unittest import TestCase
from mockito import when, mock
from ...mocks.mock_service_orders import mock_service_orders
from api.v2.models.schemas.service_order_schema import ServiceOrderSchema
from http import HTTPStatus


class ServiceOrdersSchema(TestCase):
def setUp(self):
self.client = app.test_client()

def test_body_with_id_returns_error(self):
erro_schema = [{'0': 'Id must not be sent'}]
when(ServiceOrderSchema).validate_post(
mock_service_orders['service_order_with_id']
).thenReturn(erro_schema)

payload = json.dumps({'content': [mock_service_orders['service_order_with_id']]})

# response = self.client.post(
# '/v2/service_orders',
# headers={"Content-Type": "application/json"},
# data=payload)

# error = response.json
# # self.assertEqual(response.status_code, HTTPStatus.BAD_REQUEST)
# self.assertIn('error', error)
# self.assertEqual(type(error['error']), list)
# self.assertIn('0', error['error'][0])
# self.assertEqual(error['error'][0]['0'], 'Id must not be sent')

0 comments on commit c9900f6

Please sign in to comment.