Skip to content

Commit

Permalink
Add personId partitionKey generator (#125)
Browse files Browse the repository at this point in the history
* Add personId partitionKey generator
  • Loading branch information
nikhilym committed Nov 18, 2019
1 parent 8932fb0 commit 80dba6e
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 80dba6e

Please sign in to comment.