Skip to content

Commit

Permalink
[@adrilene/@denisousa] Issue #91 - Iniciando validações de PATCH.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrilene committed Jun 29, 2020
1 parent 729e031 commit d98a40e
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 18 deletions.
24 changes: 18 additions & 6 deletions api/v2/controllers/service_orders_many_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
from flask_restful import Resource
from ..helpers.helper_response import get_response
from ..validation.validation_request import invalid_deleted_parameter
from api.v2.controllers.validators.validation_request import validate_request
from api.v2.controllers.validators.validation_request import validate_request, validate_request_id, validate_request_dict_id
from api.v2.services.service_order_service import ServiceOrderService
from api.v2.models.schemas.service_order_schema import ServiceOrderSchema
from api.v2.utils.util_response import error_response, post_response
from .validators.validation_request import validate_request_id


class ServiceOrdersManyController(Resource):
Expand All @@ -25,13 +24,26 @@ def get(self):

return get_response(service_orders, deleted_included)

def patch(self, id=None):
def patch(self):
body = request.get_json()
validade, message = validate_request(body)
if not validate:
return error_response(message)

errors = []
for index, service_order in enumerate(body['content']):
validate, message = validate_request_dict_id('service_order', service_order)
if not validate:
errors.append({index: message})

service_order_request = ServiceOrderRequest(body["content"])
so = service_order_request.get()
# ServiceOrderSchema().validate_updates(service_order, index)
# service_order_request = ServiceOrderRequest(body["content"])
# so = service_order_request.get()

# ServiceOrderService().update(so)
if errors:
return error_response(errors)

ServiceOrderService().update(so)
return "", 200

def post(self):
Expand Down
18 changes: 12 additions & 6 deletions api/v2/controllers/validators/validation_request.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from bson import ObjectId
from api.v2.services.item_service import ItemService
from api.services.equipamento_service import listar_equipamento_by_id
from ...services.service_order_service import ServiceOrderService


def invalid_deleted_parameter(param):
Expand Down Expand Up @@ -61,28 +62,33 @@ def validate_id_exists(entity, _id):
if not listar_equipamento_by_id(_id):
return (False, "Nonexistent equipamento ID")

if entity == "service_order":
if not ServiceOrderService().fetch_service_order_by_id(_id):
return (False, "Nonexistent service order ID")


return (True, "OK")


def validate_request_dict_id(entity):
def validate_request_dict_id(name_entity: str, entity: dict):
validate, message = validate_id_included(entity)
if not validate:
return (validate, message)

validate, message = validate_request_id(entity["_id"])
validate, message = validate_request_id(name_entity, entity["_id"])
if not validate:
return (validate, message)

return (True, "OK")


def validate_request_id(entity, _id):
def validate_request_id(name_entity: str, _id: str):
validate, message = validate_id_objectID(_id)
if not validate:
return (validate, message)
return validate, message

validate, message = validate_id_exists(entity, _id)
validate, message = validate_id_exists(name_entity, _id)
if not validate:
return (validate, message)
return validate, message

return (True, "OK")
6 changes: 6 additions & 0 deletions api/v2/models/schemas/schema_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ def validate_save(self, body):
return False, err.messages

return True, "OK"

def validate_updates(self, entity: dict, index: int, fields: tuple):
try:
self.load(entity, partial=fields)
except ValidationError as err:
return {index: err.messages}
4 changes: 4 additions & 0 deletions api/v2/models/schemas/service_order_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ class Meta:
calibragem = fields.Nested(CalibrationSchema, required=True)
status = fields.String(validate=validate.OneOf(["triagem", "diagnostico"]),
required=True)

def validate_updates(self, service_order: dict, index: int, fields: tuple):
fields = ('numero_ordem_servico', 'calibragem', 'status')
super().validate_updates(service_order, index, fields)
3 changes: 3 additions & 0 deletions api/v2/services/service_order_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def fetch_active(self):
def fetch_all(self):
return self.parser_mongo_response_to_list(OrdemServico.objects())

def fetch_service_order_by_id(self):
return OrdemServico.objects(id=_id).first()

def update(self, id):
pass

Expand Down
4 changes: 2 additions & 2 deletions tests/v2/controllers/fixtures/service_orders.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def valid_service_order():
return {
"content": { "id": "1", "tipo": "Tipo 1"}
}
"content": { "id": "1", "tipo": "Tipo 1"}
}
26 changes: 23 additions & 3 deletions tests/v2/controllers/test_service_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_get_returns_service_orders_on_successful_request(self):
when(ServiceOrderService).fetch_active().thenReturn(service_orders)

response = self.client.get("/v2/service_orders")
api.add_resource(service_orders_many_controller.ServiceOrdersManyController, *[
api.add_resource(service_orders_many_controller.ServiceOrdersManyController, *[
'/v2/service_orders/<int:id>',
'/v2/service_orders',
])
Expand Down Expand Up @@ -67,8 +67,6 @@ def test_patch_with_id(self):

mock_method.assert_called_with(valid_service_order()['content'])



# POST tests
def test_returns_list_with_the_saved_ids(self):
service_order_id = "5ee37c19d86b6a8893d1a3a7" # Fake Id
Expand All @@ -89,3 +87,25 @@ def test_returns_list_with_the_saved_ids(self):
self.assertEqual(type(response.json["content"]), list)
for id in response.json["content"]:
self.assertEqual(ObjectId.is_valid(id), True)

# PATCH tests
def test_service_order_has_patch_route(self):
response = self.client.patch(
'/v2/service_order',
headers={"Content-Type": "application/json"})
self.assertNotEqual(response.status_code, 405)

def test_patch_service_order_has_empty_body(self):
response = self.client.patch(
'/v2/service_order',
headers={"Content-Type": "application/json"})
self.assertNotEqual(response.status_code, 405)

def test_patch_service_order_has_wrong_field(self):
pass

def test_patch_service_order_has_invalid_id(self):
pass

def test_patch_service_order_has_nonexistent_id(self):
pass
5 changes: 4 additions & 1 deletion tests/v2/mocks/mock_service_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
"calibragem": {
"status": "TESTE"
}
}
},

'valid_patch':


}

0 comments on commit d98a40e

Please sign in to comment.