@@ -103,26 +103,28 @@ def setUp(self):
103103 self .author = User .objects .create_user ('John' )
104104 self .article = Article .objects .create (title = 'The Old Witch' , author = self .author )
105105 self .random_user = User .objects .create_user ('Mr.Random' )
106+ self .random_user2 = User .objects .create_user ('Mr.Random 2' )
106107
107108 self .template = NotificationTemplate .objects .create (
108109 title = '{{article}}' ,
109110 text = '{{author}} created a new article named {{article}}.' ,
110111 trigger_action = '{{article.get_absolute_url}}' ,
111112 )
112113
113- self .notification = Notification . objects . create (
114+ self .notification = Notification (
114115 recipient = self .recipient ,
115116 template = self .template ,
116- related_objects = {
117- 'article' : self .article ,
118- 'author' : self .article .author ,
119- 'random_user' : self .random_user ,
120- },
121- extra_data = {
122- 'some_value' : 123 ,
123- 'decimal_value' : Decimal ('1.55' ),
124- }
125117 )
118+ self .notification .set_local_related_objects ({
119+ 'article' : self .article ,
120+ 'author' : self .article .author ,
121+ 'random_user' : self .random_user ,
122+ })
123+ self .notification .set_extra_data ({
124+ 'some_value' : 123 ,
125+ 'decimal_value' : Decimal ('1.55' ),
126+ })
127+ self .notification .save ()
126128
127129 def test_generated_fields_should_use_template_for_rendering (self ):
128130 self .assertEqual (self .notification .title , 'The Old Witch' )
@@ -148,11 +150,27 @@ def test_extra_data_should_be_dictionary(self):
148150 self .notification .set_extra_data (1000 )
149151
150152 def test_related_objects_and_extra_data_should_not_contain_same_keys (self ):
153+ self .notification .set_local_related_objects ({'random_user' : self .random_user })
154+ with self .assertRaises (ValueError ):
155+ self .notification .context
156+ with self .assertRaises (ValueError ):
157+ self .notification .save ()
158+
159+ self .notification .set_local_related_objects ({'some_value' : self .random_user })
160+ with self .assertRaises (ValueError ):
161+ self .notification .context
162+ with self .assertRaises (ValueError ):
163+ self .notification .save ()
164+
165+ self .notification .set_local_related_objects ({})
151166 self .notification .set_extra_data ({'article' : 123 })
167+ with self .assertRaises (ValueError ):
168+ self .notification .context
152169 with self .assertRaises (ValueError ):
153170 self .notification .save ()
154171
155172 def test_context_should_contain_related_objects_as_proxies_and_extra_data (self ):
173+ self .notification .set_local_related_objects ({'local_user' : self .random_user2 })
156174 ctx = self .notification .context
157175
158176 self .assertTrue (isinstance (ctx ['article' ], SecureRelatedObject ))
@@ -164,6 +182,9 @@ def test_context_should_contain_related_objects_as_proxies_and_extra_data(self):
164182 self .assertTrue (isinstance (ctx ['random_user' ], SecureRelatedObject ))
165183 self .assertEqual (ctx ['random_user' ]._object , self .random_user )
166184
185+ self .assertTrue (isinstance (ctx ['local_user' ], SecureRelatedObject ))
186+ self .assertEqual (ctx ['local_user' ]._object , self .random_user2 )
187+
167188 self .assertEqual (ctx ['some_value' ], 123 )
168189 self .assertEqual (ctx ['decimal_value' ], '1.55' )
169190
@@ -186,26 +207,55 @@ def test_creating_notification_should_not_be_possible_with_related_objects_in_in
186207 ['abc' , 'abc' ],
187208 {'abc' : 'abc' },
188209 )
210+ notification = Notification .objects .create (recipient = self .recipient , template = self .template )
189211 for related_objects in INVALID_RELATED_OBJECTS :
190212 with self .assertRaises (TypeError ):
191- Notification .objects .create (
192- recipient = self .recipient ,
193- template = self .template ,
194- related_objects = related_objects
195- )
213+ notification .set_local_related_objects (related_objects )
214+ notification .save ()
215+ notification .set_local_related_objects ({})
196216
197217 def test_creating_notification_should_allow_list_of_related_objects (self ):
198- notification = Notification .objects .create (
199- recipient = self .recipient ,
200- template = self .template ,
201- related_objects = [self .random_user ],
202- )
218+ notification = Notification (recipient = self .recipient , template = self .template )
219+ notification .set_local_related_objects ([self .random_user ])
220+ notification .save ()
203221
204222 self .assertEqual (notification .related_objects .count (), 1 )
205223 related_object = notification .related_objects .get ()
206224 self .assertEqual (related_object .name , None )
207225 self .assertEqual (related_object .content_object , self .random_user )
208226 self .assertEqual (notification .context , {})
209227
228+ def test_saving_notification_should_save_list_of_local_related_objects_into_db (self ):
229+ notification = Notification (recipient = self .recipient , template = self .template )
230+
231+ notification .set_local_related_objects ([self .random_user2 ])
232+ self .assertEqual (len (notification ._local_related_objects_list ), 1 )
233+ self .assertEqual (notification ._local_related_objects_list [0 ], self .random_user2 )
234+ self .assertEqual (notification .related_objects .count (), 0 )
235+
236+ notification .save ()
237+ self .assertEqual (len (notification ._local_related_objects_list ), 0 )
238+ self .assertEqual (notification .related_objects .count (), 1 )
239+
240+ related_object = notification .related_objects .get ()
241+ self .assertEqual (related_object .name , None )
242+ self .assertEqual (related_object .content_object , self .random_user2 )
243+
244+ def test_saving_notification_should_save_dictionary_of_local_related_objects_into_db (self ):
245+ notification = Notification (recipient = self .recipient , template = self .template )
246+
247+ notification .set_local_related_objects ({'random_user2' : self .random_user2 })
248+ self .assertEqual (len (notification ._local_related_objects_dict ), 1 )
249+ self .assertEqual (notification ._local_related_objects_dict ['random_user2' ], self .random_user2 )
250+ self .assertEqual (notification .related_objects .count (), 0 )
251+
252+ notification .save ()
253+ self .assertEqual (len (notification ._local_related_objects_dict ), 0 )
254+ self .assertEqual (notification .related_objects .count (), 1 )
255+
256+ related_object = notification .related_objects .get ()
257+ self .assertEqual (related_object .name , 'random_user2' )
258+ self .assertEqual (related_object .content_object , self .random_user2 )
259+
210260 def test_notification_should_have_string_representation (self ):
211261 self .assertEqual (str (self .notification ), 'notification #{}' .format (self .notification .pk ))
0 commit comments