forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
71 lines (55 loc) · 2.04 KB
/
tests.py
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
from typing import Optional
from django.test import SimpleTestCase
from django.http import HttpResponse
from .request_globals import RequestGlobal
class TestGlobal(RequestGlobal):
__test__ = False # Prevents this class from being picked up by pytest
test = None
def init(self):
self.test = "test"
class PassthroughMiddleware:
def __call__(self, _request):
return
def capture_test_global(_get_response):
def inner(_request):
request_global_test_obj.obj = TestGlobal()
return HttpResponse()
return inner
request_global_test_obj = None
class RequestGlobalTest(SimpleTestCase):
obj: Optional[TestGlobal]
def setUp(self) -> None:
global request_global_test_obj
RequestGlobal.clear_globals()
request_global_test_obj = self
def test_clear_globals(self):
obj = TestGlobal()
RequestGlobal.clear_globals()
obj2 = TestGlobal()
self.assertIsNot(obj, obj2)
def test_returns_same_object(self):
obj = TestGlobal()
self.assertIsInstance(obj, TestGlobal)
obj2 = TestGlobal()
self.assertIs(obj, obj2)
obj3 = TestGlobal()
self.assertIs(obj, obj3)
def test_is_request_specific(self):
with self.modify_settings(MIDDLEWARE={"append": ["lib.tests.capture_test_global"]}):
obj2 = TestGlobal()
self.client.get("/")
self.assertIsInstance(self.obj, TestGlobal)
obj2 = self.obj
self.client.get("/")
self.assertIsInstance(self.obj, TestGlobal)
self.assertIsNot(self.obj, obj2)
def test_is_cleared_at_the_end(self):
with self.modify_settings(MIDDLEWARE={"append": ["lib.tests.capture_test_global"]}):
self.client.get("/")
obj2 = TestGlobal()
self.assertIsInstance(self.obj, TestGlobal)
self.assertIsInstance(obj2, TestGlobal)
self.assertIsNot(self.obj, obj2)
def test_init_called(self):
obj = TestGlobal()
self.assertEqual(obj.test, "test")