class TwoToOne(Mapping): source_schema = Schema2 target_schema = Schema1 my_str = Get('peas') my_int = Get('carrots') another_str = Str(Get('beans'))
class SquarePegToRoundHole(Mapping): source_schema = SquarePeg target_schema = RoundHole id = Concat('from_square', ':', Str(Get('id'))) name = Get('name') diameter = Do(largest_square, Get('width'))
def test_can_mix_transformations(self): original = {"foo": 1, "bar": 2} concat_getter = Concat(Str(Get("foo")), ":", Str(Get("bar"))) result = concat_getter(original) expected = u"1:2" self.assertEqual(expected, result) int_concatter = Int(Concat("1", Str(Get("bar")), "3")) result = int_concatter(original) self.assertEqual(123, result)
class MyMap(Mapping): numbers = ManySubmap(Sub, Get('items'))
class Simpler(Mapping): numbers = Many(Int, Get('wow'))
class Hm(Mapping): wow = Int(Get('wow'), required=False)
class InnerNoschema(Mapping): wow = Get('something')
class Inner(Mapping): source_schema = SourceSub target_schema = TargetSub wow = Get('something')
class Inner(Mapping): source_schema = InnerSource target_schema = InnerTarget goal = Get("okay")
def test_can_get_from_dict(self): my_dict = {"path": "goal"} result = Get("path")(my_dict) self.assertEqual(result, "goal")
class ImpliesSchemas(Mapping): id = Concat('author', ':', Get('nom_de_plume')) name = Get('nom_de_plume') book = Get('best_known_for')
class MappingB(MappingA): target_schema = SchemaB root_veg = Get('carrots')
class MappingA(Mapping): source_schema = SchemaA legumes = Get('beans')
class Mymap(Mapping): target_schema = MySchema content = Get('post', 'caption', 'text', default="")
class Mymap(Mapping): source_schema = FirstSchema target_schema = OtherSchema cool = Get('wow') bad = Get('umm', default=3)
class Inner(Mapping): goal = Get("okay")
class Outer(Mapping): inner = Submapping(Inner, Get("nested"))
def test_can_nest(self): my_dict = {"path": {"subpath": "goal"}} result = Get("path", "subpath")(my_dict) self.assertEqual(result, "goal")
class Outer(Mapping): source_schema = OuterSource target_schema = OuterTarget inner = Submapping(Inner, Get("nested"))
def test_can_nest_deeply(self): my_dict = {"path": {"deep": {"deeper": {"deepest": "goal"}}}} result = Get("path", "deep", "deeper", "deepest")(my_dict) self.assertEqual(result, "goal")
class Outer(Mapping): source_schema = Source target_schema = Target inner = Submapping(Inner, Get("inner"))
def test_can_get_from_obj(self): class MyObj(object): path = "goal" result = Get("path")(MyObj()) self.assertEqual(result, "goal")
class OuterNoschema(Mapping): inner = Submapping(InnerNoschema, Get("inner"))
def test_default_is_not_required(self): my_dict = {"path": "goal"} result = Get("else")(my_dict) self.assertIsNone(result)
class MyMap(Mapping): source_schema = Source numbers = Many(Int, Get('together')) more_numbers = Many(Int, Get('sep_1'), Get('sep_2'), Const(6))
def test_mandatory_raises(self): my_dict = {"path": "goal"} with self.assertRaises(Missing): Get("other", required=True)(my_dict)
class Sub(Mapping): inner = Get('wow')
class Inner(Mapping): goal = Get("flat")
class Deep(Mapping): nested = ManySubmap(Sub, Get("one", "two"))
class OneToTwoBase(Mapping): peas = Get('my_str') carrots = Get('my_int') beans = Int(Get('another_str'))