def test_right_fields(): courier = CourierModel(**TEST_DATA) assert courier.courier_id == TEST_DATA['courier_id'] assert courier.courier_type == TEST_DATA['courier_type'] assert courier.regions == TEST_DATA['regions'] assert courier.working_hours == TEST_DATA['working_hours']
async def add_couriers(request: Request) -> response.HTTPResponse: couriers, invalid_couriers_id = [], [] for courier in request.json['data']: try: courier = CourierModel(**courier) except ValidationError as e: invalid_couriers_id += [courier.get('courier_id', -1)] error_logger.warning(e.json(indent=4)) else: couriers += [courier] if invalid_couriers_id: error_logger.warning( "Request rejected, it contains invalid couriers (%s)", len(invalid_couriers_id)) context = validation_error('couriers', invalid_couriers_id) return response.json(context, status=400) added_couriers = await app.db.add_couriers(couriers) return response.json(added_couriers, status=201)
def test_add_couriers_with_valid_couriers(add_couriers_mock: mock.AsyncMock): json = TEST_COURIERS.copy() expected_couriers = [CourierModel(**courier) for courier in json['data']] expected_resp_json = { "couriers": [{ "id": courier.courier_id } for courier in expected_couriers] } request, response = app.test_client.post('/couriers', json=json) add_couriers_mock.assert_awaited_with(expected_couriers) assert response.status == 201 assert response.json == expected_resp_json
async def create_db_connection(app: Sanic, loop) -> None: await app.db.connect() if env.bool('migrate', False): await app.db.migrate() @app.listener('after_server_stop') async def close_db_connection(app: Sanic, loop) -> None: await app.db.close() @app.post('/couriers') @doc.tag("Add couriers") @doc.summary("Add some couriers to the service") @doc.consumes(doc.JsonBody({"data": [CourierModel.schema()]}), location="body", required=True, content_type="application/json") @doc.response(201, {"couriers": [{"id": int}]}, description="Couriers added") @doc.response(400, {"validation_error": { "couriers": [{ "id": int }] }}, description="Some of couriers are invalid") async def add_couriers(request: Request) -> response.HTTPResponse: couriers, invalid_couriers_id = [], [] for courier in request.json['data']: try: courier = CourierModel(**courier)
def test_several_working_hours(): test_data = TEST_DATA.copy() test_data['working_hours'] = ['12:55-13:10', '11:19-12:50', '21:50-23:10'] CourierModel(**test_data)
def test_not_serializable_working_hours(): test_data = TEST_DATA.copy() test_data['working_hours'] = ['12_55-13_10'] with pytest.raises(ValidationError): CourierModel(**test_data)
def test_empty_regions_list(): test_data = TEST_DATA.copy() test_data['regions'] = [] courier = CourierModel(**test_data) assert courier.regions == []
def test_float_regions(value): test_data = TEST_DATA.copy() test_data['regions'] = value with pytest.raises(ValidationError): CourierModel(**test_data)
def test_wrong_type(value): test_data = TEST_DATA.copy() test_data['courier_type'] = value with pytest.raises(ValidationError): CourierModel(**test_data)
def test_lack_of_fields(): test_data = TEST_DATA.copy() test_data.pop('courier_id') with pytest.raises(ValidationError): CourierModel(**test_data)
def test_extra_fields(): with pytest.raises(ValidationError): CourierModel(field='value', **TEST_DATA)