def test_update_category_forbidden(self): # Login a student account student = test_utils.create_student_user_profile() test_utils.create_article() response = self.client.login(username=student, password='******') self.assertTrue(response) # Test update a category fail and get forbidden code 403 response = self.client.post(reverse('delete_category', args=[ 'bar', ]), data={'name': 'foo'}) self.assertEqual(Subject.objects.filter(name='foo').count(), 0) self.assertEqual(response.status_code, 403)
def test_rating(self): user = test_utils.create_user() user2 = UserProfile.objects.create_user("another") article = test_utils.create_article() article2 = Article(title="Even More Terrible Code", body=article.body, author=article.author, category=article.category) article2.save() neg_val = -1 zero_val = 0 large_val = 6 # Test creating rating rating0 = Rating(user=user, article=article, rating=3) rating0.save() self.assertEqual(Rating.objects.all().count(), 1) self.assertEqual(Rating.objects.all()[0].rating, 3) self.assertEqual(Rating.objects.all()[0].user, user) self.assertEqual(Rating.objects.all()[0].article, article) rating1 = Rating(user=user, article=article, rating=4) # Test integrity self.assertRaises(ValidationError, lambda: rating1.save()) # Test same user could rate different articles and same article could be rated by different users rating2 = Rating(user=user2, article=article, rating=5) rating3 = Rating(user=user, article=article2, rating=5) self.assertIsNone(rating2.save()) self.assertIsNone(rating3.save()) self.assertEqual(Rating.objects.all().count(), 3) # Test the number of ratings created by a user is correct self.assertEqual(user.rating_set.count(), 2) # Test when ratings are deleted, they are also deleted from a user's rating set Rating.objects.all().delete() self.assertEqual(user.rating_set.count(), 0) # Test when a user is deleted, the user's ratings are all also deleted rating0 = Rating(user=user2, article=article, rating=3) rating0.save() rating1 = Rating(user=user2, article=article2, rating=5) rating1.save() self.assertEqual(Rating.objects.all().count(), 2) UserProfile.objects.filter(user=user2.user).delete() self.assertEqual(Rating.objects.all().count(), 0) # Test a rating with negative value rating2 = Rating(user=user, article=article, rating=neg_val) self.assertRaises(ValidationError, lambda: rating2.full_clean()) # Test a rating with zero value rating3 = Rating(user=user, article=article, rating=zero_val) self.assertRaises(ValidationError, lambda: rating3.full_clean()) # Test a rating with value larger than max value rating4 = Rating(user=user, article=article, rating=large_val) self.assertRaises(ValidationError, lambda: rating4.full_clean())
def test_update_category(self): # Login a staff account staff = test_utils.create_staff_user_profile() test_utils.create_article() response = self.client.login(username=staff, password='******') self.assertTrue(response) # Test delete a category response = self.client.post(reverse('update_category', args=[ 'bar', ]), data={'name': 'foo'}) self.assertEqual(Subject.objects.filter(name='bar').count(), 0) self.assertEqual(Subject.objects.filter(name='foo').count(), 1) self.assertEqual(Article.objects.filter(category='foo').count(), 1) self.assertEqual(response.status_code, 302) self.assertRedirects(response, reverse('index'))
def test_comment(self): text = "Testing..." user = test_utils.create_user() article = test_utils.create_article() comment = Comment(user=user, article=article, text=text) comment.save() self.assertEqual(Comment.objects.all().count(), 1) self.assertEqual(Comment.objects.all()[0].text, text) self.assertEqual(Comment.objects.all()[0].article, article) self.assertEqual(Comment.objects.all()[0].user, user)
def test_article_views(self): article1 = test_utils.create_article() article2 = Article(title="Even More Terrible Code", body=article1.body, author=article1.author, category=article1.category) article2.save() article_views = ArticleViews(article=article1, views=1) article_views.save() # Test correct number of ArticleViews models self.assertEqual(ArticleViews.objects.all().count(), 1) article_views2 = ArticleViews(article=article2, views=3) article_views2.save() # Test correct number of ArticleViews models self.assertEqual(ArticleViews.objects.all().count(), 2) # Test integrity article_views3 = ArticleViews(article=article2, views=45) with transaction.atomic(): self.assertRaises(IntegrityError, lambda: article_views3.save()) # reset ArticleViews.objects.all().delete() self.assertEqual(ArticleViews.objects.all().count(), 0) # Test different articles could have different views on same date article_views = ArticleViews(article=article1, views=5) article_views.save() article_views4 = ArticleViews(article=article2, views=5) self.assertIsNone(article_views4.save()) # Test same article could have different views on different date with freeze_time(lambda: datetime(2018, 2, 4)): article_views5 = ArticleViews(article=article1, views=5) self.assertIsNone(article_views5.save()) # Test get the correct number of total views self.assertEqual( ArticleViews.objects.get_total_views(article=article1), 10) # Test get the correct number of trending articles self.assertEqual(len(ArticleViews.objects.get_trending_articles()), 2) # Test correctness of add_views ArticleViews.objects.add_view(article=article1) self.assertEqual( ArticleViews.objects.filter(article=article1, date=date.today())[0].views, article_views.views + 1)
def test_category_exist(self): test_utils.create_article() # s = Subject(name="bar") response = self.client.get(reverse('category', args=['bar'])) self.assertEqual(response.context['category'].name, 'bar') self.assertEqual(len(response.context['articles']), 1)