Beispiel #1
0
 def __init__(self, param):
     self.logger = logging.getLogger(__name__)
     self.json_utils = JsonUtils()
     self.path = param['path']
     self.default_value = param['default_value']
     self.filter_key_list = param['filter_key_list']
     self.value = None
Beispiel #2
0
class Memento():
    def __init__(self, param):
        self.logger = logging.getLogger(__name__)
        self.json_utils = JsonUtils()
        self.path = param['path']
        self.default_value = param['default_value']
        self.filter_key_list = param['filter_key_list']
        self.value = None

    def load(self):
        self.logger.info('load memento: {0}'.format(self.path))
        try:
            json = self.json_utils.load(self.path)
            self.value = self.json_utils.remove_type(json)
        except IOError as e:
            self.logger.info('cannot find memento file: {0}'.format(self.path))
            self.value = self.default_value 
        except ValueError as e:
            self.logger.info('failed to load memento: {0}'.format(self.path))
            self.value = self.default_value 
        except KeyError as e:
            self.logger.info('failed to build load value: {0}'.format(self.path))
            self.value = self.default_value 
        
    def save(self):
        self.logger.info('save memento: {0}'.format(self.path))
        json = self.json_utils.filter_key_list(self.value, self.filter_key_list)
        json = self.json_utils.add_type(json)
        self.json_utils.save(json, self.path)

    def set_value(self, value):
        self.value = value

    def get_value(self):
        return self.value
Beispiel #3
0
 def setUp(self):
     self.json_utils = JsonUtils()
Beispiel #4
0
class JsonUtilsTest(unittest.TestCase):
    def setUp(self):
        self.json_utils = JsonUtils()

    def tearDown(self):
        self.json_utils = None

    def test_load(self):
        path = "./stockcat/tests/unit/data/json/memento.json"
        actual = self.json_utils.load(path)

        self.assertEqual(actual["state"], {"value": "spider", "type": "str"})

        self.assertEqual(actual["all_entry_list"]["type"], "list")
        self.assertEqual(len(actual["all_entry_list"]["value"]), 1)
        actual_date = actual["all_entry_list"]["value"][0]["date"]
        expected_date = {"value": "2010-12-31", "type": "datetime.date"}
        self.assertEqual(actual_date, expected_date)
        actual_date = actual["all_entry_list"]["value"][0]["stock_symbol"]
        expected_date = {"value": "2330", "type": "str"}
        self.assertEqual(actual_date, expected_date)

    def test_filter_key_list(self):
        json = {"state": "spider", "all_entry_list": [{"stock_symbol": "2330", "date": datetime.date(2010, 12, 31)}]}
        actual = self.json_utils.filter_key_list(json, ["state"])
        self.assertEqual(actual, {"state": "spider"})

    def test_add_type(self):
        json = {"state": "spider", "all_entry_list": [{"stock_symbol": "2330", "date": datetime.date(2010, 12, 31)}]}
        actual = self.json_utils.add_type(json)

        self.assertEqual(actual["state"], {"value": "spider", "type": "str"})

        self.assertEqual(actual["all_entry_list"]["type"], "list")
        self.assertEqual(len(actual["all_entry_list"]["value"]), 1)
        actual_date = actual["all_entry_list"]["value"][0]["date"]
        expected_date = {"value": "2010-12-31", "type": "datetime.date"}
        self.assertEqual(actual_date, expected_date)
        actual_date = actual["all_entry_list"]["value"][0]["stock_symbol"]
        expected_date = {"value": "2330", "type": "str"}
        self.assertEqual(actual_date, expected_date)

    def test_remove_type(self):
        json = {
            "state": {"type": "str", "value": "spider"},
            "all_entry_list": {
                "type": "list",
                "value": [
                    {
                        "date": {"type": "datetime.date", "value": "2010-12-31"},
                        "stock_symbol": {"type": "str", "value": "2330"},
                    }
                ],
            },
        }
        actual = self.json_utils.remove_type(json)

        self.assertEqual(actual["state"], "spider")

        expected = [{"stock_symbol": "2330", "date": datetime.date(2010, 12, 31)}]
        self.assertEqual(actual["all_entry_list"], expected)