def test_compare_customers(self): customers = [ dict(regnum=str(1), guid="", name="test1", maxPrice=50325235.53, guaranteeApp=95830945.54), dict(regnum=str(2), guid="", name="test2", maxPrice=20325235.53, guaranteeApp=35830945.54) ] result1 = Root() \ .set_publication_date(1) \ .add_customers( customers, lambda obj, c: c .set_properties( max_price=obj['maxPrice'], guarantee_app=obj['guaranteeApp'], customer_name=obj['name'], customer_guid=obj['guid'] ) .add_field(Field( value="1", name="test1", displayName="fdn", type=FieldType.String )) ) result2 = Root() \ .set_publication_date(2) \ .add_customers( customers, lambda obj, c: c .set_properties( max_price=obj['maxPrice'] + 50, guarantee_app=obj['guaranteeApp'] + 50, customer_name=obj['name'], customer_guid=obj['guid'] ) .add_field(Field( value="2", name="test1", displayName="fdn", type=FieldType.String )) ) result = result2.compare_many([result1]) expected = "{\"general\":{\"customers\":[{\"0\":{\"fn\":\"maxPrice\",\"ft\":\"Price\",\"fv\":50325285.53,\"fdn\":\"Цена контракта\",\"ch\":{\"1\":50325235.53,\"2\":50325285.53}},\"1\":{\"fn\":\"guaranteeApp\",\"ft\":\"Price\",\"fv\":95830995.54,\"fdn\":\"Обеспечение заявки\",\"ch\":{\"1\":95830945.54,\"2\":95830995.54}},\"2\":{\"fn\":\"customer\",\"ft\":\"Object\",\"fv\":{\"guid\":\"\",\"name\":\"test1\"},\"fdn\":\"Заказчик\"},\"3\":{\"fn\":\"test1\",\"ft\":\"String\",\"fv\":\"2\",\"fdn\":\"fdn\",\"ch\":{\"1\":\"1\",\"2\":\"2\"}}},{\"0\":{\"fn\":\"maxPrice\",\"ft\":\"Price\",\"fv\":20325285.53,\"fdn\":\"Цена контракта\",\"ch\":{\"1\":20325235.53,\"2\":20325285.53}},\"1\":{\"fn\":\"guaranteeApp\",\"ft\":\"Price\",\"fv\":35830995.54,\"fdn\":\"Обеспечение заявки\",\"ch\":{\"1\":35830945.54,\"2\":35830995.54}},\"2\":{\"fn\":\"customer\",\"ft\":\"Object\",\"fv\":{\"guid\":\"\",\"name\":\"test2\"},\"fdn\":\"Заказчик\"},\"3\":{\"fn\":\"test1\",\"ft\":\"String\",\"fv\":\"2\",\"fdn\":\"fdn\",\"ch\":{\"1\":\"1\",\"2\":\"2\"}}}]}}" self.assertEqual(result.to_json(), expected)
def test_to_dict_convertable_to_dict(self): obj = Field(name="Price", type=FieldType.Integer, value=100, displayName="Цена") expected = dumps( dict(fn="Price", ft=FieldType.Integer.name, fv=100, fdn="Цена")) self.assertEqual(dumps(obj.to_dict()), expected)
def test_to_dict_return_valid_dict(self): obj = Field(name="Price", type=FieldType.Integer, value=100, displayName="Цена") expected = dict(fn="Price", ft=FieldType.Integer.name, fv=100, fdn="Цена") self.assertEqual(obj.to_dict(), expected)
def test_compare_different(self): obj1 = Field(name="Price", type=FieldType.Integer, value=100, displayName="Цена") obj2 = Field(name="Price", type=FieldType.Integer, value=50, displayName="Цена") obj1.compare(obj2, 100, 200) self.assertEqual(obj1.changes, dict([[100, 50], [200, 100]]))
def test_compare_same(self): obj1 = Field(name="Price", type=FieldType.Integer, value=100, displayName="Цена") obj2 = Field(name="Price", type=FieldType.Integer, value=100, displayName="Цена") obj1.compare(obj2, 100, 200) self.assertEqual(obj1.changes, dict())
def test_cell_and_field_are_same_except_fdn(self): field = Field(name="Price", type=FieldType.Integer, value=100, displayName="Цена").to_dict() field.pop('fdn', None) cell = Cell(name="Price", type=FieldType.Integer, value=100, displayName="Цена").to_dict() self.assertEqual(field, cell)
def test_validate_valid_empty(self): errors = Field(name="Price", type=FieldType.Integer, value=100, displayName="Цена").validate() self.assertEqual(len(errors), 0)
def test_validate_with_customers(self): customers = [ dict(regnum=1, guid="", name="test1", maxPrice=50325235.53, guaranteeApp=95830945.54), dict(regnum=2, guid="", name="test2", maxPrice=20325235.53, guaranteeApp=35830945.54) ] errors = Root() \ .add_customers( customers, lambda obj, c: c .set_properties( max_price=obj['maxPrice'], guarantee_app=obj['guaranteeApp'], customer_name=obj['name'], customer_guid=obj['guid'] ) .add_field(Field( value="1", name="test1", displayName="fdn", type=FieldType.String )) ) \ .validate() self.assertEqual(errors, [])
def test_validate_invalid_non_unique_children(self): errors = Customer() \ .add_field(Field( name="field", displayName="Field 1", type=FieldType.Date, value=1, )) \ .add_field(Field( name="field", displayName="Field 2", type=FieldType.Date, value=2, )) \ .validate("parent") self.assertEqual(len(errors), 1)
def test_from_json_with_array(self): json1 = Root() \ .add_category( lambda c: c .set_properties( name="Category1", displayName="Категория 1", modifications=[Modification.Calendar, Modification.Help] ) .add_field(Field( type=FieldType.Price, name="Field1", displayName="Поле категории 1", value=124124.54, modifications=[Modification.CurEUR], changes=dict([[1, "Старое поле категории 1"], [2, "Поле категории 1"]]) )) ) \ .add_category( lambda c: c .set_properties( displayName="Category2", name="Category2" ) .add_array( lambda ca: ca .set_properties( name="Array1", displayName="Array 1" ) .add_field(Field( name="Field1", displayName="Поле 1", value="Значение 1", type=FieldType.String )) ) ) \ .to_json() json2 = Root().from_json(json1).to_json() self.assertEqual(json1, json2)
def test_to_json_with_array(self): result = Root() \ .add_category( lambda c: c .set_properties( name="Category1", displayName="Категория 1", modifications=[Modification.Calendar, Modification.Help] ) .add_field(Field( type=FieldType.Price, name="Field1", displayName="Поле категории 1", value=124124.54, modifications=[Modification.CurEUR], changes=dict([[1, "Старое поле категории 1"], [2, "Поле категории 1"]]) )) ) \ .add_category( lambda c: c .set_properties( displayName="Category2", name="Category2" ) .add_array( lambda ca: ca .set_properties( name="Array1", displayName="Array 1" ) .add_field(Field( name="Field1", displayName="Поле 1", value="Значение 1", type=FieldType.String )) ) ) \ .to_json() expected = "{\"0\":{\"fn\":\"Category1\",\"ft\":\"Category\",\"fv\":{\"0\":{\"fn\":\"Field1\",\"ft\":\"Price\",\"fv\":124124.54,\"fdn\":\"Поле категории 1\",\"md\":[\"CurEUR\"],\"ch\":{\"1\":\"Старое поле категории 1\",\"2\":\"Поле категории 1\"}}},\"fdn\":\"Категория 1\",\"md\":[\"Calendar\",\"Help\"]},\"1\":{\"fn\":\"Category2\",\"ft\":\"Category\",\"fv\":{\"0\":{\"fn\":\"Array1\",\"ft\":\"Array\",\"fv\":{\"0\":{\"fn\":\"Field1\",\"ft\":\"String\",\"fv\":\"Значение 1\",\"fdn\":\"Поле 1\"}},\"fdn\":\"Array 1\"}},\"fdn\":\"Category2\"},\"general\":{\"customers\":[]}}" self.assertEqual(result, expected)
def test_validate_valid(self): errors = Customer() \ .add_field(Field( name="field", displayName="Field 1", type=FieldType.Date, value=1, )) \ .validate("parent") self.assertEqual(len(errors), 0)
def test_validate_invalid_child(self): errors = Customer(customer_name='customer') \ .add_field(Field( name="field", type=FieldType.Date, value=1, )) \ .validate("parent") self.assertEqual(len(errors), 1) self.assertTrue( all([e.startswith("parent.customer.field:") for e in errors]))
def test_from_dict_valid(self): obj1_dict = Customer() \ .set_properties(customer_name="customer") \ .add_field(Field( name="field", displayName="Field 1", type=FieldType.Date, value=1, )) \ .to_dict() clean_dict = loads( dumps(obj1_dict, ensure_ascii=False), encoding='utf-8') obj2 = Customer().from_dict(clean_dict) obj2_dict = obj2.to_dict() self.assertEqual(obj2.name, 'customer') self.assertDictEqual(obj1_dict, obj2_dict)
def test_from_dict_return_valid_dict(self): obj1 = Field(name="Price", type=FieldType.Integer, value=100, displayName="Цена") obj1_dict = obj1.to_dict() clean_dict = loads(dumps(obj1_dict, ensure_ascii=False), encoding="utf-8") obj2 = Field().from_dict(clean_dict) obj2_dict = obj2.to_dict() self.assertDictEqual(obj1_dict, obj2_dict)
def test_from_json_with_customers(self): customers = [ dict(regnum=str(1), guid="", name="test1", maxPrice=50325235.53, guaranteeApp=95830945.54), dict(regnum=str(2), guid="", name="test2", maxPrice=20325235.53, guaranteeApp=35830945.54) ] json1 = Root() \ .add_customers( customers, lambda obj, c: c .set_properties( max_price=obj['maxPrice'], guarantee_app=obj['guaranteeApp'], customer_name=obj['name'], customer_guid=obj['guid'] ) .add_field(Field( value="1", name="test1", displayName="fdn", type=FieldType.String )) ) \ .to_json() json2 = Root().from_json(json1).to_json() self.assertEqual(json1, json2)
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_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_validate_parent(self): errors = Field().validate("parent") self.assertEqual(len(errors), 3) self.assertTrue(all([e.startswith("parent.?: ") for e in errors]))
def test_validate_name_specified_with_parent(self): errors = Field(name="child").validate("parent") self.assertEqual(len(errors), 2) self.assertTrue(all([e.startswith("parent.child: ") for e in errors]))
def test_to_dict_returns_none_when_value_not_set(self): obj = Field() self.assertEqual(obj.to_dict(), None)
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)