Skip to content

Commit

Permalink
fix: Announcement active state
Browse files Browse the repository at this point in the history
Announcement active state wasn't properly exposed to the frontend.
Fixed.
  • Loading branch information
gbdlin committed Sep 13, 2024
1 parent 6128134 commit 2f52cdd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
9 changes: 7 additions & 2 deletions plugin_store/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async def create_announcement(
db: Annotated["Database", Depends(database)],
announcement: api_announcements.AnnouncementRequest,
):
return await db.create_announcement(title=announcement.title, text=announcement.text)
return await db.create_announcement(title=announcement.title, text=announcement.text, active=announcement.active)


@app.get("/v1/announcements/-/current", response_model=list[api_announcements.CurrentAnnouncementResponse])
Expand Down Expand Up @@ -121,7 +121,12 @@ async def update_announcement(
existing_announcement: Annotated["Announcement", Depends(get_announcement)],
new_announcement: api_announcements.AnnouncementRequest,
):
return await db.update_announcement(existing_announcement, title=new_announcement.title, text=new_announcement.text)
return await db.update_announcement(
existing_announcement,
title=new_announcement.title,
text=new_announcement.text,
active=new_announcement.active,
)


@app.delete(
Expand Down
2 changes: 2 additions & 0 deletions plugin_store/api/models/announcements.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ class Config:
class AnnouncementRequest(BaseModel):
title: str
text: str

active: bool = True
9 changes: 6 additions & 3 deletions plugin_store/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def init(self):

async def list_announcements(self, active: bool = True):
statement = select(Announcement)
if not active:
statement = statement.where(Announcement.active == True)
if active:
statement = statement.where(Announcement.active.is_(True))
statement = statement.order_by(desc(Announcement.created))
result = (await self.session.execute(statement)).scalars().all()
return result or []
Expand All @@ -84,12 +84,13 @@ async def get_announcement(self, announcement_id: UUID) -> Announcement | None:
except NoResultFound:
return None

async def create_announcement(self, title: str, text: str) -> Announcement | None:
async def create_announcement(self, title: str, text: str, active: bool) -> Announcement | None:
nested = await self.session.begin_nested()
async with self.lock:
announcement = Announcement(
title=title,
text=text,
active=active,
)
try:
self.session.add(announcement)
Expand All @@ -106,6 +107,8 @@ async def update_announcement(self, announcement: Announcement, **kwargs) -> Ann
announcement.title = kwargs["title"]
if "text" in kwargs:
announcement.text = kwargs["text"]
if "active" in kwargs:
announcement.active = kwargs["active"]
try:
self.session.add(announcement)
except Exception:
Expand Down
11 changes: 11 additions & 0 deletions tests/db_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ async def seed_test_db(db_sessionmaker: "async_sessionmaker") -> None:
text="This is only a drill!",
created=datetime(2023, 11, 16, 0, 0, 0, tzinfo=UTC),
updated=datetime(2023, 11, 16, 0, 0, 0, tzinfo=UTC),
active=True,
)
session.add(announcement1)
announcement2 = Announcement( # type: ignore[call-arg]
Expand All @@ -176,8 +177,18 @@ async def seed_test_db(db_sessionmaker: "async_sessionmaker") -> None:
text="Seriously! Just a drill!",
created=datetime(2023, 11, 16, 0, 1, 0, tzinfo=UTC),
updated=datetime(2023, 11, 16, 0, 1, 0, tzinfo=UTC),
active=True,
)
session.add(announcement2)
announcement3 = Announcement( # type: ignore[call-arg]
id=UUID("89abcdef-79ab-7cde-99e0-9870d2e2dcdb"),
title="Hidden test announcement",
text="This one is inactive!!",
created=datetime(2023, 11, 16, 0, 2, 0, tzinfo=UTC),
updated=datetime(2023, 11, 16, 0, 2, 0, tzinfo=UTC),
active=False,
)
session.add(announcement3)
await session.commit()


Expand Down
24 changes: 19 additions & 5 deletions tests/test_announcement_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,28 @@ async def test_announcement_list(
assert response.status_code == 200
data = response.json()

assert len(data) == 2
assert len(data) == 3
assert data[0] == {
"id": "89abcdef-79ab-7cde-99e0-9870d2e2dcdb",
"title": "Hidden test announcement",
"text": "This one is inactive!!",
"active": False,
"created": "2023-11-16T00:02:00Z",
"updated": "2023-11-16T00:02:00Z",
}
assert data[1] == {
"id": "89abcdef-79ab-7cde-99e0-56b0d2e2dcdb",
"title": "Test announcement 2",
"text": "Seriously! Just a drill!",
"active": True,
"created": "2023-11-16T00:01:00Z",
"updated": "2023-11-16T00:01:00Z",
}
assert data[1] == {
assert data[2] == {
"id": "01234568-79ab-7cde-a445-b9f117ca645d",
"title": "Test announcement 1",
"text": "This is only a drill!",
"active": True,
"created": "2023-11-16T00:00:00Z",
"updated": "2023-11-16T00:00:00Z",
}
Expand All @@ -90,6 +100,7 @@ async def test_announcement_create(
"id": "018c22ac-d000-7444-9111-111111111111",
"title": "Test 3",
"text": "Drill test!",
"active": True,
"created": "2023-12-01T00:00:00Z",
"updated": "2023-12-01T00:00:00Z",
}
Expand All @@ -99,7 +110,7 @@ async def test_announcement_create(
assert response.status_code == 200
data = response.json()

assert len(data) == 3
assert len(data) == 4


@pytest.mark.parametrize("client", [lazy_fixture("client_unauth"), lazy_fixture("client_auth")])
Expand All @@ -112,7 +123,7 @@ async def test_announcement_list_current(
assert response.status_code == 200
data = response.json()

assert len(data) == 2
assert len(data) == 2, data
assert data[0] == {
"id": "89abcdef-79ab-7cde-99e0-56b0d2e2dcdb",
"title": "Test announcement 2",
Expand Down Expand Up @@ -142,6 +153,7 @@ async def test_announcement_fetch(
"id": "01234568-79ab-7cde-a445-b9f117ca645d",
"title": "Test announcement 1",
"text": "This is only a drill!",
"active": True,
"created": "2023-11-16T00:00:00Z",
"updated": "2023-11-16T00:00:00Z",
}
Expand All @@ -165,6 +177,7 @@ async def test_announcement_update(
"id": "01234568-79ab-7cde-a445-b9f117ca645d",
"title": "First test announcement",
"text": "Drilling!",
"active": True,
"created": "2023-11-16T00:00:00Z",
"updated": "2023-12-01T00:00:00Z",
}
Expand All @@ -185,5 +198,6 @@ async def test_announcement_delete(
assert response.status_code == 200
data = response.json()

assert len(data) == 1
assert len(data) == 2
assert data[0]["id"] != "01234568-79ab-7cde-a445-b9f117ca645d"
assert data[1]["id"] != "01234568-79ab-7cde-a445-b9f117ca645d"

0 comments on commit 2f52cdd

Please sign in to comment.