Skip to content

Commit

Permalink
Updated acceptance tests for exclude-field and used FastAPI TestClient
Browse files Browse the repository at this point in the history
  • Loading branch information
feba-rajan committed Aug 1, 2024
1 parent 685146c commit cf1f153
Showing 1 changed file with 162 additions and 41 deletions.
203 changes: 162 additions & 41 deletions tests/acceptance/parameters/test_exclude_field_parameter.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,79 @@
"""
Module to test how we anticipate the exclude_field parameter. This parameter was primarily
requested so that larger unnessary fields can be excluded (mainly geometry but will apply
to others too). although the field parameter can be used it rwquires you to know the specific
fields for every dataset. This allows you to still get those but always exclude a field
We do not expect the exclude field to have any effect on the html so we use test client instead
of server url for testing as it's easier and more efficient.
"""

import pytest
import requests
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from application.db.models import EntityOrm


@pytest.mark.parametrize(
"exclude_field, expected_fields",
"exclude, expected_fields",
[
(
["geometry"],
["organisation-entity"],
{
"entry-date",
"start-date",
"end-date",
"entity",
"name",
"dataset",
"typology",
"reference",
"prefix",
"organisation-entity",
"point",
"notes",
},
),
(
["point", "geometry"],
["entry-date", "prefix"],
{
"entry-date",
"start-date",
"end-date",
"entity",
"name",
"dataset",
"typology",
"reference",
"prefix",
"organisation-entity",
"notes",
"point",
},
),
],
)
def test_entity_search_excludes_field_for_json_response(
client, db_session, exclude_field, expected_fields
def test_entity_search_exclude_field_for_json_response(
app_test_data: dict,
client: TestClient,
app_db_session: Session,
exclude: list,
expected_fields: set,
):
"""
When searching across entities can we remove fields form the entities using the exclude_fields
parameter
"""
params = {"exclude_field": ",".join(exclude_field)} if exclude_field else {}
params = {"exclude_field": ",".join(exclude)} if exclude else {}

# TODO entities need loading into the databse use the db_session to add data
# Clear existing data
app_db_session.query(EntityOrm).delete()
app_db_session.commit()

# TODO change below to using a Fast API test client instead
response = requests.get(f"{client}/entity.json", params=params)
# Load entities into the database
for entity_data in app_test_data["entities"]:
app_db_session.add(EntityOrm(**entity_data))
app_db_session.commit()

response = client.get("/entity.json", params=params)
response_json = response.json()
entities = response_json.get("entities", [])

assert isinstance(entities, list), "Expected 'entities' to be a list"
assert (
len(entities) > 0
), "No entities being returned so cannot check that the fields are correct"

for entity in entities:
# Check if excluded fields are not present
for field in exclude:
assert (
field not in entity
), f"Field '{field}' should not be in entity: {entity}"

# Check if expected fields are present
for field in expected_fields:
assert (
field in entity
), f"Expected field '{field}' not found in entity: {entity}"
for field in exclude_field:
assert (
field not in entity
), f"Field '{field}' should not be in entity: {entity}"


@pytest.mark.parametrize(
Expand All @@ -92,15 +90,23 @@ def test_entity_search_excludes_field_for_json_response(
"organisation-entity",
"geometry",
"point",
"notes",
}
],
)
def test_get_entity_json_no_exclude_field(client, db_session, expected_fields):
# TODO entities need loading into the databse use the db_session to add data
def test_entity_search_no_exclude_field_for_json_response(
app_test_data: dict,
client: TestClient,
app_db_session: Session,
expected_fields: set,
):
app_db_session.query(EntityOrm).delete()
app_db_session.commit()
# Load entities into the database
for entity_data in app_test_data["entities"]:
app_db_session.add(EntityOrm(**entity_data))
app_db_session.commit()

# TODO change below to using a Fast API test client instead
response = requests.get(f"{client}/entity.json")
response = client.get("/entity.json")
assert response.status_code == 200

response_json = response.json()
Expand All @@ -114,3 +120,118 @@ def test_get_entity_json_no_exclude_field(client, db_session, expected_fields):
assert (
field in entity
), f"Expected field '{field}' not found in entity: {entity}"


@pytest.mark.parametrize(
"exclude, expected_properties",
[
(
["notes"],
{
"entry-date",
"start-date",
"end-date",
"entity",
"name",
"dataset",
"typology",
"reference",
"prefix",
"organisation-entity",
},
),
(
["prefix", "notes"],
{
"entry-date",
"start-date",
"end-date",
"entity",
"name",
"dataset",
"typology",
"reference",
"organisation-entity",
},
),
],
)
def test_entity_search_exclude_field_for_geojson_response(
app_test_data: dict,
client: TestClient,
app_db_session: Session,
exclude: list,
expected_properties: set,
):
app_db_session.query(EntityOrm).delete()
app_db_session.commit()
# Load entities into the database
for feature_properties in app_test_data["entities"]:
app_db_session.add(EntityOrm(**feature_properties))
app_db_session.commit()

# Make the request with the exclude parameters
params = {"exclude": ",".join(exclude)} if exclude else {}
response = client.get("/entity.geojson", params=params)
response_json = response.json()
entities = response_json.get("entities", [])
assert isinstance(entities, list), "Expected 'features' to be a list"

# Check each feature
for feature in entities:
properties = feature.get("properties", {})
for field in exclude:
assert (
field not in properties
), f"Property '{field}' should not be in feature properties: {properties}"
for field in expected_properties:
assert (
field in properties
), f"Expected property '{field}' not found in feature properties: {properties}"


@pytest.mark.parametrize(
"expected_properties",
[
(
{
"entry-date",
"start-date",
"end-date",
"entity",
"name",
"dataset",
"typology",
"reference",
"prefix",
"organisation-entity",
"notes",
},
)
],
)
def test_entity_search_no_exclude_field_for_geojson_response(
app_test_data: dict,
client: TestClient,
app_db_session: Session,
expected_properties: set,
):
app_db_session.query(EntityOrm).delete()
app_db_session.commit()
# Load entities into the database
for feature_properties in app_test_data["entities"]:
app_db_session.add(EntityOrm(**feature_properties))
app_db_session.commit()

response = client.get("/entity.geojson")
response_json = response.json()
entities = response_json.get("entities", [])
assert isinstance(entities, list), "Expected 'features' to be a list"

# Check each feature
for feature in entities:
properties = feature.get("properties", {})
for field in expected_properties:
assert (
field in properties
), f"Expected property '{field}'not found in feature properties: {properties}"

0 comments on commit cf1f153

Please sign in to comment.