diff --git a/ask-sdk-dynamodb-persistence-adapter/ask_sdk_dynamodb/partition_keygen.py b/ask-sdk-dynamodb-persistence-adapter/ask_sdk_dynamodb/partition_keygen.py index 0e401e9..7cf54d1 100644 --- a/ask-sdk-dynamodb-persistence-adapter/ask_sdk_dynamodb/partition_keygen.py +++ b/ask-sdk-dynamodb-persistence-adapter/ask_sdk_dynamodb/partition_keygen.py @@ -59,3 +59,32 @@ def device_id_partition_keygen(request_envelope): except AttributeError: raise PersistenceException("Couldn't retrieve device id from " "request envelope, for partition key use") + + +def person_id_partition_keygen(request_envelope): + # type: (RequestEnvelope) -> str + """Retrieve person id from request envelope, to use as object key. + + This method retrieves the `person id` specific to a voice profile from + the request envelope if it exists, to be used as an object key. If it + doesn't exist, then the `user id` is returned instead. + + :param request_envelope: Request Envelope passed during skill + invocation + :type request_envelope: ask_sdk_model.RequestEnvelope + :return: person Id retrieved from request envelope if exists, else + fall back on User Id + :rtype: str + :raises: :py:class:`ask_sdk_core.exceptions.PersistenceException` + """ + try: + person_id = request_envelope.context.system.person.person_id + except AttributeError: + try: + person_id = user_id_partition_keygen( + request_envelope=request_envelope) + except PersistenceException: + raise PersistenceException( + "Couldn't retrieve person id or user id from request envelope, " + "for partition key use") + return person_id diff --git a/ask-sdk-dynamodb-persistence-adapter/tests/unit/test_partition_keygen.py b/ask-sdk-dynamodb-persistence-adapter/tests/unit/test_partition_keygen.py index 79510a6..5789ec6 100644 --- a/ask-sdk-dynamodb-persistence-adapter/tests/unit/test_partition_keygen.py +++ b/ask-sdk-dynamodb-persistence-adapter/tests/unit/test_partition_keygen.py @@ -17,11 +17,12 @@ # import unittest -from ask_sdk_model import RequestEnvelope, Context, User, Device +from ask_sdk_model import RequestEnvelope, Context, User, Device, Person from ask_sdk_model.interfaces.system import SystemState from ask_sdk_core.exceptions import PersistenceException from ask_sdk_dynamodb.partition_keygen import ( - user_id_partition_keygen, device_id_partition_keygen) + user_id_partition_keygen, device_id_partition_keygen, + person_id_partition_keygen) class TestPartitionKeyGenerators(unittest.TestCase): @@ -31,6 +32,7 @@ def setUp(self): self.system = SystemState() self.user = User() self.device = Device() + self.person = Person() def test_valid_user_id_partition_keygen(self): self.user.user_id = "123" @@ -138,6 +140,74 @@ def test_device_id_partition_keygen_raise_error_when_device_null(self): "null device provided in context.system of " "request envelope") + def test_valid_person_id_partition_keygen(self): + self.person.person_id = "123" + self.system.person = self.person + self.context.system = self.system + self.request_envelope.context = self.context + + assert person_id_partition_keygen(self.request_envelope) == "123", ( + "Person Id Partition Key Generation retrieved wrong person id from " + "valid person object in request envelope") + + def test_person_id_partition_keygen_return_user_id_when_person_null(self): + self.person = None + self.system.person = self.person + self.user.user_id = "123" + self.system.user = self.user + self.context.system = self.system + self.request_envelope.context = self.context + + assert person_id_partition_keygen(self.request_envelope) == "123", ( + "Person Id Partition Key Generation retrieved wrong id from " + "request envelope when person is none and user id exists") + + def test_person_id_partition_keygen_raise_error_when_person_null_user_null( + self): + self.context.system = self.system + self.request_envelope.context = self.context + with self.assertRaises(PersistenceException) as exc: + person_id_partition_keygen(request_envelope=self.request_envelope) + + assert ("Couldn't retrieve person id or user id from request " + "envelope") in str( + exc.exception), ( + "Person Id Partition Key Generation didn't throw exception when " + "person and user are null in request envelope") + + def test_person_id_partition_keygen_raise_error_when_request_envelope_null(self): + with self.assertRaises(PersistenceException) as exc: + person_id_partition_keygen(request_envelope=None) + + assert ("Couldn't retrieve person id or user id from request " + "envelope") in str( + exc.exception), ( + "Person Id Partition Key Generation didn't throw exception when " + "null request envelope is provided") + + def test_person_id_partition_keygen_raise_error_when_context_null(self): + with self.assertRaises(PersistenceException) as exc: + person_id_partition_keygen(request_envelope=self.request_envelope) + + assert ("Couldn't retrieve person id or user id from request " + "envelope") in str( + exc.exception), ( + "Person Id Partition Key Generation didn't throw exception when " + "null context provided in request envelope") + + def test_person_id_partition_keygen_raise_error_when_system_null(self): + self.request_envelope.context = self.context + + with self.assertRaises(PersistenceException) as exc: + person_id_partition_keygen(request_envelope=self.request_envelope) + + assert ("Couldn't retrieve person id or user id from request " + "envelope") in str( + exc.exception), ( + "Partition Id Partition Key Generation didn't throw exception when " + "null system provided in context of " + "request envelope") + def tearDown(self): self.request_envelope = None self.context = None