コード例 #1
0
class ProjectTest(BaseCase):
    def setUp(self):
        self.project = Project()
        self.table_name = 'artifact'

    def test_add_table(self):
        self.isinstance(self.project, Project)

        table = self.project.add(self.table_name)
        self.isinstance(table, Table)
        self.eq(table.name, self.table_name)
        self.eq(self.project.get(self.table_name), table)

        self.isinstance(table.pattern, Pattern)
        self.eq(table.pattern.type, Pattern.DICT)
        self.eq(table.pattern.fields, {})

    def test_dublicate_add_table(self):
        self.project.add(self.table_name)
        self.eq(self.project.add(self.table_name), None)

    def test_remove_table(self):
        table = self.project.add(self.table_name)

        self.eq(self.project.remove('artifact2'), None)
        self.eq(self.project.get(self.table_name), table)

        self.eq(self.project.remove(self.table_name), table)
        self.eq(self.project.get(self.table_name), None)

    def test_remove_all_table(self):
        table1 = self.project.add('test1')
        table2 = self.project.add('test2')

        self.eq(self.project.get('test1'), table1)
        self.eq(self.project.get('test2'), table2)
        self.eq(self.project.fields, {'test1': table1, 'test2': table2})

        self.eq(self.project.remove_all(), True)

        self.eq(self.project.get('test1'), None)
        self.eq(self.project.get('test2'), None)
        self.eq(self.project.fields, {})

    def test_add_table_with_pattern(self):
        table = self.project.add(self.table_name, type=Pattern.DYNAMIC_DICT)

        self.eq(table.pattern.type, Pattern.DYNAMIC_DICT)

    def test_project_values(self):
        self.eq(self.project.values.get('test'), 0)

        self.eq(self.project.values.incr('test'), 1)
        self.eq(self.project.values.get('test'), 1)

        self.eq(self.project.values.incr('test'), 2)
        self.eq(self.project.values.incr('test'), 3)
        self.eq(self.project.values.incr('test'), 4)
        self.eq(self.project.values.get('test'), 4)

        self.eq(self.project.values.get('test1'), 0)
        self.eq(self.project.values.get('test2'), 0)
        self.eq(self.project.values.incr('test2'), 1)
        self.eq(self.project.values.incr('test2'), 2)

    def test_data(self):
        self.project.add('test').add(1)
        self.project.add('test2').add(2)
        self.project.values.incr('test_id')

        self.eq(
            self.project.data(), {
                'values': {
                    'test_id': 1
                },
                'table': {
                    'test': {
                        '1': {}
                    },
                    'test2': {
                        '2': {}
                    }
                }
            })
コード例 #2
0
ファイル: zt_project.py プロジェクト: kashirov/json_explorer
class ProjectTest(BaseCase):

    def setUp(self):
        self.project = Project()
        self.table_name = 'artifact'

    def test_add_table(self):
        self.isinstance(self.project, Project)

        table = self.project.add(self.table_name)
        self.isinstance(table, Table)
        self.eq(table.name, self.table_name)
        self.eq(self.project.get(self.table_name), table)

        self.isinstance(table.pattern, Pattern)
        self.eq(table.pattern.type, Pattern.DICT)
        self.eq(table.pattern.fields, {})

    def test_dublicate_add_table(self):
        self.project.add(self.table_name)
        self.eq(self.project.add(self.table_name), None)

    def test_remove_table(self):
        table = self.project.add(self.table_name)

        self.eq(self.project.remove('artifact2'), None)
        self.eq(self.project.get(self.table_name), table)

        self.eq(self.project.remove(self.table_name), table)
        self.eq(self.project.get(self.table_name), None)

    def test_remove_all_table(self):
        table1 = self.project.add('test1')
        table2 = self.project.add('test2')

        self.eq(self.project.get('test1'), table1)
        self.eq(self.project.get('test2'), table2)
        self.eq(self.project.fields, {'test1': table1, 'test2': table2})

        self.eq(self.project.remove_all(), True)

        self.eq(self.project.get('test1'), None)
        self.eq(self.project.get('test2'), None)
        self.eq(self.project.fields, {})

    def test_add_table_with_pattern(self):
        table = self.project.add(self.table_name, type=Pattern.DYNAMIC_DICT)

        self.eq(table.pattern.type, Pattern.DYNAMIC_DICT)

    def test_project_values(self):
        self.eq(self.project.values.get('test'), 0)

        self.eq(self.project.values.incr('test'), 1)
        self.eq(self.project.values.get('test'), 1)

        self.eq(self.project.values.incr('test'), 2)
        self.eq(self.project.values.incr('test'), 3)
        self.eq(self.project.values.incr('test'), 4)
        self.eq(self.project.values.get('test'), 4)

        self.eq(self.project.values.get('test1'), 0)
        self.eq(self.project.values.get('test2'), 0)
        self.eq(self.project.values.incr('test2'), 1)
        self.eq(self.project.values.incr('test2'), 2)

    def test_data(self):
        self.project.add('test').add(1)
        self.project.add('test2').add(2)
        self.project.values.incr('test_id')

        self.eq(self.project.data(), {'values': {'test_id': 1},
                        'table': {'test': {'1': {}}, 'test2': {'2': {}}}})
コード例 #3
0
ファイル: zt_dumps.py プロジェクト: anton62k/json_explorer
class Test(BaseCase):
    def setUp(self):
        self.project = Project()

        test1 = self.project.add('test1')
        test1.pattern.add('test1', type=Pattern.INT, default=1)
        test2 = self.project.add('test2')
        test2.pattern.add('test2', type=Pattern.INT, default=2)
        test3 = self.project.add('test3')
        test3.pattern.add('test3', type=Pattern.INT, default=3)
        test4 = self.project.add('test4', type=Pattern.LIST)
        test4.pattern.items.add('field4', type=Pattern.STR, default='test4')
        test5 = self.project.add('test5', type=Pattern.DYNAMIC_DICT)
        test5.pattern.items.add('field5', type=Pattern.STR, default='test5')

        test1.add('1', data={'test1': 1})
        test1.add('2', data={'test1': 2})
        test1.add('3', data={'test1': 3})
        test1.add('10', data={'test1': 4})
        test1.add('11', data={'test1': 5})

        test2.add('test1', data={'test2': 6})
        test2.add('test2', data={'test2': 7})
        test2.add('test3', data={'test2': 8})

        test3.add('test_1', data={'test3': 9})
        test3.add('test_2', data={'test3': 10})
        test3.add('test_3', data={'test3': 11})

        test4.add(1, data=[{'field4': 'test4_1'}])
        test4.add(2, data=[{'field4': 'test4_2'}])

        test5.add(1).add('1', data={'field5': 'test5_1'})
        test5.add(2).add('test', data={'field5': 'test5_2'})
        test5.add(3).add('test')

        self.path = tempfile.mkdtemp()
        self.path_table = os.path.join(self.path, 'table')

    def clear_project(self):
        shutil.rmtree(self.path)

    def get_data_doc(self, table, doc):
        filepath = os.path.join(self.path_table, table.name,
                                '%s.json' % doc.name)
        return self.get_data_file(filepath)

    def get_data_file(self, filepath):
        f = codecs.open(filepath, 'r', 'utf8')
        json_str = f.read()
        f.close()
        return json.loads(json_str, 'utf8')

    def get_data_pattern(self, table):
        filepath = os.path.join(self.path, 'scheme', '%s.json' % table.name)
        return self.get_data_file(filepath)

    def test_all_dumps(self):
        dumps(self.path, self.project)

        for table in self.project:
            for doc in table:
                file_data = self.get_data_doc(table, doc)
                self.eq(doc.data(), file_data)

            pattern_data = self.get_data_pattern(table)
            self.eq(table.pattern.data(), pattern_data)

        self.eq(
            self.get_data_file(
                os.path.join(self.path, 'setting', 'values.json')),
            dict(self.project.values))

        self.clear_project()

    def test_remove_doc(self):
        dumps(self.path, self.project)

        self.project.get('test2').remove('test1')
        dumps(self.path, self.project)

        filepath = os.path.join(self.path_table, 'test2', 'test1.json')
        self.eq(os.path.exists(filepath), False)

        self.clear_project()

    def test_remove_table(self):
        dumps(self.path, self.project)

        self.project.remove('test2')
        dumps(self.path, self.project)

        filepath = os.path.join(self.path_table, 'test2')
        self.eq(os.path.exists(filepath), False)

        self.clear_project()
コード例 #4
0
ファイル: zt_dumps.py プロジェクト: kashirov/json_explorer
class Test(BaseCase):

    def setUp(self):
        self.project = Project()

        test1 = self.project.add('test1')
        test1.pattern.add('test1', type=Pattern.INT, default=1)
        test2 = self.project.add('test2')
        test2.pattern.add('test2', type=Pattern.INT, default=2)
        test3 = self.project.add('test3')
        test3.pattern.add('test3', type=Pattern.INT, default=3)
        test4 = self.project.add('test4', type=Pattern.LIST)
        test4.pattern.items.add('field4', type=Pattern.STR, default='test4')
        test5 = self.project.add('test5', type=Pattern.DYNAMIC_DICT)
        test5.pattern.items.add('field5', type=Pattern.STR, default='test5')

        test1.add('1', data={'test1': 1})
        test1.add('2', data={'test1': 2})
        test1.add('3', data={'test1': 3})
        test1.add('10', data={'test1': 4})
        test1.add('11', data={'test1': 5})

        test2.add('test1', data={'test2': 6})
        test2.add('test2', data={'test2': 7})
        test2.add('test3', data={'test2': 8})

        test3.add('test_1', data={'test3': 9})
        test3.add('test_2', data={'test3': 10})
        test3.add('test_3', data={'test3': 11})

        test4.add(1, data=[{'field4': 'test4_1'}])
        test4.add(2, data=[{'field4': 'test4_2'}])

        test5.add(1).add('1', data={'field5': 'test5_1'})
        test5.add(2).add('test', data={'field5': 'test5_2'})
        test5.add(3).add('test')

        self.path = tempfile.mkdtemp()
        self.path_table = os.path.join(self.path, 'table')

    def clear_project(self):
        shutil.rmtree(self.path)

    def get_data_doc(self, table, doc):
        filepath = os.path.join(self.path_table, table.name, '%s.json' % doc.name)
        return self.get_data_file(filepath)

    def get_data_file(self, filepath):
        f = codecs.open(filepath, 'r', 'utf8')
        json_str = f.read()
        f.close()
        return json.loads(json_str, 'utf8')

    def get_data_pattern(self, table):
        filepath = os.path.join(self.path, 'scheme', '%s.json' % table.name)
        return self.get_data_file(filepath)

    def test_all_dumps(self):
        dumps(self.path, self.project)

        for table in self.project:
            for doc in table:
                file_data = self.get_data_doc(table, doc)
                self.eq(doc.data(), file_data)

            pattern_data = self.get_data_pattern(table)
            self.eq(table.pattern.data(), pattern_data)

        self.eq(self.get_data_file(os.path.join(self.path, 'setting',
                                    'values.json')), dict(self.project.values))

        self.clear_project()

    def test_remove_doc(self):
        dumps(self.path, self.project)

        self.project.get('test2').remove('test1')
        dumps(self.path, self.project)

        filepath = os.path.join(self.path_table, 'test2', 'test1.json')
        self.eq(os.path.exists(filepath), False)

        self.clear_project()

    def test_remove_table(self):
        dumps(self.path, self.project)

        self.project.remove('test2')
        dumps(self.path, self.project)

        filepath = os.path.join(self.path_table, 'test2')
        self.eq(os.path.exists(filepath), False)

        self.clear_project()