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

__init__

Item.__init__(dico={})

Load a raw DynamoDb Item and enhance it with ou helpers. Also set the cached ItemSize to None to mark it as not computed. This avoids unnecessary computations on temporary Items.

Parameters:dico – Raw DynamoDB request Item

Item manipulations

filter

Item.filter(fields)

Return a dict containing only the keys specified in fields. If fields 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 by UpdateItem. 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 as DeleteItem, UpdateItem and PutItem.

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 or False 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 at name or key.name if not specified.

Parameters:
  • keyKey or PrimaryKey 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 above max_size

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 of other (right operand) are dicarded. The other fields from self are kept. This proves to be extremely useful to support ALL_NEW and ALL_OLD return specification of UpdateItem in a clean and readable manner.

Parameters:otherItem to be used as filter
Returns:dict with fields of self not in or different from other