def test_complex(self): test_json = { "apple": 1, "plum": [{ "a": { "c": 1 } }, { "a": { "c": 2 } }, { "a": { "c": 3 } }], "peach": { "newlist": { "results": [2, 3, 4] } } } value_path1 = "plum.a.c" value_path2 = "peach.newlist.results" self.assertEqual( Value(value_path1, True).getValue(test_json), [1, 2, 3], "incorrect result") self.assertEqual( Value(value_path2, True).getValue(test_json), [2, 3, 4], "incorrect result")
def test_one_level(self): test_json = {"apple": 1, "plum": 2, "peach": 3, "required": None} self.assertEqual(Value("apple", True).getValue(test_json), [1], "incorrect value") self.assertEqual(Value("apple", False).getValue(test_json), [1], "incorrect value") self.assertEqual(Value("pear", False).getValue(test_json), [None], "incorrect value") with self.assertRaises(KeyError): Value("pear", True).getValue(test_json) with self.assertRaises(ValueError): Value("required", True).getValue(test_json)
class ConcName(Flattener): __field_names__ = ["name", "age"] name = DerivedValue( concatenation, dict(lastName=Value("lastName", False), firstName=Value("firstName", False)))
class ListValues(Flattener): __field_names__ = [ "firstName", "lastName", "age", "phoneNumber", "phoneType" ] phoneNumber = ListValue(Value("phoneNumber.number", False)) phoneType = ListValue(Value("phoneNumber.type", False))
def test_derived_value(self): test_json = {"apple": 1, "plum": [1, 2, 3], "peach": 234} def func(a1, a2): return a1[0] * sum(a2) self.assertEqual(DerivedValue(func, dict(a1=Value("apple", False), a2=Value("plum", False))).getValue(test_json), [6])
def test_list_value(self): test_json = {"apple": 1, "plum": [1, 2, 3], "peach": 234} value_path = "plum" self.assertEqual(ListValue(Value(value_path, True)).getValue(test_json), [1,2,3], "incorrect result") test_json2 = {"apple": 1, "plum": [{"a": 1}, {"a": 2}, {"a": 3}], "peach": 234} value_path="plum.a" self.assertEqual(ListValue(Value(value_path, True)).getValue(test_json2), [1, 2, 3], "incorrect result")
class ListJsonFlattener(Flattener): __field_names__ = ["id", "name", "zip", "results_other_list"] results_other_list = ListValue( DerivedValue( func, dict(results=Value("results", False), other_list=Value("other_list", False))))
class Lines(Flattener): __field_names__ = { "object", "my_field_name", "date", "plan_id", "period_start", "period_end" } my_field_name = Value("amount_due", False) plan_id = ListValue(Value("lines.data.plan.id", False)) period_start = ListValue(Value("lines.data.period.start", False)) period_end = ListValue(Value("lines.data.period.end", False))
class Nested(Flattener): __field_names__ = [ "firstName", "lastName", "age", "streetAddress", "city", "state", "postalCode" ] streetAddress = Value("address.streetAddress", False) city = Value("address.city", False) state = Value("address.state", False) postalCode = Value("address.postalCode", False)
class MultipleLists(Flattener): __field_names__ = ["user", "timestamp", "food", "drink"] food = ListValue( DerivedValue( get_food, dict(food_order=Value("food_order", True), drink_order=Value("drink_order", True)))) drink = ListValue( DerivedValue( get_drink, dict(food_order=Value("food_order", True), drink_order=Value("drink_order", True))))
class NestedList(Flattener): __field_names__ = ["user", "date", "x_coord", "y_coord"] x_coord = ListValue( DerivedValue( get_x, dict(coordinates=Value("daily_movement.coordinates", False)))) y_coord = ListValue( DerivedValue( get_y, dict(coordinates=Value("daily_movement.coordinates", False))))
def test_multi_level(self): test_json = {"a": {"b": 1, "c": 2, "d": {"e": 3, "f": {"g": "h"}}}} value_path = "a.d.f.g" self.assertEqual( Value(value_path, True).getValue(test_json), ["h"], "incorrect result")
class ListJsonFlattener(Flattener): __field_names__ = ["id", "name", "zip", "results", "other_list"] results = ListValue(Value("results", False))
class ListJsonFlattener(Flattener): __field_names__ = {"id", "name", "zip", "results"} results = ListValue(Value("results", False))
class SimpleJsonFlattener(Flattener): __field_names__ = {"id", "name", "zip"} id = Value("name", False) name = Value("id", False)
class RenamedView(Flattener): __field_names__ = ["last", "first", "age"] last = Value("lastName", False) first = Value("firstName", False)