1717#
1818import unittest
1919
20- from ask_sdk_model import RequestEnvelope , Context , User , Device
20+ from ask_sdk_model import RequestEnvelope , Context , User , Device , Person
2121from ask_sdk_model .interfaces .system import SystemState
2222from ask_sdk_core .exceptions import PersistenceException
2323from ask_sdk_dynamodb .partition_keygen import (
24- user_id_partition_keygen , device_id_partition_keygen )
24+ user_id_partition_keygen , device_id_partition_keygen ,
25+ person_id_partition_keygen )
2526
2627
2728class TestPartitionKeyGenerators (unittest .TestCase ):
@@ -31,6 +32,7 @@ def setUp(self):
3132 self .system = SystemState ()
3233 self .user = User ()
3334 self .device = Device ()
35+ self .person = Person ()
3436
3537 def test_valid_user_id_partition_keygen (self ):
3638 self .user .user_id = "123"
@@ -138,6 +140,74 @@ def test_device_id_partition_keygen_raise_error_when_device_null(self):
138140 "null device provided in context.system of "
139141 "request envelope" )
140142
143+ def test_valid_person_id_partition_keygen (self ):
144+ self .person .person_id = "123"
145+ self .system .person = self .person
146+ self .context .system = self .system
147+ self .request_envelope .context = self .context
148+
149+ assert person_id_partition_keygen (self .request_envelope ) == "123" , (
150+ "Person Id Partition Key Generation retrieved wrong person id from "
151+ "valid person object in request envelope" )
152+
153+ def test_person_id_partition_keygen_return_user_id_when_person_null (self ):
154+ self .person = None
155+ self .system .person = self .person
156+ self .user .user_id = "123"
157+ self .system .user = self .user
158+ self .context .system = self .system
159+ self .request_envelope .context = self .context
160+
161+ assert person_id_partition_keygen (self .request_envelope ) == "123" , (
162+ "Person Id Partition Key Generation retrieved wrong id from "
163+ "request envelope when person is none and user id exists" )
164+
165+ def test_person_id_partition_keygen_raise_error_when_person_null_user_null (
166+ self ):
167+ self .context .system = self .system
168+ self .request_envelope .context = self .context
169+ with self .assertRaises (PersistenceException ) as exc :
170+ person_id_partition_keygen (request_envelope = self .request_envelope )
171+
172+ assert ("Couldn't retrieve person id or user id from request "
173+ "envelope" ) in str (
174+ exc .exception ), (
175+ "Person Id Partition Key Generation didn't throw exception when "
176+ "person and user are null in request envelope" )
177+
178+ def test_person_id_partition_keygen_raise_error_when_request_envelope_null (self ):
179+ with self .assertRaises (PersistenceException ) as exc :
180+ person_id_partition_keygen (request_envelope = None )
181+
182+ assert ("Couldn't retrieve person id or user id from request "
183+ "envelope" ) in str (
184+ exc .exception ), (
185+ "Person Id Partition Key Generation didn't throw exception when "
186+ "null request envelope is provided" )
187+
188+ def test_person_id_partition_keygen_raise_error_when_context_null (self ):
189+ with self .assertRaises (PersistenceException ) as exc :
190+ person_id_partition_keygen (request_envelope = self .request_envelope )
191+
192+ assert ("Couldn't retrieve person id or user id from request "
193+ "envelope" ) in str (
194+ exc .exception ), (
195+ "Person Id Partition Key Generation didn't throw exception when "
196+ "null context provided in request envelope" )
197+
198+ def test_person_id_partition_keygen_raise_error_when_system_null (self ):
199+ self .request_envelope .context = self .context
200+
201+ with self .assertRaises (PersistenceException ) as exc :
202+ person_id_partition_keygen (request_envelope = self .request_envelope )
203+
204+ assert ("Couldn't retrieve person id or user id from request "
205+ "envelope" ) in str (
206+ exc .exception ), (
207+ "Partition Id Partition Key Generation didn't throw exception when "
208+ "null system provided in context of "
209+ "request envelope" )
210+
141211 def tearDown (self ):
142212 self .request_envelope = None
143213 self .context = None
0 commit comments