def test_from_dict(self): obj1_dict = Head(name="Price", displayName="Цена").to_dict() clean_dict = loads(dumps(obj1_dict, ensure_ascii=False), encoding='utf-8') obj2_dict = Head().from_dict(clean_dict).to_dict() self.assertDictEqual(obj1_dict, obj2_dict)
def test_add_cell_appends_head(self): obj = Row() \ .add_cell(Head( name="Price", displayName="Цена", )) \ .add_cell(Head( name="Date", displayName="Дата", )) self.assertEqual(len(obj.cells), 2)
def test_validate_invalid_children(self): errors = Row() \ .add_cells([ Head( name="Price", ), Head( name="Date", ) ]) \ .validate("tb.0", "parent") self.assertEqual(len(errors), 2) self.assertTrue(all([e.startswith('parent.tb.0.') for e in errors]))
def test_validate_row_size_mismatch(self): errors = Body(name="table") \ .set_header(lambda f: f.add_cells([ Head(name="h1", displayName="Head 1"), Head(name="h2", displayName="Head 2") ])) \ .add_row(lambda r: r.add_cell(Cell( name="h1", type=FieldType.Date, value=1 ))) \ .validate("parent") self.assertEqual(len(errors), 1)
def test_validate_invalid_children_not_unique(self): errors = Row() \ .add_cells([ Head( name="Price", displayName="Цена", ), Head( name="Price", displayName="Цена", ) ]) \ .validate("tb.0", "parent") self.assertEqual(len(errors), 1) self.assertTrue(all([e.startswith('parent.tb.0:') for e in errors]))
def test_to_dict_head_convertable_to_dict(self): obj = Row() \ .add_cells([ Head( name="Price", displayName="Цена", ), Head( name="Date", displayName="Дата", ) ]) \ .to_dict() expected = "{\"0\": {\"fn\": \"Price\", \"fv\": \"Цена\"}, \"1\": {\"fn\": \"Date\", \"fv\": \"Дата\"}}" self.assertEqual(dumps(obj, ensure_ascii=False), expected)
def test_to_dict_returns_valid_dictionary(self): some_list = [ dict(SomeShit="SomeShit1", Nullable="Nullable1", OtherShit=1), dict(SomeShit="SomeShit2", Nullable="Nullable2", OtherShit=2), dict(SomeShit="SomeShit2", Nullable="Nullable2", OtherShit=2) ] obj = Body() \ .set_properties( name="Table1", displayName="Таблица 1" ) \ .set_header( lambda th: th .add_cells([ Head(name="Head1", displayName="Header 1"), Head(name="Head2", displayName="Header 2"), Head(name="Head3", displayName="Header 3") ]) ) \ .add_rows( some_list, lambda list_element, row: row.add_cells([ Cell( name="Head1", type=FieldType.Integer, value=list_element['SomeShit'] ), Cell( name="Head2", type=FieldType.String, value=list_element['Nullable'] ), Cell( name="Head3", type=FieldType.String, value=list_element['OtherShit'] ) ]) ) \ .to_dict() # print(dumps(obj, ensure_ascii=False)) expected = "{\"fn\": \"Table1\", \"ft\": \"Table\", \"fv\": {\"th\": {\"0\": {\"fn\": \"Head1\", \"fv\": \"Header 1\"}, \"1\": {\"fn\": \"Head2\", \"fv\": \"Header 2\"}, \"2\": {\"fn\": \"Head3\", \"fv\": \"Header 3\"}}, \"tb\": {\"0\": {\"0\": {\"fn\": \"Head1\", \"ft\": \"Integer\", \"fv\": \"SomeShit1\"}, \"1\": {\"fn\": \"Head2\", \"ft\": \"String\", \"fv\": \"Nullable1\"}, \"2\": {\"fn\": \"Head3\", \"ft\": \"String\", \"fv\": 1}}, \"1\": {\"0\": {\"fn\": \"Head1\", \"ft\": \"Integer\", \"fv\": \"SomeShit2\"}, \"1\": {\"fn\": \"Head2\", \"ft\": \"String\", \"fv\": \"Nullable2\"}, \"2\": {\"fn\": \"Head3\", \"ft\": \"String\", \"fv\": 2}}, \"2\": {\"0\": {\"fn\": \"Head1\", \"ft\": \"Integer\", \"fv\": \"SomeShit2\"}, \"1\": {\"fn\": \"Head2\", \"ft\": \"String\", \"fv\": \"Nullable2\"}, \"2\": {\"fn\": \"Head3\", \"ft\": \"String\", \"fv\": 2}}}}, \"fdn\": \"Таблица 1\"}" self.assertEqual(dumps(obj, ensure_ascii=False), expected)
def test_from_dict_th(self): row1_dict = Row() \ .add_cells([ Head( name="Price", displayName="Цена", ), Head( name="Price", displayName="Цена", ) ]) \ .to_dict() clean_dict = loads(dumps(row1_dict, ensure_ascii=False), encoding='utf-8') row2_dict = Row().from_dict('th', clean_dict).to_dict() self.assertDictEqual(row1_dict, row2_dict)
def test_from_dict(self): bd1_dict = Body(name="table", displayName="Table 1") \ .set_header(lambda f: f.add_cells([ Head(name="h1", displayName="Head 1"), Head(name="h2", displayName="Head 2") ])) \ .add_row(lambda r: r.add_cells([ Cell( name="h1", type=FieldType.Date, value=1 ), Cell( name="h2", type=FieldType.Date, value=2 ) ])) \ .add_row(lambda r: r.add_cells([ Cell( name="h1", type=FieldType.Date, value=3 ), Cell( name="h2", type=FieldType.Date, value=4 ) ])) \ .to_dict() clean_dict = loads(dumps(bd1_dict, ensure_ascii=False), encoding='utf-8') bd2_dict = Body().from_dict(clean_dict).to_dict() self.assertDictEqual(bd1_dict, bd2_dict)
def test_from_json_with_table(self): some_list = [ dict(SomeShit="SomeShit1", Nullable="Nullable1", OtherShit=1), dict(SomeShit="SomeShit2", Nullable="Nullable2", OtherShit=2), dict(SomeShit="SomeShit2", Nullable="Nullable2", OtherShit=2) ] json1 = Root() \ .add_general(Field( name="General1", type=FieldType.DateTime, value=5039466, displayName="Основное поле 1" ) ) \ .add_category( lambda c: c .set_properties( name="Category1", displayName="Категория 1", modifications=[ Modification.Calendar, Modification.Help] ) .add_table( lambda t: t .set_properties( name="Table1", displayName="Таблица 1" ) .set_header( lambda th: th .add_cells([ Head(name="Head1", displayName="Header 1"), Head(name="Head2", displayName="Header 2"), Head(name="Head3", displayName="Header 3") ])) .add_rows( some_list, lambda list_element, row: row.add_cells([ Cell( name="Head1", type=FieldType.Integer, value=list_element['SomeShit'] ), Cell( name="Head2", type=FieldType.String, value=list_element['Nullable'] ), Cell( name="Head3", type=FieldType.String, value=list_element['OtherShit'] ) ]) ) ) .add_field(Field( type=FieldType.String, name="Field1", displayName="Поле категории 1", value="просто строка", modifications=[Modification.CurEUR] )) ) \ .add_category( lambda c: c .set_properties( name="Category2", displayName="Категория 2", modifications=[ Modification.Calendar, Modification.Help] ) .add_field(Field( type=FieldType.Price, name="Field2", displayName="Поле категории 2", value=124124.54, modifications=[Modification.CurEUR], changes=dict([[1, "Старое поле категории 2"], [2, "Поле категории 2"]]) )) ).to_json() json2 = Root().from_json(json1).to_json() self.assertEqual(json1, json2)
def test_to_dict_returns_only_two_fields(self): obj = Head(name="Price", displayName="Цена") expected = dict(fn="Price", fv="Цена") self.assertEqual(obj.to_dict(), expected)
def test_to_dict_convertable_to_dict(self): obj = Head(name="Price", displayName="Цена") expected = dumps(dict(fn="Price", fv="Цена")) self.assertEqual(dumps(obj.to_dict()), expected)
def test_to_json_with_table(self): some_list = [ dict( SomeShit="SomeShit1", Nullable="Nullable1", OtherShit=1 ), dict( SomeShit="SomeShit2", Nullable="Nullable2", OtherShit=2 ), dict( SomeShit="SomeShit2", Nullable="Nullable2", OtherShit=2 ) ] result = Root() \ .add_general(Field( name="General1", type=FieldType.DateTime, value=5039466, displayName="Основное поле 1" ) ) \ .add_category( lambda c: c .set_properties( name="Category1", displayName="Категория 1", modifications=[ Modification.Calendar, Modification.Help] ) .add_table( lambda t: t .set_properties( name="Table1", displayName="Таблица 1" ) .set_header( lambda th: th .add_cells([ Head(name="Head1", displayName="Header 1"), Head(name="Head2", displayName="Header 2"), Head(name="Head3", displayName="Header 3") ])) .add_rows( some_list, lambda list_element, row: row.add_cells([ Cell( name="Head1", type=FieldType.Integer, value=list_element['SomeShit'] ), Cell( name="Head2", type=FieldType.String, value=list_element['Nullable'] ), Cell( name="Head3", type=FieldType.String, value=list_element['OtherShit'] ) ]) ) ) .add_field(Field( type=FieldType.String, name="Field1", displayName="Поле категории 1", value="просто строка", modifications=[Modification.CurEUR] )) ) \ .add_category( lambda c: c .set_properties( name="Category2", displayName="Категория 2", modifications=[ Modification.Calendar, Modification.Help] ) .add_field(Field( type=FieldType.Price, name="Field2", displayName="Поле категории 2", value=124124.54, modifications=[Modification.CurEUR], changes=dict([[1, "Старое поле категории 2"], [2, "Поле категории 2"]]) )) ) \ .to_json() self.assertEqual( result, "{\"0\":{\"fn\":\"Category1\",\"ft\":\"Category\",\"fv\":{\"0\":{\"fn\":\"Table1\",\"ft\":\"Table\",\"fv\":{\"th\":{\"0\":{\"fn\":\"Head1\",\"fv\":\"Header 1\"},\"1\":{\"fn\":\"Head2\",\"fv\":\"Header 2\"},\"2\":{\"fn\":\"Head3\",\"fv\":\"Header 3\"}},\"tb\":{\"0\":{\"0\":{\"fn\":\"Head1\",\"ft\":\"Integer\",\"fv\":\"SomeShit1\"},\"1\":{\"fn\":\"Head2\",\"ft\":\"String\",\"fv\":\"Nullable1\"},\"2\":{\"fn\":\"Head3\",\"ft\":\"String\",\"fv\":1}},\"1\":{\"0\":{\"fn\":\"Head1\",\"ft\":\"Integer\",\"fv\":\"SomeShit2\"},\"1\":{\"fn\":\"Head2\",\"ft\":\"String\",\"fv\":\"Nullable2\"},\"2\":{\"fn\":\"Head3\",\"ft\":\"String\",\"fv\":2}},\"2\":{\"0\":{\"fn\":\"Head1\",\"ft\":\"Integer\",\"fv\":\"SomeShit2\"},\"1\":{\"fn\":\"Head2\",\"ft\":\"String\",\"fv\":\"Nullable2\"},\"2\":{\"fn\":\"Head3\",\"ft\":\"String\",\"fv\":2}}}},\"fdn\":\"Таблица 1\"},\"1\":{\"fn\":\"Field1\",\"ft\":\"String\",\"fv\":\"просто строка\",\"fdn\":\"Поле категории 1\",\"md\":[\"CurEUR\"]}},\"fdn\":\"Категория 1\",\"md\":[\"Calendar\",\"Help\"]},\"1\":{\"fn\":\"Category2\",\"ft\":\"Category\",\"fv\":{\"0\":{\"fn\":\"Field2\",\"ft\":\"Price\",\"fv\":124124.54,\"fdn\":\"Поле категории 2\",\"md\":[\"CurEUR\"],\"ch\":{\"1\":\"Старое поле категории 2\",\"2\":\"Поле категории 2\"}}},\"fdn\":\"Категория 2\",\"md\":[\"Calendar\",\"Help\"]},\"general\":{\"0\":{\"fn\":\"General1\",\"ft\":\"DateTime\",\"fv\":5039466,\"fdn\":\"Основное поле 1\"},\"customers\":[]}}")
def test_compare_categories_with_general(self): some_list = [ dict(SomeShit="SomeShit1", Nullable="Nullable1", OtherShit=1), dict(SomeShit="SomeShit2", Nullable="Nullable2", OtherShit=2), dict(SomeShit="SomeShit2", Nullable="Nullable2", OtherShit=2) ] obj1 = Root() \ .set_publication_date(1) \ .add_general(Field( name="General1", type=FieldType.DateTime, value=5039466, displayName="Основное поле 1" ) ) \ .add_category( lambda c: c .set_properties( name="Category1", displayName="Категория 1", modifications=[ Modification.Calendar, Modification.Help] ) .add_table( lambda t: t .set_properties( name="Table1", displayName="Таблица 1" ) .set_header( lambda th: th .add_cells([ Head(name="Head1", displayName="Header 1"), Head(name="Head2", displayName="Header 2"), Head(name="Head3", displayName="Header 3") ])) .add_rows( some_list, lambda list_element, row: row.add_cells([ Cell( name="Head1", type=FieldType.Integer, value=list_element['SomeShit'] ), Cell( name="Head2", type=FieldType.String, value=list_element['Nullable'] ), Cell( name="Head3", type=FieldType.String, value=list_element['OtherShit'] ) ]) ) ) .add_field(Field( type=FieldType.String, name="Field1", displayName="Поле категории 1", value="просто строка", modifications=[Modification.CurEUR] )) ) \ .add_category( lambda c: c .set_properties( name="Category2", displayName="Категория 2", modifications=[ Modification.Calendar, Modification.Help] ) .add_field(Field( type=FieldType.Price, name="Field2", displayName="Поле категории 2", value=124124.54, modifications=[Modification.CurEUR] )) ) obj2 = deepcopy(obj1) obj2.set_publication_date(2) obj2.general[0].value += 50 obj2.containers[0].entities[1].value = 'другая строка' obj2.containers[1].entities[0].value -= 50 result = obj2.compare_many([obj1]) expected = "{\"0\":{\"fn\":\"Category1\",\"ft\":\"Category\",\"fv\":{\"0\":{\"fn\":\"Table1\",\"ft\":\"Table\",\"fv\":{\"th\":{\"0\":{\"fn\":\"Head1\",\"fv\":\"Header 1\"},\"1\":{\"fn\":\"Head2\",\"fv\":\"Header 2\"},\"2\":{\"fn\":\"Head3\",\"fv\":\"Header 3\"}},\"tb\":{\"0\":{\"0\":{\"fn\":\"Head1\",\"ft\":\"Integer\",\"fv\":\"SomeShit1\"},\"1\":{\"fn\":\"Head2\",\"ft\":\"String\",\"fv\":\"Nullable1\"},\"2\":{\"fn\":\"Head3\",\"ft\":\"String\",\"fv\":1}},\"1\":{\"0\":{\"fn\":\"Head1\",\"ft\":\"Integer\",\"fv\":\"SomeShit2\"},\"1\":{\"fn\":\"Head2\",\"ft\":\"String\",\"fv\":\"Nullable2\"},\"2\":{\"fn\":\"Head3\",\"ft\":\"String\",\"fv\":2}},\"2\":{\"0\":{\"fn\":\"Head1\",\"ft\":\"Integer\",\"fv\":\"SomeShit2\"},\"1\":{\"fn\":\"Head2\",\"ft\":\"String\",\"fv\":\"Nullable2\"},\"2\":{\"fn\":\"Head3\",\"ft\":\"String\",\"fv\":2}}}},\"fdn\":\"Таблица 1\"},\"1\":{\"fn\":\"Field1\",\"ft\":\"String\",\"fv\":\"другая строка\",\"fdn\":\"Поле категории 1\",\"md\":[\"CurEUR\"],\"ch\":{\"1\":\"просто строка\",\"2\":\"другая строка\"}}},\"fdn\":\"Категория 1\",\"md\":[\"Calendar\",\"Help\"]},\"1\":{\"fn\":\"Category2\",\"ft\":\"Category\",\"fv\":{\"0\":{\"fn\":\"Field2\",\"ft\":\"Price\",\"fv\":124074.54,\"fdn\":\"Поле категории 2\",\"md\":[\"CurEUR\"],\"ch\":{\"1\":124124.54,\"2\":124074.54}}},\"fdn\":\"Категория 2\",\"md\":[\"Calendar\",\"Help\"]},\"general\":{\"0\":{\"fn\":\"General1\",\"ft\":\"DateTime\",\"fv\":5039516,\"fdn\":\"Основное поле 1\",\"ch\":{\"1\":5039466,\"2\":5039516}},\"customers\":[]}}" self.assertEqual(result.to_json(), expected)