Skip to content

Commit

Permalink
test for navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
yu23ki14 committed Aug 17, 2024
1 parent 6d2c980 commit 94c6cb9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
16 changes: 5 additions & 11 deletions api/birdxplorer_api/routers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
LanguageIdentifier,
Note,
NoteId,
PaginationMeta,
ParticipantId,
Post,
PostId,
Topic,
TopicId,
TwitterTimestamp,
UserEnrollment,
PaginationMeta
)
from birdxplorer_common.storage import Storage

Expand Down Expand Up @@ -98,7 +98,7 @@ def get_posts(
created_at_start: Union[None, TwitterTimestamp, str] = Query(default=None),
created_at_end: Union[None, TwitterTimestamp, str] = Query(default=None),
offset: int = Query(default=0, ge=0),
limit: int = Query(default=100, gt=0, le=1000)
limit: int = Query(default=100, gt=0, le=1000),
) -> PostListResponse:
posts = None

Expand All @@ -122,8 +122,8 @@ def get_posts(
posts = list(storage.get_posts())

total_count = len(posts)
paginated_posts = posts[offset:offset + limit]
base_url = str(request.url).split('?')[0]
paginated_posts = posts[offset : offset + limit]
base_url = str(request.url).split("?")[0]
next_offset = offset + limit
prev_offset = max(offset - limit, 0)
next_url = None
Expand All @@ -133,12 +133,6 @@ def get_posts(
if offset > 0:
prev_url = f"{base_url}?offset={prev_offset}&limit={limit}"

return PostListResponse(
data=paginated_posts,
meta=PaginationMeta(
next=next_url,
prev=prev_url
)
)
return PostListResponse(data=paginated_posts, meta=PaginationMeta(next=next_url, prev=prev_url))

return router
34 changes: 25 additions & 9 deletions api/tests/routers/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def test_posts_get(client: TestClient, post_samples: List[Post]) -> None:
response = client.get("/api/v1/data/posts")
assert response.status_code == 200
res_json = response.json()
assert res_json == {"data": [json.loads(d.model_dump_json()) for d in post_samples]}
assert res_json == {
"data": [json.loads(d.model_dump_json()) for d in post_samples],
"meta": {"next": None, "prev": None},
}


def test_posts_get_has_post_id_filter(client: TestClient, post_samples: List[Post]) -> None:
Expand All @@ -35,57 +38,70 @@ def test_posts_get_has_post_id_filter(client: TestClient, post_samples: List[Pos
"data": [
json.loads(post_samples[0].model_dump_json()),
json.loads(post_samples[2].model_dump_json()),
]
],
"meta": {"next": None, "prev": None},
}


def test_posts_get_has_note_id_filter(client: TestClient, post_samples: List[Post], note_samples: List[Note]) -> None:
response = client.get(f"/api/v1/data/posts/?noteId={','.join([n.note_id for n in note_samples])}")
assert response.status_code == 200
res_json = response.json()
assert res_json == {"data": [json.loads(post_samples[0].model_dump_json())]}
assert res_json == {"data": [json.loads(post_samples[0].model_dump_json())], "meta": {"next": None, "prev": None}}


def test_posts_get_has_created_at_filter_start_and_end(client: TestClient, post_samples: List[Post]) -> None:
response = client.get("/api/v1/data/posts/?createdAtStart=2006-7-25 00:00:00&createdAtEnd=2006-7-30 23:59:59")
assert response.status_code == 200
res_json = response.json()
assert res_json == {"data": [json.loads(post_samples[1].model_dump_json())]}
assert res_json == {"data": [json.loads(post_samples[1].model_dump_json())], "meta": {"next": None, "prev": None}}


def test_posts_get_has_created_at_filter_start(client: TestClient, post_samples: List[Post]) -> None:
response = client.get("/api/v1/data/posts/?createdAtStart=2006-7-25 00:00:00")
assert response.status_code == 200
res_json = response.json()
assert res_json == {"data": [json.loads(post_samples[i].model_dump_json()) for i in (1, 2)]}
assert res_json == {
"data": [json.loads(post_samples[i].model_dump_json()) for i in (1, 2)],
"meta": {"next": None, "prev": None},
}


def test_posts_get_has_created_at_filter_end(client: TestClient, post_samples: List[Post]) -> None:
response = client.get("/api/v1/data/posts/?createdAtEnd=2006-7-30 00:00:00")
assert response.status_code == 200
res_json = response.json()
assert res_json == {"data": [json.loads(post_samples[i].model_dump_json()) for i in (0, 1)]}
assert res_json == {
"data": [json.loads(post_samples[i].model_dump_json()) for i in (0, 1)],
"meta": {"next": None, "prev": None},
}


def test_posts_get_created_at_range_filter_accepts_integer(client: TestClient, post_samples: List[Post]) -> None:
response = client.get("/api/v1/data/posts/?createdAtStart=1153921700000&createdAtEnd=1154921800000")
assert response.status_code == 200
res_json = response.json()
assert res_json == {"data": [json.loads(post_samples[1].model_dump_json())]}
assert res_json == {"data": [json.loads(post_samples[1].model_dump_json())], "meta": {"next": None, "prev": None}}


def test_posts_get_created_at_start_filter_accepts_integer(client: TestClient, post_samples: List[Post]) -> None:
response = client.get("/api/v1/data/posts/?createdAtStart=1153921700000")
assert response.status_code == 200
res_json = response.json()
assert res_json == {"data": [json.loads(post_samples[i].model_dump_json()) for i in (1, 2)]}
assert res_json == {
"data": [json.loads(post_samples[i].model_dump_json()) for i in (1, 2)],
"meta": {"next": None, "prev": None},
}


def test_posts_get_created_at_end_filter_accepts_integer(client: TestClient, post_samples: List[Post]) -> None:
response = client.get("/api/v1/data/posts/?createdAtEnd=1154921800000")
assert response.status_code == 200
res_json = response.json()
assert res_json == {"data": [json.loads(post_samples[i].model_dump_json()) for i in (0, 1)]}
assert res_json == {
"data": [json.loads(post_samples[i].model_dump_json()) for i in (0, 1)],
"meta": {"next": None, "prev": None},
}


def test_posts_get_timestamp_out_of_range(client: TestClient, post_samples: List[Post]) -> None:
Expand Down
5 changes: 3 additions & 2 deletions common/birdxplorer_common/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractmethod
from datetime import datetime, timezone
from enum import Enum
from typing import Any, Dict, List, Literal, Type, TypeAlias, TypeVar, Union, Optional
from typing import Any, Dict, List, Literal, Optional, Type, TypeAlias, TypeVar, Union

from pydantic import BaseModel as PydanticBaseModel
from pydantic import ConfigDict, GetCoreSchemaHandler, HttpUrl, TypeAdapter
Expand Down Expand Up @@ -673,6 +673,7 @@ class Post(BaseModel):
repost_count: NonNegativeInt
impression_count: NonNegativeInt


class PaginationMeta(BaseModel):
next: Optional[HttpUrl] = None
prev: Optional[HttpUrl] = None
prev: Optional[HttpUrl] = None

0 comments on commit 94c6cb9

Please sign in to comment.