-
Notifications
You must be signed in to change notification settings - Fork 4
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
Reduce get next question queries #12
Changes from 3 commits
74cec65
b02ef3f
a002679
50b35b9
0be351c
7d9fd55
89bf25e
e89765f
a3f3f5b
90776ee
da12f70
c7cf082
082fd3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from django.test import TestCase | ||
|
||
from django.db import connection | ||
from django.test.utils import override_settings | ||
from emailusername.models import User | ||
|
||
from questions import models | ||
from questions.views import _get_next_question | ||
from questions.views import NextQuestion | ||
|
||
|
||
class TestGetNextQuestion(TestCase): | ||
|
||
def setUp(self): | ||
# Create a user | ||
self.PASSWORD = 'p' | ||
self.USERNAME = 'foo@bar.com' | ||
self.user = User(email=self.USERNAME) | ||
self.user.set_password(self.PASSWORD) | ||
self.user.save() | ||
|
||
def _assign_question_to_user(self, user, question): | ||
tag = models.Tag.objects.create(name='faketag') | ||
|
||
models.UserTag.objects.create( | ||
tag=tag, | ||
user=user, | ||
enabled=True | ||
) | ||
models.QuestionTag.objects.create( | ||
tag=tag, | ||
question=question, | ||
enabled=True | ||
) | ||
|
||
return tag | ||
|
||
def test_user_with_no_questions(self): | ||
next_question = _get_next_question(self.user) | ||
|
||
self.assertIsInstance(next_question, NextQuestion) | ||
self.assertIsNone(next_question.question) | ||
|
||
@override_settings(DEBUG=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What effect does DEBUG=True have on the test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Allows you to count queries via - |
||
def test_user_with_one_question(self): | ||
question = models.Question.objects.create( | ||
question='fakebar', | ||
) | ||
self._assign_question_to_user(self.user, question) | ||
|
||
next_question = _get_next_question(self.user) | ||
|
||
self.assertIsInstance(next_question, NextQuestion) | ||
self.assertIsNotNone(next_question.question) | ||
self.assertEqual(len(connection.queries), 8) | ||
|
||
questions = models.Question.objects.get_user_questions(self.user) | ||
|
||
self.assertEqual(questions.count(), 1) | ||
|
||
@override_settings(DEBUG=True) | ||
def test_user_with_ten_questions(self): | ||
for i in range(10): | ||
question = models.Question.objects.create( | ||
question='fakebar', | ||
) | ||
self._assign_question_to_user(self.user, question) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be interesting here to have: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. c. one question with a tag by the user, with an associated UserTag with enabled=False There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely, these were just the first two tests I came up with to explore the |
||
|
||
next_question = _get_next_question(self.user) | ||
|
||
self.assertIsInstance(next_question, NextQuestion) | ||
self.assertIsNotNone(next_question.question) | ||
self.assertEqual(len(connection.queries), 53) | ||
|
||
questions = models.Question.objects.get_user_questions(self.user) | ||
|
||
self.assertEqual(questions.count(), 10) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This name
_assign_question_to_user
is misleading to me, because it sounds like the question is getting assigned to the user. What is really happening is that the question is getting associated with a tag, and the user is selecting that tag. I don't know what a better name would be, maybe something generic like_associate_tags()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm right, although what the method really accomplishes is linking a question to a user via
Tags
, which are basically relationships between questions and users with some metadata, correct?What about a name like
_associate_question_with_user
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or maybe my idea of what
Tags
are is a bit off 😕