Files
@ f1c8e90b77d3
Branch filter:
Location: symposion_app/registrasion/tests/test_batch.py
f1c8e90b77d3
4.9 KiB
text/x-python
Makes the form type optional for reports
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | import datetime
import pytz
from django.core.exceptions import ValidationError
from controller_helpers import TestingCartController
from test_cart import RegistrationCartTestCase
from registrasion.controllers.batch import BatchController
from registrasion.controllers.discount import DiscountController
from registrasion.controllers.product import ProductController
from registrasion.models import commerce
from registrasion.models import conditions
UTC = pytz.timezone('UTC')
class BatchTestCase(RegistrationCartTestCase):
def test_no_caches_outside_of_batches(self):
cache_1 = BatchController.get_cache(self.USER_1)
cache_2 = BatchController.get_cache(self.USER_2)
# Identity testing is important here
self.assertIsNot(cache_1, cache_2)
def test_cache_clears_at_batch_exit(self):
with BatchController.batch(self.USER_1):
cache_1 = BatchController.get_cache(self.USER_1)
cache_2 = BatchController.get_cache(self.USER_1)
self.assertIsNot(cache_1, cache_2)
def test_caches_identical_within_nestings(self):
with BatchController.batch(self.USER_1):
cache_1 = BatchController.get_cache(self.USER_1)
with BatchController.batch(self.USER_2):
cache_2 = BatchController.get_cache(self.USER_1)
cache_3 = BatchController.get_cache(self.USER_1)
self.assertIs(cache_1, cache_2)
self.assertIs(cache_2, cache_3)
def test_caches_are_independent_for_different_users(self):
with BatchController.batch(self.USER_1):
cache_1 = BatchController.get_cache(self.USER_1)
with BatchController.batch(self.USER_2):
cache_2 = BatchController.get_cache(self.USER_2)
self.assertIsNot(cache_1, cache_2)
def test_cache_clears_are_independent_for_different_users(self):
with BatchController.batch(self.USER_1):
cache_1 = BatchController.get_cache(self.USER_1)
with BatchController.batch(self.USER_2):
cache_2 = BatchController.get_cache(self.USER_2)
with BatchController.batch(self.USER_2):
cache_3 = BatchController.get_cache(self.USER_2)
cache_4 = BatchController.get_cache(self.USER_1)
self.assertIs(cache_1, cache_4)
self.assertIsNot(cache_1, cache_2)
self.assertIsNot(cache_2, cache_3)
def test_new_caches_for_new_batches(self):
with BatchController.batch(self.USER_1):
cache_1 = BatchController.get_cache(self.USER_1)
with BatchController.batch(self.USER_1):
cache_2 = BatchController.get_cache(self.USER_1)
with BatchController.batch(self.USER_1):
cache_3 = BatchController.get_cache(self.USER_1)
self.assertIs(cache_2, cache_3)
self.assertIsNot(cache_1, cache_2)
def test_memoisation_happens_in_batch_context(self):
with BatchController.batch(self.USER_1):
output_1 = self._memoiseme(self.USER_1)
with BatchController.batch(self.USER_1):
output_2 = self._memoiseme(self.USER_1)
self.assertIs(output_1, output_2)
def test_memoisaion_does_not_happen_outside_batch_context(self):
output_1 = self._memoiseme(self.USER_1)
output_2 = self._memoiseme(self.USER_1)
self.assertIsNot(output_1, output_2)
def test_memoisation_is_user_independent(self):
with BatchController.batch(self.USER_1):
output_1 = self._memoiseme(self.USER_1)
with BatchController.batch(self.USER_2):
output_2 = self._memoiseme(self.USER_2)
output_3 = self._memoiseme(self.USER_1)
self.assertIsNot(output_1, output_2)
self.assertIs(output_1, output_3)
def test_memoisation_clears_outside_batches(self):
with BatchController.batch(self.USER_1):
output_1 = self._memoiseme(self.USER_1)
with BatchController.batch(self.USER_1):
output_2 = self._memoiseme(self.USER_1)
self.assertIsNot(output_1, output_2)
@classmethod
@BatchController.memoise
def _memoiseme(self, user):
return object()
def test_batch_end_functionality_is_called(self):
class Ender(object):
end_count = 0
def end_batch(self):
self.end_count += 1
@BatchController.memoise
def get_ender(user):
return Ender()
# end_batch should get called once on exiting the batch
with BatchController.batch(self.USER_1):
ender = get_ender(self.USER_1)
self.assertEquals(1, ender.end_count)
# end_batch should get called once on exiting the batch
# no matter how deep the object gets cached
with BatchController.batch(self.USER_1):
with BatchController.batch(self.USER_1):
ender = get_ender(self.USER_1)
self.assertEquals(1, ender.end_count)
|