def test_update_item(self): self.storage_mocker.StubOutWithMock(storage, 'get_item') hash_key = "4.5621201231232132132132132132132142354E126" range_key = "range" storage.get_item( mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), select_type=mox.IgnoreArg(), consistent=mox.IgnoreArg() ).AndReturn( models.SelectResult( items=[ { "hash_key": models.AttributeValue('N', hash_key), "range_key": models.AttributeValue('S', range_key), "attr_value": models.AttributeValue('S', 'val') } ] ) ) self.storage_mocker.StubOutWithMock(storage, 'describe_table') storage.describe_table(mox.IgnoreArg(), 'test_table').AndReturn( models.TableMeta( '00000000-0000-0000-0000-000000000000', models.TableSchema( { 'hash_key': models.AttributeType('N'), 'range_key': models.AttributeType('S') }, ['hash_key', 'range_key'], ), models.TableMeta.TABLE_STATUS_ACTIVE, None ) ) self.storage_mocker.StubOutWithMock(storage, 'update_item') storage.update_item( mox.IgnoreArg(), mox.IgnoreArg(), key_attribute_map=mox.IgnoreArg(), attribute_action_map=mox.IgnoreArg(), expected_condition_map=mox.IgnoreArg()).AndReturn((True, None)) self.storage_mocker.ReplayAll() table = ddb_table.Table( 'test_table', connection=self.DYNAMODB_CON ) item = table.get_item(consistent=False, hash_key=1, range_key="range") item['attr_value'] = 'updated' item.partial_save() self.storage_mocker.VerifyAll()
def test_select_item_count(self): self.storage_mocker.StubOutWithMock(storage, 'query') storage.query( mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), select_type=models.SelectType.count(), index_name=mox.IgnoreArg(), limit=mox.IgnoreArg(), exclusive_start_key=mox.IgnoreArg(), consistent=mox.IgnoreArg(), order_type=mox.IgnoreArg() ).AndReturn( models.SelectResult( count=100500 ) ) self.storage_mocker.ReplayAll() table = ddb_table.Table( 'test_table', connection=self.DYNAMODB_CON ) count = table.query_count(consistent=False, hash_key__eq=1) self.assertEqual(count, 100500) self.storage_mocker.VerifyAll()
def test_select_item(self): self.storage_mocker.StubOutWithMock(storage, 'select_item') blob_data1 = bytes(bytearray([1, 2, 3, 4, 5])) blob_data2 = bytes(bytearray([5, 4, 3, 2, 1])) hash_key = "4.5621201231232132132132132132132142354E126" range_key = "range" storage.select_item( IgnoreArg(), IgnoreArg(), IgnoreArg(), select_type=IgnoreArg(), index_name=IgnoreArg(), limit=IgnoreArg(), exclusive_start_key=IgnoreArg(), consistent=IgnoreArg(), order_type=IgnoreArg(), ).AndReturn( models.SelectResult(items=[{ "hash_key": models.AttributeValue(models.ATTRIBUTE_TYPE_NUMBER, decimal.Decimal(hash_key)), "range_key": models.AttributeValue(models.ATTRIBUTE_TYPE_STRING, range_key), "value_blob": models.AttributeValue(models.ATTRIBUTE_TYPE_BLOB, blob_data1), "value_blob_set": models.AttributeValue(models.ATTRIBUTE_TYPE_BLOB_SET, set([blob_data1, blob_data2])) }])) self.storage_mocker.ReplayAll() table = Table('test_table', connection=self.DYNAMODB_CON) items = list(table.query(consistent=False, hash_key__eq=1)) expected_item = { "hash_key": decimal.Decimal(hash_key), "range_key": range_key, "value_blob": types.Binary(blob_data1), "value_blob_set": set([types.Binary(blob_data1), types.Binary(blob_data2)]) } self.assertEqual(len(items), 1) self.assertDictEqual(expected_item, dict(items[0].items())) self.storage_mocker.VerifyAll()
def test_query_count(self, mock_query): mock_query.return_value = models.SelectResult(count=100500) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } conn = httplib.HTTPConnection('localhost:8080') url = '/v1/fake_project_id/data/tables/Threads/query' body = """ { "key_conditions": { "ForumName" : { "attribute_value_list": [ { "S": "Testing OS API" } ], "comparison_operator": "EQ" }, "LastPostDateTime" : { "attribute_value_list": [ { "S": "3/10/14" } ], "comparison_operator": "GT" } }, "select": "COUNT" } """ expected_response = { "count": 100500, } conn.request("POST", url, headers=headers, body=body) response = conn.getresponse() self.assertTrue(mock_query.called) json_response = response.read() response_payload = json.loads(json_response) self.assertEqual(expected_response, response_payload)
def test_update_item(self): self.storage_mocker.StubOutWithMock(storage, 'select_item') hash_key = "4.5621201231232132132132132132132142354E126" range_key = "range" storage.select_item( IgnoreArg(), IgnoreArg(), IgnoreArg(), select_type=IgnoreArg(), limit=IgnoreArg(), consistent=IgnoreArg()).AndReturn( models.SelectResult( items=[{ "hash_key": models.AttributeValue.number(hash_key), "range_key": models.AttributeValue.str(range_key), "attr_value": models.AttributeValue.str('val') }])) self.storage_mocker.StubOutWithMock(storage, 'describe_table') storage.describe_table(IgnoreArg(), 'test_table').AndReturn( models.TableMeta( models.TableSchema( { 'hash_key': models.ATTRIBUTE_TYPE_NUMBER, 'range_key': models.ATTRIBUTE_TYPE_STRING }, ['hash_key', 'range_key'], ), models.TableMeta.TABLE_STATUS_ACTIVE)) self.storage_mocker.StubOutWithMock(storage, 'update_item') storage.update_item(IgnoreArg(), IgnoreArg(), key_attribute_map=IgnoreArg(), attribute_action_map=IgnoreArg(), expected_condition_map=IgnoreArg()).AndReturn(True) self.storage_mocker.ReplayAll() table = Table('test_table', connection=self.DYNAMODB_CON) item = table.get_item(consistent=False, hash_key=1, range_key="range") item['attr_value'] = 'updated' item.partial_save() self.storage_mocker.VerifyAll()
def test_get_item(self): self.storage_mocker.StubOutWithMock(storage, 'get_item') blob_data1 = bytes(bytearray([1, 2, 3, 4, 5])) blob_data2 = bytes(bytearray([5, 4, 3, 2, 1])) hash_key = "4.5621201231232132132132132132132142354E126" range_key = "range" storage.get_item( mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg(), select_type=mox.IgnoreArg(), consistent=mox.IgnoreArg() ).AndReturn( models.SelectResult( items=[ { "hash_key": models.AttributeValue('N', hash_key), "range_key": models.AttributeValue('S', range_key), "value_blob": models.AttributeValue( 'B', decoded_value=blob_data1 ), "value_blob_set": models.AttributeValue( 'BS', decoded_value={blob_data1, blob_data2} ) } ] ) ) self.storage_mocker.ReplayAll() table = ddb_table.Table( 'test_table', connection=self.DYNAMODB_CON ) item = table.get_item(consistent=False, hash_key=1, range_key="range") expected_item = { "hash_key": decimal.Decimal(hash_key), "range_key": range_key, "value_blob": types.Binary(blob_data1), "value_blob_set": set([types.Binary(blob_data1), types.Binary(blob_data2)]) } self.assertDictEqual(expected_item, dict(item.items())) self.storage_mocker.VerifyAll()
def test_query(self, mock_query): items = [ { 'ForumName': models.AttributeValue.str('Testing OS API'), 'LastPostDateTime': models.AttributeValue.str('3/18/14'), 'Posts': models.AttributeValue.str_set(['Opening post']) }, { 'ForumName': models.AttributeValue.str('Testing OS API'), 'LastPostDateTime': models.AttributeValue.str('3/19/14'), 'Posts': models.AttributeValue.str_set(['Hi', 'Hello']) }, ] last_evaluated_key = { 'ForumName': models.AttributeValue.str('Testing OS API'), 'LastPostDateTime': models.AttributeValue.str('3/19/14'), } mock_query.return_value = models.SelectResult( items=items, last_evaluated_key=last_evaluated_key, count=2) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } conn = httplib.HTTPConnection('localhost:8080') url = '/v1/fake_project_id/data/tables/Threads/query' body = """ { "attributes_to_get": [ "ForumName", "LastPostDateTime", "Posts" ], "exclusive_start_key": { "ForumName" : { "S": "Testing OS API" }, "LastPostDayTime" : { "S": "3/1/14" } }, "index_name": "LastPostIndex", "limit": 2, "consistent_read": true, "scan_index_forward": true, "key_conditions": { "ForumName" : { "attribute_value_list": [ { "S": "Testing OS API" } ], "comparison_operator": "EQ" }, "LastPostDateTime" : { "attribute_value_list": [ { "S": "3/10/14" } ], "comparison_operator": "GT" } }, "select": "SPECIFIC_ATTRIBUTES" } """ expected_response = { "count": 2, "items": [ { 'ForumName': { 'S': 'Testing OS API' }, 'LastPostDateTime': { 'S': '3/18/14' }, 'Posts': { 'SS': ['Opening post'] } }, { 'ForumName': { 'S': 'Testing OS API' }, 'LastPostDateTime': { 'S': '3/19/14' }, 'Posts': { 'SS': ['Hi', 'Hello'] } }, ], "last_evaluated_key": { 'ForumName': { 'S': 'Testing OS API' }, 'LastPostDateTime': { 'S': '3/19/14' }, } } conn.request("POST", url, headers=headers, body=body) response = conn.getresponse() self.assertTrue(mock_query.called) json_response = response.read() response_payload = json.loads(json_response) self.assertEqual(expected_response, response_payload)