def test_if_an_old_question_appears_in_top_questions(self): # Create a new category new_category = Category(name="Test Category 1") new_category.save() # Create ten questions for that category questions = test_fixture1(new_category) eight_days_ago = timezone.now() - timedelta(days=8) some_old_question = Question(name="old question 1", category=new_category, views=100000, posted=eight_days_ago) some_old_question.save() # Save the questions for question in questions: question.save() # Get response from index view response = self.client.get(reverse('index')) response_questions = response.context['top_questions'] last_week = timezone.now() - timedelta(days=7) valid = True # Check if they are returned in order for question in response_questions: if question.posted < last_week: valid = False self.assertEqual(valid, True)
def test_if_categry_questions_are_returned_in_order_latest_first(self): # Create a new category new_category = Category(name="Test Category 1") new_category.save() # Create ten questions for that category questions = test_fixture1(new_category) # Save the questions for question in questions: question.save() # Get response from category view response = self.client.get( reverse('category', kwargs={'category_name_slug': new_category.slug})) response_questions = response.context['questions'] valid = True # Check if they are returned in order # The time posted should be in descending order question_posted = response_questions[0].posted for question in response_questions[1:]: if question.posted > question_posted: valid = False else: question_posted = question.posted self.assertEqual(valid, True)
def test_category_contains_correct_slug_field(self): # Create a new category new_category = Category(name="Test Category 1") new_category.save() # Check slug was generated correctly self.assertEquals(new_category.slug, "test-category-1")
def test_if_top_questions_are_returned_in_order_by_views(self): # Create a new category new_category = Category(name="Test Category 1") new_category.save() # Create ten questions for that category questions = test_fixture1(new_category) # Save the questions for question in questions: question.save() # Get response from index view response = self.client.get(reverse('index')) response_questions = response.context['top_questions'] valid = True # Check if they are returned in order question_view = response_questions[0].views for question in response_questions[1:]: if question.views > question_view: valid = False else: question_view = question.views self.assertEqual(valid, True)
def test_if_unanswered_questions_are_returned_in_order_of_posted(self): # Create a new category new_category = Category(name="Test Category 1") new_category.save() # Create ten questions for that category questions = test_fixture1(new_category) # Save the questions for question in questions: question.save() # Get response from index view response = self.client.get(reverse('index')) response_questions = response.context['unanswered_questions'] valid = True # Check if they are returned in order # The time posted should be in ascending order question_posted = response_questions[0].posted for question in response_questions[1:]: if question.posted < question_posted: valid = False else: question_view = question.views self.assertEqual(valid, True)
def test_if_question_is_answered(self): # Create a new category new_category = Category(name="Test Category 1") new_category.save() # Create and save a question for that category question = test_fixture1(new_category) question.save() # Create and save answers for the question answers = test_fixture2(question, new_category) for answer in answers: answer.save() # Set answer 1 as best answer question.answered = answers[0] question.save() # Get response from show_question view response = self.client.get( reverse('show_question', kwargs={ 'category_name_slug': new_category.slug, 'question_id': question.pk })) best_answer = response.context['answer'] self.assertEqual(answers[0], best_answer)
def test_if_answers_are_returned_in_order_earliest_first(self): # Create a new category new_category = Category(name="Test Category 1") new_category.save() # Create and save a question for that category question = test_fixture1(new_category) question.save() # Create and save answers for the question answers = test_fixture2(question, new_category) for answer in answers: answer.save() # Get response from show_question view response = self.client.get( reverse('show_question', kwargs={ 'category_name_slug': new_category.slug, 'question_id': question.pk })) response_answers = response.context['answers_list'] valid = True # Check if they are returned in order # The time posted should be in ascending order answer_posted = response_answers[0].posted for answer in response_answers[1:]: if answer.posted < answer_posted: valid = False else: answer_posted = answer.posted self.assertEqual(valid, True)
def test_if_question_does_not_exist(self): # Create a new category new_category = Category(name="Test Category 1") new_category.save() # Get a response when the question does not exist response = self.client.get( reverse('show_question', kwargs={ 'category_name_slug': new_category.slug, 'question_id': 1234 })) self.assertEqual(response.context['question'], None)
def test_if_unanswered_questions_are_actually_unanswered(self): # Create a new category new_category = Category(name="Test Category 1") new_category.save() # Create ten questions for that category questions = test_fixture1(new_category) # Save the questions for questions in questions: questions.save() # Get response from index view response = self.client.get(reverse('index')) response_questions = response.context['unanswered_questions'] valid = True # Check all questions are unanswered for question in response_questions: if question.answered != None: valid = False self.assertEqual(valid, True)