# Path: g_sorcery/fileutils.py
# class FileJSON(FileJSONData):
#     """
#     Class for JSON files. Supports custom JSON serialization
#     provided by g_sorcery.serialization.
#     """
# 
#     def read_content(self):
#         """
#         Read JSON file.
#         """
#         content = {}
#         with open(self.path, 'r') as f:
#             content = json.load(f, object_hook=deserializeHook)
#         return content
# 
#     def write_content(self, content):
#         """
#         Write JSON file.
#         """
#         with open(self.path, 'w') as f:
#             json.dump(content, f, indent=2, sort_keys=True, cls=JSONSerializer)
# 
# Path: g_sorcery/exceptions.py
# class FileJSONError(GSorceryError):
#     pass
# 
# Path: g_sorcery/g_collections.py
# class serializable_elist(object):
#     """
#     A JSON serializable version of elist.
#     """
# 
#     __slots__ = ('data')
# 
#     def __init__(self, iterable=None, separator=' '):
#         '''
#         iterable: initialize from iterable's items
#         separator: string used to join list members with for __str__()
#         '''
#         self.data = elist(iterable or [], separator)
# 
#     def __eq__(self, other):
#         return self.data == other.data
# 
#     def __iter__(self):
#         return iter(self.data)
# 
#     def __str__(self):
#         '''Custom output function
#         '''
#         return str(self.data)
# 
#     def append(self, x):
#         self.data.append(x)
# 
#     def serialize(self):
#         return {"separator": self.data._sep_, "data" : self.data}
# 
#     @classmethod
#     def deserialize(cls, value):
#         return serializable_elist(value["data"], separator = value["separator"])
# 
# Path: tests/base.py
# class BaseTest(unittest.TestCase):
# 
#     def setUp(self):
#         self.tempdir = TemporaryDirectory()
# 
#     def tearDown(self):
#         del self.tempdir
# 
# Path: tests/serializable.py
# 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"])


import json
import os
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
from tests.serializable import NonSerializableClass, SerializableClass, DeserializableClass
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    test_FileJSON.py
    ~~~~~~~~~~~~~~~~

    FileJSON test suite

    :copyright: (c) 2013-2015 by Jauhien Piatlicki
    :license: GPL-2, see LICENSE for more details.
"""




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):

# Path: g_sorcery/fileutils.py
# class FileJSON(FileJSONData):
#     """
#     Class for JSON files. Supports custom JSON serialization
#     provided by g_sorcery.serialization.
#     """
# 
#     def read_content(self):
#         """
#         Read JSON file.
#         """
#         content = {}
#         with open(self.path, 'r') as f:
#             content = json.load(f, object_hook=deserializeHook)
#         return content
# 
#     def write_content(self, content):
#         """
#         Write JSON file.
#         """
#         with open(self.path, 'w') as f:
#             json.dump(content, f, indent=2, sort_keys=True, cls=JSONSerializer)
# 
# Path: g_sorcery/exceptions.py
# class FileJSONError(GSorceryError):
#     pass
# 
# Path: g_sorcery/g_collections.py
# class serializable_elist(object):
#     """
#     A JSON serializable version of elist.
#     """
# 
#     __slots__ = ('data')
# 
#     def __init__(self, iterable=None, separator=' '):
#         '''
#         iterable: initialize from iterable's items
#         separator: string used to join list members with for __str__()
#         '''
#         self.data = elist(iterable or [], separator)
# 
#     def __eq__(self, other):
#         return self.data == other.data
# 
#     def __iter__(self):
#         return iter(self.data)
# 
#     def __str__(self):
#         '''Custom output function
#         '''
#         return str(self.data)
# 
#     def append(self, x):
#         self.data.append(x)
# 
#     def serialize(self):
#         return {"separator": self.data._sep_, "data" : self.data}
# 
#     @classmethod
#     def deserialize(cls, value):
#         return serializable_elist(value["data"], separator = value["separator"])
# 
# Path: tests/base.py
# class BaseTest(unittest.TestCase):
# 
#     def setUp(self):
#         self.tempdir = TemporaryDirectory()
# 
#     def tearDown(self):
#         del self.tempdir
# 
# Path: tests/serializable.py
# 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"])


# Path: tests/test_FileJSON.py
import json
import os
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
from tests.serializable import NonSerializableClass, SerializableClass, DeserializableClass

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    test_FileJSON.py
    ~~~~~~~~~~~~~~~~

    FileJSON test suite

    :copyright: (c) 2013-2015 by Jauhien Piatlicki
    :license: GPL-2, see LICENSE for more details.
"""




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"])