Commits

Jean-Tiare Le Bigot  committed 6b2f25c

add preliminary batch_get_item method, test, bugfixes

  • Participants
  • Parent commits 3cae696

Comments (0)

Files changed (14)

     db = connect_boto()
 
     # Done ! just use it wherever in your project as usual.
-    db = boto.connect_dynamodb()
     db.list_tables() # get list of tables (empty at this stage)
 
 

File ddbmock/database/db.py

 # -*- coding: utf-8 -*-
 
 from .table import Table
+from collections import defaultdict
+from ddbmock.errors import ResourceNotFoundException
 
 # All validations are performed on *incomming* data => already done :)
 
     def get_table(self, name):
         if name in self.data:
             return self.data[name]
-        return None
+        raise ResourceNotFoundException("Table {} does not exist".format(name))
 
     def create_table(self, name, data):
         if name in self.data:
 
     def delete_table(self, name):
         if name not in self.data:
-            return None
+            raise ResourceNotFoundException("Table {} does not exist".format(name))
         self.data[name].delete()
         ret = self.data[name].to_dict()
         del self.data[name]
         return ret
+
+    def get_batch(self, batch):
+        ret = defaultdict(dict)
+
+        for tablename, keys in batch.iteritems():
+            fields = keys[u'AttributesToGet'] if u'AttributesToGet' in keys else []
+            table = self.get_table(tablename)
+            units = 0
+            items = []
+            for key in keys[u'Keys']:
+                item = table.get(key, fields)
+                units += 0.5  # STUB
+                if item: items.append(item)
+            ret[tablename][u'Items'] = items
+            ret[tablename][u'ConsumedCapacityUnits'] = len(items)*0.5
+
+        return ret

File ddbmock/database/table.py

         hash_key = key.read_key(self.hash_key, u'HashKeyElement')
         range_key = key.read_key(self.range_key, u'RangeKeyElement')
 
+        if self.range_key is None and u'RangeKeyElement' in key:
+            raise ValidationException("Table {} has no range_key".format(self.name))
+
         item = self.data[hash_key][range_key]
 
         return item.filter(fields)

File ddbmock/tests/functional/boto/test_query.py

 }
 
 # Please note that most query features are not yet implemented hence not tested
-class TestGetItem(unittest.TestCase):
+class TestQuery(unittest.TestCase):
     def setUp(self):
         from ddbmock.database.db import DynamoDB
         from ddbmock.database.table import Table

File ddbmock/tests/functional/boto/test_scan.py

 }
 
 # Please note that most query features are not yet implemented hence not tested
-class TestGetItem(unittest.TestCase):
+class TestScan(unittest.TestCase):
     def setUp(self):
         from ddbmock.database.db import DynamoDB
         from ddbmock.database.table import Table

File ddbmock/validators/types.py

 
 limit = all(
     int,
-    range(min=1, msg="Limit parameter must be a positive integer")
+    range(min=1, msg="Limit parameter must be a positive integer"),
 )
 
 scan_index_forward = all(
 
 expected_schema = {
     field_name: any(
-        {"Exists":false},
-        {"Exists":true, "Value": field_value}, # pas sur de la vraie syntaxe :/
-        {"Value":field_value},
+        {"Exists": false},
+        {"Exists": true, "Value": field_value}, # pas sur de la vraie syntaxe :/
+        {"Value": field_value},
     )
 }
 

File ddbmock/views/delete_item.py

 
     name = post[u'TableName']
     table = DynamoDB().get_table(name)
-    if table is None:
-        raise ResourceNotFoundException("Table {} does not exist".format(name))
 
     ret = {
         "ConsumedCapacityUnits": 1, #FIXME: stub

File ddbmock/views/delete_table.py

 from pyramid.view import view_config
 from ddbmock.database import DynamoDB
 from ddbmock.validators import dynamodb_api_validate
-from ddbmock.errors import wrap_exceptions, ResourceNotFoundException
+from ddbmock.errors import wrap_exceptions
 
 # Real work
 @wrap_exceptions
 def delete_table(post):
     name = post[u'TableName']
     ret = DynamoDB().delete_table(name)
-    if ret is None:
-        raise ResourceNotFoundException("Table {} does not exist".format(name))
 
     return {
         'TableDescription': ret,

File ddbmock/views/describe_table.py

     name = post[u'TableName']
     table = DynamoDB().get_table(name)
 
-    if table is None:
-        raise ResourceNotFoundException("Table {} does not exist".format(name))
-
     return {
         "Table": table.to_dict()
     }

File ddbmock/views/get_item.py

     #TODO: ConsistentRead
     name = post[u'TableName']
     table = DynamoDB().get_table(name)
-    if table is None:
-        raise ResourceNotFoundException("Table {} does not exist".format(name))
 
     return {
         "ConsumedCapacityUnits": capacity, #FIXME: stub

File ddbmock/views/put_item.py

 
     name = post[u'TableName']
     table = DynamoDB().get_table(name)
-    if table is None:
-        raise ResourceNotFoundException("Table {} does not exist".format(name))
 
     ret = {
         "ConsumedCapacityUnits": 1, #FIXME: stub

File ddbmock/views/query.py

 
     name = post[u'TableName']
     table = DynamoDB().get_table(name)
-    if table is None:
-        raise ResourceNotFoundException("Table {} does not exist".format(name))
 
     results, last_key = table.query(
         post[u'HashKeyValue'],

File ddbmock/views/scan.py

 
     name = post[u'TableName']
     table = DynamoDB().get_table(name)
-    if table is None:
-        raise ResourceNotFoundException("Table {} does not exist".format(name))
 
     results, last_key, scanned_count = table.scan(
         post[u'ScanFilter'],

File ddbmock/views/update_item.py

 
     name = post[u'TableName']
     table = DynamoDB().get_table(name)
-    if table is None:
-        raise ResourceNotFoundException("Table {} does not exist".format(name))
 
     ret = {
         "ConsumedCapacityUnits": 1, #FIXME: stub