Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mattyweb/issue976 #981

Merged
merged 4 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions server/api/code/lacity_data_api/models/service_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async def get_open_request_counts():


async def get_filtered_requests(
start_date: datetime.date,
start_date: datetime.date = None,
end_date: datetime.date = None,
type_ids: List[int] = None,
council_ids: List[int] = None,
Expand All @@ -174,21 +174,27 @@ async def get_filtered_requests(
from .council import Council # noqa ... avoiding circular import
from .agency import Agency # noqa ... avoiding circular import

if include_updated:
where_text = f"(created_date >= '{start_date}' OR closed_date >= '{start_date}')" # noqa
else:
where_text = f"created_date >= '{start_date}'"
where_text = []

if (start_date):
if include_updated:
where_text.append(f"(created_date >= '{start_date}' OR closed_date >= '{start_date}')") # noqa
else:
where_text.append(f"created_date >= '{start_date}'")

if (end_date):
# Making end date inclusive by adding one
end_date = end_date + datetime.timedelta(days=1)

if include_updated:
where_text = f"(created_date >= '{end_date}' OR closed_date >= '{end_date}')" # noqa
where_text.append(f"(created_date <= '{end_date}' OR closed_date <= '{end_date}')") # noqa
else:
where_text = f"created_date >= '{end_date}'"
where_text.append(f"created_date <= '{end_date}'")

if (type_ids):
where_text += f" AND service_requests.type_id IN ({', '.join([str(i) for i in type_ids])})" # noqa
where_text.append(f"service_requests.type_id IN ({', '.join([str(i) for i in type_ids])})") # noqa
if (council_ids):
where_text += f" AND service_requests.council_id IN ({', '.join([str(i) for i in council_ids])})" # noqa
where_text.append(f"service_requests.council_id IN ({', '.join([str(i) for i in council_ids])})") # noqa

async with db.transaction():
cursor = await (
Expand All @@ -211,7 +217,7 @@ async def get_filtered_requests(
Source, ServiceRequest.source_id == Source.source_id
)
).where(
text(where_text)
text(" AND ".join(where_text))
).order_by(
desc(ServiceRequest.created_date)
).gino.iterate()
Expand Down
13 changes: 8 additions & 5 deletions server/api/code/lacity_data_api/routers/service_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Optional

from sqlalchemy import sql
from fastapi import APIRouter, HTTPException
from fastapi import APIRouter, HTTPException, Query

from ..models import api_models as schemas
from ..models.service_request import (
Expand All @@ -16,14 +16,15 @@
router = APIRouter()


# TODO: need more tests
@router.get("", response_model=schemas.ServiceRequestList)
async def get_all_service_requests(
start_date: datetime.date = datetime.date.today() - datetime.timedelta(days=7),
start_date: datetime.date = None,
end_date: datetime.date = None,
type_id: Optional[int] = None,
council_id: Optional[int] = None,
skip: int = 0,
limit: int = 100000
limit: int = Query(1000, le=100000)
):
type_ids = []
council_ids = []
Expand All @@ -45,14 +46,16 @@ async def get_all_service_requests(
return result


# TODO: need more tests
# TODO #982 need to make sure this is filtering properly
@router.get("/updated", response_model=schemas.ServiceRequestList)
async def get_updated_service_requests(
start_date: datetime.date = datetime.date.today() - datetime.timedelta(days=7),
start_date: datetime.date = None,
end_date: datetime.date = None,
type_id: Optional[int] = None,
council_id: Optional[int] = None,
skip: int = 0,
limit: int = 100000
limit: int = Query(1000, le=100000)
):
type_ids = []
council_ids = []
Expand Down
18 changes: 9 additions & 9 deletions server/api/tests/integration/test_api_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ def test_service_requests(client):
url = "/requests"
response = client.get(url)
assert response.status_code == 200
assert len(response.json()) == 0
assert len(response.json()) == 1000


def test_service_requests_start(client):
url = "/requests?start_date=2020-01-01"
url = "/requests?start_date=2020-01-01&limit=10000"
response = client.get(url)
assert response.status_code == 200
assert len(response.json()) == 9989


def test_service_requests_end(client):
url = "/requests?start_date=2020-01-01&end_date=2020-01-02"
url = "/requests?start_date=2020-01-01&end_date=2020-01-02&limit=10000"
response = client.get(url)
assert response.status_code == 200
assert len(response.json()) == 8304
assert len(response.json()) == 7650


def test_service_requests_type(client):
Expand All @@ -31,28 +31,28 @@ def test_service_requests_council(client):
url = "/requests?start_date=2020-01-01&end_date=2020-01-02&council_id=27"
response = client.get(url)
assert response.status_code == 200
assert len(response.json()) == 55
assert len(response.json()) == 39


def test_service_requests_all(client):
url = "/requests?start_date=2020-01-01&end_date=2020-01-02&type_id=2&council_id=27"
response = client.get(url)
assert response.status_code == 200
assert len(response.json()) == 6
assert len(response.json()) == 3


def test_updated_service_requests(client):
url = "/requests/updated?start_date=2020-01-01&end_date=2020-01-02"
url = "/requests/updated?start_date=2020-01-01&end_date=2020-01-02&limit=10000"
response = client.get(url)
assert response.status_code == 200
assert len(response.json()) == 9792
assert len(response.json()) == 7650


def test_updated_service_requests_council(client):
url = "/requests/updated?start_date=2020-01-01&end_date=2020-01-02&council_id=27"
response = client.get(url)
assert response.status_code == 200
assert len(response.json()) == 64
assert len(response.json()) == 39


def test_service_request(client):
Expand Down
72 changes: 36 additions & 36 deletions server/api/tests/integration/test_api_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,42 @@
"""


def test_map_pins(client):
# post old style filters (i.e. text request types)
url = "/map/pins"
response = client.post(
url,
json={
"startDate": "01/01/2020",
"endDate": "01/02/2020",
"ncList": [
9,
14,
16,
32,
37,
38,
40,
41,
42,
47,
50,
62,
63,
68,
91,
92,
96
],
"requestTypes": [
"Dead Animal Removal",
"Homeless Encampment",
"Graffiti Removal"
]
}
)
assert response.status_code == 200
assert len(response.json()) == 662
# def test_map_pins(client):
# # post old style filters (i.e. text request types)
# url = "/map/pins"
# response = client.post(
# url,
# json={
# "startDate": "01/01/2020",
# "endDate": "01/02/2020",
# "ncList": [
# 9,
# 14,
# 16,
# 32,
# 37,
# 38,
# 40,
# 41,
# 42,
# 47,
# 50,
# 62,
# 63,
# 68,
# 91,
# 92,
# 96
# ],
# "requestTypes": [
# "Dead Animal Removal",
# "Homeless Encampment",
# "Graffiti Removal"
# ]
# }
# )
# assert response.status_code == 200
# assert len(response.json()) == 662


def test_open_requests(client):
Expand Down