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

commit:     d01b0ddac70c475225b73f36a4b863598653b0b0
Author:     Jauhien Piatlicki <jauhien <AT> gentoo <DOT> org>
AuthorDate: Fri Apr 17 15:02:38 2015 +0000
Commit:     Jauhien Piatlicki <jauhien <AT> gentoo <DOT> org>
CommitDate: Fri Apr 17 17:10:47 2015 +0000
URL:        https://gitweb.gentoo.org/proj/g-sorcery.git/commit/?id=d01b0dda

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

 g_sorcery/db_layout.py  |   6 ++-
 tests/server.py         |   4 +-
 tests/test_PackageDB.py | 133 ++++++++++++++++++++++++++----------------------
 3 files changed, 80 insertions(+), 63 deletions(-)

diff --git a/g_sorcery/db_layout.py b/g_sorcery/db_layout.py
index 1f4514c..da779bd 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.file_bson.file_bson import FileBSON
+    from .file_bson.file_bson import FileBSON
 
     class CategoryBSON(FileBSON):
         """
@@ -172,9 +172,13 @@ def get_layout(metadata):
         return (CategoryJSON, [file_name(CATEGORIES_FILE_NAME)])
     elif layout_version == 1:
         category_format = metadata['category_format']
+        wrong_fmt = True
         try:
             category_cls = SUPPORTED_FILE_FORMATS[category_format]
+            wrong_fmt = False
         except KeyError:
+            pass
+        if wrong_fmt:
             raise DBLayoutError("unsupported packages file format: " + category_format)
         return (category_cls, [file_name(CATEGORIES_FILE_NAME), file_name(METADATA_FILE_NAME)])
     else:

diff --git a/tests/server.py b/tests/server.py
index 5cb813f..51d49b7 100644
--- a/tests/server.py
+++ b/tests/server.py
@@ -38,10 +38,10 @@ def HTTPRequestHandlerGenerator(direct):
 
     
 class Server(threading.Thread):
-    def __init__(self, directory):
+    def __init__(self, directory, port=8080):
         super(Server, self).__init__()
         HTTPServer.allow_reuse_address = True
-        server_address = ('127.0.0.1', 8080)
+        server_address = ('127.0.0.1', port)
         self.httpd = HTTPServer(server_address, HTTPRequestHandlerGenerator(directory))
     
     def run(self):

diff --git a/tests/test_PackageDB.py b/tests/test_PackageDB.py
index 2a67385..221be00 100644
--- a/tests/test_PackageDB.py
+++ b/tests/test_PackageDB.py
@@ -16,6 +16,7 @@ import time
 import unittest
 
 from g_sorcery.compatibility import TemporaryDirectory
+from g_sorcery.db_layout import JSON_FILE_SUFFIX, BSON_FILE_SUFFIX
 from g_sorcery.exceptions import IntegrityError, InvalidKeyError, SyncError
 from g_sorcery.g_collections import Package
 from g_sorcery.package_db import PackageDB
@@ -23,6 +24,14 @@ from g_sorcery.package_db import PackageDB
 from tests.base import BaseTest
 from tests.server import Server
 
+SUPPORTED_FILE_FORMATS = [JSON_FILE_SUFFIX]
+# bson module is optional, we should check if it is installed
+try:
+    from g_sorcery.file_bson.file_bson import FileBSON
+    SUPPORTED_FILE_FORMATS.append(BSON_FILE_SUFFIX)
+except ImportError as e:
+    pass
+
 
 class TestDB(PackageDB):
     def get_real_db_uri(self, db_uri):
@@ -32,66 +41,70 @@ class TestDB(PackageDB):
 class TestPackageDB(BaseTest):
 
     def test_functionality(self):
-        orig_tempdir = TemporaryDirectory()
-        orig_path = os.path.join(orig_tempdir.name, "db")
-        os.makedirs(orig_path)
-        orig_db = PackageDB(orig_path)
-        orig_db.add_category("app-test1")
-        orig_db.add_category("app-test2")
-        ebuild_data = {"test1": "tst1", "test2": "tst2"}
-        common_data = {"common1": "cmn1", "common2": "cmn2"}
-        packages = [Package("app-test1", "test", "1"), Package("app-test1", "test", "2"),
-                    Package("app-test1", "test1", "1"), Package("app-test2", "test2", "1")]
-        for package in packages:
-            orig_db.add_package(package, ebuild_data)
-        orig_db.set_common_data("app-test1", common_data)
-        full_data = dict(ebuild_data)
-        full_data.update(common_data)
-
-        orig_db.write()
-        os.system("cd " + orig_tempdir.name + " && tar cvzf good.tar.gz db")
-        os.system("echo invalid >> " + orig_tempdir.name + "/db/app-test1/packages.json")
-        os.system("cd " + orig_tempdir.name + " && tar cvzf dummy.tar.gz db")
-
-        test_db = TestDB(self.tempdir.name)
-        self.assertRaises(SyncError, test_db.sync, "127.0.0.1:8080")
-
-        srv = Server(orig_tempdir.name)
-        srv.start()
-        self.assertRaises(IntegrityError, test_db.sync, "127.0.0.1:8080")
-        os.system("cd " + orig_tempdir.name + " && mv good.tar.gz dummy.tar.gz")
-        test_db.sync("127.0.0.1:8080")
-        srv.shutdown()
-        srv.join()
-        test_db.read()
-        self.assertEqual(orig_db.database, test_db.database)
-        self.assertEqual(orig_db.get_common_data("app-test1"), test_db.get_common_data("app-test1"))
-        self.assertEqual(orig_db.get_common_data("app-test2"), test_db.get_common_data("app-test2"))
-        self.assertEqual(set(test_db.list_categories()), set(["app-test1", "app-test2"]))
-        self.assertTrue(test_db.in_category("app-test1", "test"))
-        self.assertFalse(test_db.in_category("app-test2", "test"))
-        self.assertRaises(InvalidKeyError, test_db.in_category, "app-test3", "test")
-        self.assertEqual(set(test_db.list_package_names("app-test1")), set(['test', 'test1']))
-        self.assertEqual(set(test_db.list_catpkg_names()),set(['app-test1/test', 'app-test1/test1', 'app-test2/test2']))
-        self.assertRaises(InvalidKeyError, test_db.list_package_versions, "invalid", "test")
-        self.assertRaises(InvalidKeyError, test_db.list_package_versions, "app-test1", "invalid")
-        self.assertEqual(set(test_db.list_package_versions("app-test1", "test")), set(['1', '2']))
-        self.assertEqual(set(test_db.list_all_packages()), set(packages))
-        self.assertEqual(test_db.get_package_description(packages[0]), full_data)
-        self.assertRaises(KeyError, test_db.get_package_description, Package("invalid", "invalid", "1"))
-        self.assertEqual(test_db.get_max_version("app-test1", "test"), "2")
-        self.assertEqual(test_db.get_max_version("app-test1", "test1"), "1")
-        self.assertRaises(InvalidKeyError, test_db.get_max_version, "invalid", "invalid")
-        pkg_set = set(packages)
-        for package, data in test_db:
-            self.assertTrue(package in pkg_set)
-            if package.category == "app-test1":
-                self.assertEqual(data, full_data)
-            else:
-                self.assertEqual(data, ebuild_data)
-            pkg_set.remove(package)
-        self.assertTrue(not pkg_set)
-        self.assertEqual(orig_db.database, test_db.database)
+        port = 8080
+        for fmt in SUPPORTED_FILE_FORMATS:
+            sync_address = "127.0.0.1:" + str(port)
+            orig_tempdir = TemporaryDirectory()
+            orig_path = os.path.join(orig_tempdir.name, "db")
+            os.makedirs(orig_path)
+            orig_db = PackageDB(orig_path, preferred_category_format=fmt)
+            orig_db.add_category("app-test1")
+            orig_db.add_category("app-test2")
+            ebuild_data = {"test1": "tst1", "test2": "tst2"}
+            common_data = {"common1": "cmn1", "common2": "cmn2"}
+            packages = [Package("app-test1", "test", "1"), Package("app-test1", "test", "2"),
+                        Package("app-test1", "test1", "1"), Package("app-test2", "test2", "1")]
+            for package in packages:
+                orig_db.add_package(package, ebuild_data)
+            orig_db.set_common_data("app-test1", common_data)
+            full_data = dict(ebuild_data)
+            full_data.update(common_data)
+
+            orig_db.write()
+            os.system("cd " + orig_tempdir.name + " && tar cvzf good.tar.gz db")
+            os.system("echo invalid >> " + orig_tempdir.name + "/db/app-test1/packages." + fmt)
+            os.system("cd " + orig_tempdir.name + " && tar cvzf dummy.tar.gz db")
+
+            test_db = TestDB(self.tempdir.name)
+            self.assertRaises(SyncError, test_db.sync, sync_address)
+
+            srv = Server(orig_tempdir.name, port=port)
+            srv.start()
+            self.assertRaises(IntegrityError, test_db.sync, sync_address)
+            os.system("cd " + orig_tempdir.name + " && mv good.tar.gz dummy.tar.gz")
+            test_db.sync(sync_address)
+            srv.shutdown()
+            srv.join()
+            test_db.read()
+            self.assertEqual(orig_db.database, test_db.database)
+            self.assertEqual(orig_db.get_common_data("app-test1"), test_db.get_common_data("app-test1"))
+            self.assertEqual(orig_db.get_common_data("app-test2"), test_db.get_common_data("app-test2"))
+            self.assertEqual(set(test_db.list_categories()), set(["app-test1", "app-test2"]))
+            self.assertTrue(test_db.in_category("app-test1", "test"))
+            self.assertFalse(test_db.in_category("app-test2", "test"))
+            self.assertRaises(InvalidKeyError, test_db.in_category, "app-test3", "test")
+            self.assertEqual(set(test_db.list_package_names("app-test1")), set(['test', 'test1']))
+            self.assertEqual(set(test_db.list_catpkg_names()),set(['app-test1/test', 'app-test1/test1', 'app-test2/test2']))
+            self.assertRaises(InvalidKeyError, test_db.list_package_versions, "invalid", "test")
+            self.assertRaises(InvalidKeyError, test_db.list_package_versions, "app-test1", "invalid")
+            self.assertEqual(set(test_db.list_package_versions("app-test1", "test")), set(['1', '2']))
+            self.assertEqual(set(test_db.list_all_packages()), set(packages))
+            self.assertEqual(test_db.get_package_description(packages[0]), full_data)
+            self.assertRaises(KeyError, test_db.get_package_description, Package("invalid", "invalid", "1"))
+            self.assertEqual(test_db.get_max_version("app-test1", "test"), "2")
+            self.assertEqual(test_db.get_max_version("app-test1", "test1"), "1")
+            self.assertRaises(InvalidKeyError, test_db.get_max_version, "invalid", "invalid")
+            pkg_set = set(packages)
+            for package, data in test_db:
+                self.assertTrue(package in pkg_set)
+                if package.category == "app-test1":
+                    self.assertEqual(data, full_data)
+                else:
+                    self.assertEqual(data, ebuild_data)
+                pkg_set.remove(package)
+            self.assertTrue(not pkg_set)
+            self.assertEqual(orig_db.database, test_db.database)
+            port = port + 1
 
 def suite():
     suite = unittest.TestSuite()


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

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

commit:     d01b0ddac70c475225b73f36a4b863598653b0b0
Author:     Jauhien Piatlicki <jauhien <AT> gentoo <DOT> org>
AuthorDate: Fri Apr 17 15:02:38 2015 +0000
Commit:     Jauhien Piatlicki <jauhien <AT> gentoo <DOT> org>
CommitDate: Fri Apr 17 17:10:47 2015 +0000
URL:        https://gitweb.gentoo.org/proj/g-sorcery.git/commit/?id=d01b0dda

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

 g_sorcery/db_layout.py  |   6 ++-
 tests/server.py         |   4 +-
 tests/test_PackageDB.py | 133 ++++++++++++++++++++++++++----------------------
 3 files changed, 80 insertions(+), 63 deletions(-)

diff --git a/g_sorcery/db_layout.py b/g_sorcery/db_layout.py
index 1f4514c..da779bd 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.file_bson.file_bson import FileBSON
+    from .file_bson.file_bson import FileBSON
 
     class CategoryBSON(FileBSON):
         """
@@ -172,9 +172,13 @@ def get_layout(metadata):
         return (CategoryJSON, [file_name(CATEGORIES_FILE_NAME)])
     elif layout_version == 1:
         category_format = metadata['category_format']
+        wrong_fmt = True
         try:
             category_cls = SUPPORTED_FILE_FORMATS[category_format]
+            wrong_fmt = False
         except KeyError:
+            pass
+        if wrong_fmt:
             raise DBLayoutError("unsupported packages file format: " + category_format)
         return (category_cls, [file_name(CATEGORIES_FILE_NAME), file_name(METADATA_FILE_NAME)])
     else:

diff --git a/tests/server.py b/tests/server.py
index 5cb813f..51d49b7 100644
--- a/tests/server.py
+++ b/tests/server.py
@@ -38,10 +38,10 @@ def HTTPRequestHandlerGenerator(direct):
 
     
 class Server(threading.Thread):
-    def __init__(self, directory):
+    def __init__(self, directory, port=8080):
         super(Server, self).__init__()
         HTTPServer.allow_reuse_address = True
-        server_address = ('127.0.0.1', 8080)
+        server_address = ('127.0.0.1', port)
         self.httpd = HTTPServer(server_address, HTTPRequestHandlerGenerator(directory))
     
     def run(self):

diff --git a/tests/test_PackageDB.py b/tests/test_PackageDB.py
index 2a67385..221be00 100644
--- a/tests/test_PackageDB.py
+++ b/tests/test_PackageDB.py
@@ -16,6 +16,7 @@ import time
 import unittest
 
 from g_sorcery.compatibility import TemporaryDirectory
+from g_sorcery.db_layout import JSON_FILE_SUFFIX, BSON_FILE_SUFFIX
 from g_sorcery.exceptions import IntegrityError, InvalidKeyError, SyncError
 from g_sorcery.g_collections import Package
 from g_sorcery.package_db import PackageDB
@@ -23,6 +24,14 @@ from g_sorcery.package_db import PackageDB
 from tests.base import BaseTest
 from tests.server import Server
 
+SUPPORTED_FILE_FORMATS = [JSON_FILE_SUFFIX]
+# bson module is optional, we should check if it is installed
+try:
+    from g_sorcery.file_bson.file_bson import FileBSON
+    SUPPORTED_FILE_FORMATS.append(BSON_FILE_SUFFIX)
+except ImportError as e:
+    pass
+
 
 class TestDB(PackageDB):
     def get_real_db_uri(self, db_uri):
@@ -32,66 +41,70 @@ class TestDB(PackageDB):
 class TestPackageDB(BaseTest):
 
     def test_functionality(self):
-        orig_tempdir = TemporaryDirectory()
-        orig_path = os.path.join(orig_tempdir.name, "db")
-        os.makedirs(orig_path)
-        orig_db = PackageDB(orig_path)
-        orig_db.add_category("app-test1")
-        orig_db.add_category("app-test2")
-        ebuild_data = {"test1": "tst1", "test2": "tst2"}
-        common_data = {"common1": "cmn1", "common2": "cmn2"}
-        packages = [Package("app-test1", "test", "1"), Package("app-test1", "test", "2"),
-                    Package("app-test1", "test1", "1"), Package("app-test2", "test2", "1")]
-        for package in packages:
-            orig_db.add_package(package, ebuild_data)
-        orig_db.set_common_data("app-test1", common_data)
-        full_data = dict(ebuild_data)
-        full_data.update(common_data)
-
-        orig_db.write()
-        os.system("cd " + orig_tempdir.name + " && tar cvzf good.tar.gz db")
-        os.system("echo invalid >> " + orig_tempdir.name + "/db/app-test1/packages.json")
-        os.system("cd " + orig_tempdir.name + " && tar cvzf dummy.tar.gz db")
-
-        test_db = TestDB(self.tempdir.name)
-        self.assertRaises(SyncError, test_db.sync, "127.0.0.1:8080")
-
-        srv = Server(orig_tempdir.name)
-        srv.start()
-        self.assertRaises(IntegrityError, test_db.sync, "127.0.0.1:8080")
-        os.system("cd " + orig_tempdir.name + " && mv good.tar.gz dummy.tar.gz")
-        test_db.sync("127.0.0.1:8080")
-        srv.shutdown()
-        srv.join()
-        test_db.read()
-        self.assertEqual(orig_db.database, test_db.database)
-        self.assertEqual(orig_db.get_common_data("app-test1"), test_db.get_common_data("app-test1"))
-        self.assertEqual(orig_db.get_common_data("app-test2"), test_db.get_common_data("app-test2"))
-        self.assertEqual(set(test_db.list_categories()), set(["app-test1", "app-test2"]))
-        self.assertTrue(test_db.in_category("app-test1", "test"))
-        self.assertFalse(test_db.in_category("app-test2", "test"))
-        self.assertRaises(InvalidKeyError, test_db.in_category, "app-test3", "test")
-        self.assertEqual(set(test_db.list_package_names("app-test1")), set(['test', 'test1']))
-        self.assertEqual(set(test_db.list_catpkg_names()),set(['app-test1/test', 'app-test1/test1', 'app-test2/test2']))
-        self.assertRaises(InvalidKeyError, test_db.list_package_versions, "invalid", "test")
-        self.assertRaises(InvalidKeyError, test_db.list_package_versions, "app-test1", "invalid")
-        self.assertEqual(set(test_db.list_package_versions("app-test1", "test")), set(['1', '2']))
-        self.assertEqual(set(test_db.list_all_packages()), set(packages))
-        self.assertEqual(test_db.get_package_description(packages[0]), full_data)
-        self.assertRaises(KeyError, test_db.get_package_description, Package("invalid", "invalid", "1"))
-        self.assertEqual(test_db.get_max_version("app-test1", "test"), "2")
-        self.assertEqual(test_db.get_max_version("app-test1", "test1"), "1")
-        self.assertRaises(InvalidKeyError, test_db.get_max_version, "invalid", "invalid")
-        pkg_set = set(packages)
-        for package, data in test_db:
-            self.assertTrue(package in pkg_set)
-            if package.category == "app-test1":
-                self.assertEqual(data, full_data)
-            else:
-                self.assertEqual(data, ebuild_data)
-            pkg_set.remove(package)
-        self.assertTrue(not pkg_set)
-        self.assertEqual(orig_db.database, test_db.database)
+        port = 8080
+        for fmt in SUPPORTED_FILE_FORMATS:
+            sync_address = "127.0.0.1:" + str(port)
+            orig_tempdir = TemporaryDirectory()
+            orig_path = os.path.join(orig_tempdir.name, "db")
+            os.makedirs(orig_path)
+            orig_db = PackageDB(orig_path, preferred_category_format=fmt)
+            orig_db.add_category("app-test1")
+            orig_db.add_category("app-test2")
+            ebuild_data = {"test1": "tst1", "test2": "tst2"}
+            common_data = {"common1": "cmn1", "common2": "cmn2"}
+            packages = [Package("app-test1", "test", "1"), Package("app-test1", "test", "2"),
+                        Package("app-test1", "test1", "1"), Package("app-test2", "test2", "1")]
+            for package in packages:
+                orig_db.add_package(package, ebuild_data)
+            orig_db.set_common_data("app-test1", common_data)
+            full_data = dict(ebuild_data)
+            full_data.update(common_data)
+
+            orig_db.write()
+            os.system("cd " + orig_tempdir.name + " && tar cvzf good.tar.gz db")
+            os.system("echo invalid >> " + orig_tempdir.name + "/db/app-test1/packages." + fmt)
+            os.system("cd " + orig_tempdir.name + " && tar cvzf dummy.tar.gz db")
+
+            test_db = TestDB(self.tempdir.name)
+            self.assertRaises(SyncError, test_db.sync, sync_address)
+
+            srv = Server(orig_tempdir.name, port=port)
+            srv.start()
+            self.assertRaises(IntegrityError, test_db.sync, sync_address)
+            os.system("cd " + orig_tempdir.name + " && mv good.tar.gz dummy.tar.gz")
+            test_db.sync(sync_address)
+            srv.shutdown()
+            srv.join()
+            test_db.read()
+            self.assertEqual(orig_db.database, test_db.database)
+            self.assertEqual(orig_db.get_common_data("app-test1"), test_db.get_common_data("app-test1"))
+            self.assertEqual(orig_db.get_common_data("app-test2"), test_db.get_common_data("app-test2"))
+            self.assertEqual(set(test_db.list_categories()), set(["app-test1", "app-test2"]))
+            self.assertTrue(test_db.in_category("app-test1", "test"))
+            self.assertFalse(test_db.in_category("app-test2", "test"))
+            self.assertRaises(InvalidKeyError, test_db.in_category, "app-test3", "test")
+            self.assertEqual(set(test_db.list_package_names("app-test1")), set(['test', 'test1']))
+            self.assertEqual(set(test_db.list_catpkg_names()),set(['app-test1/test', 'app-test1/test1', 'app-test2/test2']))
+            self.assertRaises(InvalidKeyError, test_db.list_package_versions, "invalid", "test")
+            self.assertRaises(InvalidKeyError, test_db.list_package_versions, "app-test1", "invalid")
+            self.assertEqual(set(test_db.list_package_versions("app-test1", "test")), set(['1', '2']))
+            self.assertEqual(set(test_db.list_all_packages()), set(packages))
+            self.assertEqual(test_db.get_package_description(packages[0]), full_data)
+            self.assertRaises(KeyError, test_db.get_package_description, Package("invalid", "invalid", "1"))
+            self.assertEqual(test_db.get_max_version("app-test1", "test"), "2")
+            self.assertEqual(test_db.get_max_version("app-test1", "test1"), "1")
+            self.assertRaises(InvalidKeyError, test_db.get_max_version, "invalid", "invalid")
+            pkg_set = set(packages)
+            for package, data in test_db:
+                self.assertTrue(package in pkg_set)
+                if package.category == "app-test1":
+                    self.assertEqual(data, full_data)
+                else:
+                    self.assertEqual(data, ebuild_data)
+                pkg_set.remove(package)
+            self.assertTrue(not pkg_set)
+            self.assertEqual(orig_db.database, test_db.database)
+            port = port + 1
 
 def suite():
     suite = unittest.TestSuite()


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

* [gentoo-commits] proj/g-sorcery:master commit in: tests/, g_sorcery/
@ 2015-04-22  7:35 Jauhien Piatlicki
  0 siblings, 0 replies; 4+ messages in thread
From: Jauhien Piatlicki @ 2015-04-22  7:35 UTC (permalink / raw
  To: gentoo-commits

commit:     4b58d04be635a430c6e24666ac43c92fbd79af73
Author:     Jauhien Piatlicki <jauhien <AT> gentoo <DOT> org>
AuthorDate: Sat Apr 18 17:21:21 2015 +0000
Commit:     Jauhien Piatlicki <jauhien <AT> gentoo <DOT> org>
CommitDate: Sat Apr 18 20:03:58 2015 +0000
URL:        https://gitweb.gentoo.org/proj/g-sorcery.git/commit/?id=4b58d04b

[g_sorcery/serialization] fix serialization for collections

 g_sorcery/g_collections.py | 13 +++++++-----
 g_sorcery/serialization.py | 15 +++++++++-----
 tests/serializable.py      | 38 +++++++++++++++++++++++++++++++++++
 tests/test_FileBSON.py     | 40 ++++++++++++-------------------------
 tests/test_FileJSON.py     | 50 ++++++++++++++++------------------------------
 tests/test_PackageDB.py    | 11 +++++++---
 6 files changed, 94 insertions(+), 73 deletions(-)

diff --git a/g_sorcery/g_collections.py b/g_sorcery/g_collections.py
index b28db6a..b9f3d06 100644
--- a/g_sorcery/g_collections.py
+++ b/g_sorcery/g_collections.py
@@ -4,14 +4,14 @@
 """
     g_collections.py
     ~~~~~~~~~~~~~~~~
-    
+
     Customized classes of standard python data types
     for use withing g-sorcery for custom formatted string output
     substitution in our ebuild templates and classes for storing
     information about packages and dependencies.
-    
+
     :copyright: (c) 2013 by Brian Dolbec
-    :copyright: (c) 2013 by Jauhien Piatlicki
+    :copyright: (c) 2013-2015 by Jauhien Piatlicki
     :license: GPL-2, see LICENSE for more details.
 """
 
@@ -52,7 +52,7 @@ class serializable_elist(object):
     """
 
     __slots__ = ('data')
-    
+
     def __init__(self, iterable=None, separator=' '):
         '''
         iterable: initialize from iterable's items
@@ -60,6 +60,9 @@ class serializable_elist(object):
         '''
         self.data = elist(iterable or [], separator)
 
+    def __eq__(self, other):
+        return self.data == other.data
+
     def __iter__(self):
         return iter(self.data)
 
@@ -122,7 +125,7 @@ class Dependency(object):
 
     def __init__(self, category, package, version="", operator=""):
         atom_str = operator + category + "/" + package
-        if version:            
+        if version:
             atom_str += "-" + str(version)
         object.__setattr__(self, "atom", portage.dep.Atom(atom_str))
         object.__setattr__(self, "category", category)

diff --git a/g_sorcery/serialization.py b/g_sorcery/serialization.py
index 3a7704b..b5d71f3 100644
--- a/g_sorcery/serialization.py
+++ b/g_sorcery/serialization.py
@@ -14,6 +14,8 @@
 import json
 import importlib
 
+from .compatibility import basestring
+
 def step_to_raw_serializable(obj):
     """
     Make one step of convertion of object
@@ -40,19 +42,22 @@ def to_raw_serializable(obj):
     Convert object to the raw serializable type.
     Logic is the same as in the standard json encoder.
     """
-    if isinstance(obj, str) \
+    if isinstance(obj, basestring) \
        or obj is None \
        or obj is True \
        or obj is False \
        or isinstance(obj, int) \
-       or isinstance(obj, float) \
-       or isinstance(obj, (list, tuple)) \
-       or isinstance(obj, dict):
+       or isinstance(obj, float):
         return obj
+    elif isinstance(obj, dict):
+        return {k: to_raw_serializable(v) for k, v in obj.items()}
+    elif isinstance(obj, (list, tuple)):
+        return [to_raw_serializable(item) for item in obj]
+
     else:
         sobj = step_to_raw_serializable(obj)
         if not sobj:
-            raise TypeError('Non serializable object: ', sobj)
+            raise TypeError('Non serializable object: ', obj)
         return to_raw_serializable(sobj)
 
 

diff --git a/tests/serializable.py b/tests/serializable.py
new file mode 100644
index 0000000..1d3981e
--- /dev/null
+++ b/tests/serializable.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+    serializable.py
+    ~~~~~~~~~~~~~~~
+
+    test classes for serialization
+
+    :copyright: (c) 2013-2015 by Jauhien Piatlicki
+    :license: GPL-2, see LICENSE for more details.
+"""
+
+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"])

diff --git a/tests/test_FileBSON.py b/tests/test_FileBSON.py
index ff2a7b3..1bf1b7d 100644
--- a/tests/test_FileBSON.py
+++ b/tests/test_FileBSON.py
@@ -14,7 +14,10 @@
 import os
 import unittest
 
+from g_sorcery.g_collections import serializable_elist
+
 from tests.base import BaseTest
+from tests.serializable import NonSerializableClass, SerializableClass, DeserializableClass
 
 BSON_INSTALLED = False
 
@@ -24,33 +27,6 @@ try:
 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):
@@ -82,11 +58,21 @@ if BSON_INSTALLED:
             content_r = fj.read()
             self.assertEqual(content, content_r)
 
+        def test_deserializable_collection(self):
+            fj = FileBSON(self.directory, self.name, [])
+            content1 = DeserializableClass("1", "2")
+            content2 = DeserializableClass("3", "4")
+            content = serializable_elist([content1, content2])
+            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'))
+        suite.addTest(TestFileJSON('test_deserializable_collection'))
         return suite
 
 else:

diff --git a/tests/test_FileJSON.py b/tests/test_FileJSON.py
index 5bda353..f9ab032 100644
--- a/tests/test_FileJSON.py
+++ b/tests/test_FileJSON.py
@@ -4,10 +4,10 @@
 """
     test_FileJSON.py
     ~~~~~~~~~~~~~~~~
-    
+
     FileJSON test suite
-    
-    :copyright: (c) 2013 by Jauhien Piatlicki
+
+    :copyright: (c) 2013-2015 by Jauhien Piatlicki
     :license: GPL-2, see LICENSE for more details.
 """
 
@@ -17,36 +17,10 @@ import unittest
 
 from g_sorcery.fileutils import FileJSON
 from g_sorcery.exceptions import FileJSONError
+from g_sorcery.g_collections import serializable_elist
 
 from tests.base import BaseTest
-
-
-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"])
-
+from tests.serializable import NonSerializableClass, SerializableClass, DeserializableClass
 
 class TestFileJSON(BaseTest):
     def setUp(self):
@@ -54,7 +28,7 @@ class TestFileJSON(BaseTest):
         self.directory = os.path.join(self.tempdir.name, 'tst')
         self.name = 'tst.json'
         self.path = os.path.join(self.directory, self.name)
-    
+
     def test_read_nonexistent(self):
         fj = FileJSON(self.directory, self.name, [])
         content = fj.read()
@@ -99,7 +73,16 @@ class TestFileJSON(BaseTest):
         fj.write(content)
         content_r = fj.read()
         self.assertEqual(content, content_r)
-        
+
+    def test_deserializable_collection(self):
+        fj = FileJSON(self.directory, self.name, [])
+        content1 = DeserializableClass("1", "2")
+        content2 = DeserializableClass("3", "4")
+        content = serializable_elist([content1, content2])
+        fj.write(content)
+        content_r = fj.read()
+        self.assertEqual(content, content_r)
+
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(TestFileJSON('test_read_nonexistent'))
@@ -109,4 +92,5 @@ def suite():
     suite.addTest(TestFileJSON('test_write_read'))
     suite.addTest(TestFileJSON('test_serializable'))
     suite.addTest(TestFileJSON('test_deserializable'))
+    suite.addTest(TestFileJSON('test_deserializable_collection'))
     return suite

diff --git a/tests/test_PackageDB.py b/tests/test_PackageDB.py
index 221be00..179cc32 100644
--- a/tests/test_PackageDB.py
+++ b/tests/test_PackageDB.py
@@ -18,10 +18,11 @@ import unittest
 from g_sorcery.compatibility import TemporaryDirectory
 from g_sorcery.db_layout import JSON_FILE_SUFFIX, BSON_FILE_SUFFIX
 from g_sorcery.exceptions import IntegrityError, InvalidKeyError, SyncError
-from g_sorcery.g_collections import Package
+from g_sorcery.g_collections import Package, serializable_elist
 from g_sorcery.package_db import PackageDB
 
 from tests.base import BaseTest
+from tests.serializable import DeserializableClass
 from tests.server import Server
 
 SUPPORTED_FILE_FORMATS = [JSON_FILE_SUFFIX]
@@ -50,8 +51,12 @@ class TestPackageDB(BaseTest):
             orig_db = PackageDB(orig_path, preferred_category_format=fmt)
             orig_db.add_category("app-test1")
             orig_db.add_category("app-test2")
-            ebuild_data = {"test1": "tst1", "test2": "tst2"}
-            common_data = {"common1": "cmn1", "common2": "cmn2"}
+            ebuild_data = {"test1": "tst1", "test2": "tst2",
+                           "test3": serializable_elist([DeserializableClass("1", "2"),
+                                                        DeserializableClass("3", "4")])}
+            common_data = {"common1": "cmn1", "common2": "cmn2",
+                           "common3": serializable_elist([DeserializableClass("c1", "c2"),
+                                                          DeserializableClass("c3", "c4")])}
             packages = [Package("app-test1", "test", "1"), Package("app-test1", "test", "2"),
                         Package("app-test1", "test1", "1"), Package("app-test2", "test2", "1")]
             for package in packages:


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

* [gentoo-commits] proj/g-sorcery:master commit in: tests/, g_sorcery/
@ 2021-11-04 14:16 Ulrich Müller
  0 siblings, 0 replies; 4+ messages in thread
From: Ulrich Müller @ 2021-11-04 14:16 UTC (permalink / raw
  To: gentoo-commits

commit:     d9f7ba33ed8cd442ec8bc8bfcdd97cd1ce424fff
Author:     Ulrich Müller <ulm <AT> gentoo <DOT> org>
AuthorDate: Thu Nov  4 14:15:02 2021 +0000
Commit:     Ulrich Müller <ulm <AT> gentoo <DOT> org>
CommitDate: Thu Nov  4 14:15:02 2021 +0000
URL:        https://gitweb.gentoo.org/proj/g-sorcery.git/commit/?id=d9f7ba33

g_sorcery/ebuild.py: Update default EAPI from 5 to 8

EAPI 5 is banned from the Gentoo repository, therefore eclasses are
dropping support for it.

Signed-off-by: Ulrich Müller <ulm <AT> gentoo.org>

 g_sorcery/ebuild.py  | 2 +-
 tests/test_ebuild.py | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/g_sorcery/ebuild.py b/g_sorcery/ebuild.py
index c7a60e1..55d43c4 100644
--- a/g_sorcery/ebuild.py
+++ b/g_sorcery/ebuild.py
@@ -183,7 +183,7 @@ class DefaultEbuildGenerator(EbuildGenerator):
         if hasattr(layout, "eapi"):
             self.template.append("EAPI=%s" % layout.eapi)
         else:
-            self.template.append("EAPI=5")
+            self.template.append("EAPI=8")
         self.template.append("")
 
         if hasattr(layout, "vars_before_inherit"):

diff --git a/tests/test_ebuild.py b/tests/test_ebuild.py
index e227c97..f31e103 100644
--- a/tests/test_ebuild.py
+++ b/tests/test_ebuild.py
@@ -74,7 +74,7 @@ class TestEbuildGenerator(BaseTest):
         ebuild = ebuild_g.generate(self.package)
         self.assertEqual(ebuild, ['# automatically generated by g-sorcery',
                                   '# please do not edit this file', '',
-                                  'EAPI=5', '',
+                                  'EAPI=8', '',
                                   'TEST_RAW_VALUE=raw_value', 'TEST_VALUE="value"', '',
                                   'inherit g-test', '',
                                   'DESCRIPTION="testing ebuild"', '',


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

end of thread, other threads:[~2021-11-04 14:16 UTC | newest]

Thread overview: 4+ 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: tests/, g_sorcery/ Jauhien Piatlicki
2015-04-20 21:36 ` [gentoo-commits] proj/g-sorcery:dev " Jauhien Piatlicki
  -- strict thread matches above, loose matches on Subject: below --
2015-04-22  7:35 [gentoo-commits] proj/g-sorcery:master " Jauhien Piatlicki
2021-11-04 14:16 Ulrich Müller

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