def _processCart(self, order_data, customer_id): try: # Convert from Schema into JSON serialized_data = CreateOrderSchema().dump(order_data).data # Get available products to check whether product is available self.products_rpc.checkSpecificList(serialized_data) # result = self.orders_rpc.createOrder(serialized_data['order_details']) result_message = self.orders_rpc.processCart(serialized_data, customer_id) except ProductNotFound as ex: self.logger.info(ex) raise ProductNotFound("Product not found!") except ProductOutOfStock as ex: self.logger.info(ex) raise ProductOutOfStock("Product is out of stock!") except Exception as ex: self.logger.info(ex) raise return result_message
class TestHttpEntrypoint(object): @pytest.mark.parametrize( ('exc', 'expected_error', 'expected_status_code', 'expected_message'), [ (ValueError('unexpected'), 'UNEXPECTED_ERROR', 500, 'unexpected'), (ValidationError('v1'), 'VALIDATION_ERROR', 400, 'v1'), (ProductNotFound('p1'), 'PRODUCT_NOT_FOUND', 404, 'p1'), (OrderNotFound('o1'), 'ORDER_NOT_FOUND', 404, 'o1'), (TypeError('t1'), 'BAD_REQUEST', 400, 't1'), ]) def test_error_handling(self, exc, expected_error, expected_status_code, expected_message): entrypoint = HttpEntrypoint('GET', 'url') entrypoint.expected_exceptions = ( ValidationError, ProductNotFound, OrderNotFound, TypeError, ) response = entrypoint.response_from_exception(exc) response_data = json.loads(response.data.decode()) assert response.mimetype == 'application/json' assert response.status_code == expected_status_code assert response_data['error'] == expected_error assert response_data['message'] == expected_message
def test_product_not_found(self, gateway_service, web_session): gateway_service.products_rpc.get.side_effect = ( ProductNotFound('missing')) # call the gateway service to get order #1 response = web_session.get('/products/foo') assert response.status_code == 404 payload = response.json() assert payload['error'] == 'PRODUCT_NOT_FOUND' assert payload['message'] == 'missing'
def _create_order(self, order_data): # check order product ids are valid valid_product_ids = {prod['id'] for prod in self.products_rpc.list()} for item in order_data['order_details']: if item['product_id'] not in valid_product_ids: raise ProductNotFound("Product Id {}".format( item['product_id'])) # Call orders-service to create the order. # Dump the data through the schema to ensure the values are serialized # correctly. serialized_data = CreateOrderSchema().dump(order_data).data result = self.orders_rpc.create_order(serialized_data['order_details']) return result['id']