public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] proj/g-sorcery:dev commit in: g_sorcery/bson/, tests/, g_sorcery/, g_sorcery/file_bson/, /
  2015-04-22  7:35 [gentoo-commits] proj/g-sorcery:master commit in: g_sorcery/file_bson/, /, g_sorcery/, tests/, g_sorcery/bson/ Jauhien Piatlicki
@ 2015-04-20 21:36 ` Jauhien Piatlicki
  0 siblings, 0 replies; 2+ messages in thread
From: Jauhien Piatlicki @ 2015-04-20 21:36 UTC (permalink / raw
  To: gentoo-commits

commit:     0ace08976a688a6d74cb86c70a156a87b3453214
Author:     Jauhien Piatlicki <jauhien <AT> gentoo <DOT> org>
AuthorDate: Fri Apr 17 14:34:46 2015 +0000
Commit:     Jauhien Piatlicki <jauhien <AT> gentoo <DOT> org>
CommitDate: Fri Apr 17 17:10:31 2015 +0000
URL:        https://gitweb.gentoo.org/proj/g-sorcery.git/commit/?id=0ace0897

[g_sorcery/file_bson] fix BSON support and add tests for it

 g_sorcery/db_layout.py                             |  2 +-
 g_sorcery/exceptions.py                            |  3 -
 g_sorcery/{bson => file_bson}/__init__.py          |  0
 g_sorcery/{bson/bson.py => file_bson/file_bson.py} |  8 +-
 g_sorcery/serialization.py                         |  4 +-
 setup.py                                           |  4 +-
 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
similarity index 100%
rename from g_sorcery/bson/__init__.py
rename to g_sorcery/file_bson/__init__.py

diff --git a/g_sorcery/bson/bson.py b/g_sorcery/file_bson/file_bson.py
similarity index 91%
rename from g_sorcery/bson/bson.py
rename to 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)
 
 

diff --git a/setup.py b/setup.py
index 866a38f..95b715e 100644
--- a/setup.py
+++ b/setup.py
@@ -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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [gentoo-commits] proj/g-sorcery:master commit in: g_sorcery/file_bson/, /, g_sorcery/, tests/, g_sorcery/bson/
@ 2015-04-22  7:35 Jauhien Piatlicki
  2015-04-20 21:36 ` [gentoo-commits] proj/g-sorcery:dev commit in: g_sorcery/bson/, tests/, g_sorcery/, g_sorcery/file_bson/, / Jauhien Piatlicki
  0 siblings, 1 reply; 2+ messages in thread
From: Jauhien Piatlicki @ 2015-04-22  7:35 UTC (permalink / raw
  To: gentoo-commits

commit:     0ace08976a688a6d74cb86c70a156a87b3453214
Author:     Jauhien Piatlicki <jauhien <AT> gentoo <DOT> org>
AuthorDate: Fri Apr 17 14:34:46 2015 +0000
Commit:     Jauhien Piatlicki <jauhien <AT> gentoo <DOT> org>
CommitDate: Fri Apr 17 17:10:31 2015 +0000
URL:        https://gitweb.gentoo.org/proj/g-sorcery.git/commit/?id=0ace0897

[g_sorcery/file_bson] fix BSON support and add tests for it

 g_sorcery/db_layout.py                             |  2 +-
 g_sorcery/exceptions.py                            |  3 -
 g_sorcery/{bson => file_bson}/__init__.py          |  0
 g_sorcery/{bson/bson.py => file_bson/file_bson.py} |  8 +-
 g_sorcery/serialization.py                         |  4 +-
 setup.py                                           |  4 +-
 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
similarity index 100%
rename from g_sorcery/bson/__init__.py
rename to g_sorcery/file_bson/__init__.py

diff --git a/g_sorcery/bson/bson.py b/g_sorcery/file_bson/file_bson.py
similarity index 91%
rename from g_sorcery/bson/bson.py
rename to 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)
 
 

diff --git a/setup.py b/setup.py
index 866a38f..95b715e 100644
--- a/setup.py
+++ b/setup.py
@@ -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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-04-22  7:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-04-22  7:35 [gentoo-commits] proj/g-sorcery:master commit in: g_sorcery/file_bson/, /, g_sorcery/, tests/, g_sorcery/bson/ Jauhien Piatlicki
2015-04-20 21:36 ` [gentoo-commits] proj/g-sorcery:dev commit in: g_sorcery/bson/, tests/, g_sorcery/, g_sorcery/file_bson/, / Jauhien Piatlicki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox