Fixes #4076: Recommendations from collections.
[wolnelektury.git] / src / funding / tests.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from datetime import date, timedelta
5 from django.test import TestCase
6 from .models import Offer, Perk
7
8
9 class PerksTest(TestCase):
10     def setUp(self):
11         self.today = date.today()
12         self.offer1 = Offer.objects.create(
13             author='author1', title='title1', slug='slug1',
14             target=100, start=self.today, end=self.today)
15
16     def test_perks(self):
17         perk = Perk.objects.create(price=20, name='Perk 20')
18         perk1 = Perk.objects.create(offer=self.offer1, price=50, name='Perk 50')
19         offer2 = Offer.objects.create(
20             author='author2', title='title2', slug='slug2',
21             target=100, start=self.today-timedelta(1), end=self.today-timedelta(1))
22         perk2 = Perk.objects.create(offer=offer2, price=1, name='Perk 1')
23
24         self.assertEqual(
25             set(self.offer1.get_perks(10)),
26             set())
27         self.assertEqual(
28             set(self.offer1.get_perks(70)),
29             {perk, perk1})
30
31
32 class FundingTest(TestCase):
33     def setUp(self):
34         self.today = date.today()
35         self.offer_past = Offer.objects.create(
36             author='an-author', title='past', slug='past',
37             target=100, start=self.today-timedelta(1), end=self.today-timedelta(1))
38         self.offer_current = Offer.objects.create(
39             author='an-author', title='current', slug='current',
40             target=100, start=self.today, end=self.today)
41         self.offer_future = Offer.objects.create(
42             author='an-author', title='future', slug='future',
43             target=100, start=self.today+timedelta(1), end=self.today+timedelta(1))
44
45     def test_current(self):
46         self.assertTrue(self.offer_current.is_current())
47         self.assertFalse(self.offer_past.is_current())
48         self.assertEqual(Offer.current(), self.offer_current)
49         self.assertEqual(
50             set(Offer.past()),
51             {self.offer_past}
52         )
53         self.assertEqual(
54             set(Offer.public()),
55             {self.offer_past, self.offer_current}
56         )
57
58     def test_interrupt(self):
59         # A new offer starts, ending the previously current one.
60         offer_interrupt = Offer.objects.create(
61             author='an-author', title='interrupt', slug='interrupt',
62             target=100, start=self.today-timedelta(1), end=self.today+timedelta(1))
63
64         self.assertTrue(offer_interrupt.is_current())
65         self.assertFalse(self.offer_current.is_current())
66         self.assertEqual(Offer.current(), offer_interrupt)
67         self.assertEqual(
68             set(Offer.past()),
69             {self.offer_past, self.offer_current}
70         )
71         self.assertEqual(
72             set(Offer.public()),
73             {self.offer_past, self.offer_current, offer_interrupt}
74         )