/
test_notification.py
127 lines (89 loc) · 3.67 KB
/
test_notification.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
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
import pickle
import time
from unittest import TestCase
from nose.tools import istest
import redis
from pycket.driver import RedisDriver
from pycket.session import SessionManager, SessionMixin
from pycket.notification import NotificationManager, NotificationMixin
class RedisTestCase(TestCase):
dataset = None
def setUp(self):
if self.dataset is None:
self.dataset = redis.Redis(db=RedisDriver.DEFAULT_STORAGE_IDENTIFIERS['db_notifications'])
self.dataset.flushall()
class NotificationMixinTest(TestCase):
@istest
def starts_handler_with_session_manager(self):
class StubHandler(NotificationMixin):
settings = {}
self.assertIsInstance(StubHandler().notifications, NotificationManager)
class NotificationManagerTest(RedisTestCase):
@istest
def persists_in_a_different_name_from_session_manager(self):
self.assertNotEqual(RedisDriver.DEFAULT_STORAGE_IDENTIFIERS['db_notifications'], RedisDriver.DEFAULT_STORAGE_IDENTIFIERS['db_sessions'])
@istest
def gets_a_notification_only_once(self):
handler = StubHandler()
manager = NotificationManager(handler)
manager.set('foo', 'bar')
self.assertEqual(manager.get('foo'), 'bar')
self.assertIsNone(manager.get('foo'))
@istest
def removes_notification_from_database_after_retrieval(self):
handler = StubHandler()
manager = NotificationManager(handler)
manager.set('foo', 'bar')
raw_notifications = self.dataset.get(handler.session_id)
notifications = pickle.loads(raw_notifications)
self.assertEqual(notifications.keys(), ['foo'])
manager.get('foo')
raw_notifications = self.dataset.get(handler.session_id)
notifications = pickle.loads(raw_notifications)
self.assertEqual(notifications.keys(), [])
@istest
def gets_default_value_if_provided_and_not_in_dataset(self):
handler = StubHandler()
manager = NotificationManager(handler)
value = manager.get('foo', 'Default')
self.assertEqual(value, 'Default')
@istest
def sets_object_with_dict_key(self):
handler = StubHandler()
manager = NotificationManager(handler)
manager['foo'] = 'bar'
self.assertEqual(manager['foo'], 'bar')
@istest
def doesnt_conflict_with_sessions(self):
test_case = self
class StubHandler(SessionMixin, NotificationMixin):
session_id = 'session-id'
settings = {}
def get_secure_cookie(self, name):
return self.session_id
def test(self):
self.session.set('foo', 'foo-session')
self.notifications.set('foo', 'foo-notification')
test_case.assertEqual(self.session.get('foo'), 'foo-session')
test_case.assertEqual(self.notifications.get('foo'), 'foo-notification')
test_case.assertIsNone(self.notifications.get('foo'))
handler = StubHandler()
handler.test()
@istest
def uses_custom_notifications_database_if_provided(self):
handler = StubHandler()
handler.settings = {
'pycket_redis': {
'db_sessions': 10,
'db_notifications': 11,
}
}
manager = NotificationManager(handler)
manager.set('foo', 'bar')
self.assertEqual(manager.driver.dataset.connection_pool._available_connections[0].db, 11)
class StubHandler(object):
session_id = 'session-id'
def __init__(self, settings=None):
self.settings = settings if settings is not None else {}
def get_secure_cookie(self, name):
return self.session_id