Table class

class ddbmock.database.table.Table(name, rt, wt, hash_key, range_key, status='CREATING')

Table abstraction. Actual ddbmock.database.item.Item are stored in store.

Constructors

__init__

Table.__init__(name, rt, wt, hash_key, range_key, status='CREATING')

Create a new Table. When manually creating a table, make sure you registered it in ddbmock.database.db.DynamoDB with a something like dynamodb.data[name] = Table(name, "...").

Even though there are DELAY_CREATING seconds before the status is updated to ACTIVE, the table is immediately available. This is a slight difference with real DynamooDB to ease unit and functionnal tests.

Parameters:
  • name – Valid table name. No further checks are performed.
  • rt – Provisioned read throughput.
  • wt – Provisioned write throughput.
  • hash_keyddbmock.database.key.Key instance describe the hash_key
  • hash_keyddbmock.database.key.Key instance describe the range_key or None if table has no range_key
  • status – (optional) Valid initial table status. If Table needd to be avaible immediately, use ACTIVE, otherwise, leave default value.

Note

rt and wt are only used by DescribeTable and UpdateTable. No throttling is nor will ever be done.

from_dict

classmethod Table.from_dict(data)

Alternate constructor which deciphers raw DynamoDB request data before ultimately calling regular __init__ method.

See __init__() for more insight.

Parameters:data – raw DynamoDB request data.
Returns:fully initialized Table instance

Table manipulations

truncate

Table.truncate()

Remove all Items from this table. This is like a reset. Might be very usefull in unit and functional tests.

delete

Table.delete()

If the table was ACTIVE, update its state to DELETING. This is not a destructor, only a sate updater and the Table instance will still be valid afterward. In all othercases, raise ddbmock.errors.ResourceInUseException.

If you want to perform the full table delete cycle, please use ddbmock.database.db.DynamoDB.delete_table() instead

Raises:ddbmock.errors.ResourceInUseException is the table was not in Active state

activate

Table.activate()

Unconditionnaly set Table status to ACTIVE. This method is automatically called by the constructor once DELAY_CREATING is over.

update_throughput

Table.update_throughput(rt, wt)

Update table throughput. Same conditions and limitations as real DynamoDB applies:

  • No more that 1 decrease operation per UTC day.
  • No more than doubling throughput at once.
  • Table must be in ACTIVE state.

Table status is then set to UPDATING until DELAY_UPDATING delay is over. Like real DynamoDB, the Table can still be used during this period

Parameters:
  • rt – New read throughput
  • wt – New write throughput
Raises:

ddbmock.errors.ResourceInUseException if table was not in ACTIVE state

Raises:

ddbmock.errors.LimitExceededException if the other above conditions are not met.

get_size

Table.get_size()

Compute the whole table size using the same rules as the real DynamoDB. Actual memory usage in ddbmock will be much higher due to dict and Python overheadd.

Note

Real DynamoDB updates this result every 6 hours or so while this is an “on demand” call.

Returns:cumulated size of all items following DynamoDB size computation.

to_dict

Table.to_dict(verbose=True)

Serialize this table to DynamoDB compatible format. Every fields are realistic, including the TableSizeBytes which relies on get_size.()

Some DynamoDB requests only send a minimal version of Table metadata. to reproduce this behavior, just set verbose to False.

Parameters:verbose – Set to False to skip table size computation.
Returns:Serialized version of table metadata compatible with DynamoDB API syntax.

Items manipulations

delete_item

Table.delete_item(key, expected)

Delete item at key from the databse provided that it matches expected values.

This operation is atomic and blocks all other pending write operations.

Parameters:
  • key – Raw DynamoDB request hash and range key dict.
  • expected – Raw DynamoDB request conditions.
Returns:

deepcopy of ddbmock.database.item.Item as it was before deletion.

Raises:

ddbmock.errors.ConditionalCheckFailedException if conditions are not met.

update_item

Table.update_item(key, actions, expected)

Apply actions to item at key provided that it matches expected.

This operation is atomic and blocks all other pending write operations.

Parameters:
  • key – Raw DynamoDB request hash and range key dict.
  • actions – Raw DynamoDB request actions.
  • expected – Raw DynamoDB request conditions.
Returns:

both deepcopies of ddbmock.database.item.Item as it was (before, after) the update.

Raises:

ddbmock.errors.ConditionalCheckFailedException if conditions are not met.

Raises:

ddbmock.errors.ValidationException if actions attempted to modify the key or the resulting Item is biggere than config.MAX_ITEM_SIZE

put

Table.put(item, expected)

Save item in the database provided that expected matches. Even though DynamoDB UpdateItem operation only supports returning ALL_OLD or NONE, this method returns both old and new values as the throughput, computed in the view, takes the maximum of both size into account.

This operation is atomic and blocks all other pending write operations.

Parameters:
  • item – Raw DynamoDB request item.
  • expected – Raw DynamoDB request conditions.
Returns:

both deepcopies of ddbmock.database.item.Item as it was (before, after) the update or empty item if not found.

Raises:

ddbmock.errors.ConditionalCheckFailedException if conditions are not met.

get

Table.get(key, fields)

Get fields from ddbmock.database.item.Item at key.

Parameters:
  • key – Raw DynamoDB request key.
  • fields – Raw DynamoDB request array of field names to return. Empty to return all.
Returns:

reference to ddbmock.database.item.Item at key or None when not found

Raises:

ddbmock.errors.ValidationException if a range_key was provided while table has none.

query

Table.query(hash_key, rk_condition, fields, start, reverse, limit)

Return fields of all items with provided hash_key whose range_key matches rk_condition.

Parameters:
  • hash_key – Raw DynamoDB request hash_key.
  • rk_condition – Raw DynamoDB request range_key condition.
  • fields – Raw DynamoDB request array of field names to return. Empty to return all.
  • start – Raw DynamoDB request key of the first item to scan. Empty array to indicate first item.
  • reverse – Set to True to parse the range keys backward.
  • limit – Maximum number of items to return in this batch. Set to 0 or less for no maximum.
Returns:

Results(results, cumulated_size, last_key)

Raises:

ddbmock.errors.ValidationException if start['HashKeyElement'] is not hash_key

scan

Table.scan(scan_conditions, fields, start, limit)

Return fields of all items matching scan_conditions. No matter the start key, scan allways starts from teh beginning so that it might be quite slow.

Parameters:
  • scan_conditions – Raw DynamoDB request conditions.
  • fields – Raw DynamoDB request array of field names to return. Empty to return all.
  • start – Raw DynamoDB request key of the first item to scan. Empty array to indicate first item.
  • limit – Maximum number of items to return in this batch. Set to 0 or less for no maximum.
Returns:

Results(results, cumulated_size, last_key, scanned_count)