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. Iffieldsevaluates to False (None, empty, ...), the original dict is returned untouched.Internal
ItemSizeof 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
actionsto the current item. Mostly used byUpdateItem. This also resets the cached item size.Warning
There is a corner case in
ADDaction. 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.ValidationExceptionwhenever attempting an illegual action
assert_match_expected¶
-
Item.assert_match_expected(expected)¶ Make sure this Items matches the
expectedvalues. This may be used by any signe item write operation such asDeleteItem,UpdateItemandPutItem.Parameters: expected – Raw DynamoDB request expected values Raises: ddbmock.errors.ConditionalCheckFailedExceptionif 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: Trueon success orFalseon 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: Trueon success
read_key¶
-
Item.read_key(key, name=None, max_size=0)¶ Provided
key, read field value atnameorkey.nameif not specified.Parameters: - key –
KeyorPrimaryKeyto read - name – override name field of key
- max_size – if specified, check that the item is bellow a treshold
Returns: field value at
keyRaises: ddbmock.errors.ValidationExceptionif 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=NoneReturns: ItemSizeDynamoDB 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 fromselfare kept. This proves to be extremely useful to supportALL_NEWandALL_OLDreturn specification ofUpdateItemin a clean and readable manner.Parameters: other – Itemto be used as filterReturns: dict with fields of selfnot in or different fromother