Table class¶
-
class
ddbmock.database.table.Table(name, rt, wt, hash_key, range_key, status='CREATING')¶ Table abstraction. Actual
ddbmock.database.item.Itemare stored instore.
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 inddbmock.database.db.DynamoDBwith a something likedynamodb.data[name] = Table(name, "...").Even though there are
DELAY_CREATINGseconds before the status is updated toACTIVE, 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_key –
ddbmock.database.key.Keyinstance describe thehash_key - hash_key –
ddbmock.database.key.Keyinstance describe therange_keyorNoneif table has norange_key - status – (optional) Valid initial table status. If Table needd to be avaible immediately, use
ACTIVE, otherwise, leave default value.
Note
rtandwtare only used byDescribeTableandUpdateTable. 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 Tableinstance
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 toDELETING. This is not a destructor, only a sate updater and the Table instance will still be valid afterward. In all othercases, raiseddbmock.errors.ResourceInUseException.If you want to perform the full table delete cycle, please use
ddbmock.database.db.DynamoDB.delete_table()insteadRaises: ddbmock.errors.ResourceInUseExceptionis the table was not inActivestate
activate¶
-
Table.activate()¶ Unconditionnaly set Table status to
ACTIVE. This method is automatically called by the constructor onceDELAY_CREATINGis 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
ACTIVEstate.
Table status is then set to
UPDATINGuntilDELAY_UPDATINGdelay is over. Like real DynamoDB, the Table can still be used during this periodParameters: - rt – New read throughput
- wt – New write throughput
Raises: ddbmock.errors.ResourceInUseExceptionif table was not inACTIVEstateRaises: ddbmock.errors.LimitExceededExceptionif 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
TableSizeByteswhich relies onget_size.()Some DynamoDB requests only send a minimal version of Table metadata. to reproduce this behavior, just set
verbosetoFalse.Parameters: verbose – Set to Falseto 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
keyfrom the databse provided that it matchesexpectedvalues.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.Itemas it was before deletion.Raises: ddbmock.errors.ConditionalCheckFailedExceptionif conditions are not met.
update_item¶
-
Table.update_item(key, actions, expected)¶ Apply
actionsto item atkeyprovided that it matchesexpected.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.Itemas it was (before, after) the update.Raises: ddbmock.errors.ConditionalCheckFailedExceptionif conditions are not met.Raises: ddbmock.errors.ValidationExceptionifactionsattempted to modify the key or the resulting Item is biggere thanconfig.MAX_ITEM_SIZE
put¶
-
Table.put(item, expected)¶ Save
itemin the database provided thatexpectedmatches. Even though DynamoDBUpdateItemoperation only supports returningALL_OLDorNONE, this method returns botholdandnewvalues 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.Itemas it was (before, after) the update or empty item if not found.Raises: ddbmock.errors.ConditionalCheckFailedExceptionif conditions are not met.
get¶
-
Table.get(key, fields)¶ Get
fieldsfromddbmock.database.item.Itematkey.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.ItematkeyorNonewhen not foundRaises: ddbmock.errors.ValidationExceptionif arange_keywas provided while table has none.
query¶
-
Table.query(hash_key, rk_condition, fields, start, reverse, limit)¶ Return
fieldsof all items with providedhash_keywhoserange_keymatchesrk_condition.Parameters: - hash_key – Raw DynamoDB request hash_key.
- rk_condition – Raw DynamoDB request
range_keycondition. - 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
Trueto 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.ValidationExceptionifstart['HashKeyElement']is nothash_key
scan¶
-
Table.scan(scan_conditions, fields, start, limit)¶ Return
fieldsof all items matchingscan_conditions. No matter thestartkey,scanallways 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)