예제 #1
0
def test_cluster_run(owner_store, pet_store):
    owner_endpoint = Resource(owner_store, Owner)
    pet_endpoint = Resource(pet_store, Pet)

    manager = APIManager({"owners": owner_endpoint, "pets": pet_endpoint})

    client = TestClient(manager.app)

    assert client.get("/").status_code == 404
    payload = {"name_eq": "Person1", "limit": 10, "all_fields": True}

    res = dynamic_model_search_response_helper(client=client,
                                               payload=payload,
                                               base="/owners/?")
    data, meta, err = get_fields(res)
    assert res.status_code == 200
    assert len(data) == 1
    assert data[0]["name"] == "Person1"

    payload = {"name_eq": "Pet1", "limit": 10, "all_fields": True}

    res = dynamic_model_search_response_helper(client=client,
                                               payload=payload,
                                               base="/pets/?")
    data, meta, err = get_fields(res)
    assert res.status_code == 200
    assert len(data) == 1
    assert data[0]["name"] == "Pet1"
예제 #2
0
def test_init_resource(owner_store):
    num_default_api_routes = 2
    endpoint = Resource(owner_store, Owner)
    assert len(endpoint.router.routes) == num_default_api_routes
    assert endpoint.router.routes[0]

    endpoint = Resource(owner_store, "tests.api.test_resource.Owner")
    assert len(endpoint.router.routes) == num_default_api_routes
    assert endpoint.router.routes[0]
예제 #3
0
def test_resource_msonable(owner_store):
    endpoint = Resource(owner_store, Owner)
    endpoint_dict = endpoint.as_dict()

    for k in ["@class", "@module", "store", "model"]:
        assert k in endpoint_dict

    assert isinstance(endpoint_dict["model"], str)
    assert endpoint_dict["model"] == "tests.api.test_resource.Owner"
예제 #4
0
def test_cluster_pprint(owner_store, pet_store):
    endpoint_main = Resource(owner_store, Owner, description="main")
    endpoint_main_temp = Resource(pet_store, Pet, description="main_temp")
    endpoint_temp = Resource(owner_store, Owner, description="temp")

    manager = APIManager({
        "/temp": endpoint_temp,
        "/main": endpoint_main,
        "/main/temp": endpoint_main_temp,
    })

    res = manager.sort()

    assert res == ["/main", "/main/temp", "/temp"]
예제 #5
0
def test_cluster_dict_behavior(owner_store, pet_store):
    owner_endpoint = Resource(owner_store, Owner)
    pet_endpoint = Resource(pet_store, Pet)

    manager = APIManager({"owners": owner_endpoint})

    assert manager["owners"] == owner_endpoint  # test __getitem__
    assert len(manager) == 1  # test len
    assert "owners" in manager.keys()  # test keys
    assert "owners" in manager  # test __contains__
    assert "pets" not in manager.keys()  # test __keys__

    manager["pets"] = pet_endpoint  # test __setitem__
    assert len(manager) == 2
    assert manager["pets"] == pet_endpoint
예제 #6
0
def test_resource_dynamic_model_search_lt_gt(owner_store):
    endpoint = Resource(owner_store, Owner)
    app = FastAPI()
    app.include_router(endpoint.router)

    client = TestClient(app)
    payload = {
        "age_lt":
        101  # see if there are 10 entries, all age should be less than 100
    }

    res = dynamic_model_search_response_helper(client=client,
                                               payload=payload,
                                               base="/?")
    data, meta, err = get_fields(res)

    assert res.status_code == 200
    assert len(data) == 10

    payload = {
        "age_gt": 0
    }  # there should be 10 entries, all age are greater than 10
    res = dynamic_model_search_response_helper(client=client,
                                               payload=payload,
                                               base="/?")
    data, meta, err = get_fields(res)
    assert res.status_code == 200
    assert len(data) == 10
예제 #7
0
def test_resource_dynamic_model_search_eq_noteq(owner_store):
    endpoint = Resource(owner_store, Owner)
    app = FastAPI()
    app.include_router(endpoint.router)

    client = TestClient(app)
    payload = {"name_eq": "PersonAge9", "all_fields": True}
    res = dynamic_model_search_response_helper(client=client,
                                               payload=payload,
                                               base="/?")
    data, meta, err = get_fields(res)

    # String eq
    assert res.status_code == 200
    assert len(data) == 1
    assert data[0]["name"] == "PersonAge9"

    # int Eq
    assert data[0]["age"] == 9

    # float Eq
    payload = {"weight_eq": 150.0, "all_fields": True}
    res = dynamic_model_search_response_helper(client=client,
                                               payload=payload,
                                               base="/?")
    data, meta, err = get_fields(res)
    assert res.status_code == 200
    assert len(data) == 1
    assert data[0]["weight"] == 150.0

    # Str Not eq
    payload = {"name_not_eq": "PersonAge9", "all_fields": True}
    res = dynamic_model_search_response_helper(client=client,
                                               payload=payload,
                                               base="/?")
    data, meta, err = get_fields(res)
    assert res.status_code == 200
    assert len(data) == 10

    # int Not Eq
    payload = {"age_not_eq": 9, "all_fields": True}
    res = dynamic_model_search_response_helper(client=client,
                                               payload=payload,
                                               base="/?")
    data, meta, err = get_fields(res)
    assert res.status_code == 200
    assert len(data) == 10

    # float Not Eq
    payload = {"weight_not_eq": 150.0, "all_fields": True}
    res = dynamic_model_search_response_helper(client=client,
                                               payload=payload,
                                               base="/?")
    data, meta, err = get_fields(res)
    assert res.status_code == 200
    assert len(data) == 10
예제 #8
0
def test_resource_get_by_key(owner_store):
    endpoint = Resource(owner_store, Owner)
    app = FastAPI()
    app.include_router(endpoint.router)

    client = TestClient(app)

    assert client.get("/").status_code == 200

    assert client.get("/Person1/").status_code == 200
    assert client.get("/Person1/").json()["data"][0]["name"] == "Person1"
예제 #9
0
def test_resource_no_explicit_eq(owner_store):
    endpoint = Resource(owner_store, Owner)
    app = FastAPI()
    app.include_router(endpoint.router)

    client = TestClient(app)

    payload = {"name": "PersonAge9", "all_fields": True}
    res = dynamic_model_search_response_helper(client=client,
                                               payload=payload,
                                               base="/?",
                                               debug=True)
    data, meta, err = get_fields(res)

    # String eq with no explicit eq
    assert res.status_code == 200
    assert len(data) == 1
    assert data[0]["name"] == "PersonAge9"
예제 #10
0
def test_resource_dynamic_model_search_in_notin(owner_store):
    endpoint = Resource(owner_store, Owner)
    app = FastAPI()
    app.include_router(endpoint.router)

    client = TestClient(app)

    # In
    url = "/?age_in=9&age_in=101&all_fields=true&limit=10"
    res = client.get(url)
    data, meta, err = get_fields(res)
    assert res.status_code == 200
    assert len(data) == 1

    # Not In
    url = "/?limit=10&all_fields=true&age_not_in=9&age_not_in=10"
    res = client.get(url)
    data, meta, err = get_fields(res)
    assert res.status_code == 200
    assert len(data) == 10
예제 #11
0
def test_resource_compound(owner_store):
    endpoint = Resource(owner_store, Owner)
    app = FastAPI()
    app.include_router(endpoint.router)

    client = TestClient(app)

    payload = {
        "name": "PersonAge20Weight200",
        "all_fields": True,
        "weight": 200,
        "age": 20,
    }
    res = dynamic_model_search_response_helper(client=client,
                                               payload=payload,
                                               base="/?",
                                               debug=True)
    data, meta, err = get_fields(res)

    assert res.status_code == 200
    assert len(data) == 1
    assert data[0]["name"] == "PersonAge20Weight200"

    payload = {
        "name": "PersonAge20Weight200",
        "all_fields": False,
        "fields": "name, age",
        "weight": 200,
        "age": 20,
    }
    res = dynamic_model_search_response_helper(client=client,
                                               payload=payload,
                                               base="/?",
                                               debug=True)
    data, meta, err = get_fields(res)
    assert res.status_code == 200
    assert len(data) == 1
    assert data[0]["name"] == "PersonAge20Weight200"
    assert data[0]["age"] == 20
    assert "weight" not in data[0]