Commits

Anonymous committed 8226bc3 Draft

code cleanup: there is no need to initialize auto_inc element

Comments (0)

Files changed (3)

 
 - OverwriteError inherits from ConflictError so that ``raise_on_conflict`` always raises ``ConflictError`` while staying retro-compatible
 - fix bug #17: enforce type coercion in ``DynamoDBModel.__init__``. (thanks luckyasser)
+- (internal) no more "MAGIC_ITEM" initialisation for ``auto_inc_int``. It is not needed.
 
 ====================
 DynamoDBMapper 1.7.0

dynamodb_mapper/model.py

             )
         return self._connection
 
-    def _create_autoincrement_magic_item(self, table):
-        item = table.new_item(hash_key=MAGIC_KEY, attrs={
-            "__max_hash_key__": 0
-        })
-        # Conditional write: don't risk overwriting the DB.
-        item.put({item.hash_key_name: False})
-
     def set_credentials(self, aws_access_key_id, aws_secret_access_key):
         """Set the DynamoDB credentials. If boto is already configured on this
         machine, this step is optional.
         table = conn.create_table(cls.__table__, schema, read_units, write_units)
         table.refresh(wait_for_active=wait_for_active)
 
-        if hash_key_type is autoincrement_int:
-            self._create_autoincrement_magic_item(table)
-
         dblog.debug("Created table %s(%s, %s)", cls.__table__, hash_key_name, range_key_name)
 
         return table

real_dynamodb_tests.py

 import unittest
 from boto.exception import DynamoDBResponseError
 from boto.dynamodb.exceptions import DynamoDBKeyNotFoundError
-from dynamodb_mapper.model import DynamoDBModel, ConnectionBorg, OverwriteError, ConflictError
+from dynamodb_mapper.model import DynamoDBModel, ConnectionBorg, OverwriteError, ConflictError, autoincrement_int
 
 PREFIX=u"jttest-"
 
         u"cheats": set([u"Konami"]),
     }
 
-def init_table():
+class Trac(DynamoDBModel):
+    __table__ = PREFIX+u"trac"
+    __hash_key__ = u"id"
+    __schema__ = {
+        u"id": autoincrement_int,
+        u"title": unicode,
+        u"description": unicode,
+    }
+
+def init_tables():
+    conn = ConnectionBorg()
     try:
-        conn = ConnectionBorg()
         conn.create_table(DoomMap, 10, 10, wait_for_active=True)
     except DynamoDBResponseError, e:
         if e.body['message'].find(u"Duplicate table name") < 0:
             raise
+    try:
+        conn.create_table(Trac, 10, 10, wait_for_active=True)
+    except DynamoDBResponseError, e:
+        if e.body['message'].find(u"Duplicate table name") < 0:
+            raise
 
 # tests
 class TestSequenceFunctions(unittest.TestCase):
 
         self.assertEqual(bunch, len(data))
 
+    def test_auto_inc(self):
+        #insert a first item
+        ticket1 = Trac(title="auto_inc is not working", description="better not")
+        ticket1.save()
+        self.assertNotEqual(0, ticket1.id)
+        #insert another ticket
+        ticket2 = Trac(title="no idea what the bug is", description="random")
+        ticket2.save()
+        self.assertNotEqual(0, ticket2.id)
+        self.assertEqual(ticket1.id+1, ticket2.id)
+        #delete
+        ticket1.delete()
+        ticket2.delete()
+
+
 if __name__ == '__main__':
-    init_table()
+    init_tables()
     unittest.main()