Commits

Jean-Tiare Le Bigot  committed ec402ab

make DynamoDB._internal_delete_table idempotent in case table ref has already been deleted manually (cf unit testing). issue #8

  • Participants
  • Parent commits 66c9acf

Comments (0)

Files changed (3)

File ddbmock/database/db.py

         return self.data[name]
 
     # FIXME: what if the table ref changed in the mean time ?
-    def _internal_delete_table(self, name):
-        """This is ran only after the timer is exhausted"""
-        if name in self.data:
+    def _internal_delete_table(self, table):
+        """This is ran only after the timer is exhausted.
+        Important note: this function is idempotent. If another table with the
+        same name has been created in the mean time, it won't be overwrittem.
+        This is the kind of special cases you might encounter in testing
+        environenment"""
+        name = table.name
+        if name in self.data and self.data[name] is table:
             self.data[name].store.truncate()  # FIXME: should be moved in table
             del self.data[name]
             del self.store[name, False]

File ddbmock/database/table.py

 
         self.status = "DELETING"
 
-        Timer(config.DELAY_DELETING, callback, [self.name]).start()
+        Timer(config.DELAY_DELETING, callback, [self]).start()
 
     def activate(self):
         self.status = "ACTIVE"

File tests/unit/test_db.py

     def setUp(self):
         from ddbmock.database import dynamodb
 
-        dynamodb.data[TABLE_NAME] = mock.Mock()
+        self.t1 = mock.Mock()
+        self.t1.name = TABLE_NAME
+
+        dynamodb.data[TABLE_NAME] = self.t1
         dynamodb.store[TABLE_NAME, False] = dynamodb.data[TABLE_NAME]
 
     def test_internal_delete_table(self):
         from ddbmock.database import dynamodb
 
+        # make sure that deleting another table with the same name does nothing
+        t2 = mock.Mock()
+        t2.name = TABLE_NAME
+
         # delete a table
-        dynamodb._internal_delete_table(TABLE_NAME)
-        self.assertNotIn(TABLE_NAME, dynamodb.data)
+        dynamodb._internal_delete_table(t2)
+        self.assertIn(TABLE_NAME, dynamodb.data)
 
         # make sure deleting already deleted table does not harm
-        dynamodb._internal_delete_table(TABLE_NAME)
+        dynamodb._internal_delete_table(self.t1)
 
     def test_delete_table(self):
         from ddbmock.database import dynamodb