diff --git a/bda/tests.py b/bda/tests.py index 22efc5a2..2741084f 100644 --- a/bda/tests.py +++ b/bda/tests.py @@ -1,22 +1,79 @@ -# -*- coding: utf-8 -*- -""" -This file demonstrates writing tests using the unittest module. These will pass -when you run "manage.py test". +import json -Replace this with more appropriate tests for your application. -""" +from django.test import TestCase, Client +from django.utils import timezone -from __future__ import division -from __future__ import print_function -from __future__ import unicode_literals +from .models import Tirage, Spectacle, Salle, CategorieSpectacle -from django.test import TestCase +class TestBdAViews(TestCase): + def setUp(self): + self.tirage = Tirage.objects.create( + title="Test tirage", + appear_catalogue=True, + ouverture=timezone.now(), + fermeture=timezone.now(), + ) + self.category = CategorieSpectacle.objects.create(name="Category") + self.location = Salle.objects.create(name="here") + Spectacle.objects.bulk_create([ + Spectacle( + title="foo", date=timezone.now(), location=self.location, + price=0, slots=42, tirage=self.tirage, listing=False, + category=self.category + ), + Spectacle( + title="bar", date=timezone.now(), location=self.location, + price=1, slots=142, tirage=self.tirage, listing=False, + category=self.category + ), + Spectacle( + title="baz", date=timezone.now(), location=self.location, + price=2, slots=242, tirage=self.tirage, listing=False, + category=self.category + ), + ]) + def test_catalogue(self): + """Test the catalogue JSON API""" + client = Client() -class SimpleTest(TestCase): - def test_basic_addition(self): - """ - Tests that 1 + 1 always equals 2. - """ - self.assertEqual(1 + 1, 2) + # The `list` hooh + resp = client.get("/bda/catalogue/list") + self.assertJSONEqual( + resp.content.decode("utf-8"), + [{"id": self.tirage.id, "title": self.tirage.title}] + ) + + # The `details` hook + resp = client.get( + "/bda/catalogue/details?id={}".format(self.tirage.id) + ) + self.assertJSONEqual( + resp.content.decode("utf-8"), + { + "categories": [{ + "id": self.category.id, + "name": self.category.name + }], + "locations": [{ + "id": self.location.id, + "name": self.location.name + }], + } + ) + + # The `descriptions` hook + resp = client.get( + "/bda/catalogue/descriptions?id={}".format(self.tirage.id) + ) + raw = resp.content.decode("utf-8") + try: + results = json.loads(raw) + except ValueError: + self.fail("Not valid JSON: {}".format(raw)) + self.assertEqual(len(results), 3) + self.assertEqual( + {(s["title"], s["price"], s["slots"]) for s in results}, + {("foo", 0, 42), ("bar", 1, 142), ("baz", 2, 242)} + )