public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Jauhien Piatlicki" <piatlicki@gmail.com>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/g-sorcery:master commit in: tests/
Date: Sun, 15 Sep 2013 22:38:22 +0000 (UTC)	[thread overview]
Message-ID: <1379280850.9ee27d984bb6f56e20c937e1436230c2c06e8987.jauhien@gentoo> (raw)

commit:     9ee27d984bb6f56e20c937e1436230c2c06e8987
Author:     Jauhien Piatlicki (jauhien) <piatlicki <AT> gmail <DOT> com>
AuthorDate: Sun Sep 15 21:34:10 2013 +0000
Commit:     Jauhien Piatlicki <piatlicki <AT> gmail <DOT> com>
CommitDate: Sun Sep 15 21:34:10 2013 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/g-sorcery.git;a=commit;h=9ee27d98

add tests for FileJSON with serialization

---
 tests/test_FileJSON.py | 112 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)

diff --git a/tests/test_FileJSON.py b/tests/test_FileJSON.py
new file mode 100644
index 0000000..5bda353
--- /dev/null
+++ b/tests/test_FileJSON.py
@@ -0,0 +1,112 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+    test_FileJSON.py
+    ~~~~~~~~~~~~~~~~
+    
+    FileJSON test suite
+    
+    :copyright: (c) 2013 by Jauhien Piatlicki
+    :license: GPL-2, see LICENSE for more details.
+"""
+
+import json
+import os
+import unittest
+
+from g_sorcery.fileutils import FileJSON
+from g_sorcery.exceptions import FileJSONError
+
+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"])
+
+
+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_read_nonexistent(self):
+        fj = FileJSON(self.directory, self.name, [])
+        content = fj.read()
+        self.assertEqual(content, {})
+        self.assertTrue(os.path.isfile(self.path))
+
+    def test_read_nonexistent_mandatory_key(self):
+        fj = FileJSON(self.directory, self.name, ["mandatory1", "mandatory2"])
+        content = fj.read()
+        self.assertEqual(content, {"mandatory1":"", "mandatory2":""})
+        self.assertTrue(os.path.isfile(self.path))
+
+    def test_read_luck_of_mandatory_key(self):
+        fj = FileJSON(self.directory, self.name, ["mandatory"])
+        os.makedirs(self.directory)
+        with open(self.path, 'w') as f:
+            json.dump({"test":"test"}, f)
+        self.assertRaises(FileJSONError, fj.read)
+
+    def test_write_luck_of_mandatory_key(self):
+        fj = FileJSON(self.directory, self.name, ["mandatory"])
+        self.assertRaises(FileJSONError, fj.write, {"test":"test"})
+
+    def test_write_read(self):
+        fj = FileJSON(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 = FileJSON(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 = FileJSON(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_read_nonexistent'))
+    suite.addTest(TestFileJSON('test_read_nonexistent_mandatory_key'))
+    suite.addTest(TestFileJSON('test_read_luck_of_mandatory_key'))
+    suite.addTest(TestFileJSON('test_write_luck_of_mandatory_key'))
+    suite.addTest(TestFileJSON('test_write_read'))
+    suite.addTest(TestFileJSON('test_serializable'))
+    suite.addTest(TestFileJSON('test_deserializable'))
+    return suite


             reply	other threads:[~2013-09-15 22:38 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-15 22:38 Jauhien Piatlicki [this message]
  -- strict thread matches above, loose matches on Subject: below --
2021-05-04 15:50 [gentoo-commits] proj/g-sorcery:master commit in: tests/ Ulrich Müller
2021-05-04 15:50 Ulrich Müller
2021-05-04 15:50 Ulrich Müller
2013-09-18 22:52 Jauhien Piatlicki
2013-09-18 22:52 Jauhien Piatlicki
2013-09-18 22:09 Jauhien Piatlicki
2013-09-18 22:09 Jauhien Piatlicki
2013-09-18  0:57 Jauhien Piatlicki
2013-09-16 14:33 Jauhien Piatlicki
2013-09-16  0:19 Jauhien Piatlicki
2013-09-15 22:38 Jauhien Piatlicki
2013-09-15 22:38 Jauhien Piatlicki
2013-07-17 15:41 Jauhien Piatlicki
2013-07-14 23:39 Jauhien Piatlicki
2013-07-08 23:53 Jauhien Piatlicki
2013-07-02 14:48 Jauhien Piatlicki
2013-07-02 10:21 Jauhien Piatlicki
2013-06-23  0:44 Jauhien Piatlicki
2013-06-23  0:44 Jauhien Piatlicki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1379280850.9ee27d984bb6f56e20c937e1436230c2c06e8987.jauhien@gentoo \
    --to=piatlicki@gmail.com \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox