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

Returns quiz_id in response of create_quiz POST request #56

Merged
merged 4 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions app/routers/quizzes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
router = APIRouter(prefix="/quiz", tags=["Quiz"])


@router.post("/", response_model=QuizResponse)
@router.post("/")
async def create_quiz(quiz: Quiz):
"""Returns the ID of created quiz in a dictionary with key as 'quiz_id'"""
quiz = jsonable_encoder(quiz)
new_quiz = client.quiz.quizzes.insert_one(quiz)
created_quiz = client.quiz.quizzes.find_one({"_id": new_quiz.inserted_id})
Expand All @@ -20,7 +21,9 @@ async def create_quiz(quiz: Quiz):

client.quiz.questions.insert_many(questions)

return JSONResponse(status_code=status.HTTP_201_CREATED, content=created_quiz)
return JSONResponse(
status_code=status.HTTP_201_CREATED, content={"quiz_id": new_quiz.inserted_id}
)


@router.get("/{quiz_id}", response_model=QuizResponse)
Expand Down
3 changes: 2 additions & 1 deletion app/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def setUp(self):
# create a question is through the quiz endpoint which is why we are using the quiz endpoint
# to create questions and a quiz
response = self.client.post(quizzes.router.prefix + "/", json=self.quiz_data)
self.quiz = json.loads(response.content)
self.quiz_id = json.loads(response.content)["quiz_id"]
self.quiz = self.client.get(quizzes.router.prefix + f"/{self.quiz_id}").json()


class SessionsBaseTestCase(BaseTestCase):
Expand Down
5 changes: 4 additions & 1 deletion app/tests/test_quizzes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ def setUp(self):
self.id = self.quiz["_id"]
self.length = len(self.quiz_data["question_sets"][0]["questions"])

def test_setup_quizId_and_quiz(self):
assert self.quiz_id == self.quiz["_id"]

def test_create_quiz(self):
response = self.client.post(quizzes.router.prefix + "/", json=self.quiz_data)
response = json.loads(response.content)
id = response["_id"]
id = response["quiz_id"]
response = self.client.get(f"{quizzes.router.prefix}/{id}")
assert response.status_code == 200

Expand Down
7 changes: 4 additions & 3 deletions app/tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ def test_create_session_with_valid_quiz_id_and_first_session(self):
data = open("app/tests/dummy_data/homework_quiz.json")
quiz_data = json.load(data)
response = self.client.post(quizzes.router.prefix + "/", json=quiz_data)
quiz = json.loads(response.content)
quiz_id = json.loads(response.content)["quiz_id"]
quiz = self.client.get(quizzes.router.prefix + f"/{quiz_id}").json()
response = self.client.post(
sessions.router.prefix + "/", json={"quiz_id": quiz["_id"], "user_id": 1}
)
assert response.status_code == 201
session = json.loads(response.content)
assert session["is_first"] is True
assert len(session["session_answers"]) == len(
quiz_data["question_sets"][0]["questions"]
assert len(session["session_answers"]) == sum(
len(qset["questions"]) for qset in quiz_data["question_sets"]
)

def test_create_session_with_valid_quiz_id_and_previous_session(self):
Expand Down