@@ -21,13 +21,23 @@ def setUp(self):
2121 'sort_key' : 1520150272000000
2222 },
2323 {
24- 'article_id' : 'testid000001' ,
25- 'user_id' : 'test02' ,
26- 'sort_key' : 1520150272000001
24+ 'article_id' : 'testid000002' ,
25+ 'user_id' : 'test03_01' ,
26+ 'sort_key' : 1520150272000002
27+ },
28+ {
29+ 'article_id' : 'testid000002' ,
30+ 'user_id' : 'test03_02' ,
31+ 'sort_key' : 1520150272000002
2732 },
2833 {
2934 'article_id' : 'testid000002' ,
30- 'user_id' : 'test03' ,
35+ 'user_id' : 'test03_03' ,
36+ 'sort_key' : 1520150272000002
37+ },
38+ {
39+ 'article_id' : 'testid000002' ,
40+ 'user_id' : 'test03_04' ,
3141 'sort_key' : 1520150272000002
3242 }
3343 ]
@@ -38,7 +48,7 @@ def setUp(self):
3848 )
3949
4050 # create article_info_table
41- article_info_table_items = [
51+ self . article_info_table_items = [
4252 {
4353 'article_id' : 'testid000000' ,
4454 'title' : 'title1' ,
@@ -55,19 +65,36 @@ def setUp(self):
5565 },
5666 {
5767 'article_id' : 'testid000002' ,
58- 'title' : 'title3 ' ,
59- 'status' : 'draft ' ,
60- 'user_id' : 'article_user_id_01 ' ,
68+ 'title' : 'title3_updated ' ,
69+ 'status' : 'public ' ,
70+ 'user_id' : 'article_user_id_02 ' ,
6171 'sort_key' : 1520150272000002
6272 }
6373 ]
6474 TestsUtil .create_table (
6575 self .dynamodb ,
6676 os .environ ['ARTICLE_INFO_TABLE_NAME' ],
67- article_info_table_items
77+ self . article_info_table_items
6878 )
6979
70- TestsUtil .create_table (self .dynamodb , os .environ ['NOTIFICATION_TABLE_NAME' ], [])
80+ self .notification_items = [
81+ {
82+ 'notification_id' : 'like-article_user_id_02-testid000002' ,
83+ 'user_id' : 'article_user_id_02' ,
84+ 'sort_key' : 1520150272000010 ,
85+ 'article_id' : 'testid000002' ,
86+ 'article_title' : 'title3' ,
87+ 'type' : 'like' ,
88+ 'liked_count' : 4 ,
89+ 'created_at' : 1520150272
90+ }
91+ ]
92+
93+ TestsUtil .create_table (
94+ self .dynamodb ,
95+ os .environ ['NOTIFICATION_TABLE_NAME' ],
96+ self .notification_items
97+ )
7198
7299 self .unread_notification_manager_items = [
73100 {
@@ -93,7 +120,7 @@ def assert_bad_request(self, params):
93120 def test_main_ok_exist_article_id (self ):
94121 params = {
95122 'pathParameters' : {
96- 'article_id' : self .article_liked_user_table_items [0 ]['article_id' ]
123+ 'article_id' : self .article_info_table_items [0 ]['article_id' ]
97124 },
98125 'requestContext' : {
99126 'authorizer' : {
@@ -141,11 +168,11 @@ def test_main_ok_exist_article_id(self):
141168 ).get ('Item' )
142169 self .assertEqual (unread_notification_manager ['unread' ], True )
143170
144- @patch ('time.time' , MagicMock (return_value = 1520150272.000003 ))
171+ @patch ('time.time' , MagicMock (return_value = 1520150272.000015 ))
145172 def test_create_notification_and_unread_notification_manager (self ):
146173 params = {
147174 'pathParameters' : {
148- 'article_id' : self .article_liked_user_table_items [1 ]['article_id' ]
175+ 'article_id' : self .article_info_table_items [1 ]['article_id' ]
149176 },
150177 'requestContext' : {
151178 'authorizer' : {
@@ -168,34 +195,73 @@ def test_create_notification_and_unread_notification_manager(self):
168195 notification_after = notification_table .scan ()['Items' ]
169196 unread_notification_manager_after = unread_notification_manager_table .scan ()['Items' ]
170197
171- expected_notifications = [
172- {
173- 'user_id' : 'article_user_id_01' ,
174- 'sort_key' : 1520150272000003 ,
175- 'article_id' : 'testid000001' ,
176- 'article_title' : 'title2' ,
177- 'type' : 'like' ,
178- 'acted_user_id' : 'test06' ,
179- 'created_at' : 1520150272
180- }
181- ]
198+ expected_notification = {
199+ 'notification_id' : 'like-article_user_id_01-testid000001' ,
200+ 'user_id' : 'article_user_id_01' ,
201+ 'sort_key' : 1520150272000015 ,
202+ 'article_id' : 'testid000001' ,
203+ 'article_title' : 'title2' ,
204+ 'type' : 'like' ,
205+ 'liked_count' : 1 ,
206+ 'created_at' : 1520150272
207+ }
182208
209+ notification = notification_table .get_item (Key = {'notification_id' : 'like-article_user_id_01-testid000001' }).get ('Item' )
183210 unread_notification_manager = unread_notification_manager_table .get_item (
184211 Key = {'user_id' : 'article_user_id_01' }
185212 ).get ('Item' )
186213
187214 self .assertEqual (response ['statusCode' ], 200 )
188- self .assertEqual (notification_after , expected_notifications )
215+ self .assertEqual (notification , expected_notification )
189216 self .assertEqual (len (notification_after ), len (notification_before ) + 1 )
190217 self .assertEqual (unread_notification_manager ['unread' ], True )
191218 self .assertEqual (len (unread_notification_manager_after ), len (unread_notification_manager_before ) + 1 )
192219
220+ @patch ('time.time' , MagicMock (return_value = 1520150272.000015 ))
221+ def test_main_with_updating_notification (self ):
222+ params = {
223+ 'pathParameters' : {
224+ 'article_id' : self .article_info_table_items [2 ]['article_id' ]
225+ },
226+ 'requestContext' : {
227+ 'authorizer' : {
228+ 'claims' : {
229+ 'cognito:username' : 'test06'
230+ }
231+ }
232+ }
233+ }
234+
235+ notification_table = self .dynamodb .Table (os .environ ['NOTIFICATION_TABLE_NAME' ])
236+ notification_before = notification_table .scan ()['Items' ]
237+
238+ response = MeArticlesLikeCreate (event = params , context = {}, dynamodb = self .dynamodb ).main ()
239+
240+ notification_after = notification_table .scan ()['Items' ]
241+
242+ expected_notification = {
243+ 'notification_id' : 'like-article_user_id_02-testid000002' ,
244+ 'user_id' : 'article_user_id_02' ,
245+ 'sort_key' : 1520150272000015 ,
246+ 'article_id' : 'testid000002' ,
247+ 'article_title' : 'title3_updated' ,
248+ 'type' : 'like' ,
249+ 'liked_count' : 5 ,
250+ 'created_at' : 1520150272
251+ }
252+
253+ notification = notification_table .get_item (Key = {'notification_id' : 'like-article_user_id_02-testid000002' }).get ('Item' )
254+
255+ self .assertEqual (response ['statusCode' ], 200 )
256+ self .assertEqual (notification , expected_notification )
257+ self .assertEqual (len (notification_after ), len (notification_before ))
258+
193259 @patch ('me_articles_like_create.MeArticlesLikeCreate._MeArticlesLikeCreate__create_like_notification' ,
194260 MagicMock (side_effect = Exception ()))
195261 def test_raise_exception_in_creating_notification (self ):
196262 params = {
197263 'pathParameters' : {
198- 'article_id' : self .article_liked_user_table_items [0 ]['article_id' ]
264+ 'article_id' : self .article_info_table_items [0 ]['article_id' ]
199265 },
200266 'requestContext' : {
201267 'authorizer' : {
0 commit comments