Ejemplo n.º 1
0
	def test_cascading(self):
		class Bar(flatty.Schema):
			first_name = None
			last_name = None
					
		class Foo(flatty.Schema):
			a = None
			b = Bar
			
		foo1 = Foo(a = 5, b = Bar(
								first_name="Chris",
								last_name="Young"
								)
				)
		
		flatted = flatty.flatit(foo1)
		obj1 = flatty.unflatit(Foo, flatted)
		
		self.assertTrue(is_plain_dict(flatted))
		self.assertTrue(isinstance(obj1, Foo))
		self.assertTrue(isinstance(obj1.a, int))
		self.assertTrue(isinstance(obj1.b, Bar))
		self.assertEqual(obj1.a, 5)
		self.assertEqual(foo1.a, obj1.a)
		self.assertEqual(foo1.b.first_name, obj1.b.first_name)
		self.assertEqual(foo1.b.last_name, obj1.b.last_name)
		self.assertEqual(foo1.b.first_name, "Chris")
		self.assertEqual(foo1.b.last_name, "Young")
		
		#Testing for sideeffects when doing a second instance
		foo2 = Foo(a = 3, b = Bar(
								first_name="Karin",
								last_name="Mayer"
								)
				)
		flatted = flatty.flatit(foo2)
		obj2 = flatty.unflatit(Foo, flatted)
		
		self.assertTrue(is_plain_dict(flatted))
		self.assertTrue(isinstance(obj2, Foo))
		self.assertTrue(isinstance(obj2.a, int))
		self.assertTrue(isinstance(obj2.b, Bar))
		self.assertEqual(obj2.a, 3)
		self.assertEqual(foo2.a, obj2.a)
		self.assertEqual(foo2.b.first_name, obj2.b.first_name)
		self.assertEqual(foo2.b.last_name, obj2.b.last_name)
		self.assertEqual(foo2.b.first_name, "Karin")
		self.assertEqual(foo2.b.last_name, "Mayer")
Ejemplo n.º 2
0
    def test_cascading(self):
        class Bar(flatty.Schema):
            first_name = None
            last_name = None

        class Foo(flatty.Schema):
            a = None
            b = Bar

        foo1 = Foo(a=5, b=Bar(first_name="Chris", last_name="Young"))

        flatted = flatty.flatit(foo1)
        obj1 = flatty.unflatit(Foo, flatted)

        self.assertTrue(is_plain_dict(flatted))
        self.assertTrue(isinstance(obj1, Foo))
        self.assertTrue(isinstance(obj1.a, int))
        self.assertTrue(isinstance(obj1.b, Bar))
        self.assertEqual(obj1.a, 5)
        self.assertEqual(foo1.a, obj1.a)
        self.assertEqual(foo1.b.first_name, obj1.b.first_name)
        self.assertEqual(foo1.b.last_name, obj1.b.last_name)
        self.assertEqual(foo1.b.first_name, "Chris")
        self.assertEqual(foo1.b.last_name, "Young")

        #Testing for sideeffects when doing a second instance
        foo2 = Foo(a=3, b=Bar(first_name="Karin", last_name="Mayer"))
        flatted = flatty.flatit(foo2)
        obj2 = flatty.unflatit(Foo, flatted)

        self.assertTrue(is_plain_dict(flatted))
        self.assertTrue(isinstance(obj2, Foo))
        self.assertTrue(isinstance(obj2.a, int))
        self.assertTrue(isinstance(obj2.b, Bar))
        self.assertEqual(obj2.a, 3)
        self.assertEqual(foo2.a, obj2.a)
        self.assertEqual(foo2.b.first_name, obj2.b.first_name)
        self.assertEqual(foo2.b.last_name, obj2.b.last_name)
        self.assertEqual(foo2.b.first_name, "Karin")
        self.assertEqual(foo2.b.last_name, "Mayer")
Ejemplo n.º 3
0
	def test_default_values(self):
		class Country(flatty.Schema):
			desc = str
			pop = int(7)
			num_rivers = 231
		
		class World(flatty.Schema):
			countries = flatty.TypedDict.set_type(Country)
			
		world = World(countries={'austria':Country()})
		self.assertEqual(world.countries['austria'].pop, 7)
		self.assertEqual(world.countries['austria'].num_rivers, 231)
		
		world2 = World(countries={'hungary':Country(pop=20, num_rivers =10)})
		self.assertEqual(world.countries['austria'].pop, 7)
		self.assertEqual(world.countries['austria'].num_rivers, 231)
		self.assertEqual(world2.countries['hungary'].pop, 20)
		self.assertEqual(world2.countries['hungary'].num_rivers, 10)
		
		flat_dict = flatty.flatit(world)
		self.assertTrue(is_plain_dict(flat_dict))
		restored_world = flatty.unflatit(World, flat_dict)
		self.assertEqual(restored_world.countries['austria'].pop, 7)
		self.assertEqual(restored_world.countries['austria'].num_rivers, 231)
		flat_dict2 = flatty.flatit(restored_world)
		self.assertEqual(flat_dict, flat_dict2)
		self.assertTrue(is_plain_dict(flat_dict2))
		
		
		flat_dict = flatty.flatit(world2)
		self.assertTrue(is_plain_dict(flat_dict))
		restored_world = flatty.unflatit(World, flat_dict)
		self.assertEqual(restored_world.countries['hungary'].pop, 20)
		self.assertEqual(restored_world.countries['hungary'].num_rivers, 10)
		flat_dict2 = flatty.flatit(restored_world)
		self.assertTrue(is_plain_dict(flat_dict2))
		self.assertEqual(flat_dict, flat_dict2)
Ejemplo n.º 4
0
    def test_default_values(self):
        class Country(flatty.Schema):
            desc = str
            pop = int(7)
            num_rivers = 231

        class World(flatty.Schema):
            countries = flatty.TypedDict.set_type(Country)

        world = World(countries={'austria': Country()})
        self.assertEqual(world.countries['austria'].pop, 7)
        self.assertEqual(world.countries['austria'].num_rivers, 231)

        world2 = World(countries={'hungary': Country(pop=20, num_rivers=10)})
        self.assertEqual(world.countries['austria'].pop, 7)
        self.assertEqual(world.countries['austria'].num_rivers, 231)
        self.assertEqual(world2.countries['hungary'].pop, 20)
        self.assertEqual(world2.countries['hungary'].num_rivers, 10)

        flat_dict = flatty.flatit(world)
        self.assertTrue(is_plain_dict(flat_dict))
        restored_world = flatty.unflatit(World, flat_dict)
        self.assertEqual(restored_world.countries['austria'].pop, 7)
        self.assertEqual(restored_world.countries['austria'].num_rivers, 231)
        flat_dict2 = flatty.flatit(restored_world)
        self.assertEqual(flat_dict, flat_dict2)
        self.assertTrue(is_plain_dict(flat_dict2))

        flat_dict = flatty.flatit(world2)
        self.assertTrue(is_plain_dict(flat_dict))
        restored_world = flatty.unflatit(World, flat_dict)
        self.assertEqual(restored_world.countries['hungary'].pop, 20)
        self.assertEqual(restored_world.countries['hungary'].num_rivers, 10)
        flat_dict2 = flatty.flatit(restored_world)
        self.assertTrue(is_plain_dict(flat_dict2))
        self.assertEqual(flat_dict, flat_dict2)
Ejemplo n.º 5
0
	def test_typed_dict(self):
		class Name(flatty.Schema):
			first_name = None
			last_name = None
					
		class Foo(flatty.Schema):
			a = None
			b = flatty.TypedDict.set_type(Name)
		
		t1 = Foo()
		t1.a ="hallo"
		
		l1 = Name()
		l1.first_name = "hans"
		l1.last_name = "conrad"
		
		l2 = Name()
		l2.first_name = "karl"
		l2.last_name = "hirsch"
		
		t1.b = {}
		t1.b['first'] =l1
		t1.b['second'] =l2
		  
		flat_dict = flatty.flatit(t1)
		n1 = flatty.unflatit(Foo, flat_dict)
		
		self.assertTrue(is_plain_dict(flat_dict))
		self.assertEqual(t1.a, n1.a)
		self.assertEqual(len(t1.b), len(n1.b))
		self.assertTrue(isinstance(n1.b, dict))
		self.assertTrue(str(type(n1.b)) == str(flatty.TypedDict))
		self.assertTrue(isinstance(n1.b, flatty.TypedDict))
		
		for k,v in t1.b.items():
			self.assertTrue(isinstance(n1.b[k], Name))
			self.assertEqual(t1.b[k].first_name, n1.b[k].first_name)
			self.assertEqual(t1.b[k].last_name, n1.b[k].last_name)
			
		#reflattening and compare if results doesn't change
		flat_dict2 = flatty.flatit(n1)
		self.assertEqual(flat_dict, flat_dict2)
Ejemplo n.º 6
0
    def test_typed_dict(self):
        class Name(flatty.Schema):
            first_name = None
            last_name = None

        class Foo(flatty.Schema):
            a = None
            b = flatty.TypedDict.set_type(Name)

        t1 = Foo()
        t1.a = "hallo"

        l1 = Name()
        l1.first_name = "hans"
        l1.last_name = "conrad"

        l2 = Name()
        l2.first_name = "karl"
        l2.last_name = "hirsch"

        t1.b = {}
        t1.b['first'] = l1
        t1.b['second'] = l2

        flat_dict = flatty.flatit(t1)
        n1 = flatty.unflatit(Foo, flat_dict)

        self.assertTrue(is_plain_dict(flat_dict))
        self.assertEqual(t1.a, n1.a)
        self.assertEqual(len(t1.b), len(n1.b))
        self.assertTrue(isinstance(n1.b, dict))
        self.assertTrue(str(type(n1.b)) == str(flatty.TypedDict))
        self.assertTrue(isinstance(n1.b, flatty.TypedDict))

        for k, v in list(t1.b.items()):
            self.assertTrue(isinstance(n1.b[k], Name))
            self.assertEqual(t1.b[k].first_name, n1.b[k].first_name)
            self.assertEqual(t1.b[k].last_name, n1.b[k].last_name)

        #reflattening and compare if results doesn't change
        flat_dict2 = flatty.flatit(n1)
        self.assertEqual(flat_dict, flat_dict2)
Ejemplo n.º 7
0
	def test_deep_nested_classes(self):
		class Region(flatty.Schema):
			name = str
		
		class Country(flatty.Schema):
			size = int
			regions = flatty.TypedList.set_type(Region)
		
		class World(flatty.Schema):
			countries = flatty.TypedDict.set_type(Country)
		
		regions = [Region(name='styria'),Region(name='carinthia')]
		countries = {'austria':Country(size=7,regions=regions)}
		world = World(countries = countries)
		
		flat_dict = flatty.flatit(world)
		self.assertTrue(is_plain_dict(flat_dict))
		world2 = flatty.unflatit(World, flat_dict)
		flat_dict2 = flatty.flatit(world2)
		self.assertEqual(flat_dict, flat_dict2)
Ejemplo n.º 8
0
    def test_deep_nested_classes(self):
        class Region(flatty.Schema):
            name = str

        class Country(flatty.Schema):
            size = int
            regions = flatty.TypedList.set_type(Region)

        class World(flatty.Schema):
            countries = flatty.TypedDict.set_type(Country)

        regions = [Region(name='styria'), Region(name='carinthia')]
        countries = {'austria': Country(size=7, regions=regions)}
        world = World(countries=countries)

        flat_dict = flatty.flatit(world)
        self.assertTrue(is_plain_dict(flat_dict))
        world2 = flatty.unflatit(World, flat_dict)
        flat_dict2 = flatty.flatit(world2)
        self.assertEqual(flat_dict, flat_dict2)
Ejemplo n.º 9
0
	def test_typed_list(self):
		class Name(flatty.Schema):
			first_name = None
			last_name = None
					
		class Foo(flatty.Schema):
			a = None
			b = flatty.TypedList.set_type(Name)
			
		t1 = Foo()
		t1.a ="hallo"
		
		l1 = Name()
		l1.first_name = "hans"
		l1.last_name = "conrad"
		
		l2 = Name()
		l2.first_name = "karl"
		l2.last_name = "hirsch"
		
		t1.b = []
		t1.b.append(l1)
		t1.b.append(l2)
		  
		marsh = flatty.flatit(t1)
		n1 = flatty.unflatit(Foo, marsh)
		self.assertTrue(is_plain_dict(marsh))
		self.assertEqual(t1.a, n1.a)
		self.assertEqual(len(t1.b), len(n1.b))
		self.assertTrue(isinstance(n1.b, list))
		self.assertTrue(isinstance(n1.b,flatty.TypedList))
		
		for i in range(len(t1.b)):
			self.assertTrue(isinstance(n1.b[i], Name))
			self.assertEqual(t1.b[i].first_name, n1.b[i].first_name)
			self.assertEqual(t1.b[i].last_name, n1.b[i].last_name)
			
		#reflattening and compare if results doesn't change
		marsh2 = flatty.flatit(n1)
		self.assertEqual(marsh, marsh2)
Ejemplo n.º 10
0
    def test_typed_list(self):
        class Name(flatty.Schema):
            first_name = None
            last_name = None

        class Foo(flatty.Schema):
            a = None
            b = flatty.TypedList.set_type(Name)

        t1 = Foo()
        t1.a = "hallo"

        l1 = Name()
        l1.first_name = "hans"
        l1.last_name = "conrad"

        l2 = Name()
        l2.first_name = "karl"
        l2.last_name = "hirsch"

        t1.b = []
        t1.b.append(l1)
        t1.b.append(l2)

        marsh = flatty.flatit(t1)
        n1 = flatty.unflatit(Foo, marsh)
        self.assertTrue(is_plain_dict(marsh))
        self.assertEqual(t1.a, n1.a)
        self.assertEqual(len(t1.b), len(n1.b))
        self.assertTrue(isinstance(n1.b, list))
        self.assertTrue(isinstance(n1.b, flatty.TypedList))

        for i in range(len(t1.b)):
            self.assertTrue(isinstance(n1.b[i], Name))
            self.assertEqual(t1.b[i].first_name, n1.b[i].first_name)
            self.assertEqual(t1.b[i].last_name, n1.b[i].last_name)

        #reflattening and compare if results doesn't change
        marsh2 = flatty.flatit(n1)
        self.assertEqual(marsh, marsh2)