class Test(BaseCase): def setUp(self): self.project = Project() self.table = self.project.add('test', type=Pattern.DYNAMIC_DICT) def test_dynamic_dict(self): self.eq(self.table.pattern.type, Pattern.DYNAMIC_DICT) self.table.pattern.items.add('test', type=Pattern.INT, default=1) doc = self.table.add(1) self.eq(doc.pattern.type, Pattern.DYNAMIC_DICT) item = doc.add(1) self.eq(item.name, '1') self.eq(item.data(), {'test': 1}) item = doc.add('test', data={'test': 2}) self.eq(item.name, 'test') self.eq(item.data(), {'test': 2}) item = doc.add(10, data={'test1': 2}) self.eq(item.name, '10') self.eq(item.data(), {'test': 1}) self.eq(doc.data(), {'1': {'test': 1}, 'test': {'test': 2}, '10': {'test': 1}}) doc.remove(1) self.eq(doc.data(), {'test': {'test': 2}, '10': {'test': 1}}) doc.remove('test') self.eq(doc.data(), {'10': {'test': 1}}) doc.remove('10') self.eq(doc.data(), {}) def test_data(self): doc = self.table.add(1, data={'test': {}, 'test2': {}}) self.eq(doc.data(), {'test': {}, 'test2': {}}) self.table.pattern.items.add('test', type=Pattern.INT) self.eq(doc.data(), {'test': {'test': 0}, 'test2': {'test': 0}}) doc = self.table.add(2, data={'test': {}, 'test2': {}}) self.eq(doc.data(), {'test': {'test': 0}, 'test2': {'test': 0}}) doc = self.table.add(3, data={'test': {'test': 12}, 'test2': {'test': 85}}) self.eq(doc.data(), {'test': {'test': 12}, 'test2': {'test': 85}}) def test_nested_data(self): table = self.project.add('nested_table') table.pattern.add('test', type=Pattern.DYNAMIC_DICT) table.pattern.get('test').items.add('field', type=Pattern.INT) data = {'test': {'1': {'field': 100}, 'r2': {'field': 78}}} doc = table.add(1, data=data) self.eq(doc.data(), data)
class Test(BaseCase): def setUp(self): self.project = Project() self.project.values.incr('test') self.project.values.incr('test2') 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 test_load(self): dumps(self.path, self.project) project_load = loads(self.path) for table in self.project: table_load = project_load.get(table.name) self.eq(table.keys(), table_load.keys()) self.eq(table.data(), table_load.data()) self.eq(table.pattern.data(), table_load.pattern.data()) self.eq(dict(self.project.values), dict(project_load.values)) self.clear_project() def clear_project(self): shutil.rmtree(self.path)
class Test(BaseCase): def setUp(self): self.project = Project() self.project.values.incr("test") self.project.values.incr("test2") 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 test_load(self): dumps(self.path, self.project) project_load = loads(self.path) for table in self.project: table_load = project_load.get(table.name) self.eq(table.keys(), table_load.keys()) self.eq(table.data(), table_load.data()) self.eq(table.pattern.data(), table_load.pattern.data()) self.eq(dict(self.project.values), dict(project_load.values)) self.clear_project() def clear_project(self): shutil.rmtree(self.path)
def setUp(self): self.project = Project() self.manager = self.project.manager self.price = self.manager.add('price', type=Pattern.DICT) self.price.add('type', type=Pattern.STR, values=['coins', 'coins_gold'], default='coins') self.price.add('value', type=Pattern.INT, default=10)
def setUp(self): self.project = Project() self.table = self.project.add('test', type=Pattern.DICT) self.int_p = self.table.pattern.add('int', type=Pattern.INT, default=10) self.float_p = self.table.pattern.add('float', type=Pattern.FLOAT, default=11.2) self.str_p = self.table.pattern.add('str', type=Pattern.STR, default='test')
class Test(BaseCase): def setUp(self): self.project = Project() self.table = self.project.add('test', type=Pattern.DICT) self.int_p = self.table.pattern.add('int', type=Pattern.INT, default=10) self.float_p = self.table.pattern.add('float', type=Pattern.FLOAT, default=11.2) self.str_p = self.table.pattern.add('str', type=Pattern.STR, default='test') def test_hook(self): doc = self.table.add(1, data={'int': 12, 'str': 'test2', 'float': 14.9}) self.eq(doc.get('int').get(), hook_int_float(self.int_p, 10, 12)) self.eq(doc.get('str').get(), hook_str(self.str_p, 'test', 'test2')) self.eq(doc.get('float').get(), hook_int_float(self.float_p, 11.2, 14.9)) def test_hook_incr(self): pattern = Pattern(project=self.project, type=Pattern.INT, incr='id') # incr self.eq(hook_incr(pattern, None, 0), 1) # already incr self.eq(hook_incr(pattern, None, 12), 12) # already set self.eq(hook_incr(pattern, 33, 0), 33) # incr self.eq(hook_incr(pattern, None, 0), 2)
def setUp(self): self.project = Project() self.artifact = self.project.add('artifact') self.artifact.pattern.add('int', type=Pattern.INT) self.artifact.pattern.add('str', type=Pattern.STR) self.artifact.pattern.add('float', type=Pattern.FLOAT) self.table_list = self.project.add('table_list', type=Pattern.LIST) self.table_list.pattern.items.add('int', type=Pattern.INT) self.table_dynamic = self.project.add('table_dynamic', type=Pattern.DYNAMIC_DICT) self.table_dynamic.pattern.items.add('str', type=Pattern.STR) self.table_list2 = self.project.add('table_list2', type=Pattern.LIST, item_type=Pattern.INT)
def loads(path): project = Project() tables_path = os.path.join(path, "table") scheme_path = os.path.join(path, "scheme") setting_path = os.path.join(path, "setting") values = get_data_file(os.path.join(setting_path, "values.json")) project.values.update(values) for table_item in os.listdir(tables_path): scheme_data = get_data_file(os.path.join(scheme_path, "%s.json" % table_item)) table = project.add(table_item, data=scheme_data) table_path = os.path.join(tables_path, table_item) for doc_item in os.listdir(table_path): doc_data = get_data_file(os.path.join(table_path, doc_item)) table.add(doc_item.replace(".json", ""), data=doc_data) return project
def setUp(self): self.project = Project() self.project.values.incr('test') self.project.values.incr('test2') 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 loads(path): project = Project() tables_path = os.path.join(path, 'table') scheme_path = os.path.join(path, 'scheme') setting_path = os.path.join(path, 'setting') values = get_data_file(os.path.join(setting_path, 'values.json')) project.values.update(values) for table_item in os.listdir(tables_path): scheme_data = get_data_file(os.path.join(scheme_path, '%s.json' % table_item)) table = project.add(table_item, data=scheme_data) table_path = os.path.join(tables_path, table_item) for doc_item in os.listdir(table_path): doc_data = get_data_file(os.path.join(table_path, doc_item)) table.add(doc_item.replace('.json', ''), data=doc_data) return project
def setUp(self): self.project = Project() self.project.values.incr("test") self.project.values.incr("test2") 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 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')
class Test(BaseCase): def setUp(self): self.project = Project() self.table = self.project.add('test', type=Pattern.DICT) self.int_p = self.table.pattern.add('int', type=Pattern.INT, default=10) self.float_p = self.table.pattern.add('float', type=Pattern.FLOAT, default=11.2) self.str_p = self.table.pattern.add('str', type=Pattern.STR, default='test') def test_hook(self): doc = self.table.add(1, data={ 'int': 12, 'str': 'test2', 'float': 14.9 }) self.eq(doc.get('int').get(), hook_int_float(self.int_p, 10, 12)) self.eq(doc.get('str').get(), hook_str(self.str_p, 'test', 'test2')) self.eq( doc.get('float').get(), hook_int_float(self.float_p, 11.2, 14.9)) def test_hook_incr(self): pattern = Pattern(project=self.project, type=Pattern.INT, incr='id') # incr self.eq(hook_incr(pattern, None, 0), 1) # already incr self.eq(hook_incr(pattern, None, 12), 12) # already set self.eq(hook_incr(pattern, 33, 0), 33) # incr self.eq(hook_incr(pattern, None, 0), 2)
def setUp(self): self.project = Project() self.table = self.project.add('test', type=Pattern.DYNAMIC_DICT)
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()
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()
class Test(BaseCase): def setUp(self): self.project = Project() self.artifact = self.project.add('artifact') self.artifact.pattern.add('int', type=Pattern.INT) self.artifact.pattern.add('str', type=Pattern.STR) self.artifact.pattern.add('float', type=Pattern.FLOAT) self.table_list = self.project.add('table_list', type=Pattern.LIST) self.table_list.pattern.items.add('int', type=Pattern.INT) self.table_dynamic = self.project.add('table_dynamic', type=Pattern.DYNAMIC_DICT) self.table_dynamic.pattern.items.add('str', type=Pattern.STR) self.table_list2 = self.project.add('table_list2', type=Pattern.LIST, item_type=Pattern.INT) def test(self): for i in xrange(1, 11): doc = self.artifact.add(i) self.eq(dict(doc.stats()), {'int': 1, 'float': 1, 'str': 1, 'dict': 1 }) self.eq(dict(self.artifact.stats()), {'int': 10, 'float': 10, 'document': 10, 'str': 10, 'dict': 10}) self.eq(dict(self.project.stats()), {'int': 10, 'float': 10, 'document': 10, 'str': 10, 'table': 4, 'dict': 10}) # for i in xrange(1, 6): doc = self.table_list.add(i) self.eq(dict(doc.stats()), {}) for j in xrange(1, 21): item = doc.add() self.eq(dict(item.stats()), {'int': 1, 'dict': 1}) self.eq(dict(doc.stats()), {'int': j, 'dict': j}) self.eq(dict(self.table_list.stats()), {'int': 100, 'document': 5, 'dict': 100}) self.eq(dict(self.project.stats()), {'int': 110, 'float': 10, 'document': 15, 'str': 10, 'table': 4, 'dict': 110}) # for i in xrange(1, 16): doc = self.table_dynamic.add(i) self.eq(dict(doc.stats()), {}) for j in xrange(1, 6): item = doc.add(j) self.eq(dict(item.stats()), {'str': 1, 'dict': 1}) self.eq(dict(doc.stats()), {'str': j, 'dict': j}) self.eq(dict(self.table_dynamic.stats()), {'str': 75, 'document': 15, 'dict': 75}) self.eq(dict(self.project.stats()), {'int': 110, 'float': 10, 'document': 30, 'str': 85, 'table': 4, 'dict': 185}) # for i in xrange(1, 11): doc = self.table_list2.add(i) self.eq(dict(doc.stats()), {}) for j in xrange(1, 21): item = doc.add() self.eq(dict(item.stats()), {'int': 1}) self.eq(dict(doc.stats()), {'int': j}) self.eq(dict(self.table_list2.stats()), {'int': 200, 'document': 10}) self.eq(dict(self.project.stats()), {'int': 310, 'float': 10, 'document': 40, 'str': 85, 'table': 4, 'dict': 185})
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': {} } } })
class Test(BaseCase): def setUp(self): self.project = Project() self.manager = self.project.manager self.price = self.manager.add('price', type=Pattern.DICT) self.price.add('type', type=Pattern.STR, values=['coins', 'coins_gold'], default='coins') self.price.add('value', type=Pattern.INT, default=10) def test_add_common(self): map_object = self.project.add('map_object', type=Pattern.DICT) map_object_price = map_object.pattern.add('price', common='price') self.eq(self.price, map_object_price) artifact = self.project.add('artifact', type=Pattern.DICT) artifact_price = artifact.pattern.add('artifact_price', common='price') self.eq(self.price, artifact_price) self.eq(artifact.pattern.add('test', common='unreal_common'), None) surprise = self.project.add('surprise', type=Pattern.DICT) surprise_price = surprise.pattern.add('list', type=Pattern.LIST, item_common='price').items self.eq(surprise_price, self.price) def test_change_item_type(self): map_object = self.project.add('map_object', type=Pattern.DICT) map_object_price = map_object.pattern.add('price', type=Pattern.LIST) self.neq(map_object_price.items, self.price) map_object_price.change_type_item(None, item_common='price') self.eq(self.price, map_object_price.items) def test_common_root(self): artifact = self.project.add('artifact', common='price') self.eq(artifact.pattern, self.price) def test_document(self): artifact = self.project.add('artifact') artifact.pattern.add('price', common='price') artifact.pattern.add('test', type=Pattern.STR, default='temp') ruin = self.project.add('ruin') ruin.pattern.add('price', common='price') ruin.pattern.add('value', type=Pattern.INT, default=0) # art1 = artifact.add(1) self.eq(art1.data(), {'test': 'temp', 'price': {'type': 'coins', 'value': 10}}) art1.get('price.value').set(20) art1.get('test').set('temp1') art1.get('price.type').set('coins_gold') self.eq(art1.data(), {'test': 'temp1', 'price': {'type': 'coins_gold', 'value': 20}}) # r = ruin.add('mo2') self.eq(r.data(), {'value': 0, 'price': {'type': 'coins', 'value': 10}}) r.get('price.value').set(22) r.get('value').set(2) r.get('price.type').set('coins_gold') self.eq(r.data(), {'value': 2, 'price': {'type': 'coins_gold', 'value': 22}}) # change common pattern self.price.add('chance', type=Pattern.LIST, item_type=Pattern.INT) art1.get('price.chance').add().set(1) art1.get('price.chance').add().set(2) art1.get('price.chance').add() self.eq(art1.data(), {'test': 'temp1', 'price': {'type': 'coins_gold', 'value': 20, 'chance': [1, 2, 0]}}) r.get('price.chance').add().set(111) r.get('price.chance').add().set(26) self.eq(r.data(), {'value': 2, 'price': {'type': 'coins_gold', 'value': 22, 'chance': [111, 26]}})
class Test(BaseCase): def setUp(self): self.project = Project() self.table = self.project.add('test') self.pattern = self.table.pattern def test_add_int_pattern(self): doc = self.table.add(1) self.eq(doc.pattern.type, Pattern.DICT) pattern_field_int = self.pattern.add('field_int', type=Pattern.INT, default=12) self.isinstance(doc.get('field_int'), Field) self.eq(doc.get('field_int').pattern.type, Pattern.INT) self.eq(doc.get('field_int').pattern, pattern_field_int) self.eq(doc.get('field_int').get(), pattern_field_int.default) def test_add_list_pattern(self): doc = self.table.add(1) pattern_list = self.pattern.add('field_list', type=Pattern.LIST) self.isinstance(doc.get('field_list'), Document) self.eq(doc.get('field_list').pattern.type, Pattern.LIST) self.eq(doc.get('field_list').pattern, pattern_list) self.eq(doc.get('field_list').type_list, 'list') def create_pattern(self): self.pattern.add('text', type=Pattern.DICT) self.pattern.get('text').add('ru', type=Pattern.DICT) self.pattern.get('text').get('ru').add('title', type=Pattern.STR, default='русский заголовок') self.pattern.get('text').add('eng', type=Pattern.DICT) self.pattern.get('text').get('eng').add('title', type=Pattern.STR, default='english title') self.pattern.add('gift', type=Pattern.LIST) self.pattern.get('gift').items.add('type', type=Pattern.STR, values=['coins', 'coins_gold'], default='coins') self.pattern.get('gift').items.add('value', type=Pattern.LIST, item_type=Pattern.INT) self.pattern.get('gift').items.get('value').items.default = 12 def test_pattern(self): self.create_pattern() data = { 'text': { 'ru': { 'title': 'test value' }, 'eng': { 'title': 'sample' } }, 'gift': [{ 'type': 'coins1', 'value': [1] }, { 'type': 'coins_gold', 'value': [1, 34] }] } doc = self.table.add(1, data=data) self.eq( doc.data(), { 'text': { 'ru': { 'title': 'test value' }, 'eng': { 'title': 'sample' } }, 'gift': [{ 'type': 'coins', 'value': [1] }, { 'type': 'coins_gold', 'value': [1, 34] }] }) self.eq(doc.get('text').get('ru').get('title').get(), 'test value') self.eq(doc.get('text').get('eng').get('title').get(), 'sample') # gift = doc.get('gift').add(data=\ {'type': 'coins_gold', 'value': [5, 4, 67, '']}) self.eq(gift.data(), {'type': 'coins_gold', 'value': [5, 4, 67, 12]}) self.eq(doc.get('gift').length(), 3) # new_doc = self.table.add(2, data=doc.data()) self.eq(new_doc.data(), doc.data()) def test_signals(self): self.create_pattern() doc = self.table.add(1) # add signal self.pattern.add('price', type=Pattern.DICT) self.pattern.get('price').add('type', type=Pattern.STR, default='type') self.pattern.get('price').add('value', type=Pattern.INT, default=100) self.eq(doc.get('price').data(), {'type': 'type', 'value': 100}) self.pattern.add('list', type=Pattern.LIST, item_type=Pattern.LIST) self.pattern.get('list').items.items.add('param', type=Pattern.STR, default='test') self.pattern.get('list').items.items.add('include_list', type=Pattern.LIST) self.eq(doc.get('list').type_list, 'list') self.eq(doc.get('list').pattern.type, Pattern.LIST) self.eq(doc.get('list').pattern.items.type, Pattern.LIST) self.eq(doc.get('list').pattern.items.items.type, Pattern.DICT) self.eq( doc.get('list').pattern.items.items.get('param').type, Pattern.STR) self.eq( doc.get('list').pattern.items.items.get('include_list').type, Pattern.LIST) self.eq( doc.get('list').pattern.items.items.get('include_list').items.type, Pattern.DICT) doc.get('list').add() self.eq(doc.get('list').get(0).type_list, 'list') self.eq(doc.get('list').data(), [[]]) doc.get('list').get(0).add() doc.get('list').get(0).get(0).get('include_list').add() self.eq( doc.get('list').data(), [[{ 'param': 'test', 'include_list': [{}] }]]) # remove and change_type_item signal self.pattern.get('list').items.items.remove('include_list') self.eq(doc.get('list').data(), [[{'param': 'test'}]]) self.pattern.get('list').items.items.remove('param') self.eq(doc.get('list').data(), [[{}]]) self.pattern.get('list').items.change_type_item(Pattern.INT, default=45) self.eq(doc.get('list').data(), [[]]) doc.get('list').get(0).add() self.eq(doc.get('list').data(), [[45]]) self.pattern.get('list').change_type_item(Pattern.DICT) self.eq(doc.get('list').data(), []) doc.get('list').add() self.eq(doc.get('list').data(), [{}])
class Test(BaseCase): def setUp(self): self.project = Project() self.table = self.project.add('test') self.pattern = self.table.pattern def test_int(self): pattern_int = self.table.pattern.add('field_int', type=Pattern.INT, \ default=1, min= -5, max=10) self.eq(pattern_int.type, Pattern.INT) self.eq(pattern_int.default, 1) self.eq(pattern_int.max, 10) self.eq(pattern_int.min, -5) doc = self.table.add(1) self.eq(doc.get('field_int').get(), 1) doc.get('field_int').set(3) self.eq(doc.get('field_int').get(), 3) doc.get('field_int').set(-6) self.eq(doc.get('field_int').get(), 3) doc.get('field_int').set(11) self.eq(doc.get('field_int').get(), 3) doc.get('field_int').set(-5) self.eq(doc.get('field_int').get(), -5) doc.get('field_int').set(10) self.eq(doc.get('field_int').get(), 10) # not min pattern_int.min = None doc.get('field_int').set(-100) self.eq(doc.get('field_int').get(), -100) # not max pattern_int.max = None doc.get('field_int').set(100) self.eq(doc.get('field_int').get(), 100) # add values in pattern doc.get('field_int').set(1) pattern_int.values = [1, 2, 6] doc.get('field_int').set(2) self.eq(doc.get('field_int').get(), 2) doc.get('field_int').set(6) self.eq(doc.get('field_int').get(), 6) doc.get('field_int').set(2) self.eq(doc.get('field_int').get(), 2) doc.get('field_int').set(0) self.eq(doc.get('field_int').get(), 2) def test_float(self): pattern_int = self.table.pattern.add('field', type=Pattern.FLOAT, \ default=1.2, min= -5.1, max=10.6) self.eq(pattern_int.type, Pattern.FLOAT) self.eq(pattern_int.default, 1.2) self.eq(pattern_int.max, 10.6) self.eq(pattern_int.min, -5.1) doc = self.table.add(1) self.eq(doc.get('field').get(), 1.2) doc.get('field').set(3.6) self.eq(doc.get('field').get(), 3.6) doc.get('field').set(-5.11) self.eq(doc.get('field').get(), 3.6) doc.get('field').set(10.7) self.eq(doc.get('field').get(), 3.6) doc.get('field').set(-5.05) self.eq(doc.get('field').get(), -5.05) doc.get('field').set(10.4) self.eq(doc.get('field').get(), 10.4) # not min pattern_int.min = None doc.get('field').set(-100.156) self.eq(doc.get('field').get(), -100.156) # not max pattern_int.max = None doc.get('field').set(100.89) self.eq(doc.get('field').get(), 100.89) # add values in pattern doc.get('field').set(1.2) pattern_int.values = [1.2, 2.36, 6.598] doc.get('field').set(2.36) self.eq(doc.get('field').get(), 2.36) doc.get('field').set(6.598) self.eq(doc.get('field').get(), 6.598) doc.get('field').set(2.36) self.eq(doc.get('field').get(), 2.36) doc.get('field').set(0.358) self.eq(doc.get('field').get(), 2.36) def test_str(self): pattern = self.table.pattern.add('field', type=Pattern.STR, \ default='t') self.eq(pattern.type, Pattern.STR) self.eq(pattern.default, 't') doc = self.table.add(1) self.eq(doc.get('field').get(), 't') pattern.values = ['a', 'b', 'c'] doc.get('field').set('a') self.eq(doc.get('field').get(), 'a') doc.get('field').set('c') self.eq(doc.get('field').get(), 'c') doc.get('field').set('x') self.eq(doc.get('field').get(), 'c') def test_signal(self): first = self.table.add(1) pattern = self.table.pattern.add('field', type=Pattern.INT, \ default=12) second = self.table.add(2) self.eq(second.get('field').get(), 12) self.eq(first.get('field').get(), 12) self.eq(pattern, first.get('field').pattern) self.eq(first.get('field').pattern, second.get('field').pattern) # dict pattern = self.table.pattern.add('field_dict', type=Pattern.DICT) self.isinstance(first.get('field_dict'), Document) self.isinstance(second.get('field_dict'), Document) def test_pattern_items(self): pattern = self.table.pattern.add('list', type=Pattern.LIST) self.eq(pattern.type, Pattern.LIST) self.assertRaises(PatternError, pattern.add, ('test',)) self.isinstance(pattern.items, Pattern) self.eq(pattern.items.type, Pattern.DICT) def test_add_pattern_int(self): pattern = self.table.pattern.add('pattern', type=Pattern.INT) self.eq(pattern.type, Pattern.INT) self.assertRaises(PatternError, pattern.add, ('test',)) def test_add_pattern_float(self): pattern = self.table.pattern.add('pattern', type=Pattern.FLOAT) self.eq(pattern.type, Pattern.FLOAT) self.assertRaises(PatternError, pattern.add, ('test',)) def test_add_pattern_str(self): pattern = self.table.pattern.add('pattern', type=Pattern.STR) self.eq(pattern.type, Pattern.STR) self.assertRaises(PatternError, pattern.add, ('test',)) def get_data_schema(self): return { "type": "dict", "properties": { "text": { "type": "dict", "properties": { "ru": { "type": "dict", "properties": { "title": { "default": "русский заголовок", "type": "str" } } }, "eng": { "type": "dict", "properties": { "title": { "default": "english title", "type": "str" } } } } }, "dynamic": { "items": { "type": "int" }, "type": "dynamic_dict" }, "gift": { "items": { "type": "dict", "properties": { "type": { "default": "coins", "values": [ "coins", "coins_gold" ], "type": "str" }, "value": { "items": { "default": 12, "type": "int" }, "type": "list" } } }, "type": "list" } } } def test_get_data(self): # get data self.pattern.add('text', type=Pattern.DICT) self.pattern.get('text').add('ru', type=Pattern.DICT) self.pattern.get('text').get('ru').add('title', type=Pattern.STR, default='русский заголовок') self.pattern.get('text').add('eng', type=Pattern.DICT) self.pattern.get('text').get('eng').add('title', type=Pattern.STR, default='english title') self.pattern.add('gift', type=Pattern.LIST) self.pattern.get('gift').items.add('type', type=Pattern.STR, values=['coins', 'coins_gold'], default='coins') self.pattern.get('gift').items.add('value', type=Pattern.LIST, item_type=Pattern.INT) self.pattern.get('gift').items.get('value').items.default = 12 self.pattern.add('dynamic', type=Pattern.DYNAMIC_DICT, item_type=Pattern.INT) self.eq(self.pattern.data(), self.get_data_schema()) # set data self.pattern.remove_all() self.eq(self.pattern.data(), {'type': 'dict', 'properties': {}}) self.pattern.parse_data(self.get_data_schema()) self.eq(self.pattern.data(), self.get_data_schema()) self.eq(self.pattern.get('text').type, Pattern.DICT) self.eq(self.pattern.get('text').get('ru').type, Pattern.DICT) self.eq(self.pattern.get('text').get('eng').type, Pattern.DICT) self.eq(self.pattern.get('text').get('ru').get('title').type, Pattern.STR) self.eq(self.pattern.get('text').get('ru').get('title').default, 'русский заголовок') self.eq(self.pattern.get('text').get('eng').type, Pattern.DICT) self.eq(self.pattern.get('text').get('eng').get('title').default, 'english title') self.eq(self.pattern.get('gift').type, Pattern.LIST) self.eq(self.pattern.get('gift').items.type, Pattern.DICT) self.eq(self.pattern.get('gift').items.get('type').type, Pattern.STR) self.eq(self.pattern.get('gift').items.get('type').default, 'coins') self.eq(self.pattern.get('gift').items.get('type').values, ['coins', 'coins_gold']) self.eq(self.pattern.get('gift').items.get('value').type, Pattern.LIST) self.eq(self.pattern.get('gift').items.get('value').items.type, Pattern.INT) self.eq(self.pattern.get('gift').items.get('value').items.default, 12) self.eq(self.pattern.get('dynamic').type, Pattern.DYNAMIC_DICT) self.eq(self.pattern.get('dynamic').items.type, Pattern.INT) self.eq(self.pattern.get('dynamic').items.default, 0) def test_link_project(self): self.pattern.remove_all() self.eq(self.pattern.data(), {'type': 'dict', 'properties': {}}) self.pattern.parse_data(self.get_data_schema()) self.eq(self.pattern.project, self.project) self.eq(self.pattern.get('text').project, self.project) self.eq(self.pattern.get('text').get('ru').project, self.project) self.eq(self.pattern.get('text').get('ru').get('title').project, self.project) self.eq(self.pattern.get('gift').items.project, self.project) self.eq(self.pattern.get('gift').items.get('type').project, self.project) self.eq(self.pattern.get('gift').items.get('value').project, self.project) self.eq(self.pattern.get('gift').items.get('value').items.project, self.project) # dinamic self.pattern.remove_all() self.pattern.add('text', type=Pattern.DICT) self.pattern.get('text').add('ru', type=Pattern.DICT) self.pattern.get('text').get('ru').add('title', type=Pattern.STR, default='русский заголовок') self.pattern.get('text').add('eng', type=Pattern.DICT) self.pattern.get('text').get('eng').add('title', type=Pattern.STR, default='english title') self.pattern.add('gift', type=Pattern.LIST) self.pattern.get('gift').items.add('type', type=Pattern.STR, values=['coins', 'coins_gold'], default='coins') self.pattern.get('gift').items.add('value', type=Pattern.LIST, item_type=Pattern.INT) self.pattern.get('gift').items.get('value').items.default = 12 self.eq(self.pattern.project, self.project) self.eq(self.pattern.get('text').project, self.project) self.eq(self.pattern.get('text').get('ru').project, self.project) self.eq(self.pattern.get('text').get('ru').get('title').project, self.project) self.eq(self.pattern.get('gift').items.project, self.project) self.eq(self.pattern.get('gift').items.get('type').project, self.project) self.eq(self.pattern.get('gift').items.get('value').project, self.project) self.eq(self.pattern.get('gift').items.get('value').items.project, self.project)
def setUp(self): self.project = Project() self.table_name = 'artifact'
class TableTest(BaseCase): def setUp(self): self.project = Project() self.table = self.project.add('test') def test_add(self): self.eq(self.table.name, 'test') doc = self.table.add('1') self.isinstance(doc, Document) self.eq(doc.name, '1') doc2 = self.table.add('test') self.isinstance(doc2, Document) self.eq(doc2.name, 'test') def test_add_dublicate(self): self.neq(self.table.add('1'), None) self.eq(len(self.table.keys()), 1) self.eq(self.table.add('1'), None) self.eq(len(self.table.keys()), 1) self.eq(self.table.add(1), None) self.eq(len(self.table.keys()), 1) self.neq(self.table.add(2), None) self.eq(len(self.table.keys()), 2) self.neq(self.table.add('test'), None) self.eq(len(self.table.keys()), 3) self.eq(self.table.add('test'), None) self.eq(len(self.table.keys()), 3) def test_remove(self): doc = self.table.add('test') self.eq(len(self.table.keys()), 1) self.eq(self.table.get('test'), doc) doc2 = self.table.add('test2') self.eq(len(self.table.keys()), 2) self.eq(self.table.get('test2'), doc2) self.eq(self.table.remove('test'), doc) self.eq(self.table.get('test'), None) self.eq(len(self.table.keys()), 1) self.eq(self.table.remove('test2'), doc2) self.eq(self.table.get('test2'), None) self.eq(len(self.table.keys()), 0) def test_remove_all(self): self.table.add('test1') self.table.add('test2') self.table.remove_all() self.eq(self.table.get('test1'), None) self.eq(self.table.get('test2'), None) self.eq(len(self.table.keys()), 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': {}}}})
class Test(BaseCase): def setUp(self): self.project = Project() self.artifact = self.project.add('artifact') self.artifact.pattern.add('int', type=Pattern.INT) self.artifact.pattern.add('str', type=Pattern.STR) self.artifact.pattern.add('float', type=Pattern.FLOAT) self.table_list = self.project.add('table_list', type=Pattern.LIST) self.table_list.pattern.items.add('int', type=Pattern.INT) self.table_dynamic = self.project.add('table_dynamic', type=Pattern.DYNAMIC_DICT) self.table_dynamic.pattern.items.add('str', type=Pattern.STR) self.table_list2 = self.project.add('table_list2', type=Pattern.LIST, item_type=Pattern.INT) def test(self): for i in xrange(1, 11): doc = self.artifact.add(i) self.eq(dict(doc.stats()), { 'int': 1, 'float': 1, 'str': 1, 'dict': 1 }) self.eq(dict(self.artifact.stats()), { 'int': 10, 'float': 10, 'document': 10, 'str': 10, 'dict': 10 }) self.eq( dict(self.project.stats()), { 'int': 10, 'float': 10, 'document': 10, 'str': 10, 'table': 4, 'dict': 10 }) # for i in xrange(1, 6): doc = self.table_list.add(i) self.eq(dict(doc.stats()), {}) for j in xrange(1, 21): item = doc.add() self.eq(dict(item.stats()), {'int': 1, 'dict': 1}) self.eq(dict(doc.stats()), {'int': j, 'dict': j}) self.eq(dict(self.table_list.stats()), { 'int': 100, 'document': 5, 'dict': 100 }) self.eq( dict(self.project.stats()), { 'int': 110, 'float': 10, 'document': 15, 'str': 10, 'table': 4, 'dict': 110 }) # for i in xrange(1, 16): doc = self.table_dynamic.add(i) self.eq(dict(doc.stats()), {}) for j in xrange(1, 6): item = doc.add(j) self.eq(dict(item.stats()), {'str': 1, 'dict': 1}) self.eq(dict(doc.stats()), {'str': j, 'dict': j}) self.eq(dict(self.table_dynamic.stats()), { 'str': 75, 'document': 15, 'dict': 75 }) self.eq( dict(self.project.stats()), { 'int': 110, 'float': 10, 'document': 30, 'str': 85, 'table': 4, 'dict': 185 }) # for i in xrange(1, 11): doc = self.table_list2.add(i) self.eq(dict(doc.stats()), {}) for j in xrange(1, 21): item = doc.add() self.eq(dict(item.stats()), {'int': 1}) self.eq(dict(doc.stats()), {'int': j}) self.eq(dict(self.table_list2.stats()), {'int': 200, 'document': 10}) self.eq( dict(self.project.stats()), { 'int': 310, 'float': 10, 'document': 40, 'str': 85, 'table': 4, 'dict': 185 })
class Test(BaseCase): def setUp(self): self.project = Project() self.table = self.project.add('test') def test_one_field(self): self.table.pattern.add('id', type=Pattern.INT, incr='test.id', default=12) self.eq(self.table.add(1).get('id').get(), 1) self.eq(self.table.get(1).get('id').set(12), 1) self.eq(self.table.get(1).get('id').get(), 1) self.eq(self.table.add(2).get('id').get(), 2) self.eq(self.table.get(2).get('id').set(14), 2) self.eq(self.table.get(2).get('id').get(), 2) self.eq(self.table.add(3).get('id').get(), 3) self.eq(self.table.get(2).get('id').set(14), 2) self.eq(self.table.get(2).get('id').get(), 2) def test_many_fields(self): self.table.pattern.add('id', type=Pattern.INT, incr='test.id', default=12) self.table.pattern.add('value', type=Pattern.INT, incr='test.id', default=14) self.table.pattern.add('id1', type=Pattern.INT, incr='test.id1', default=12) self.table.pattern.add('value1', type=Pattern.INT, incr='test.value1', default=14) self.table.pattern.add('list', type=Pattern.LIST) self.table.pattern.get('list').change_type_item(Pattern.INT, incr='test.id') self.eq(self.table.add(1).get('id').get(), 1) self.eq(self.table.get(1).get('value').get(), 2) self.eq(self.table.get(1).get('id1').get(), 1) self.eq(self.table.get(1).get('value1').get(), 1) self.eq(self.table.add(2).get('id').get(), 3) self.eq(self.table.get(2).get('value').get(), 4) self.eq(self.table.get(2).get('id1').get(), 2) self.eq(self.table.get(2).get('value1').get(), 2) self.eq(self.table.add(3).get('id').get(), 5) self.eq(self.table.get(3).get('value').get(), 6) self.eq(self.table.get(3).get('id1').get(), 3) self.eq(self.table.get(3).get('value1').get(), 3) self.eq(self.table.get(3).get('list').add().get(), 7) self.eq(self.table.get(3).get('list').get(0).set(11), 7) self.eq(self.table.get(1).get('list').add().get(), 8) self.eq(self.table.get(2).get('list').add().get(), 9) self.eq(self.table.get(3).get('list').add().get(), 10) self.eq(self.table.get(3).get('list').get(1).set(12), 10) def test_many_tables(self): self.table.pattern.add('id', type=Pattern.INT, incr='global_id', default=12) self.table2 = self.project.add('test2') self.table2.pattern.add('id2', type=Pattern.INT, incr='global_id', default=12) self.eq(self.table.add(1).get('id').get(), 1) self.eq(self.table2.add(10).get('id2').get(), 2) self.eq(self.table2.add(2).get('id2').get(), 3) self.eq(self.table.add(2).get('id').get(), 4)
def setUp(self): self.project = Project() self.table = self.project.add('test')
class Test(BaseCase): def setUp(self): self.project = Project() self.manager = self.project.manager self.price = self.manager.add('price', type=Pattern.DICT) self.price.add('type', type=Pattern.STR, values=['coins', 'coins_gold'], default='coins') self.price.add('value', type=Pattern.INT, default=10) def test_add_common(self): map_object = self.project.add('map_object', type=Pattern.DICT) map_object_price = map_object.pattern.add('price', common='price') self.eq(self.price, map_object_price) artifact = self.project.add('artifact', type=Pattern.DICT) artifact_price = artifact.pattern.add('artifact_price', common='price') self.eq(self.price, artifact_price) self.eq(artifact.pattern.add('test', common='unreal_common'), None) surprise = self.project.add('surprise', type=Pattern.DICT) surprise_price = surprise.pattern.add('list', type=Pattern.LIST, item_common='price').items self.eq(surprise_price, self.price) def test_change_item_type(self): map_object = self.project.add('map_object', type=Pattern.DICT) map_object_price = map_object.pattern.add('price', type=Pattern.LIST) self.neq(map_object_price.items, self.price) map_object_price.change_type_item(None, item_common='price') self.eq(self.price, map_object_price.items) def test_common_root(self): artifact = self.project.add('artifact', common='price') self.eq(artifact.pattern, self.price) def test_document(self): artifact = self.project.add('artifact') artifact.pattern.add('price', common='price') artifact.pattern.add('test', type=Pattern.STR, default='temp') ruin = self.project.add('ruin') ruin.pattern.add('price', common='price') ruin.pattern.add('value', type=Pattern.INT, default=0) # art1 = artifact.add(1) self.eq(art1.data(), { 'test': 'temp', 'price': { 'type': 'coins', 'value': 10 } }) art1.get('price.value').set(20) art1.get('test').set('temp1') art1.get('price.type').set('coins_gold') self.eq(art1.data(), { 'test': 'temp1', 'price': { 'type': 'coins_gold', 'value': 20 } }) # r = ruin.add('mo2') self.eq(r.data(), { 'value': 0, 'price': { 'type': 'coins', 'value': 10 } }) r.get('price.value').set(22) r.get('value').set(2) r.get('price.type').set('coins_gold') self.eq(r.data(), { 'value': 2, 'price': { 'type': 'coins_gold', 'value': 22 } }) # change common pattern self.price.add('chance', type=Pattern.LIST, item_type=Pattern.INT) art1.get('price.chance').add().set(1) art1.get('price.chance').add().set(2) art1.get('price.chance').add() self.eq( art1.data(), { 'test': 'temp1', 'price': { 'type': 'coins_gold', 'value': 20, 'chance': [1, 2, 0] } }) r.get('price.chance').add().set(111) r.get('price.chance').add().set(26) self.eq( r.data(), { 'value': 2, 'price': { 'type': 'coins_gold', 'value': 22, 'chance': [111, 26] } })
class Test(BaseCase): def setUp(self): self.project = Project() self.table = self.project.add('test') self.pattern = self.table.pattern def test_add_int_pattern(self): doc = self.table.add(1) self.eq(doc.pattern.type, Pattern.DICT) pattern_field_int = self.pattern.add('field_int', type=Pattern.INT, default=12) self.isinstance(doc.get('field_int'), Field) self.eq(doc.get('field_int').pattern.type, Pattern.INT) self.eq(doc.get('field_int').pattern, pattern_field_int) self.eq(doc.get('field_int').get(), pattern_field_int.default) def test_add_list_pattern(self): doc = self.table.add(1) pattern_list = self.pattern.add('field_list', type=Pattern.LIST) self.isinstance(doc.get('field_list'), Document) self.eq(doc.get('field_list').pattern.type, Pattern.LIST) self.eq(doc.get('field_list').pattern, pattern_list) self.eq(doc.get('field_list').type_list, 'list') def create_pattern(self): self.pattern.add('text', type=Pattern.DICT) self.pattern.get('text').add('ru', type=Pattern.DICT) self.pattern.get('text').get('ru').add('title', type=Pattern.STR, default='русский заголовок') self.pattern.get('text').add('eng', type=Pattern.DICT) self.pattern.get('text').get('eng').add('title', type=Pattern.STR, default='english title') self.pattern.add('gift', type=Pattern.LIST) self.pattern.get('gift').items.add('type', type=Pattern.STR, values=['coins', 'coins_gold'], default='coins') self.pattern.get('gift').items.add('value', type=Pattern.LIST, item_type=Pattern.INT) self.pattern.get('gift').items.get('value').items.default = 12 def test_pattern(self): self.create_pattern() data = {'text': {'ru': {'title': 'test value'}, 'eng': {'title': 'sample'}}, 'gift': [{'type':'coins1', 'value':[1]}, {'type':'coins_gold', 'value':[1, 34]}] } doc = self.table.add(1, data=data) self.eq(doc.data(), {'text': {'ru': {'title': 'test value'}, 'eng': {'title': 'sample'}}, 'gift': [{'type':'coins', 'value':[1]}, {'type':'coins_gold', 'value':[1, 34]}]}) self.eq(doc.get('text').get('ru').get('title').get(), 'test value') self.eq(doc.get('text').get('eng').get('title').get(), 'sample') # gift = doc.get('gift').add(data=\ {'type': 'coins_gold', 'value': [5, 4, 67, '']}) self.eq(gift.data(), {'type': 'coins_gold', 'value': [5, 4, 67, 12]}) self.eq(doc.get('gift').length(), 3) # new_doc = self.table.add(2, data=doc.data()) self.eq(new_doc.data(), doc.data()) def test_signals(self): self.create_pattern() doc = self.table.add(1) # add signal self.pattern.add('price', type=Pattern.DICT) self.pattern.get('price').add('type', type=Pattern.STR, default='type') self.pattern.get('price').add('value', type=Pattern.INT, default=100) self.eq(doc.get('price').data(), {'type': 'type', 'value': 100}) self.pattern.add('list', type=Pattern.LIST, item_type=Pattern.LIST) self.pattern.get('list').items.items.add('param', type=Pattern.STR, default='test') self.pattern.get('list').items.items.add('include_list', type=Pattern.LIST) self.eq(doc.get('list').type_list, 'list') self.eq(doc.get('list').pattern.type, Pattern.LIST) self.eq(doc.get('list').pattern.items.type, Pattern.LIST) self.eq(doc.get('list').pattern.items.items.type, Pattern.DICT) self.eq(doc.get('list').pattern.items.items.get('param').type, Pattern.STR) self.eq(doc.get('list').pattern.items.items.get('include_list').type, Pattern.LIST) self.eq(doc.get('list').pattern.items.items.get('include_list').items.type, Pattern.DICT) doc.get('list').add() self.eq(doc.get('list').get(0).type_list, 'list') self.eq(doc.get('list').data(), [[]]) doc.get('list').get(0).add() doc.get('list').get(0).get(0).get('include_list').add() self.eq(doc.get('list').data(), [[{'param':'test', 'include_list':[{}]}]]) # remove and change_type_item signal self.pattern.get('list').items.items.remove('include_list') self.eq(doc.get('list').data(), [[{'param':'test'}]]) self.pattern.get('list').items.items.remove('param') self.eq(doc.get('list').data(), [[{}]]) self.pattern.get('list').items.change_type_item(Pattern.INT, default=45) self.eq(doc.get('list').data(), [[]]) doc.get('list').get(0).add() self.eq(doc.get('list').data(), [[45]]) self.pattern.get('list').change_type_item(Pattern.DICT) self.eq(doc.get('list').data(), []) doc.get('list').add() self.eq(doc.get('list').data(), [{}])