Jean-Tiare Le Bigot avatar Jean-Tiare Le Bigot committed 2eecd86

add a configuration param to switch between storage backends

Comments (0)

Files changed (8)

 - full ``Query`` support
 - throughput statistics to help plan real usage
 - datastore API
-- external memory store
-- integrate boto dynamodb integration tests to test suite (disabled unless '--no-skip')
+    - bundle memory store
+    - bundle sqlite store
+    - add config param to switch
 
 Removal
 -------
 - pyramid and boto entry points now shares most of the router
 - pre-instanciate DynamoDB as dynamodb
 - UpdateItem failed to save keys properly
+- integrate boto dynamodb integration tests to test suite (disabled unless '--no-skip')
 
 Upgrade
 -------

ddbmock/config.py

 
 ### Storage specific settings ###
 
-#SQLite database location
+# Storage engine ro use ('memory' or 'sqlite')
+STORAGE_ENGINE_NAME = 'memory'
+# SQLite database location
 STORAGE_SQLITE_FILE = 'dynamo.db'

ddbmock/database/storage/__init__.py

 # -*- coding: utf-8 -*-
+
+from ddbmock import config
+
+_store_mod = __import__(config.STORAGE_ENGINE_NAME, globals(), locals(), ['Store'], 1)
+Store = _store_mod.Store

ddbmock/database/storage/memory.py

 
 from collections import defaultdict
 
-class MemoryStore(object):
+class Store(object):
     def __init__(self, name):
         """ Initialize the in-memory store
         :param name: Table name.

ddbmock/database/storage/sqlite.py

 # in-memory version of sqlite
 conn = sqlite3.connect(config.STORAGE_SQLITE_FILE)
 
-class SQLiteStore(object):
+class Store(object):
     def __init__(self, name):
         """ Initialize the sqlite store
 

ddbmock/database/table.py

 
 from .key import Key, PrimaryKey
 from .item import Item, ItemSize
+from .storage import Store
 from ddbmock import config
 from collections import defaultdict, namedtuple
 from threading import Timer
 from ddbmock.errors import ValidationException, LimitExceededException, ResourceInUseException
 import time, copy, datetime
 
-# TMP
-from .storage.memory import MemoryStore as Store
-#from .storage.sqlite import SQLiteStore as Store
 
 def change_is_less_than_x_percent(current, candidate, threshold):
     """Return True iff 0% < change < 10%"""

tests/unit/test_database_storage_memory.py

 
 class TestMemoryStore(unittest.TestCase):
     def test_truncate(self):
-        from ddbmock.database.storage.memory import MemoryStore
+        from ddbmock.database.storage.memory import Store
 
-        ms = MemoryStore(NAME)
+        ms = Store(NAME)
 
         ms.data[1] = "some data"
         ms.data[2] = "some data"
         self.assertFalse(ms.data)
 
     def test_set_item(self):
-        from ddbmock.database.storage.memory import MemoryStore
+        from ddbmock.database.storage.memory import Store
 
-        ms = MemoryStore(NAME)
+        ms = Store(NAME)
 
         ms[HASH,RANGE1] = DATA1
         self.assertEqual(DATA1, ms.data[HASH][RANGE1])
 
     def test_get_item(self):
-        from ddbmock.database.storage.memory import MemoryStore
+        from ddbmock.database.storage.memory import Store
 
-        ms = MemoryStore(NAME)
+        ms = Store(NAME)
 
         ms.data[HASH][RANGE1] = DATA1
         ms.data[HASH][RANGE2] = DATA2
         self.assertEqual({RANGE1:DATA1, RANGE2:DATA2}, ms[HASH, None])
 
     def test_del_item(self):
-        from ddbmock.database.storage.memory import MemoryStore
+        from ddbmock.database.storage.memory import Store
 
-        ms = MemoryStore(NAME)
+        ms = Store(NAME)
 
         ms.data[HASH][RANGE1] = DATA1
         ms.data[HASH][RANGE2] = DATA2
         self.assertNotIn(RANGE2, ms.data[HASH])
 
     def test_iter(self):
-        from ddbmock.database.storage.memory import MemoryStore
+        from ddbmock.database.storage.memory import Store
 
-        ms = MemoryStore(NAME)
+        ms = Store(NAME)
 
         ms.data[HASH][RANGE1] = DATA1
         ms.data[HASH][RANGE2] = DATA2

tests/unit/test_database_storage_sqlite.py

         self.conn.commit()
 
     def test_truncate(self):
-        from ddbmock.database.storage.sqlite import SQLiteStore
+        from ddbmock.database.storage.sqlite import Store
 
-        store = SQLiteStore(TABLE_NAME)
+        store = Store(TABLE_NAME)
         store.truncate()
 
         count = self.conn.execute('SELECT Count(*) FROM test_table').fetchone()
         self.assertEqual(0, count[0])
 
     def test_iter(self):
-        from ddbmock.database.storage.sqlite import SQLiteStore
+        from ddbmock.database.storage.sqlite import Store
 
-        store = SQLiteStore(TABLE_NAME)
+        store = Store(TABLE_NAME)
         self.assertEqual([ITEM1, ITEM2, ITEM3, ITEM4], list(store))
 
     def test_get_item(self):
-        from ddbmock.database.storage.sqlite import SQLiteStore
+        from ddbmock.database.storage.sqlite import Store
 
-        store = SQLiteStore(TABLE_NAME)
+        store = Store(TABLE_NAME)
         self.assertEqual(ITEM2, store[(123, 'titi')])
         self.assertEqual(ITEM4, store[(456, 'toto')])
 
         self.assertRaises(KeyError, store.__getitem__, (132, '404'))
 
     def test_del_item(self):
-        from ddbmock.database.storage.sqlite import SQLiteStore
+        from ddbmock.database.storage.sqlite import Store
 
-        store = SQLiteStore(TABLE_NAME)
+        store = Store(TABLE_NAME)
         del store[123, 'toto']
         del store[123, 'titi']
         del store[404, 'titi']
         self.assertEqual([ITEM3], list(store))
 
     def test_set_item(self):
-        from ddbmock.database.storage.sqlite import SQLiteStore
+        from ddbmock.database.storage.sqlite import Store
 
-        store = SQLiteStore(TABLE_NAME)
+        store = Store(TABLE_NAME)
 
         store[123, 'titi'] = ITEM5
         store[456, 'titi'] = ITEM6
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.