5381aba922
These fields duplicate information that is already present in the Answer table. I guess they exist(ed) as an optimisation to avoid recomputing the number of positive and negative answers per proposition each time the list page is loaded. Given the small number of propositions we have in practice and the extra housekeeping required to keep these fields up to date, I consider simplicity matters more here.
109 lines
3.5 KiB
Python
109 lines
3.5 KiB
Python
from django.contrib.auth import get_user_model
|
|
from django.test import Client, TestCase
|
|
from django.urls import reverse_lazy, reverse
|
|
|
|
from gestion.models import ErnestoUser
|
|
from propositions.models import Answer, Proposition
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
def new_user(username):
|
|
u = User.objects.create_user(username=username)
|
|
ErnestoUser.objects.create(user=u, slug=username, is_ernesto=True)
|
|
return u
|
|
|
|
|
|
class PropositionCreateTest(TestCase):
|
|
url = reverse_lazy("propositions:create")
|
|
|
|
def test_anonymous_get(self):
|
|
response = Client().get(self.url)
|
|
self.assertRedirects(response, "/login?next={}".format(self.url))
|
|
|
|
def test_get(self):
|
|
user = new_user("toto")
|
|
client = Client()
|
|
client.force_login(user)
|
|
|
|
response = client.get(self.url)
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
def test_post(self):
|
|
user = new_user("toto")
|
|
client = Client()
|
|
client.force_login(user)
|
|
|
|
data = {"name": "foo", "artist": "bar", "link": "example.com"}
|
|
client.post(self.url, data)
|
|
proposition = Proposition.objects.all()
|
|
self.assertEqual(1, proposition.count())
|
|
|
|
proposition = proposition.get()
|
|
self.assertEqual(proposition.name, "foo")
|
|
self.assertEqual(proposition.artist, "bar")
|
|
self.assertEqual(proposition.link, "http://example.com")
|
|
self.assertEqual(proposition.user, user.profile)
|
|
|
|
|
|
class PropositionListTest(TestCase):
|
|
url = reverse_lazy("propositions:list")
|
|
|
|
def setUp(self):
|
|
self.user = new_user("toto")
|
|
for name in ["foo", "bar", "baz"]:
|
|
p = Proposition.objects.create(name=name, user=self.user.profile)
|
|
Answer.objects.create(proposition=p, user=self.user, answer=Answer.YES)
|
|
for name in ["oof", "rab", "zab"]:
|
|
p = Proposition.objects.create(name=name, user=self.user.profile)
|
|
Answer.objects.create(proposition=p, user=self.user, answer=Answer.NO)
|
|
|
|
def test_anonymous_get(self):
|
|
response = Client().get(self.url)
|
|
self.assertRedirects(response, "/login?next={}".format(self.url))
|
|
|
|
def test_get(self):
|
|
client = Client()
|
|
client.force_login(self.user)
|
|
|
|
response = client.get(self.url)
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
class ReponseTest(TestCase):
|
|
def setUp(self):
|
|
self.user = new_user("toto")
|
|
self.prop = Proposition.objects.create(name="foo", user=self.user.profile)
|
|
|
|
def _url(self, rep):
|
|
assert rep in ["oui", "non"]
|
|
return reverse("propositions:{}".format(rep), args=(self.prop.id,))
|
|
|
|
def test_anonymous_get(self):
|
|
client = Client()
|
|
|
|
url = reverse("propositions:oui", args=(self.prop.id,))
|
|
response = client.get(url)
|
|
self.assertRedirects(response, "/login?next={}".format(url))
|
|
|
|
url = reverse("propositions:non", args=(self.prop.id,))
|
|
response = client.get(url)
|
|
self.assertRedirects(response, "/login?next={}".format(url))
|
|
|
|
def test_get(self):
|
|
client = Client()
|
|
client.force_login(self.user)
|
|
|
|
# Oui
|
|
client.get(reverse("propositions:oui", args=(self.prop.id,)))
|
|
self.prop.refresh_from_db()
|
|
self.assertEqual(
|
|
list(self.prop.answer_set.values_list("answer", flat=True)), [Answer.YES],
|
|
)
|
|
|
|
# Oui
|
|
client.get(reverse("propositions:non", args=(self.prop.id,)))
|
|
self.prop.refresh_from_db()
|
|
self.assertEqual(
|
|
list(self.prop.answer_set.values_list("answer", flat=True)), [Answer.NO]
|
|
)
|