Skip to content

Commit 80dba6e

Browse files
authored
Add personId partitionKey generator (#125)
* Add personId partitionKey generator
1 parent 8932fb0 commit 80dba6e

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

ask-sdk-dynamodb-persistence-adapter/ask_sdk_dynamodb/partition_keygen.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,32 @@ def device_id_partition_keygen(request_envelope):
5959
except AttributeError:
6060
raise PersistenceException("Couldn't retrieve device id from "
6161
"request envelope, for partition key use")
62+
63+
64+
def person_id_partition_keygen(request_envelope):
65+
# type: (RequestEnvelope) -> str
66+
"""Retrieve person id from request envelope, to use as object key.
67+
68+
This method retrieves the `person id` specific to a voice profile from
69+
the request envelope if it exists, to be used as an object key. If it
70+
doesn't exist, then the `user id` is returned instead.
71+
72+
:param request_envelope: Request Envelope passed during skill
73+
invocation
74+
:type request_envelope: ask_sdk_model.RequestEnvelope
75+
:return: person Id retrieved from request envelope if exists, else
76+
fall back on User Id
77+
:rtype: str
78+
:raises: :py:class:`ask_sdk_core.exceptions.PersistenceException`
79+
"""
80+
try:
81+
person_id = request_envelope.context.system.person.person_id
82+
except AttributeError:
83+
try:
84+
person_id = user_id_partition_keygen(
85+
request_envelope=request_envelope)
86+
except PersistenceException:
87+
raise PersistenceException(
88+
"Couldn't retrieve person id or user id from request envelope, "
89+
"for partition key use")
90+
return person_id

ask-sdk-dynamodb-persistence-adapter/tests/unit/test_partition_keygen.py

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
#
1818
import unittest
1919

20-
from ask_sdk_model import RequestEnvelope, Context, User, Device
20+
from ask_sdk_model import RequestEnvelope, Context, User, Device, Person
2121
from ask_sdk_model.interfaces.system import SystemState
2222
from ask_sdk_core.exceptions import PersistenceException
2323
from 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

2728
class 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

Comments
 (0)