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 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.DynamoDB
with a something likedynamodb.data[name] = Table(name, "...")
.Even though there are
DELAY_CREATING
seconds 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.Key
instance describe thehash_key
- hash_key –
ddbmock.database.key.Key
instance describe therange_key
orNone
if table has norange_key
- status – (optional) Valid initial table status. If Table needd to be avaible immediately, use
ACTIVE
, otherwise, leave default value.
Note
rt
andwt
are only used byDescribeTable
andUpdateTable
. 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 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.ResourceInUseException
is the table was not inActive
state
activate¶
-
Table.
activate
()¶ Unconditionnaly set Table status to
ACTIVE
. This method is automatically called by the constructor onceDELAY_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
untilDELAY_UPDATING
delay 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.ResourceInUseException
if table was not inACTIVE
stateRaises: 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 onget_size.()
Some DynamoDB requests only send a minimal version of Table metadata. to reproduce this behavior, just set
verbose
toFalse
.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 matchesexpected
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 atkey
provided 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.Item
as it was (before, after) the update.Raises: ddbmock.errors.ConditionalCheckFailedException
if conditions are not met.Raises: ddbmock.errors.ValidationException
ifactions
attempted to modify the key or the resulting Item is biggere thanconfig.MAX_ITEM_SIZE
put¶
-
Table.
put
(item, expected)¶ Save
item
in the database provided thatexpected
matches. Even though DynamoDBUpdateItem
operation only supports returningALL_OLD
orNONE
, this method returns bothold
andnew
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
fromddbmock.database.item.Item
atkey
.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
atkey
orNone
when not foundRaises: ddbmock.errors.ValidationException
if arange_key
was provided while table has none.
query¶
-
Table.
query
(hash_key, rk_condition, fields, start, reverse, limit)¶ Return
fields
of all items with providedhash_key
whoserange_key
matchesrk_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
ifstart['HashKeyElement']
is nothash_key
scan¶
-
Table.
scan
(scan_conditions, fields, start, limit)¶ Return
fields
of all items matchingscan_conditions
. No matter thestart
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)