From d98a40e0907502fcf85e63d112fc936fd60a4548 Mon Sep 17 00:00:00 2001 From: adrilene Date: Mon, 29 Jun 2020 11:55:44 -0300 Subject: [PATCH] =?UTF-8?q?[@adrilene/@denisousa]=20Issue=20#91=20-=20Inic?= =?UTF-8?q?iando=20valida=C3=A7=C3=B5es=20de=20PATCH.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service_orders_many_controller.py | 24 ++++++++++++----- .../validators/validation_request.py | 18 ++++++++----- api/v2/models/schemas/schema_base.py | 6 +++++ api/v2/models/schemas/service_order_schema.py | 4 +++ api/v2/services/service_order_service.py | 3 +++ .../v2/controllers/fixtures/service_orders.py | 4 +-- tests/v2/controllers/test_service_orders.py | 26 ++++++++++++++++--- tests/v2/mocks/mock_service_orders.py | 5 +++- 8 files changed, 72 insertions(+), 18 deletions(-) diff --git a/api/v2/controllers/service_orders_many_controller.py b/api/v2/controllers/service_orders_many_controller.py index 8507b93..bf2fa3b 100644 --- a/api/v2/controllers/service_orders_many_controller.py +++ b/api/v2/controllers/service_orders_many_controller.py @@ -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): @@ -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): diff --git a/api/v2/controllers/validators/validation_request.py b/api/v2/controllers/validators/validation_request.py index 83469d7..4630c3f 100644 --- a/api/v2/controllers/validators/validation_request.py +++ b/api/v2/controllers/validators/validation_request.py @@ -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): @@ -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") diff --git a/api/v2/models/schemas/schema_base.py b/api/v2/models/schemas/schema_base.py index 3af3175..b6c9e52 100644 --- a/api/v2/models/schemas/schema_base.py +++ b/api/v2/models/schemas/schema_base.py @@ -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} diff --git a/api/v2/models/schemas/service_order_schema.py b/api/v2/models/schemas/service_order_schema.py index 88228f8..e6ec4fa 100644 --- a/api/v2/models/schemas/service_order_schema.py +++ b/api/v2/models/schemas/service_order_schema.py @@ -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) diff --git a/api/v2/services/service_order_service.py b/api/v2/services/service_order_service.py index 895b4a7..ccbc2b7 100644 --- a/api/v2/services/service_order_service.py +++ b/api/v2/services/service_order_service.py @@ -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 diff --git a/tests/v2/controllers/fixtures/service_orders.py b/tests/v2/controllers/fixtures/service_orders.py index 0af4a8f..83ff9be 100644 --- a/tests/v2/controllers/fixtures/service_orders.py +++ b/tests/v2/controllers/fixtures/service_orders.py @@ -1,4 +1,4 @@ def valid_service_order(): return { - "content": { "id": "1", "tipo": "Tipo 1"} - } \ No newline at end of file + "content": { "id": "1", "tipo": "Tipo 1"} + } diff --git a/tests/v2/controllers/test_service_orders.py b/tests/v2/controllers/test_service_orders.py index 29ae897..70f51f9 100644 --- a/tests/v2/controllers/test_service_orders.py +++ b/tests/v2/controllers/test_service_orders.py @@ -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/', '/v2/service_orders', ]) @@ -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 @@ -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 diff --git a/tests/v2/mocks/mock_service_orders.py b/tests/v2/mocks/mock_service_orders.py index f717a3c..dfc7f8e 100644 --- a/tests/v2/mocks/mock_service_orders.py +++ b/tests/v2/mocks/mock_service_orders.py @@ -63,6 +63,9 @@ "calibragem": { "status": "TESTE" } - } + }, + + 'valid_patch': + }