Skip to content

Commit

Permalink
fix: fix help center failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaOloton committed Aug 15, 2024
1 parent 31e7502 commit 305ee5a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
6 changes: 4 additions & 2 deletions api/v1/routes/topic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Optional
from fastapi import (
APIRouter,
Depends,
Expand Down Expand Up @@ -64,7 +65,8 @@ async def retrieve_topic(

@topic.get('/search', response_model=TopicList)
async def search_for_topic(
query: str,
title: Optional[str] = None,
content: Optional[str] = None,
db: Session = Depends(get_db)
):
"""
Expand All @@ -77,7 +79,7 @@ async def search_for_topic(
Returns:
Response: a response object containing details if successful or appropriate errors if not
"""
topics = topic_service.search(db=db,search_query=query)
topics = topic_service.search(db=db,title_query=title, content_query=content)

return success_response(
status_code=status.HTTP_200_OK,
Expand Down
8 changes: 3 additions & 5 deletions api/v1/services/topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@ def delete(self, db: Session, id: str):
db.delete(topic)
db.commit()

def search(self, db: Session, search_query: str):
def search(self, db: Session, title_query: str, content_query: str):
"""
Search for topics based on title, content, tags, or topic IDs.
"""
query = db.query(Topic).filter(
(Topic.title.ilike(f'%{search_query}%')) |
(Topic.content.ilike(f'%{search_query}%')) |
(Topic.tags.any(search_query)) |
(Topic.id == search_query) # Include searching by topic ID
(Topic.title.ilike(f'%{title_query}%')) |
(Topic.content.ilike(f'%{content_query}%'))
)
return query.all()

Expand Down
7 changes: 3 additions & 4 deletions tests/v1/topic/test_topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,9 @@ def mock_get(model, ident):

headers = {'Authorization': f'Bearer {access_token_user1}'}
data = {
"id": test_topic.id,
"title": "Uploading profile picture."
}
response = client.patch(f"/api/v1/help-center/topics", headers=headers, json=data)
response = client.patch(f"/api/v1/help-center/topics/{test_topic.id}", headers=headers, json=data)
assert response.status_code == 200

def test_delete_topic(
Expand All @@ -119,7 +118,7 @@ def mock_get(model, ident):
mock_db_session.query.return_value.filter_by.return_value.first.return_value = [test_topic]

headers = {'Authorization': f'Bearer {access_token_user1}'}
response = client.request("DELETE",f"/api/v1/help-center/topics", headers=headers, json={"id":test_topic.id})
response = client.delete(f"/api/v1/help-center/topics/{test_topic.id}", headers=headers)
assert response.status_code == 204

def test_search_topic(
Expand All @@ -138,7 +137,7 @@ def mock_get(model, ident):
mock_db_session.query.return_value.filter.return_value.first.return_value = test_user

mock_db_session.query.return_value.filter_by.return_value.first.return_value = [test_topic]
response = client.request("GET","/api/v1/help-center/search", json={"query":test_topic.title})
response = client.get(f"/api/v1/help-center/search?title={test_topic.title}")
assert response.status_code == 200

def test_fetch_a_topic(
Expand Down

0 comments on commit 305ee5a

Please sign in to comment.