How to use the fastapi.testclient.TestClient function in fastapi

To help you get started, we’ve selected a few fastapi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github QwantResearch / idunn / tests / test_recycling.py View on Github external
def test_no_recycling_in_bretagne_poi():
    """
    Check that no trash info is provided for a POI in bretagne
    """
    client = TestClient(app)
    response = client.get(url=f"http://localhost/v1/pois/osm:node:36153811")

    assert response.status_code == 200

    resp = response.json()

    assert resp["id"] == "osm:node:36153811"
    assert resp["name"] == "Multiplexe Liberté"
    assert len([x for x in resp["blocks"] if x["type"] == "recycling"]) == 0
github QwantResearch / idunn / tests / test_directions.py View on Github external
def test_direction_car(mock_directions_car):
    client = TestClient(app)
    response = client.get(
        "http://localhost/v1/directions/2.3402355%2C48.8900732%3B2.3688579%2C48.8529869",
        params={"language": "fr", "type": "driving", "exclude": "ferry"},
    )

    assert response.status_code == 200

    response_data = response.json()
    assert response_data["status"] == "success"
    assert len(response_data["data"]["routes"]) == 3
    assert all(r["geometry"] for r in response_data["data"]["routes"])
    assert response_data["data"]["routes"][0]["duration"] == 1819
    assert len(response_data["data"]["routes"][0]["legs"]) == 1
    assert len(response_data["data"]["routes"][0]["legs"][0]["steps"]) == 10
    assert response_data["data"]["routes"][0]["legs"][0]["mode"] == "CAR"
    assert "exclude=ferry" in mock_directions_car.calls[0].request.url
github QwantResearch / idunn / tests / test_api.py View on Github external
def test_contact_phone():
    """
    The louvre museum has the tag 'contact:phone'
    We test this tag is correct here
    """
    client = TestClient(app)
    response = client.get(url=f"http://localhost/v1/pois/osm:relation:7515426")

    assert response.status_code == 200

    resp = response.json()

    assert resp["id"] == "osm:relation:7515426"
    assert resp["name"] == "Louvre Museum"
    assert resp["local_name"] == "Musée du Louvre"
    assert resp["class_name"] == "museum"
    assert resp["subclass_name"] == "museum"
    assert resp["blocks"][1]["type"] == "phone"
    assert resp["blocks"][1]["url"] == "tel:+33140205229"
    assert resp["blocks"][1]["international_format"] == "+33 1 40 20 52 29"
    assert resp["blocks"][1]["local_format"] == "01 40 20 52 29"
github QwantResearch / idunn / tests / test_places.py View on Github external
def test_redirect_obsolete_address_with_lat_lon():
    client = TestClient(app)
    response = client.get(
        url=f"http://localhost/v1/places/addr:-1.12;45.6?lang=fr", allow_redirects=False
    )
    assert response.status_code == 303
    assert response.headers["location"] == "/v1/places/latlon:45.60000:-1.12000?lang=fr"
github QwantResearch / idunn / tests / test_directions.py View on Github external
def test_directions_public_transport_restricted_areas():
    client = TestClient(app)

    # Paris - South Africa
    response = client.get(
        "http://localhost/v1/directions/2.3211757,48.8604893;22.1741215,-33.1565800",
        params={"language": "fr", "type": "publictransport"},
    )
    assert response.status_code == 422

    # Pekin
    response = client.get(
        "http://localhost/v1/directions/116.2945000,39.9148800;116.4998847,39.9091405",
        params={"language": "fr", "type": "publictransport"},
    )
    assert response.status_code == 422

    #  Washington - New York
github QwantResearch / idunn / tests / test_full.py View on Github external
def test_full(mock_external_requests):
    """
    Exhaustive test that checks all possible blocks
    """
    client = TestClient(app)
    response = client.get(url=f"http://localhost/v1/pois/osm:way:7777778?lang=es")

    assert response.status_code == 200

    resp = response.json()

    assert resp == {
        "type": "poi",
        "id": "osm:way:7777778",
        "name": "Fako Allo",
        "local_name": "Fake All",
        "class_name": "museum",
        "subclass_name": "museum",
        "geometry": {
            "coordinates": [2.3250037768187326, 48.86618482685007],
            "type": "Point",
github QwantResearch / idunn / tests / test_directions.py View on Github external
def test_directions_rate_limiter(mock_directions_car_with_rate_limiter):
    client = TestClient(app)
    # rate limiter is triggered after 30 req/min by default
    for i in range(40):
        response = client.get(
            "http://localhost/v1/directions/" "2.3402355%2C48.8900732%3B2.3688579%2C48.8529869",
            params={"language": "fr", "type": "driving"},
        )
    assert response.status_code == 429
github QwantResearch / idunn / tests / test_api.py View on Github external
def test_options_requests_disabled():
    client = TestClient(app)
    response = client.options(url=f"http://localhost/v1/places/35460343")

    assert response.status_code == 405
github QwantResearch / idunn / tests / test_cache.py View on Github external
def test_wikidata_cache(cache_test_normal, basket_ball_wiki_es, monkeypatch):
    """
    We test the cache for the Wikidata ES
    """
    client = TestClient(app)

    with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
        """
        We mock all wikipedia requests since
        the information are expected to be in
        the Wiki ES
        """
        rsps.add("GET", re.compile(r"^https://.*\.wikipedia.org/"), status=200)

        response = client.get(url=f"http://localhost/v1/pois/osm:way:7777777?lang=fr")

        assert response.status_code == 200
        resp = response.json()
        """
        We should have a "wikipedia" block in the
        answer
github QwantResearch / idunn / tests / test_categories.py View on Github external
def test_endpoint_categories():
    """
        Test the endpoint 'categories':
    """
    client = TestClient(app)

    response = client.get(url=f"http://localhost/v1/categories")

    assert response.status_code == 200

    resp = response.json()
    categories = resp["categories"]

    assert len(categories) == 12
    assert categories[0] == {"name": "restaurant", "raw_filters": ["restaurant,*", "fast_food,*"]}
    assert categories[1] == {"name": "hotel", "raw_filters": ["*,hotel"]}