class ApplyAttributesTestCase(unittest.TestCase): """ Tests for L{ClassAlias.applyAttributes} """ def setUp(self): self.alias = ClassAlias(Spam, 'foo', defer=True) self.obj = Spam() def test_object(self): class Foo(object): pass attrs = {'foo': 'spam', 'bar': 'eggs'} self.obj = Foo() self.alias = ClassAlias(Foo, 'foo', defer=True) self.assertEqual(self.obj.__dict__, {}) self.alias.applyAttributes(self.obj, attrs) self.assertEqual(self.obj.__dict__, {'foo': 'spam', 'bar': 'eggs'}) def test_classic(self): class Foo: pass attrs = {'foo': 'spam', 'bar': 'eggs'} self.obj = Foo() self.alias = ClassAlias(Foo, 'foo', defer=True) self.assertEqual(self.obj.__dict__, {}) self.alias.applyAttributes(self.obj, attrs) self.assertEqual(self.obj.__dict__, {'foo': 'spam', 'bar': 'eggs'}) def test_readonly(self): self.alias.readonly_attrs = ['foo', 'bar'] attrs = {'foo': 'spam', 'bar': 'eggs'} self.assertEqual(self.obj.__dict__, {}) self.alias.applyAttributes(self.obj, attrs) self.assertEqual(self.obj.__dict__, {}) def test_exclude(self): self.alias.exclude_attrs = ['foo', 'bar'] attrs = {'foo': 'spam', 'bar': 'eggs'} self.assertEqual(self.obj.__dict__, {}) self.alias.applyAttributes(self.obj, attrs) self.assertEqual(self.obj.__dict__, {}) def test_not_dynamic(self): self.alias.static_properties = None self.alias.dynamic = False attrs = {'foo': 'spam', 'bar': 'eggs'} self.assertEqual(self.obj.__dict__, {}) self.alias.applyAttributes(self.obj, attrs) self.assertEqual(self.obj.__dict__, {}) def test_dict(self): attrs = {'foo': 'spam', 'bar': 'eggs'} self.obj = Spam() self.assertEqual(self.obj.__dict__, {}) self.alias.applyAttributes(self.obj, attrs) self.assertEqual(self.obj.__dict__, {'foo': 'spam', 'bar': 'eggs'})
class GetDecodableAttributesTestCase(unittest.TestCase): """ Tests for L{ClassAlias.getDecodableAttributes} """ def setUp(self): self.alias = ClassAlias(Spam, 'foo', defer=True) self.obj = Spam() def test_compile(self): self.assertFalse(self.alias._compiled) self.alias.applyAttributes(self.obj, {}) self.assertTrue(self.alias._compiled) def test_missing_static_property(self): self.alias.static_attrs = ['foo', 'bar'] self.alias.compile() attrs = {'foo': None} # missing bar key .. with self.assertRaises(AttributeError): self.alias.getDecodableAttributes(self.obj, attrs) def test_no_static(self): self.alias.compile() attrs = {'foo': None, 'bar': [1, 2, 3]} ret = self.alias.getDecodableAttributes(self.obj, attrs) self.assertEqual(ret, {'foo': None, 'bar': [1, 2, 3]}) def test_readonly(self): self.alias.compile() self.alias.readonly_attrs = ['bar'] attrs = {'foo': None, 'bar': [1, 2, 3]} ret = self.alias.getDecodableAttributes(self.obj, attrs) self.assertEqual(ret, {'foo': None}) def test_not_dynamic(self): self.alias.compile() self.alias.decodable_properties = set(['bar']) self.alias.dynamic = False attrs = {'foo': None, 'bar': [1, 2, 3]} ret = self.alias.getDecodableAttributes(self.obj, attrs) self.assertEqual(ret, {'bar': [1, 2, 3]}) def test_dynamic(self): self.alias.compile() self.alias.static_properties = ['bar'] self.alias.dynamic = True attrs = {'foo': None, 'bar': [1, 2, 3]} ret = self.alias.getDecodableAttributes(self.obj, attrs) self.assertEqual(ret, {'foo': None, 'bar': [1, 2, 3]}) def test_complex(self): self.alias.compile() self.alias.static_properties = ['foo', 'bar'] self.alias.exclude_attrs = ['baz', 'gak'] self.alias.readonly_attrs = ['spam', 'eggs'] attrs = { 'foo': 'foo', 'bar': 'bar', 'baz': 'baz', 'gak': 'gak', 'spam': 'spam', 'eggs': 'eggs', 'dyn1': 'dyn1', 'dyn2': 'dyn2' } ret = self.alias.getDecodableAttributes(self.obj, attrs) self.assertEqual(ret, { 'foo': 'foo', 'bar': 'bar', 'dyn2': 'dyn2', 'dyn1': 'dyn1' }) def test_complex_not_dynamic(self): self.alias.compile() self.alias.decodable_properties = ['foo', 'bar'] self.alias.exclude_attrs = ['baz', 'gak'] self.alias.readonly_attrs = ['spam', 'eggs'] self.alias.dynamic = False attrs = { 'foo': 'foo', 'bar': 'bar', 'baz': 'baz', 'gak': 'gak', 'spam': 'spam', 'eggs': 'eggs', 'dyn1': 'dyn1', 'dyn2': 'dyn2' } ret = self.alias.getDecodableAttributes(self.obj, attrs) self.assertEqual(ret, {'foo': 'foo', 'bar': 'bar'}) def test_static(self): self.alias.dynamic = False self.alias.compile() self.alias.decodable_properties = set(['foo', 'bar']) attrs = { 'foo': 'foo', 'bar': 'bar', 'baz': 'baz', 'gak': 'gak', } ret = self.alias.getDecodableAttributes(self.obj, attrs) self.assertEqual(ret, {'foo': 'foo', 'bar': 'bar'}) def test_synonym(self): self.alias.synonym_attrs = {'foo': 'bar'} self.alias.compile() self.assertFalse(self.alias.shortcut_encode) self.assertFalse(self.alias.shortcut_decode) attrs = {'bar': 'foo', 'spam': 'eggs'} ret = self.alias.getDecodableAttributes(self.obj, attrs) self.assertEqual(ret, {'foo': 'foo', 'spam': 'eggs'}) def test_complex_synonym(self): self.alias.synonym_attrs = {'foo_syn': 'bar_syn'} self.alias.compile() self.alias.static_properties = [ 'foo_syn', ] self.alias.exclude_attrs = ['baz', 'gak'] self.alias.readonly_attrs = ['spam_rd_1', 'spam_rd_2'] self.assertFalse(self.alias.shortcut_encode) self.assertFalse(self.alias.shortcut_decode) attrs = { 'bar_syn': 'foo', 'spam': 'eggs', 'spam_rd_1': 'eggs', 'spam_rd_2': 'eggs', 'baz': 'remove me', 'gak': 'remove me' } ret = self.alias.getDecodableAttributes(self.obj, attrs) self.assertEqual(ret, {'foo_syn': 'foo', 'spam': 'eggs'})