def test_route_builder_with_owner_removes_owner_field(): router = APIRouter() route_builder = RouteBuilder(models.SimpleModelWithOwner, owner_field="owner", authentication=lambda: None) route_builder.add_all_routes(router) assert "title" in route_builder.new_instance_schema.schema().keys() assert "owner" not in route_builder.new_instance_schema.schema().keys() assert "title" in route_builder.instance_schema.schema().keys() assert "owner" not in route_builder.instance_schema.schema().keys()
def get_client(app, router): route_builder = RouteBuilder( SimpleModel, config={"identifier": "uuid", "identifier_class": UUID}, ) route_builder.add_all_routes(router) app.include_router(router) return TestClient(app)
def test_route_builder_get_identifier_function(): simple_model = models.SimpleModel.objects.create(name="SimpleModel") route_builder = RouteBuilder(models.SimpleModel) identifier_function = route_builder.get_identifier_function() assert identifier_function(simple_model.id) == simple_model
def get_client(app, router): config = {"identifier": "uuid", "identifier_class": UUID} route_builder = RouteBuilder( UnauthenticatedModel, config=config, ) route_builder.add_all_routes(router) app.include_router(router) return TestClient(app)
def get_client(app, router): def filter_by_name(_): return Q(name__contains="XXX") route_builder = RouteBuilder(SimpleModel, request_fields=["name"], response_fields=["name"], query_filter=filter_by_name) route_builder.add_all_routes(router) app.include_router(router) return TestClient(app)
def get_client(app, router): config = {"identifier": "uuid", "identifier_class": UUID} route_builder = RouteBuilder( SimpleModelWithOwner, request_fields=["name"], response_fields=["name", "uuid"], config=config, owner_field="owner", authentication=check_api_key, ) route_builder.add_all_routes(router) app.include_router(router) return TestClient(app)
def get_client(app, router): config = {"identifier": "uuid", "identifier_class": UUID} route_builder1 = RouteBuilder(models.Pizza, config=config) route_builder1.add_all_routes(router) route_builder2 = RouteBuilder(models.Topping, config=config) route_builder2.add_all_routes(router) app.include_router(router) return TestClient(app)
def get_client(router): authentication = get_user_authentication() route_builder = RouteBuilder( SimpleJWTModel, request_fields=["name"], response_fields=["name", "uuid"], config={ "identifier": "uuid", "identifier_class": UUID }, authentication=authentication, ) route_builder.add_all_routes(router) application.include_router(router) return TestClient(application)
def test_bad_json_field_with_no_default(): with pytest.raises(UnSupportedFieldException) as exception: RouteBuilder(BadJSONModelWithNoDefault) assert str(exception.value) == ( "Field test_app.BadJSONModelWithNoDefault.data with no default " "is not supported, try using 'projectx.common.fields.JSONDefaultField'" )
def test_route_builder_adds_routes(): router = APIRouter() route_builder = RouteBuilder(models.SimpleModel) route_builder.add_all_routes(router) assert len(router.routes) == 6 assert router.routes[0].path == "/simplemodels/" assert router.routes[1].path == "/simplemodels/{identifier}/" assert router.routes[2].path == "/simplemodels/" assert router.routes[3].path == "/simplemodels/{identifier}/" assert router.routes[4].path == "/simplemodels/{identifier}/" assert router.routes[5].path == "/simplemodels/{identifier}/" assert router.routes[0].methods == {"GET"} assert router.routes[1].methods == {"GET"} assert router.routes[2].methods == {"POST"} assert router.routes[3].methods == {"PUT"} assert router.routes[4].methods == {"PATCH"} assert router.routes[5].methods == {"DELETE"}
def get_client(app, router): route_builder = RouteBuilder(SimpleModelWithChoices) route_builder.add_all_routes(router) app.include_router(router) application = FastAPI( title="ProjectX", description="ProjectX Demo", version="0.0.1", docs_url="/api/docs", redoc_url="/api/redoc", openapi_url="/api/v1/openapi.json", ) application.include_router(router, prefix="/api") application.openapi() return TestClient(app), route_builder
def get_client(request, app, router): config = request.param["config"] route_builder = RouteBuilder(request.param["model"], config=config) route_builder.add_all_routes(router) app.include_router(router) return TestClient(app), route_builder
def test_route_builder_invalid_fields(): with pytest.raises(InvalidFieldsException) as invalid_ex: RouteBuilder(models.SimpleModel, request_fields=["invalid"]) assert str(invalid_ex.value) == "['invalid'] not in ['config', 'created', 'id', 'last_updated', 'name', 'uuid']"
def test_route_builder_owner_field_but_no_auth(): with pytest.raises(InvalidAuthenticationException) as invalid_ex: RouteBuilder(models.SimpleModel, owner_field="user_field") assert str(invalid_ex.value) == "If you specify an owner field, you must have set the authentication function."
def test_route_builder_bad_identifier_field(): with pytest.raises(InvalidIdentifierException) as invalid_ex: RouteBuilder(models.SimpleModel, config={"identifier": "BAD_FIELD"}) assert str(invalid_ex.value) == "BAD_FIELD not in ['id', 'uuid', 'name', 'last_updated', 'created', 'config']."
def test_good_json_field(): assert RouteBuilder(GoodJSONModel)
def get_client(app, router): route_builder = RouteBuilder(SimpleModelWithDefaultFields) route_builder.add_all_routes(router) app.include_router(router) return TestClient(app), route_builder
def get_client(app, router): route_builder = RouteBuilder(ModelWithValidation) route_builder.add_all_routes(router) app.include_router(router) return TestClient(app), route_builder