Skip to content

Commit

Permalink
Create infrastructure for custom table classes
Browse files Browse the repository at this point in the history
Now one can set which table class TinyDB should use by setting the
`table_class` attribute on the TinyDB class or instance. This
gives us the flexibility to introduce more powerful features in
custom table classes without flooding `TinyDB.__init__` and `TinyDB.table`
with additional special arguments.

This also deprecates the `smart_cache` argument of `TinyDB.__init__` and
`TinyDB.table` in favor of this feature.

Example:

    TinyDB.table_class = SmartCacheTable  # For all new instances
    db.table_class = SmartCacheTable  # For all new tables
  • Loading branch information
msiemens committed Nov 27, 2014
1 parent 3f83daa commit fa2b76c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
4 changes: 3 additions & 1 deletion tests/conftest.py
@@ -1,4 +1,5 @@
import pytest
from tinydb.database import SmartCacheTable

from tinydb.middlewares import CachingMiddleware
from tinydb.storages import MemoryStorage
Expand All @@ -10,7 +11,8 @@ def get_db(smart_cache=False):
db_.purge_tables()

if smart_cache:
db_ = db_.table('_default', smart_cache=True)
db_.table_class = SmartCacheTable
db_ = db_.table('_default')

db_.insert_multiple({'int': 1, 'char': c} for c in 'abc')
return db_
Expand Down
32 changes: 30 additions & 2 deletions tests/test_tables.py
@@ -1,4 +1,8 @@
from tinydb import where
from warnings import catch_warnings
import pytest
from tinydb.utils import catch_warning
from tinydb import where, TinyDB
from tinydb.database import SmartCacheTable, Table


def test_tables_list(db):
Expand Down Expand Up @@ -57,7 +61,8 @@ def test_query_cache_size(db):


def test_smart_query_cache(db):
table = db.table('table3', smart_cache=True)
db.table_class = SmartCacheTable
table = db.table('table3')
query = where('int') == 1
dummy = where('int') == 2

Expand All @@ -83,6 +88,29 @@ def test_smart_query_cache(db):
assert table.count(where('int') == 1) == 0


def test_smart_query_cache_via_kwarg(db):
# For backwards compatibility
with pytest.raises(DeprecationWarning):
with catch_warning(DeprecationWarning):
table = db.table('table3', smart_cache=True)
assert isinstance(table, SmartCacheTable)


def test_custom_table_class_via_class_attribute(db):
TinyDB.table_class = SmartCacheTable

table = db.table('table3')
assert isinstance(table, SmartCacheTable)

TinyDB.table_class = Table


def test_custom_table_class_via_instance_attribute(db):
db.table_class = SmartCacheTable
table = db.table('table3')
assert isinstance(table, SmartCacheTable)


def test_lru_cache(db):
# Test integration into TinyDB
table = db.table('table3', cache_size=2)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_tinydb.py
Expand Up @@ -5,9 +5,14 @@
from . conftest import get_db

from tinydb import TinyDB, where
from tinydb.database import Table, SmartCacheTable
from tinydb.storages import MemoryStorage

dbs = lambda: [get_db(), get_db(smart_cache=True)]
def dbs():
yield get_db()
yield get_db(smart_cache=True)

# dbs = lambda: [get_db(), get_db(smart_cache=True)]


@pytest.mark.parametrize('db', dbs())
Expand Down
16 changes: 15 additions & 1 deletion tinydb/database.py
Expand Up @@ -2,6 +2,7 @@
Contains the :class:`database <tinydb.database.TinyDB>` and
:class:`tables <tinydb.database.Table>` implementation.
"""
import warnings
from tinydb import JSONStorage
from tinydb.utils import LRUCache

Expand Down Expand Up @@ -63,7 +64,16 @@ def table(self, name='_default', smart_cache=False, **options):
if name in self._table_cache:
return self._table_cache[name]

table_class = SmartCacheTable if smart_cache else Table
if smart_cache:
warnings.warn('Passing the smart_cache argument is deprecated. '
'Please set the table class to use via '
'`db.table_class = SmartCacheTable` or '
'`TinyDB.table_class = SmartCacheTable` instead.',
DeprecationWarning)

# If smart_cache is set, use SmartCacheTable to retain backwards
# compatibility
table_class = SmartCacheTable if smart_cache else self.table_class
table = table_class(name, self, **options)

self._table_cache[name] = table
Expand Down Expand Up @@ -511,3 +521,7 @@ def purge(self):

super(SmartCacheTable, self).purge()
self._query_cache.clear() # Query cache got invalid


# Set the default table class
TinyDB.table_class = Table

0 comments on commit fa2b76c

Please sign in to comment.