Commit 62698501 authored by luxcem's avatar luxcem

update tests for votes

parent ef2a5922
......@@ -62,6 +62,8 @@ class UnDVotedMixin(models.Model):
vote.save()
diff_score = 1
self.und_score += diff_score
# Update self score, use update and filter to avoid triggering signals
self.__class__.objects.filter(id=self.id).update(
und_score=F("und_score") + diff_score)
......@@ -84,6 +86,7 @@ class UnDVotedMixin(models.Model):
vote.save()
diff_score = -1
self.und_score += diff_score
# Update self score, use update and filter to avoid triggering signals
self.__class__.objects.filter(id=self.id).update(
und_score=F("und_score") + diff_score)
......@@ -91,5 +94,6 @@ class UnDVotedMixin(models.Model):
def update_und_score(self):
"""Reset score to the correct count (should not be necessary)"""
score = self.und_votes.aggregate(Sum("score"))["score__sum"] or 0
self.und_score = score
# Update self score, use update and filter to avoid triggering signals
self.__class__.objects.filter(id=self.id).update(und_score=score)
......@@ -10,3 +10,58 @@ def test_init():
def test_article():
article = ArticleFactory()
assert type(article) == Article
def test_votes():
article = ArticleFactory()
# Upvote
article.upvote("test_user")
article_db = Article.objects.get(id=article.id)
assert article.und_score == 1
assert article_db.und_score == 1
votes = article_db.und_votes.all()
assert len(votes) == 1
assert str(votes[0]) == "{}:{}:{}".format(
"test_user", str(article_db), 1)
# Upvote -> upvote
article.upvote("test_user")
article_db = Article.objects.get(id=article.id)
assert article.und_score == 0
assert article_db.und_score == 0
# 0 -> downvote
article.downvote("test_user")
article_db = Article.objects.get(id=article.id)
assert article.und_score == -1
assert article_db.und_score == -1
# Downvote -> Downvote
article.downvote("test_user")
article_db = Article.objects.get(id=article.id)
assert article.und_score == 0
assert article_db.und_score == 0
# Downvote -> Upvote
article.downvote("test_user")
article_db = Article.objects.get(id=article.id)
assert article.und_score == -1
assert article_db.und_score == -1
article.upvote("test_user")
article_db = Article.objects.get(id=article.id)
assert article.und_score == 1
assert article_db.und_score == 1
# Upvote -> Downwote
article.downvote("test_user")
article_db = Article.objects.get(id=article.id)
assert article.und_score == -1
assert article_db.und_score == -1
article.update_und_score()
article_db = Article.objects.get(id=article.id)
assert article.und_score == -1
assert article_db.und_score == -1
from django.urls import reverse
from django.test import TestCase
from django.contrib.contenttypes.models import ContentType
from userprofile.factories import ProfileFactory
from rp.factories import ArticleFactory
from rp.models import Article
class VoteViewTestCase(TestCase):
def setUp(self):
self.article = ArticleFactory()
self.content_type = ContentType.objects.get_for_model(
self.article
).pk
self.profile = ProfileFactory()
self.user = self.profile.user
self.client.force_login(self.user)
def test_votes(self):
url_upvote = reverse("und-upvote", kwargs={
"content_type": self.content_type,
"object_id": self.article.id
})
url_downvote = reverse("und-downvote", kwargs={
"content_type": self.content_type,
"object_id": self.article.id
})
response = self.client.get(url_upvote)
assert response.status_code == 200
article_db = Article.objects.get(id=self.article.id)
assert article_db.und_score == 1
response = self.client.get(url_downvote)
assert response.status_code == 200
article_db = Article.objects.get(id=self.article.id)
assert article_db.und_score == -1
......@@ -6,14 +6,14 @@ from django.http import HttpResponse
def upvote(request, content_type, object_id):
ct = ContentType.objects.get_for_id(content_type)
obj = ct.get_object_for_this_type(pk=object_id)
obj.upvote(user=request.user)
obj.upvote(username=request.user.username)
return HttpResponse({"success"})
def downvote(request, content_type, object_id):
ct = ContentType.objects.get_for_id(content_type)
obj = ct.get_object_for_this_type(pk=object_id)
obj.downvote(user=request.user)
obj.downvote(username=request.user.username)
return HttpResponse({"success"})
......
......@@ -6,7 +6,7 @@ import django
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
@pytest.fixture(autouse=True)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment