本文整理汇总了Python中ddbmock.database.table.Table.put方法的典型用法代码示例。如果您正苦于以下问题:Python Table.put方法的具体用法?Python Table.put怎么用?Python Table.put使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ddbmock.database.table.Table
的用法示例。
在下文中一共展示了Table.put方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestGetItem
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
class TestGetItem(unittest.TestCase):
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
from ddbmock.database.db import dynamodb
from ddbmock import main
app = main({})
from webtest import TestApp
self.app = TestApp(app)
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
self.t1 = Table(TABLE_NAME, TABLE_RT, TABLE_WT, hash_key, range_key)
dynamodb.data[TABLE_NAME] = self.t1
self.t1.put(ITEM, {})
def tearDown(self):
from ddbmock.database.db import dynamodb
dynamodb.hard_reset()
def test_get_hr_attr_to_get(self):
from ddbmock.database.db import dynamodb
request = {
"TableName": TABLE_NAME,
"Key": {
"HashKeyElement": HK,
"RangeKeyElement": RK,
},
"AttributesToGet": ["relevant_data"],
}
expected = {
u'ConsumedCapacityUnits': 0.5,
u'Item': {
u'relevant_data': ITEM[u'relevant_data'],
}
}
# Protocol check
res = self.app.post_json('/', request, headers=HEADERS, status=200)
self.assertEqual(expected, json.loads(res.body))
self.assertEqual('application/x-amz-json-1.0; charset=UTF-8', res.headers['Content-Type'])
示例2: setUp
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
t1 = Table(TABLE_NAME, TABLE_RT, TABLE_WT, hash_key, range_key, status='ACTIVE')
t1.put(ITEM1, {})
t1.put(ITEM2, {})
dynamodb.data[TABLE_NAME] = t1
示例3: TestDeleteItem
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
class TestDeleteItem(unittest.TestCase):
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
from ddbmock.database.db import dynamodb
from ddbmock import main
app = main({})
from webtest import TestApp
self.app = TestApp(app)
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
self.t1 = Table(TABLE_NAME, TABLE_RT, TABLE_WT, hash_key, range_key)
dynamodb.data[TABLE_NAME] = self.t1
self.t1.put(ITEM, {})
def tearDown(self):
from ddbmock.database.db import dynamodb
dynamodb.hard_reset()
def test_delete_item_hr(self):
from ddbmock.database.db import dynamodb
request = {
"TableName": TABLE_NAME,
"Key": {
"HashKeyElement": HK,
"RangeKeyElement": RK,
},
}
expected = {
u"ConsumedCapacityUnits": 1,
}
# Protocol check
res = self.app.post_json('/', request, headers=HEADERS, status=200)
self.assertEqual(expected, json.loads(res.body))
self.assertEqual('application/x-amz-json-1.0; charset=UTF-8', res.headers['Content-Type'])
# Live data check
self.assertNotIn((HK_VALUE, RK_VALUE), self.t1.store)
示例4: TestUpdateItem
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
class TestUpdateItem(unittest.TestCase):
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
import helpers
self.app = helpers.makeTestApp()
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
self.t1 = Table(TABLE_NAME, TABLE_RT, TABLE_WT, hash_key, range_key)
dynamodb.data[TABLE_NAME] = self.t1
self.t1.put(ITEM, {})
def tearDown(self):
from ddbmock.database.db import dynamodb
dynamodb.hard_reset()
def test_update_item_put_hr(self):
request = {
"TableName": TABLE_NAME,
"Key": {
"HashKeyElement": HK,
"RangeKeyElement": RK,
},
"AttributeUpdates": {'relevant_data': {'Value': RELEVANT_FIELD}},
}
expected = {"ConsumedCapacityUnits": 1}
# Protocol check
res = self.app.post_json('/', request, headers=HEADERS, status=200)
self.assertEqual(expected, json.loads(res.body))
self.assertEqual('application/x-amz-json-1.0; charset=UTF-8',
res.headers['Content-Type'])
# Live data check
self.assertEqual(RELEVANT_FIELD,
self.t1.store[HK_VALUE, RK_VALUE]['relevant_data'])
示例5: TestScan
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
class TestScan(unittest.TestCase):
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
self.t1 = Table(TABLE_NAME, TABLE_RT, TABLE_WT, hash_key, range_key)
dynamodb.data[TABLE_NAME] = self.t1
self.t1.put(ITEM1, {})
self.t1.put(ITEM2, {})
self.t1.put(ITEM3, {})
self.t1.put(ITEM4, {})
self.t1.put(ITEM5, {})
self.t1.put(ITEM6, {})
def tearDown(self):
from ddbmock.database.db import dynamodb
from ddbmock import clean_boto_patch
dynamodb.hard_reset()
clean_boto_patch()
def test_scan_all(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
expected = {
u"Count": 6,
u"ScannedCount": 6,
u"Items": [ITEM2, ITEM1, ITEM6, ITEM5, ITEM4, ITEM3],
u"ConsumedCapacityUnits": 1.5,
}
db = connect_boto_patch()
ret = db.layer1.scan(TABLE_NAME, None)
self.assertEqual(expected, ret)
def test_scan_paged(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
from boto.dynamodb.exceptions import DynamoDBValidationError
esk = {
u'HashKeyElement': {u'N': u'789'},
u'RangeKeyElement': {u'S': u'Waldo-5'},
}
expected1 = {
u"Count": 4,
u"ScannedCount": 4,
u"Items": [ITEM2, ITEM1, ITEM6, ITEM5],
u"ConsumedCapacityUnits": 1.5,
u'LastEvaluatedKey': esk,
}
expected2 = {
u"Count": 2,
u"ScannedCount": 2,
u"Items": [ITEM4, ITEM3],
u"ConsumedCapacityUnits": 0.5,
}
db = connect_boto_patch()
ret = db.layer1.scan(TABLE_NAME, limit=4)
self.assertEqual(expected1, ret)
ret = db.layer1.scan(TABLE_NAME, exclusive_start_key=esk)
self.assertEqual(expected2, ret)
def test_scan_all_filter_fields(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
self.maxDiff = None
expected = {
u"Count": 6,
u"ScannedCount": 6,
u"Items": [
{u"relevant_data": {u"S": u"tete"}},
{u"relevant_data": {u"S": u"tata"}},
{u"relevant_data": {u"S": u"tyty"}},
{u"relevant_data": {u"S": u"tutu"}},
{u"relevant_data": {u"S": u"toto"}},
{u"relevant_data": {u"S": u"titi"}},
],
u"ConsumedCapacityUnits": 1.5,
}
fields = [u'relevant_data']
db = connect_boto_patch()
ret = db.layer1.scan(TABLE_NAME, None, fields)
self.assertEqual(expected, ret)
#.........这里部分代码省略.........
示例6: TestScan
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
class TestScan(unittest.TestCase):
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
import helpers
self.app = helpers.makeTestApp()
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
self.t1 = Table(TABLE_NAME, TABLE_RT, TABLE_WT, hash_key, range_key)
dynamodb.data[TABLE_NAME] = self.t1
self.t1.put(ITEM1, {})
self.t1.put(ITEM2, {})
self.t1.put(ITEM3, {})
self.t1.put(ITEM4, {})
self.t1.put(ITEM5, {})
def tearDown(self):
from ddbmock.database.db import dynamodb
dynamodb.hard_reset()
def test_scan_condition_filter_fields(self):
request = {
"TableName": TABLE_NAME,
"ScanFilter": {
"relevant_data": {
"AttributeValueList": [
{"S":"toto"},
{"S":"titi"},
{"S":"tata"},
],
"ComparisonOperator": "IN",
},
},
"AttributesToGet": [u'relevant_data'],
}
expected = {
u"Count": 3,
u"ScannedCount": 5,
u"Items": [
{u"relevant_data": {u"S": u"tata"}},
{u"relevant_data": {u"S": u"toto"}},
{u"relevant_data": {u"S": u"titi"}},
],
u"ConsumedCapacityUnits": 0.5,
}
# Protocol check
res = self.app.post_json('/', request, headers=HEADERS, status=200)
self.assertEqual(expected, json.loads(res.body))
self.assertEqual('application/x-amz-json-1.0; charset=UTF-8',
res.headers['Content-Type'])
def test_scan_count_and_attrs_to_get_fails(self):
request = {
"TableName": TABLE_NAME,
"ScanFilter": {
"relevant_data": {
"AttributeValueList": [
{"S":"toto"},
{"S":"titi"},
{"S":"tata"},
],
"ComparisonOperator": "IN",
},
},
"AttributesToGet": [u'relevant_data'],
"Count": True,
}
expected = {
u'__type': u'com.amazonaws.dynamodb.v20120810#ValidationException',
u'message': u'Can not filter fields when only count is requested'
}
# Protocol check
res = self.app.post_json('/', request, headers=HEADERS, status=400)
self.assertEqual(expected, json.loads(res.body))
self.assertEqual('application/x-amz-json-1.0; charset=UTF-8',
res.headers['Content-Type'])
示例7: TestBatchWriteItem
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
class TestBatchWriteItem(unittest.TestCase):
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
from ddbmock import main
app = main({})
from webtest import TestApp
self.app = TestApp(app)
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
self.t1 = Table(TABLE_NAME1, TABLE_RT, TABLE_WT, hash_key, range_key)
self.t2 = Table(TABLE_NAME2, TABLE_RT, TABLE_WT, hash_key, None)
dynamodb.data[TABLE_NAME1] = self.t1
dynamodb.data[TABLE_NAME2] = self.t2
self.t1.put(ITEM1, {})
self.t2.put(ITEM4, {})
def tearDown(self):
from ddbmock.database.db import dynamodb
dynamodb.hard_reset()
def test_batch_write_item(self):
request = {
u"RequestItems": {
TABLE_NAME1: [
{
u"DeleteRequest": {
u"Key": {
u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE1},
u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE1},
},
},
},
{u"PutRequest": {u"Item": ITEM2}},
{u"PutRequest": {u"Item": ITEM3}},
],
TABLE_NAME2: [
{
u"DeleteRequest": {
u"Key": {
u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE2},
},
},
},
{u"PutRequest": {u"Item":ITEM5}},
],
}
}
expected = {
"Responses": {
TABLE_NAME1: {
"ConsumedCapacityUnits": 3
},
TABLE_NAME2: {
"ConsumedCapacityUnits": 2
}
}
}
# Protocol check
res = self.app.post_json('/', request, headers=HEADERS, status=200)
self.assertEqual(expected, json.loads(res.body))
self.assertEqual('application/x-amz-json-1.0; charset=UTF-8',
res.headers['Content-Type'])
示例8: TestBatchGetItem
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
class TestBatchGetItem(unittest.TestCase):
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
import helpers
self.app = helpers.makeTestApp()
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
self.t1 = Table(TABLE_NAME1, TABLE_RT, TABLE_WT, hash_key, range_key)
self.t2 = Table(TABLE_NAME2, TABLE_RT, TABLE_WT, hash_key, None)
dynamodb.data[TABLE_NAME1] = self.t1
dynamodb.data[TABLE_NAME2] = self.t2
self.t1.put(ITEM1, {})
self.t1.put(ITEM2, {})
self.t1.put(ITEM3, {})
self.t2.put(ITEM4, {})
self.t2.put(ITEM5, {})
def tearDown(self):
from ddbmock.database.db import dynamodb
dynamodb.hard_reset()
def test_batch_get_item_filter_one(self):
request = {
u"RequestItems": {
TABLE_NAME1: {
u"Keys": [
{u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE1},
u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE1},
},
{u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE1},
u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE1}
},
],
u"AttributesToGet": [u"relevant_data"],
},
TABLE_NAME2: {
u"Keys": [
{u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE3}},
],
},
}
}
expected = {
"Responses": {
"Table-HR": {
"Items": [
{"relevant_data": {"S": "tata"}},
{"relevant_data": {"S": "tata"}},
],
"ConsumedCapacityUnits": 1.0
},
"Table-H": {
"Items": [
{"relevant_data": {"S": "tutu"},
"hash_key": {"N": "789"},
"range_key": {"S": "Waldo-5"}
},
],
"ConsumedCapacityUnits": 0.5
}
}
}
# Protocol check
res = self.app.post_json('/', request, headers=HEADERS, status=200)
self.assertEqual(expected, json.loads(res.body))
self.assertEqual('application/x-amz-json-1.0; charset=UTF-8',
res.headers['Content-Type'])
def test_batch_get_item_filter_one_consistent(self):
request = {
u"RequestItems": {
TABLE_NAME1: {
u"Keys": [
{u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE1},
u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE1}
},
{u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE1},
u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE1}
},
],
u"AttributesToGet": [u"relevant_data"],
u"ConsistentRead": True,
},
TABLE_NAME2: {
u"Keys": [
{u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE3}},
],
},
#.........这里部分代码省略.........
示例9: TestUpdateItem
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
class TestUpdateItem(unittest.TestCase):
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
self.t1 = Table(TABLE_NAME, TABLE_RT, TABLE_WT, hash_key, range_key)
self.t2 = Table(TABLE_NAME2, TABLE_RT, TABLE_WT, hash_key, None)
dynamodb.data[TABLE_NAME] = self.t1
dynamodb.data[TABLE_NAME2] = self.t2
self.t1.put(cp(ITEM), {})
self.t2.put(cp(ITEM2), {})
def tearDown(self):
from ddbmock.database.db import dynamodb
from ddbmock import clean_boto_patch
dynamodb.hard_reset()
clean_boto_patch()
def test_update_item_put_hr(self):
from ddbmock import connect_boto_patch
db = connect_boto_patch()
key = {u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE}, u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE}}
key2 = {u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE2}, u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE}}
# use PUT as default action, champs existant
db.layer1.update_item(
TABLE_NAME, key, {"relevant_data": {"Value": RELEVANT_FIELD}} # Move type from 'B' to 'S'
)
self.assertEqual(RELEVANT_FIELD, self.t1.store[HK_VALUE, RK_VALUE]["relevant_data"])
# PUT explicite, champ non existant
db.layer1.update_item(TABLE_NAME, key, {"irelevant_data": {"Action": "PUT", "Value": IRELEVANT_FIELD}})
self.assertEqual(RELEVANT_FIELD, self.t1.store[HK_VALUE, RK_VALUE]["relevant_data"])
self.assertEqual(IRELEVANT_FIELD, self.t1.store[HK_VALUE, RK_VALUE]["irelevant_data"])
# PUT explicite, item non existant(full item creation)
db.layer1.update_item(TABLE_NAME, key2, {"relevant_data": {"Action": "PUT", "Value": RELEVANT_FIELD}})
self.assertEqual({TABLE_HK_TYPE: HK_VALUE2}, self.t1.store[HK_VALUE2, RK_VALUE][TABLE_HK_NAME])
self.assertEqual({TABLE_RK_TYPE: RK_VALUE}, self.t1.store[HK_VALUE2, RK_VALUE][TABLE_RK_NAME])
self.assertEqual(RELEVANT_FIELD, self.t1.store[HK_VALUE2, RK_VALUE]["relevant_data"])
def test_update_item_put_h(self):
from ddbmock import connect_boto_patch
db = connect_boto_patch()
key = {u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE}}
key2 = {u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE2}}
# use PUT as default action, champs existant
db.layer1.update_item(
TABLE_NAME2, key, {"relevant_data": {"Value": RELEVANT_FIELD}} # Move type from 'B' to 'S'
)
self.assertEqual(RELEVANT_FIELD, self.t2.store[HK_VALUE, False]["relevant_data"])
# PUT explicite, champ non existant
db.layer1.update_item(TABLE_NAME2, key, {"irelevant_data": {"Action": "PUT", "Value": IRELEVANT_FIELD}})
self.assertEqual(RELEVANT_FIELD, self.t2.store[HK_VALUE, False]["relevant_data"])
self.assertEqual(IRELEVANT_FIELD, self.t2.store[HK_VALUE, False]["irelevant_data"])
# PUT explicite, item non existant(full item creation)
db.layer1.update_item(TABLE_NAME2, key2, {"relevant_data": {"Action": "PUT", "Value": RELEVANT_FIELD}})
self.assertEqual({TABLE_HK_TYPE: HK_VALUE2}, self.t2.store[HK_VALUE2, False][TABLE_HK_NAME])
self.assertEqual(RELEVANT_FIELD, self.t2.store[HK_VALUE2, False]["relevant_data"])
def test_put_check_throughput_max_old_new(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
db = connect_boto_patch()
key = {u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE}}
self.assertEqual(
{u"ConsumedCapacityUnits": 1},
db.layer1.update_item(TABLE_NAME2, key, {FIELD_NAME: {"Action": "PUT", "Value": FIELD_SMALL}}),
)
self.assertEqual(
{u"ConsumedCapacityUnits": 2},
db.layer1.update_item(TABLE_NAME2, key, {FIELD_NAME: {"Action": "PUT", "Value": FIELD_BIG}}),
)
self.assertEqual(
{u"ConsumedCapacityUnits": 2},
db.layer1.update_item(TABLE_NAME2, key, {FIELD_NAME: {"Action": "PUT", "Value": FIELD_SMALL}}),
)
def test_update_item_delete_primary_key_fails(self):
#.........这里部分代码省略.........
示例10: TestQuery
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
class TestQuery(unittest.TestCase):
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
self.t1 = Table(TABLE_NAME, TABLE_RT, TABLE_WT, hash_key, range_key)
dynamodb.data[TABLE_NAME] = self.t1
self.t1.put(ITEM1, {})
self.t1.put(ITEM2, {})
self.t1.put(ITEM3, {})
self.t1.put(ITEM4, {})
self.t1.put(ITEM5, {})
def tearDown(self):
from ddbmock.database.db import dynamodb
from ddbmock import clean_boto_patch
dynamodb.hard_reset()
clean_boto_patch()
def test_query_all(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
expected = {
u"Count": 5,
u"Items": [ITEM1, ITEM2, ITEM3, ITEM4, ITEM5],
u"ConsumedCapacityUnits": 0.5,
}
db = connect_boto_patch()
ret = db.layer1.query(TABLE_NAME, {TABLE_HK_TYPE: HK_VALUE})
self.assertEqual(expected, ret)
# Regression test for #9
def test_query_all_404(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
expected = {
u"Count": 0,
u'Items': [],
u"ConsumedCapacityUnits": 0.5,
}
db = connect_boto_patch()
ret = db.layer1.query(TABLE_NAME, {TABLE_HK_TYPE: HK_VALUE_404})
self.assertEqual(expected, ret)
def test_query_2_first(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
expected = {
u"Count": 2,
u"Items": [ITEM1, ITEM2],
u"ConsumedCapacityUnits": 0.5,
u'LastEvaluatedKey': {
u'HashKeyElement': {u'N': u'123'},
u'RangeKeyElement': {u'S': u'Waldo-2'},
},
}
db = connect_boto_patch()
ret = db.layer1.query(TABLE_NAME, {TABLE_HK_TYPE: HK_VALUE}, limit=2)
self.assertEqual(expected, ret)
def test_query_paged(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
from boto.dynamodb.exceptions import DynamoDBValidationError
esk = {
u'HashKeyElement': {u'N': u'123'},
u'RangeKeyElement': {u'S': u'Waldo-3'},
}
bad_esk = {
u'HashKeyElement': {u'N': u'123.43'},
u'RangeKeyElement': {u'S': u'Waldo-3'},
}
expected1 = {
u"Count": 3,
u"Items": [ITEM1, ITEM2, ITEM3],
u"ConsumedCapacityUnits": 0.5,
u'LastEvaluatedKey': esk,
}
expected2 = {
u"Count": 2,
#.........这里部分代码省略.........
示例11: TestBatchGetItem
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
class TestBatchGetItem(unittest.TestCase):
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
self.t1 = Table(TABLE_NAME1, TABLE_RT, TABLE_WT, hash_key, range_key)
self.t2 = Table(TABLE_NAME2, TABLE_RT, TABLE_WT, hash_key, None)
dynamodb.data[TABLE_NAME1] = self.t1
dynamodb.data[TABLE_NAME2] = self.t2
self.t1.put(ITEM1, {})
self.t1.put(ITEM2, {})
self.t1.put(ITEM3, {})
self.t2.put(ITEM4, {})
self.t2.put(ITEM5, {})
self.t2.put(ITEM_BIG, {})
def tearDown(self):
from ddbmock.database.db import dynamodb
from ddbmock import clean_boto_patch
dynamodb.hard_reset()
clean_boto_patch()
def test_batch_get_item_nominal(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
db = connect_boto_patch()
expected = {
"Responses": {
"Table-HR": {"Items": [ITEM1, ITEM2], "ConsumedCapacityUnits": 1.0},
"Table-H": {"Items": [ITEM5, ITEM_BIG], "ConsumedCapacityUnits": 1.5},
}
}
ret = db.layer1.batch_get_item(
{
TABLE_NAME1: {
u"Keys": [
{u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE1}, u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE1}},
{u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE1}, u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE2}},
]
},
TABLE_NAME2: {
u"Keys": [
{u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE3}},
{u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE1}},
{u"HashKeyElement": {TABLE_HK_TYPE: u"404"}},
]
},
}
)
self.assertEqual(expected, ret)
def test_batch_get_item_filter_one(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
db = connect_boto_patch()
expected = {
"Responses": {
"Table-HR": {
"Items": [{"relevant_data": {"S": "tata"}}, {"relevant_data": {"S": "tata"}}],
"ConsumedCapacityUnits": 1.0,
},
"Table-H": {
"Items": [
{"relevant_data": {"S": "tutu"}, "hash_key": {"N": "789"}, "range_key": {"S": "Waldo-5"}}
],
"ConsumedCapacityUnits": 0.5,
},
}
}
ret = db.layer1.batch_get_item(
{
TABLE_NAME1: {
u"Keys": [
{u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE1}, u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE1}},
{u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE1}, u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE1}},
],
u"AttributesToGet": [u"relevant_data"],
},
TABLE_NAME2: {u"Keys": [{u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE3}}]},
}
)
self.assertEqual(expected, ret)
def test_batch_get_item_table_404(self):
from ddbmock import connect_boto_patch
#.........这里部分代码省略.........
示例12: TestGetItem
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
class TestGetItem(unittest.TestCase):
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
self.t1 = Table(TABLE_NAME, TABLE_RT, TABLE_WT, hash_key, range_key)
self.t2 = Table(TABLE_NAME2, TABLE_RT, TABLE_WT, hash_key, None)
dynamodb.data[TABLE_NAME] = self.t1
dynamodb.data[TABLE_NAME2] = self.t2
self.t1.put(ITEM, {})
self.t2.put(ITEM2, {})
self.t2.put(ITEM_BIG, {})
def tearDown(self):
from ddbmock.database.db import dynamodb
from ddbmock import clean_boto_patch
dynamodb.hard_reset()
clean_boto_patch()
def test_get_hr(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
db = connect_boto_patch()
expected = {
u'ConsumedCapacityUnits': 0.5,
u'Item': ITEM,
}
key = {
u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE},
u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE},
}
self.assertEquals(expected, db.layer1.get_item(TABLE_NAME, key))
def test_get_hr_consistent(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
db = connect_boto_patch()
expected = {
u'ConsumedCapacityUnits': 1,
u'Item': ITEM,
}
key = {
u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE},
u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE},
}
self.assertEquals(expected, db.layer1.get_item(TABLE_NAME, key, consistent_read=True))
def test_get_h(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
db = connect_boto_patch()
expected = {
u'ConsumedCapacityUnits': 0.5,
u'Item': ITEM2,
}
key = {
u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE},
}
self.assertEquals(expected, db.layer1.get_item(TABLE_NAME2, key))
def test_get_consistent_big_attributes_to_get(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
db = connect_boto_patch()
expected = {
u'ConsumedCapacityUnits': 2,
u'Item': {TABLE_HK_NAME: {TABLE_HK_TYPE: HK_VALUE2}},
}
key = {
u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE2},
}
self.assertEquals(expected, db.layer1.get_item(TABLE_NAME2, key, consistent_read=True, attributes_to_get=[TABLE_HK_NAME]))
def test_get_h_404(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
#.........这里部分代码省略.........
示例13: TestDeleteItem
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
class TestDeleteItem(unittest.TestCase):
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
self.t1 = Table(TABLE_NAME, TABLE_RT, TABLE_WT, hash_key, range_key)
self.t2 = Table(TABLE_NAME2, TABLE_RT, TABLE_WT, hash_key, None)
dynamodb.data[TABLE_NAME] = self.t1
dynamodb.data[TABLE_NAME2] = self.t2
self.t1.put(ITEM, {})
self.t2.put(ITEM2, {})
self.t2.put(ITEM_BIG, {})
def tearDown(self):
from ddbmock.database.db import dynamodb
from ddbmock import clean_boto_patch
dynamodb.hard_reset()
clean_boto_patch()
def test_delete_item_hr(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
db = connect_boto_patch()
key = {
u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE},
u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE},
}
self.assertEqual({
u'ConsumedCapacityUnits': 1,
},
db.layer1.delete_item(TABLE_NAME, key),
)
self.assertNotIn((HK_VALUE, RK_VALUE), self.t1.store)
def test_delete_item_hr_404(self):
# same behavior as found
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
db = connect_boto_patch()
key = {
u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE_404},
u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE},
}
self.assertEqual({
u'ConsumedCapacityUnits': 1,
},
db.layer1.delete_item(TABLE_NAME, key),
)
self.assertNotIn((HK_VALUE_404, RK_VALUE), self.t1.store)
def test_delete_item_hr_old(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
db = connect_boto_patch()
expected = {
u'ConsumedCapacityUnits': 1,
u'Attributes': ITEM,
}
key = {
u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE},
u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE},
}
self.assertEqual(
expected,
db.layer1.delete_item(TABLE_NAME, key, return_values=u'ALL_OLD'),
)
self.assertNotIn((HK_VALUE, RK_VALUE), self.t1.store)
def test_delete_item_h(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
db = connect_boto_patch()
key = {
u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE},
}
self.assertEqual({
u'ConsumedCapacityUnits': 1,
},
db.layer1.delete_item(TABLE_NAME2, key),
#.........这里部分代码省略.........
示例14: TestQuery
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
class TestQuery(unittest.TestCase):
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
from ddbmock import main
app = main({})
from webtest import TestApp
self.app = TestApp(app)
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
self.t1 = Table(TABLE_NAME, TABLE_RT, TABLE_WT, hash_key, range_key)
dynamodb.data[TABLE_NAME] = self.t1
self.t1.put(ITEM1, {})
self.t1.put(ITEM2, {})
self.t1.put(ITEM3, {})
self.t1.put(ITEM4, {})
self.t1.put(ITEM5, {})
def tearDown(self):
from ddbmock.database.db import dynamodb
dynamodb.hard_reset()
def test_query_condition_filter_fields(self):
request = {
"TableName": TABLE_NAME,
"HashKeyValue": {TABLE_HK_TYPE: HK_VALUE},
"RangeKeyCondition": {
"AttributeValueList": [{"S":"Waldo-2"}],
"ComparisonOperator": "GT",
},
"AttributesToGet": [u'relevant_data'],
}
expected = {
u"Count": 3,
u"Items": [
{u"relevant_data": {u"S": u"titi"}},
{u"relevant_data": {u"S": u"toto"}},
{u"relevant_data": {u"S": u"tutu"}},
],
u"ConsumedCapacityUnits": 0.5,
}
# Protocol check
res = self.app.post_json('/', request, headers=HEADERS, status=200)
self.assertEqual(expected, json.loads(res.body))
self.assertEqual('application/x-amz-json-1.0; charset=UTF-8',
res.headers['Content-Type'])
def test_query_count_and_attrs_to_get_fails(self):
request = {
"TableName": TABLE_NAME,
"HashKeyValue": {TABLE_HK_TYPE: HK_VALUE},
"RangeKeyCondition": {
"AttributeValueList": [{"S":"Waldo-2"}],
"ComparisonOperator": "GT",
},
"AttributesToGet": [u'relevant_data'],
"Count": True,
}
expected = {
u'__type': u'com.amazonaws.dynamodb.v20111205#ValidationException',
u'message': u'Can filter fields when only count is requested'
}
# Protocol check
res = self.app.post_json('/', request, headers=HEADERS, status=400)
self.assertEqual(expected, json.loads(res.body))
self.assertEqual('application/x-amz-json-1.0; charset=UTF-8',
res.headers['Content-Type'])
# Regression test for #9
def test_query_all_404(self):
request = {
"TableName": TABLE_NAME,
"HashKeyValue": {TABLE_HK_TYPE: HK_VALUE_404},
}
expected = {
u"Count": 0,
u'Items': [],
u"ConsumedCapacityUnits": 0.5,
}
# Protocol check
res = self.app.post_json('/', request, headers=HEADERS, status=200)
self.assertEqual(expected, json.loads(res.body))
self.assertEqual('application/x-amz-json-1.0; charset=UTF-8',
res.headers['Content-Type'])
示例15: TestBatchWriteItem
# 需要导入模块: from ddbmock.database.table import Table [as 别名]
# 或者: from ddbmock.database.table.Table import put [as 别名]
class TestBatchWriteItem(unittest.TestCase):
def setUp(self):
from ddbmock.database.db import dynamodb
from ddbmock.database.table import Table
from ddbmock.database.key import PrimaryKey
dynamodb.hard_reset()
hash_key = PrimaryKey(TABLE_HK_NAME, TABLE_HK_TYPE)
range_key = PrimaryKey(TABLE_RK_NAME, TABLE_RK_TYPE)
self.t1 = Table(TABLE_NAME1, TABLE_RT, TABLE_WT, hash_key, range_key)
self.t2 = Table(TABLE_NAME2, TABLE_RT, TABLE_WT, hash_key, None)
dynamodb.data[TABLE_NAME1] = self.t1
dynamodb.data[TABLE_NAME2] = self.t2
self.t1.put(ITEM1, {})
self.t2.put(ITEM4, {})
def tearDown(self):
from ddbmock.database.db import dynamodb
from ddbmock import clean_boto_patch
dynamodb.hard_reset()
clean_boto_patch()
def test_batch_write_item_nominal(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
db = connect_boto_patch()
expected = {
"Responses": {
TABLE_NAME1: {
"ConsumedCapacityUnits": 3
},
TABLE_NAME2: {
"ConsumedCapacityUnits": 2
}
}
}
ret = db.layer1.batch_write_item({
TABLE_NAME1: [
{
u"DeleteRequest": {
u"Key": {
u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE1},
u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE1},
},
},
},
{u"PutRequest": {u"Item": ITEM2}},
{u"PutRequest": {u"Item": ITEM3}},
],
TABLE_NAME2: [
{
u"DeleteRequest": {
u"Key": {
u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE2},
},
},
},
{u"PutRequest": {u"Item":ITEM5}},
],
})
self.assertEqual(expected, ret)
def test_batch_write_item_table_404(self):
from ddbmock import connect_boto_patch
from ddbmock.database.db import dynamodb
from boto.exception import BotoServerError
db = connect_boto_patch()
self.assertRaises(BotoServerError, db.layer1.batch_write_item, {
TABLE_NAME_404: [
{
u"DeleteRequest": {
u"Key": {
u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE1},
u"RangeKeyElement": {TABLE_RK_TYPE: RK_VALUE1},
},
},
},
{u"PutRequest": {u"Item": ITEM2}},
{u"PutRequest": {u"Item": ITEM3}},
],
TABLE_NAME2: [
{
u"DeleteRequest": {
u"Key": {
u"HashKeyElement": {TABLE_HK_TYPE: HK_VALUE2},
},
},
},
{u"PutRequest": {u"Item":ITEM5}},
],
#.........这里部分代码省略.........