Item class¶
-
class
ddbmock.database.item.
Item
(dico={})¶ Internal Item representation. The Item is stored in its raw DynamoDB request form and no parsing is involved unless specifically needed.
It adds a couple of handful helpers to the dict class such as DynamoDB actions, condition validations and specific size computation.
Constructors¶
Item manipulations¶
filter¶
-
Item.
filter
(fields)¶ Return a dict containing only the keys specified in
fields
. Iffields
evaluates to False (None, empty, ...), the original dict is returned untouched.Internal
ItemSize
of the filtered Item is set to original Item size as you pay for the data you operated on, not for what was actually sent over the wires.Parameters: fields – array of name of keys to keep Returns: filtered Item
apply_actions¶
-
Item.
apply_actions
(actions)¶ Apply
actions
to the current item. Mostly used byUpdateItem
. This also resets the cached item size.Warning
There is a corner case in
ADD
action. It will always behave as though the item already existed before that is to say, it the target field is a non existing set, it will always start a new one with this single value in it. In real DynamoDB, if Item was new, it should fail.Parameters: action – Raw DynamoDB request actions specification Raises: ddbmock.errors.ValidationException
whenever attempting an illegual action
assert_match_expected¶
-
Item.
assert_match_expected
(expected)¶ Make sure this Items matches the
expected
values. This may be used by any signe item write operation such asDeleteItem
,UpdateItem
andPutItem
.Parameters: expected – Raw DynamoDB request expected values Raises: ddbmock.errors.ConditionalCheckFailedException
if any of the expected values is not valid
match¶
-
Item.
match
(conditions)¶ Check if the current item matches conditions. Return False if a field is not found, or does not match. If condition is None, it is considered to match.
Condition name are assumed to be valid as Onctuous is in charge of input validation. Expect crashes otherwise :)
Parameters: - fieldname – Valid field name
- condition – Raw DynamoDB request condition of the form
{"OPERATOR": FIELDDEFINITION}
Returns: True
on success orFalse
on first failure
field_match¶
-
Item.
field_match
(fieldname, condition)¶ Check if a field matches a condition. Return False when field not found, or do not match. If condition is None, it is considered to match.
Condition name are assumed to be valid as Onctuous is in charge of input validation. Expect crashes otherwise :)
Parameters: - fieldname – Valid field name
- condition – Raw DynamoDB request condition of the form
{"OPERATOR": FIELDDEFINITION}
Returns: True
on success
read_key¶
-
Item.
read_key
(key, name=None, max_size=0)¶ Provided
key
, read field value atname
orkey.name
if not specified.Parameters: - key –
Key
orPrimaryKey
to read - name – override name field of key
- max_size – if specified, check that the item is bellow a treshold
Returns: field value at
key
Raises: ddbmock.errors.ValidationException
if field does not exist, type does not match or is abovemax_size
- key –
get_field_size¶
-
Item.
get_field_size
(fieldname)¶ Compute field size in bytes.
Parameters: fieldname – Valid field name Returns: Size of the field in bytes or 0 if the field was not found. Remember that empty fields are represented as missing values in DynamoDB.
get_size¶
-
Item.
get_size
()¶ Compute Item size as DynamoDB would. This is especially useful for enforcing the 64kb per item limit as well as the capacityUnit cost.
Note
the result is cached for efficiency. If you ever happend to directly edit values for any reason, do not forget to invalidate the cache:
self.size=None
Returns: ItemSize
DynamoDB item size in bytes
__sub__¶
-
Item.
__sub__
(other)¶ Utility function to compute a ‘diff’ of 2 Items. All fields of
self
(left operand) identical to those ofother
(right operand) are dicarded. The other fields fromself
are kept. This proves to be extremely useful to supportALL_NEW
andALL_OLD
return specification ofUpdateItem
in a clean and readable manner.Parameters: other – Item
to be used as filterReturns: dict with fields of self
not in or different fromother