def setUp(self): self.password = "******" self.user = User.objects.create_user(username="******", password=self.password) self.now = dt.now().strftime("%Y-%m-%d") self.event = Event(date=self.now, author=self.user) self.event.save()
class TestDeleteEventView(TestCase): def setUp(self): self.password = "******" self.user = User.objects.create_user(username="******", password=self.password) self.now = dt.now().strftime("%Y-%m-%d") self.event = Event(date=self.now, author=self.user) self.event.save() def test_url_resolves_to_view(self): found = resolve("/events/delete/") self.assertEqual(found.func, bbq_views.delete_event) def test_delete_existing_event(self): body = {"slug": self.event.slug} query_event = Event.objects.filter(slug=self.event.slug) self.assertEqual(len(query_event), 1) self.client.login(username="******", password="******") response = self.client.post( "/events/delete/", data=json.dumps(body), content_type="application/json", ) query_event = Event.objects.filter(slug=self.event.slug) self.assertEqual(response.status_code, 200) self.assertEqual(len(query_event), 0) def test_delete_non_logged_in(self): body = {"slug": self.event.slug} response = self.client.post( "/events/delete/", data=json.dumps(body), content_type="application/json", ) url = response.url.split("?")[0] self.assertEqual(response.status_code, 302) self.assertEqual(url, "/accounts/login/") def test_delete_non_existing(self): body = {"slug": "does_not_exist"} self.client.login(username="******", password="******") response = self.client.post( "/events/delete/", data=json.dumps(body), content_type="application/json", ) self.assertEqual(response.status_code, 302) self.assertEqual(response.url, "/error/")
def test_math(self): event = Event(author=self.user, date=self.now).save() signup_1_extras = randint(5, 10) signup_2_extras = randint(5, 10) total = signup_1_extras + signup_2_extras + 2 # counting the organizers signup_1 = SignUp(name="name_1", event=event, extras=signup_1_extras).save() signup_2 = SignUp(name="name_2", event=event, extras=signup_2_extras).save() meat_1_count = randint(10, 20) meat_2_count = randint(10, 20) for i in range(meat_1_count): MeatChoice(signup=signup_1, meat=self.meat_1).save() for i in range(meat_2_count): MeatChoice(signup=signup_2, meat=self.meat_2).save() self.client.login(username="******", password="******") response = self.client.get("/events/admin/{}".format(event.slug), {"slug": event.slug}) self.assertEqual(response.status_code, 200) self.assertEqual(response.context["meats"][self.meat_1.name], meat_1_count) self.assertEqual(response.context["meats"][self.meat_2.name], meat_2_count) self.assertEqual(response.context["total"], total)
def test_generated_url(self): event = Event(author=self.user, date=self.now).save() self.client.login(username="******", password="******") response = self.client.get("/events/admin/{}".format(event.slug), {"slug": event.slug}) self.assertEqual(response.status_code, 200) url = response.context["url"] url = ("/" + url.split("/", 1)[-1] ) # host in tests is "testserver", so take it out response = self.client.get(url) self.assertEqual(response.status_code, 200)
def test_signups(self): event = Event(author=self.user, date=self.now).save() signup_1 = SignUp(name="name_1", event=event, extras=0).save() signup_2 = SignUp(name="name_2", event=event, extras=0).save() original_signups = {signup_1, signup_2} self.client.login(username="******", password="******") response = self.client.get("/events/admin/{}".format(event.slug), {"slug": event.slug}) response_signups = {signup for signup in response.context["signups"]} self.assertEqual(original_signups, response_signups)
class TestEventsListView(TestCase): def setUp(self): self.password = "******" self.user = User.objects.create_user(username="******", password=self.password) self.now = dt.now().strftime("%Y-%m-%d") self.event_1 = Event(date=self.now, author=self.user) self.event_1.save() self.event_2 = Event(date=self.now, author=self.user) self.event_2.save() def test_url_resolves_to_view(self): found = resolve("/events/list/") self.assertEqual(found.func, bbq_views.events_list) def test_all_events_shown(self): self.client.login(username="******", password="******") response = self.client.get("/events/list/") request_event_set = {self.event_1, self.event_2} response_event_set = {event for event in response.context["events"]} self.assertEqual(request_event_set, response_event_set)
def test_date_in_past(self): yesterday = (dt.now() - td(days=1)).strftime("%Y-%m-%d") event = Event(date=yesterday, author=self.user) with self.assertRaises(ValidationError): event.full_clean()