def test_route(): """ Testing route method in RegisterUserRouter """ register_pet_use_case = RegisterPetSpy(PetRepositorySpy(), UserRepositorySpy()) register_pet_router = RegisterPetController(register_pet_use_case) attributes = { "name": faker.word(), "specie": "Dog", "age": faker.random_number(), "user_information": { "user_id": faker.random_number(), "user_name": faker.word(), }, } response = register_pet_router.route(HttpRequest(body=attributes)) # Testing input assert register_pet_use_case.registry_param["name"] == attributes["name"] assert register_pet_use_case.registry_param["specie"] == attributes["specie"] assert register_pet_use_case.registry_param["age"] == attributes["age"] assert ( register_pet_use_case.registry_param["user_information"] == attributes["user_information"] ) # Testing output assert response.status_code == 200 assert "error" not in response.body
def test_route(): """Testing route for register Pet""" register_pet_use_case = RegisterPetSpy(PetsRepositorySpy, UserRepositorySpy) register_pet_route = RegisterPetController(register_pet_use_case) attributes = { "name": faker.word(), "specie": "dog", "age": faker.random_number(), "user_info": { "user_id": faker.random_number(), "user_name": faker.word() }, } response = register_pet_route.route(HttpRequest(body=attributes)) # Testing Input assert register_pet_use_case.registry_param["name"] == attributes["name"] assert register_pet_use_case.registry_param["specie"] == attributes[ "specie"] assert register_pet_use_case.registry_param["age"] == attributes["age"] assert register_pet_use_case.registry_param["user_info"] == attributes[ "user_info"] assert response.status_code == 200 assert "error" not in response.body
def test_route_error_no_body(): """ Testing route method in RegisterUserRouter """ register_pet_use_case = RegisterPetSpy(PetRepositorySpy(), UserRepositorySpy()) register_pet_router = RegisterPetController(register_pet_use_case) response = register_pet_router.route(HttpRequest()) # Testing input assert register_pet_use_case.registry_param == {} # Testing output assert response.status_code == 400 assert "error" in response.body
def test_route_error_wrong_body(): """ Testing route method in RegisterUserController """ register_user_use_case = RegisterUserSpy(UserRepositorySpy()) register_user_router = RegisterUserController(register_user_use_case) attributes = {"name": faker.word()} response = register_user_router.route(HttpRequest(body=attributes)) # Testing input assert register_user_use_case.register_param == {} # Testing output assert response.status_code == 422 assert "error" in response.body
def test_route_error_wrong_user_information(): """ Testing route method in RegisterUserRouter """ register_pet_use_case = RegisterPetSpy(PetRepositorySpy(), UserRepositorySpy()) register_pet_router = RegisterPetController(register_pet_use_case) attributes = {"name": faker.word(), "specie": "Dog", "user_information": {}} response = register_pet_router.route(HttpRequest(body=attributes)) # Testing input assert register_pet_use_case.registry_param == {} # Testing output assert response.status_code == 422 assert "error" in response.body
def test_handle_no_query_param(): """ Testing Handle method """ find_user_use_case = FindUserSpy(UserRepositorySpy()) find_user_controller = FindUserController(find_user_use_case) http_request = HttpRequest() response = find_user_controller.route(http_request) # Testing Inputs assert find_user_use_case.by_id_and_name_param == {} assert find_user_use_case.by_id_param == {} assert find_user_use_case.by_name_param == {} # Testing Output assert response.status_code == 400 assert "error" in response.body
def test_handle_with_no_query(): """Testing handle controller method""" find_user_use_case = FindUserSpy(UserRepositorySpy) find_user_controller = FindUserController(find_user_use_case) http_request = HttpRequest() response = find_user_controller.handle(http_request) # Testing responses assert find_user_use_case.by_id_and_name_param == {} assert find_user_use_case.by_id_param == {} assert find_user_use_case.by_name_param == {} assert response.status_code == 400 assert "error" in response.body
def test_route(): """ Testing route method in RegisterUserController """ register_user_use_case = RegisterUserSpy(UserRepositorySpy()) register_user_router = RegisterUserController(register_user_use_case) attributes = {"name": faker.word(), "password": faker.word()} response = register_user_router.route(HttpRequest(body=attributes)) # Testing input assert register_user_use_case.register_param["name"] == attributes["name"] assert register_user_use_case.register_param["password"] == attributes[ "password"] # Testing output assert response.status_code == 200 assert "error" not in response.body
def test_route_by_pet_id(): """ Testing route method in FindPetController """ find_pet_use_case = FindPetSpy(PetRepositorySpy()) find_pet_router = FindPetController(find_pet_use_case) attributes = {"pet_id": faker.random_number(digits=2)} http_request = HttpRequest(query=attributes) http_response = find_pet_router.route(http_request) # Testing input assert find_pet_use_case.by_pet_id_param["pet_id"] == attributes["pet_id"] # Testing output assert http_response.status_code == 200 assert "error" not in http_response.body
def test_route_error_no_query(): """ Testing route method in FindPetController """ find_pet_use_case = FindPetSpy(PetRepositorySpy()) find_pet_router = FindPetController(find_pet_use_case) http_request = HttpRequest() http_response = find_pet_router.route(http_request) # Testing input assert find_pet_use_case.by_pet_id_param == {} assert find_pet_use_case.by_user_id_param == {} assert find_pet_use_case.by_pet_id_and_user_id_param == {} # Testing output assert http_response.status_code == 400 assert "error" in http_response.body
def test_route_error_wrong_query(): """ Testing route method in FindPetController """ find_pet_use_case = FindPetSpy(PetRepositorySpy()) find_pet_router = FindPetController(find_pet_use_case) http_request = HttpRequest( query={"something": faker.random_number(digits=2)}) http_response = find_pet_router.route(http_request) # Testing input assert find_pet_use_case.by_pet_id_param == {} assert find_pet_use_case.by_user_id_param == {} assert find_pet_use_case.by_pet_id_and_user_id_param == {} # Testing output assert http_response.status_code == 422 assert "error" in http_response.body
def test_route_query_params_error(): """ Testing Route method """ find_user_use_case = FindUserSpy(UserRepositorySpy()) find_user_controller = FindUserController(find_user_use_case) http_request = HttpRequest(query={ "id": faker.random_number(), "name": faker.word() }) response = find_user_controller.route(http_request) # Testing Inputs assert find_user_use_case.by_id_and_name_param == {} assert find_user_use_case.by_id_param == {} assert find_user_use_case.by_name_param == {} # Testing Output assert response.status_code == 422 assert "error" in response.body
def test_handle(): """ Testing Handle method """ find_user_use_case = FindUserSpy(UserRepositorySpy()) find_user_controller = FindUserController(find_user_use_case) http_request = HttpRequest(query={ "user_id": faker.random_number(), "user_name": faker.word() }) response = find_user_controller.route(http_request) # Testing Inputs assert (find_user_use_case.by_id_and_name_param["user_id"] == http_request.query["user_id"]) assert (find_user_use_case.by_id_and_name_param["name"] == http_request.query["user_name"]) # Testing Output assert response.status_code == 200 assert response.body
def flask_adapter(request: any, api_route: Type[Route]) -> any: """Adapter pattern to Flask :param - Flask Request :api_route: Composite Routes """ try: query_string_params = request.args.to_dict() if "pet_id" in query_string_params.keys(): query_string_params["pet_id"] = int(query_string_params["pet_id"]) if "user_id" in query_string_params.keys(): query_string_params["user_id"] = int( query_string_params["user_id"]) except: http_error = HttpErrors.error_400() return HttpResponse(status_code=http_error["status_code"], body=http_error["body"]) http_request = HttpRequest(header=request.headers, body=request.json, query=query_string_params) try: response = api_route.route(http_request) except IntegrityError: http_error = HttpErrors.error_409() return HttpResponse(status_code=http_error["status_code"], body=http_error["body"]) except Exception as exc: print(exc) http_error = HttpErrors.error_500() return HttpResponse(status_code=http_error["status_code"], body=http_error["body"]) return response