diff options
author | 2015-04-17 16:34:46 +0200 | |
---|---|---|
committer | 2015-04-17 19:10:31 +0200 | |
commit | 0ace08976a688a6d74cb86c70a156a87b3453214 (patch) | |
tree | f03c16b158213a33bf968feaa46f657aedac8b40 | |
parent | [g_sorcery/package_db] add category common data setter and getter to DB API (diff) | |
download | g-sorcery-0ace08976a688a6d74cb86c70a156a87b3453214.tar.gz g-sorcery-0ace08976a688a6d74cb86c70a156a87b3453214.tar.bz2 g-sorcery-0ace08976a688a6d74cb86c70a156a87b3453214.zip |
[g_sorcery/file_bson] fix BSON support and add tests for it
-rw-r--r-- | g_sorcery/db_layout.py | 2 | ||||
-rw-r--r-- | g_sorcery/exceptions.py | 3 | ||||
-rw-r--r-- | g_sorcery/file_bson/__init__.py (renamed from g_sorcery/bson/__init__.py) | 0 | ||||
-rw-r--r-- | g_sorcery/file_bson/file_bson.py (renamed from g_sorcery/bson/bson.py) | 8 | ||||
-rw-r--r-- | g_sorcery/serialization.py | 4 | ||||
-rw-r--r-- | setup.py | 4 | ||||
-rw-r--r-- | tests/test_FileBSON.py | 95 |
7 files changed, 103 insertions, 13 deletions
diff --git a/g_sorcery/db_layout.py b/g_sorcery/db_layout.py index ea4298a..1f4514c 100644 --- a/g_sorcery/db_layout.py +++ b/g_sorcery/db_layout.py @@ -40,7 +40,7 @@ SUPPORTED_FILE_FORMATS = {JSON_FILE_SUFFIX: CategoryJSON} # bson module is optional, we should check if it is installed try: - from g_sorcery.bson.bson import FileBSON + from g_sorcery.file_bson.file_bson import FileBSON class CategoryBSON(FileBSON): """ diff --git a/g_sorcery/exceptions.py b/g_sorcery/exceptions.py index 4691ce6..6d68f7f 100644 --- a/g_sorcery/exceptions.py +++ b/g_sorcery/exceptions.py @@ -52,6 +52,3 @@ class DigestError(GSorceryError): class DownloadingError(GSorceryError): pass - -class SerializationError(GSorceryError): - pass diff --git a/g_sorcery/bson/__init__.py b/g_sorcery/file_bson/__init__.py index 4265cc3..4265cc3 100644 --- a/g_sorcery/bson/__init__.py +++ b/g_sorcery/file_bson/__init__.py diff --git a/g_sorcery/bson/bson.py b/g_sorcery/file_bson/file_bson.py index fdb8bb9..7bc6b80 100644 --- a/g_sorcery/bson/bson.py +++ b/g_sorcery/file_bson/file_bson.py @@ -2,8 +2,8 @@ # -*- coding: utf-8 -*- """ - bson.py - ~~~~~~~ + file_bson.py + ~~~~~~~~~~~~ bson file format support @@ -29,10 +29,10 @@ class FileBSON(FileJSONData): content = {} bcnt = None with open(self.path, 'rb') as f: - bcnt = f.read() + bcnt = bson.BSON(f.read()) if not bcnt: raise FileJSONError('failed to read: ', self.path) - rawcnt = bson.BSON.decode(bcnt) + rawcnt = bcnt.decode() content = from_raw_serializable(rawcnt) return content diff --git a/g_sorcery/serialization.py b/g_sorcery/serialization.py index 780de6f..3a7704b 100644 --- a/g_sorcery/serialization.py +++ b/g_sorcery/serialization.py @@ -14,8 +14,6 @@ import json import importlib -from .exceptions import SerializationError - def step_to_raw_serializable(obj): """ Make one step of convertion of object @@ -54,7 +52,7 @@ def to_raw_serializable(obj): else: sobj = step_to_raw_serializable(obj) if not sobj: - raise SerializationError('Non serializable object: ', sobj) + raise TypeError('Non serializable object: ', sobj) return to_raw_serializable(sobj) @@ -15,7 +15,7 @@ import os from distutils.core import setup -SELECTABLE = ['bson'] +SELECTABLE = {'bson': 'file_bson'} use_defaults = ' '.join(list(SELECTABLE)) USE = os.environ.get("USE", use_defaults).split() @@ -23,7 +23,7 @@ USE = os.environ.get("USE", use_defaults).split() optional_modules = [] for mod in SELECTABLE: if mod in USE: - optional_modules.append('g_sorcery.%s' % mod) + optional_modules.append('g_sorcery.%s' % SELECTABLE[mod]) setup(name = 'g-sorcery', version = '0.2', diff --git a/tests/test_FileBSON.py b/tests/test_FileBSON.py new file mode 100644 index 0000000..ff2a7b3 --- /dev/null +++ b/tests/test_FileBSON.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" + test_FileBSON.py + ~~~~~~~~~~~~~~~~ + + FileBSON test suite + + :copyright: (c) 2013-2015 by Jauhien Piatlicki + :license: GPL-2, see LICENSE for more details. +""" + +import os +import unittest + +from tests.base import BaseTest + +BSON_INSTALLED = False + +try: + from g_sorcery.file_bson.file_bson import FileBSON + BSON_INSTALLED = True +except ImportError as e: + pass + +class NonSerializableClass(object): + pass + + +class SerializableClass(object): + + __slots__ = ("field1", "field2") + + def __init__(self, field1, field2): + self.field1 = field1 + self.field2 = field2 + + def __eq__(self, other): + return self.field1 == other.field1 \ + and self.field2 == other.field2 + + def serialize(self): + return {"field1": self.field1, "field2": self.field2} + + +class DeserializableClass(SerializableClass): + + @classmethod + def deserialize(cls, value): + return DeserializableClass(value["field1"], value["field2"]) + + +if BSON_INSTALLED: + + class TestFileJSON(BaseTest): + def setUp(self): + super(TestFileJSON, self).setUp() + self.directory = os.path.join(self.tempdir.name, 'tst') + self.name = 'tst.json' + self.path = os.path.join(self.directory, self.name) + + def test_write_read(self): + fj = FileBSON(self.directory, self.name, ["mandatory"]) + content = {"mandatory":"1", "test":"2"} + fj.write(content) + content_r = fj.read() + self.assertEqual(content, content_r) + + def test_serializable(self): + fj = FileBSON(self.directory, self.name, []) + content = SerializableClass("1", "2") + fj.write(content) + content_r = fj.read() + self.assertEqual(content_r, {"field1":"1", "field2":"2"}) + self.assertRaises(TypeError, fj.write, NonSerializableClass()) + + def test_deserializable(self): + fj = FileBSON(self.directory, self.name, []) + content = DeserializableClass("1", "2") + fj.write(content) + content_r = fj.read() + self.assertEqual(content, content_r) + + def suite(): + suite = unittest.TestSuite() + suite.addTest(TestFileJSON('test_write_read')) + suite.addTest(TestFileJSON('test_serializable')) + suite.addTest(TestFileJSON('test_deserializable')) + return suite + +else: + def suite(): + suite = unittest.TestSuite() + return suite |