Skip to content

Commit

Permalink
Fixed #1: Problems with JSONStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
msiemens committed Apr 26, 2014
1 parent 495f672 commit 40398b9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ you should consider using a DB that is optimized for speed like Buzhug_ or
CodernityDB_.

How to Improve TinyDB Performance
`````````````````````````````````````
`````````````````````````````````

The default storage serializes the data using JSON. To improve performance,
you can install `ujson <http://pypi.python.org/pypi/ujson>`_ , a extremely
Expand All @@ -141,6 +141,11 @@ see `semver.org <http://semver.org/>`_
Changelog
---------

**v1.0.1** (2013-07-20)
^^^^^^^^^^^^^^^^^^^^^^^

- Fixed a bug in ``JSONStorage`` that broke the database when removing entries.

**v1.0.0** (2013-07-20)
^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Version Numbering
TinyDB follows the SemVer versioning guidelines. For more information,
see `semver.org <http://semver.org/>`_

**v1.0.1** (2013-07-20)
^^^^^^^^^^^^^^^^^^^^^^^

- Fixed a bug in ``JSONStorage`` that broke the database when removing entries.


**v1.0.0** (2013-07-20)
-----------------------

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name = "tinydb",
version = "1.0.0",
version = "1.0.1",
packages = find_packages(),

# development metadata
Expand Down
29 changes: 28 additions & 1 deletion tests/tests_storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from nose.tools import *

from tinydb import TinyDB, where
from tinydb.storages import JSONStorage, MemoryStorage

path = None
Expand All @@ -15,7 +16,7 @@


def setup():
global path, element
global path

# Generate temp file
tmp = tempfile.NamedTemporaryFile(delete=False)
Expand Down Expand Up @@ -43,6 +44,32 @@ def test_json():
assert_equal(element, storage.read())


def test_json_readwrite():
# Generate temp file
tmp = tempfile.NamedTemporaryFile(delete=False)
tmp.file.close() # Close file handle

# Create TinyDB instance
db = TinyDB(tmp.name, storage=JSONStorage)

item = {'name': 'A very long entry'}
item2 = {'name': 'A short one'}

get = lambda s: db.get(where('name') == s)

db.insert(item)
assert_equal(get('A very long entry'), item)

db.remove(where('name') == 'A very long entry')
assert_equal(get('A very long entry'), None)

db.insert(item2)
assert_equal(get('A short one'), item2)

db.remove(where('name') == 'A short one')
assert_equal(get('A short one'), None)


def test_in_memory():
# Write contents
storage = MemoryStorage()
Expand Down
1 change: 1 addition & 0 deletions tinydb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def table(self, name='_default'):
:param name: The name of the table.
:type name: str
"""

if name in self._table_cache:
return self._table_cache[name]

Expand Down
1 change: 1 addition & 0 deletions tinydb/storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def write(self, data):
self._handle.seek(0)
json.dump(data, self._handle)
self._handle.flush()
self._handle.truncate()

def read(self):
self._handle.seek(0)
Expand Down

0 comments on commit 40398b9

Please sign in to comment.