Esempio n. 1
0
    def test_bases(self):
        class A:
            pass

        class B(A):
            pass

        class C(B):
            pass

        a = self._register(ClassAlias(A, 'a', defer=True))
        b = self._register(ClassAlias(B, 'b', defer=True))
        c = self._register(ClassAlias(C, 'c', defer=True))

        self.assertEqual(a.bases, None)
        self.assertEqual(b.bases, None)
        self.assertEqual(c.bases, None)

        a.compile()
        self.assertEqual(a.bases, [])

        b.compile()
        self.assertEqual(a.bases, [])
        self.assertEqual(b.bases, [(A, a)])

        c.compile()
        self.assertEqual(a.bases, [])
        self.assertEqual(b.bases, [(A, a)])
        self.assertEqual(c.bases, [(B, b), (A, a)])
Esempio n. 2
0
    def test_readonly_classic(self):
        class A:
            pass

        class B(A):
            pass

        class C(B):
            pass

        a = self._register(
            ClassAlias(A, 'a', readonly_attrs=['foo'], defer=True))
        b = self._register(ClassAlias(B, 'b', defer=True))
        c = self._register(
            ClassAlias(C, 'c', readonly_attrs=['bar'], defer=True))

        self.assertFalse(a._compiled)
        self.assertFalse(b._compiled)
        self.assertFalse(c._compiled)

        c.compile()

        self.assertTrue(a._compiled)
        self.assertTrue(b._compiled)
        self.assertTrue(c._compiled)

        self.assertEqual(a.readonly_attrs, ['foo'])
        self.assertEqual(b.readonly_attrs, ['foo'])
        self.assertEqual(c.readonly_attrs, ['bar', 'foo'])
Esempio n. 3
0
    def test_properties(self):
        class A:
            a_rw = property(lambda _: None, lambda _, x: None)
            a_ro = property(lambda _: None)

        class B(A):
            b_rw = property(lambda _: None, lambda _, x: None)
            b_ro = property(lambda _: None)

        class C(B):
            pass

        a = ClassAlias(A)

        self.assertTrue(a.dynamic)
        self.assertEqual(a.encodable_properties, ['a_ro', 'a_rw'])
        self.assertEqual(a.decodable_properties, ['a_rw'])

        b = ClassAlias(B)

        self.assertTrue(b.dynamic)
        self.assertEqual(b.encodable_properties,
                         ['a_ro', 'a_rw', 'b_ro', 'b_rw'])
        self.assertEqual(b.decodable_properties, ['a_rw', 'b_rw'])

        c = ClassAlias(C)

        self.assertTrue(c.dynamic)
        self.assertEqual(c.encodable_properties,
                         ['a_ro', 'a_rw', 'b_ro', 'b_rw'])
        self.assertEqual(c.decodable_properties, ['a_rw', 'b_rw'])
Esempio n. 4
0
    def test_static_new(self):
        class A(object):
            pass

        class B(A):
            pass

        class C(B):
            pass

        a = self._register(ClassAlias(A, 'a', static_attrs=['foo'],
                                      defer=True))
        b = self._register(ClassAlias(B, 'b', defer=True))
        c = self._register(ClassAlias(C, 'c', static_attrs=['bar'],
                                      defer=True))

        self.assertFalse(a._compiled)
        self.assertFalse(b._compiled)
        self.assertFalse(c._compiled)

        c.compile()

        self.assertTrue(a._compiled)
        self.assertTrue(b._compiled)
        self.assertTrue(c._compiled)

        self.assertEqual(a.static_attrs, ['foo'])
        self.assertEqual(b.static_attrs, ['foo'])
        self.assertEqual(c.static_attrs, ['bar', 'foo'])
Esempio n. 5
0
    def test_amf3(self):
        class A:
            pass

        class B(A):
            pass

        class C(B):
            pass

        a = self._register(ClassAlias(A, 'a', amf3=True, defer=True))
        b = self._register(ClassAlias(B, 'b', defer=True))
        c = self._register(ClassAlias(C, 'c', amf3=False, defer=True))

        self.assertFalse(a._compiled)
        self.assertFalse(b._compiled)
        self.assertFalse(c._compiled)

        c.compile()

        self.assertTrue(a._compiled)
        self.assertTrue(b._compiled)
        self.assertTrue(c._compiled)

        self.assertTrue(a.amf3)
        self.assertTrue(b.amf3)
        self.assertFalse(c.amf3)
Esempio n. 6
0
    def test_dynamic(self):
        class A:
            pass

        class B(A):
            pass

        class C(B):
            pass

        a = self._register(ClassAlias(A, 'a', dynamic=False, defer=True))
        b = self._register(ClassAlias(B, 'b', defer=True))
        c = self._register(ClassAlias(C, 'c', dynamic=True, defer=True))

        self.assertFalse(a._compiled)
        self.assertFalse(b._compiled)
        self.assertFalse(c._compiled)

        c.compile()

        self.assertTrue(a._compiled)
        self.assertTrue(b._compiled)
        self.assertTrue(c._compiled)

        self.assertFalse(a.dynamic)
        self.assertFalse(b.dynamic)
        self.assertTrue(c.dynamic)
Esempio n. 7
0
    def test_synonym_attrs(self):
        class A(object):
            pass

        class B(A):
            pass

        class C(B):
            pass

        a = self._register(
            ClassAlias(A,
                       'a',
                       synonym_attrs={
                           'foo': 'bar',
                           'bar': 'baz'
                       },
                       defer=True))
        b = self._register(ClassAlias(B, 'b', defer=True))
        c = self._register(
            ClassAlias(C, 'c', synonym_attrs={'bar': 'spam'}, defer=True))

        self.assertFalse(a._compiled)
        self.assertFalse(b._compiled)
        self.assertFalse(c._compiled)

        c.compile()

        self.assertTrue(a._compiled)
        self.assertTrue(b._compiled)
        self.assertTrue(c._compiled)

        self.assertEqual(a.synonym_attrs, {'foo': 'bar', 'bar': 'baz'})
        self.assertEqual(b.synonym_attrs, {'foo': 'bar', 'bar': 'baz'})
        self.assertEqual(c.synonym_attrs, {'foo': 'bar', 'bar': 'spam'})
Esempio n. 8
0
    def test_synonym_attrs(self):
        x = ClassAlias(Spam, synonym_attrs={'foo': 'bar'}, defer=True)

        self.assertEqual(x.synonym_attrs, {'foo': 'bar'})

        x.compile()

        self.assertEqual(x.synonym_attrs, {'foo': 'bar'})
Esempio n. 9
0
    def test_readonly(self):
        x = ClassAlias(Spam, readonly_attrs=['foo', 'bar'], defer=True)

        self.assertEqual(x.readonly_attrs, ['foo', 'bar'])

        x.compile()

        self.assertEqual(x.readonly_attrs, ['bar', 'foo'])
Esempio n. 10
0
    def test_readonly(self):
        x = ClassAlias(Spam, readonly_attrs=['foo', 'bar'], defer=True)

        self.assertEqual(x.readonly_attrs, ['foo', 'bar'])

        x.compile()

        self.assertEqual(x.readonly_attrs, ['bar', 'foo'])
Esempio n. 11
0
    def test_synonym_attrs(self):
        x = ClassAlias(Spam, synonym_attrs={'foo': 'bar'}, defer=True)

        self.assertEquals(x.synonym_attrs, {'foo': 'bar'})

        x.compile()

        self.assertEquals(x.synonym_attrs, {'foo': 'bar'})
Esempio n. 12
0
    def test_static(self):
        x = ClassAlias(Spam, static_attrs=['foo', 'bar'], defer=True)

        self.assertEqual(x.static_attrs, ['foo', 'bar'])

        x.compile()

        self.assertEqual(x.static_attrs, ['bar', 'foo'])
Esempio n. 13
0
    def test_static(self):
        x = ClassAlias(Spam, static_attrs=['foo', 'bar'], defer=True)

        self.assertEqual(x.static_attrs, ['foo', 'bar'])

        x.compile()

        self.assertEqual(x.static_attrs, ['foo', 'bar'])
Esempio n. 14
0
    def test_exclude(self):
        x = ClassAlias(Spam, exclude_attrs=['foo', 'bar'], defer=True)

        self.assertEqual(x.exclude_attrs, ['foo', 'bar'])

        x.compile()

        self.assertEqual(x.exclude_attrs, ['bar', 'foo'])
Esempio n. 15
0
    def test_exclude(self):
        x = ClassAlias(Spam, exclude_attrs=['foo', 'bar'], defer=True)

        self.assertEqual(x.exclude_attrs, ['foo', 'bar'])

        x.compile()

        self.assertEqual(x.exclude_attrs, ['bar', 'foo'])
Esempio n. 16
0
    def test_dynamic(self):
        x = ClassAlias(Spam, dynamic=True)
        self.assertTrue(x.dynamic)

        x = ClassAlias(Spam, dynamic=False)
        self.assertFalse(x.dynamic)

        x = ClassAlias(Spam)
        self.assertTrue(x.dynamic)
Esempio n. 17
0
    def test_anonymous(self):
        x = ClassAlias(Spam, None)

        x.compile()

        self.assertTrue(x.anonymous)
        self.assertTrue(x._compiled)

        self.assertEqual(x.klass, Spam)
        self.assertEqual(x.alias, '')
Esempio n. 18
0
    def test_anonymous(self):
        x = ClassAlias(Spam, None)

        x.compile()

        self.assertTrue(x.anonymous)
        self.assertTrue(x._compiled)

        self.assertEqual(x.klass, Spam)
        self.assertEqual(x.alias, '')
Esempio n. 19
0
    def test_compiled(self):
        x = ClassAlias(Spam, defer=True)

        self.assertFalse(x._compiled)

        x._compiled = True
        o = x.static_properties = object()

        x.compile()

        self.assertTrue(o is x.static_properties)
Esempio n. 20
0
    def test_compiled(self):
        x = ClassAlias(Spam, defer=True)

        self.assertFalse(x._compiled)

        x._compiled = True
        o = x.static_properties = object()

        x.compile()

        self.assertTrue(o is x.static_properties)
Esempio n. 21
0
    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'})
Esempio n. 22
0
    def test_eq(self):
        class A(object):
            pass

        class B(object):
            pass

        x = ClassAlias(A, 'org.example.A')
        y = ClassAlias(A, 'org.example.A')
        z = ClassAlias(B, 'org.example.B')

        self.assertEqual(x, A)
        self.assertEqual(x, y)
        self.assertNotEqual(x, z)
Esempio n. 23
0
    def test_str(self):
        class Eggs(object):
            pass

        x = ClassAlias(Eggs, 'org.example.eggs.Eggs')

        self.assertEqual(str(x), 'org.example.eggs.Eggs')
Esempio n. 24
0
    def test_init_kwargs(self):
        x = ClassAlias(Spam,
                       alias='foo',
                       static_attrs=('bar', ),
                       exclude_attrs=('baz', ),
                       readonly_attrs='gak',
                       amf3='spam',
                       external='eggs',
                       dynamic='goo',
                       proxy_attrs=('blarg', ))

        self.assertFalse(x.anonymous)
        self.assertEqual(x.dynamic, 'goo')
        self.assertEqual(x.amf3, 'spam')
        self.assertEqual(x.external, 'eggs')

        self.assertEqual(x.readonly_attrs, ['a', 'g', 'k'])
        self.assertEqual(x.static_attrs, ['bar'])
        self.assertEqual(x.exclude_attrs, ['baz'])
        self.assertEqual(x.proxy_attrs, ['blarg'])

        self.assertEqual(x.alias, 'foo')
        self.assertEqual(x.klass, Spam)

        # compiled attributes
        self.assertEqual(x.encodable_properties, ['bar'])
        self.assertEqual(x.decodable_properties, ['bar'])
        self.assertTrue(x._compiled)
Esempio n. 25
0
    def test_sealed_external(self):
        class A(object):
            __slots__ = ('foo', )

            class __amf__:
                external = True

            def __readamf__(self, foo):
                pass

            def __writeamf__(self, foo):
                pass

        x = ClassAlias(A)

        x.compile()

        self.assertTrue(x.sealed)
Esempio n. 26
0
    def test_sealed_external(self):
        class A(object):
            __slots__ = ('foo',)

            class __amf__:
                external = True

            def __readamf__(self, foo):
                pass

            def __writeamf__(self, foo):
                pass

        x = ClassAlias(A)

        x.compile()

        self.assertTrue(x.sealed)
Esempio n. 27
0
    def test_init_args(self):
        class ClassicFoo:
            def __init__(self, foo, bar):
                pass

        class NewFoo(object):
            def __init__(self, foo, bar):
                pass

        self.assertRaises(TypeError, ClassAlias, ClassicFoo)
        ClassAlias(NewFoo)
Esempio n. 28
0
    def test_anonymous(self):
        alias = ClassAlias(Spam, defer=True)

        pyamf.CLASS_CACHE['foo'] = alias
        pyamf.CLASS_CACHE[Spam] = alias

        self.assertTrue(alias.anonymous)
        ret = pyamf.unregister_class(Spam)

        self.assertTrue('foo' in pyamf.CLASS_CACHE)
        self.assertFalse(Spam in pyamf.CLASS_CACHE)
        self.assertTrue(ret is alias)
Esempio n. 29
0
    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'})
Esempio n. 30
0
    def test_slots_new(self):
        class A(object):
            __slots__ = ('foo', 'bar')

        class B(A):
            __slots__ = ('gak', )

        class C(B):
            pass

        class D(C, B):
            __slots__ = ('spam', )

        a = ClassAlias(A)

        self.assertFalse(a.dynamic)
        self.assertEquals(a.encodable_properties, ['bar', 'foo'])
        self.assertEquals(a.decodable_properties, ['bar', 'foo'])

        b = ClassAlias(B)

        self.assertFalse(b.dynamic)
        self.assertEquals(b.encodable_properties, ['bar', 'foo', 'gak'])
        self.assertEquals(b.decodable_properties, ['bar', 'foo', 'gak'])

        c = ClassAlias(C)

        self.assertTrue(c.dynamic)
        self.assertEquals(c.encodable_properties, ['bar', 'foo', 'gak'])
        self.assertEquals(c.decodable_properties, ['bar', 'foo', 'gak'])

        d = ClassAlias(D)

        self.assertTrue(d.dynamic)
        self.assertEquals(d.encodable_properties,
                          ['bar', 'foo', 'gak', 'spam'])
        self.assertEquals(d.decodable_properties,
                          ['bar', 'foo', 'gak', 'spam'])
Esempio n. 31
0
    def test_init(self):
        x = ClassAlias(Spam)

        self.assertTrue(x.anonymous)
        self.assertTrue(x.dynamic)
        self.assertFalse(x.amf3)
        self.assertFalse(x.external)

        self.assertEqual(x.readonly_attrs, None)
        self.assertEqual(x.static_attrs, [])
        self.assertEqual(x.exclude_attrs, None)
        self.assertEqual(x.proxy_attrs, None)

        self.assertEqual(x.alias, '')
        self.assertEqual(x.klass, Spam)

        # compiled attributes
        self.assertEqual(x.decodable_properties, None)
        self.assertEqual(x.encodable_properties, None)
        self.assertTrue(x._compiled)
Esempio n. 32
0
    def test_init_deferred(self):
        """
        Test for initial deferred compliation
        """
        x = ClassAlias(Spam, defer=True)

        self.assertTrue(x.anonymous)
        self.assertEqual(x.dynamic, None)
        self.assertFalse(x.amf3)
        self.assertFalse(x.external)

        self.assertEqual(x.readonly_attrs, None)
        self.assertEqual(x.static_attrs, None)
        self.assertEqual(x.exclude_attrs, None)
        self.assertEqual(x.proxy_attrs, None)

        self.assertEqual(x.alias, '')
        self.assertEqual(x.klass, Spam)

        # compiled attributes
        self.assertFalse(hasattr(x, 'static_properties'))
        self.assertFalse(x._compiled)
Esempio n. 33
0
class GetEncodableAttributesTestCase(unittest.TestCase):
    """
    Tests for L{ClassAlias.getEncodableAttributes}
    """

    def setUp(self):
        self.alias = ClassAlias(Spam, 'foo', defer=True)
        self.obj = Spam()

    def test_empty(self):
        sa, da = self.alias.getEncodableAttributes(self.obj)

        self.assertEquals(sa, da, None)

    def test_static(self):
        self.alias.static_attrs = ['foo', 'bar']
        self.alias.compile()

        self.obj.foo = 'bar'
        # leave self.obj.bar
        self.assertFalse(hasattr(self.obj, 'bar'))

        sa, da = self.alias.getEncodableAttributes(self.obj)

        self.assertEquals(sa, {'foo': 'bar', 'bar': pyamf.Undefined})
        self.assertEquals(da, None)

    def test_not_dynamic(self):
        self.alias.compile()
        self.alias.dynamic = False

        self.assertEquals(self.alias.getEncodableAttributes(self.obj), (None, None))

    def test_dynamic(self):
        self.alias.compile()

        self.assertEquals(self.alias.encodable_properties, None)
        self.obj.foo = 'bar'
        self.obj.bar = 'foo'

        sa, da = self.alias.getEncodableAttributes(self.obj)
        self.assertEquals(sa, None)
        self.assertEquals(da, {'foo': 'bar', 'bar': 'foo'})

    def test_proxy(self):
        from pyamf import flex

        self.alias.amf3 = True
        self.alias.proxy_attrs = ('foo', 'bar')
        self.alias.compile()

        self.assertEquals(self.alias.proxy_attrs, ['bar', 'foo'])

        self.obj.foo = ['bar', 'baz']
        self.obj.bar = {'foo': 'gak'}

        sa, da = self.alias.getEncodableAttributes(self.obj)
        self.assertEquals(sa, None)
        self.assertEquals(da.keys(), ['foo', 'bar'])

        self.assertTrue(isinstance(da['foo'], flex.ArrayCollection))
        self.assertEquals(da['foo'], ['bar', 'baz'])

        self.assertTrue(isinstance(da['bar'], flex.ObjectProxy))
        self.assertEquals(da['bar']._amf_object, {'foo': 'gak'})
Esempio n. 34
0
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_proxy(self):
        from pyamf import flex

        c = pyamf.get_encoder(pyamf.AMF3)

        self.alias.proxy_attrs = ('foo', 'bar')
        self.alias.compile()

        self.assertEqual(self.alias.proxy_attrs, ['bar', 'foo'])

        attrs = {
            'foo': flex.ArrayCollection(['bar', 'baz']),
            'bar': flex.ObjectProxy({'foo': 'gak'})
        }

        ret = self.alias.getDecodableAttributes(self.obj, attrs, c)

        self.assertEqual(ret, {'foo': ['bar', 'baz'], 'bar': {'foo': 'gak'}})

    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'})
Esempio n. 35
0
    def test_createInstance(self):
        x = ClassAlias(Spam, 'org.example.spam.Spam')

        y = x.createInstance()

        self.assertTrue(isinstance(y, Spam))
Esempio n. 36
0
 def setUp(self):
     self.alias = ClassAlias(Spam, 'foo')
     self.obj = Spam()
Esempio n. 37
0
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 ..

        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.assertEquals(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_proxy(self):
        from pyamf import flex

        c = pyamf.get_encoder(pyamf.AMF3)

        self.alias.proxy_attrs = ('foo', 'bar')
        self.alias.compile()

        self.assertEqual(self.alias.proxy_attrs, ['bar', 'foo'])

        attrs = {
            'foo': flex.ArrayCollection(['bar', 'baz']),
            'bar': flex.ObjectProxy({'foo': 'gak'})
        }

        ret = self.alias.getDecodableAttributes(self.obj, attrs, c)

        self.assertEqual(ret, {
            'foo': ['bar', 'baz'],
            'bar': {'foo': 'gak'}
        })

    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 = {
            'foo': 'foo',
            'spam': 'eggs'
        }

        ret = self.alias.getDecodableAttributes(self.obj, attrs)

        self.assertEquals(ret, {'bar': 'foo', 'spam': 'eggs'})
Esempio n. 38
0
class GetEncodableAttributesTestCase(unittest.TestCase):
    """
    Tests for L{ClassAlias.getEncodableAttributes}
    """

    def setUp(self):
        self.alias = ClassAlias(Spam, 'foo', defer=True)
        self.obj = Spam()

    def test_empty(self):
        attrs = self.alias.getEncodableAttributes(self.obj)

        self.assertEqual(attrs, {})

    def test_static(self):
        self.alias.static_attrs = ['foo', 'bar']
        self.alias.compile()

        self.obj.foo = 'bar'
        # leave self.obj.bar
        self.assertFalse(hasattr(self.obj, 'bar'))

        attrs = self.alias.getEncodableAttributes(self.obj)

        self.assertEqual(attrs, {'foo': 'bar', 'bar': pyamf.Undefined})

    def test_not_dynamic(self):
        self.alias.compile()
        self.alias.dynamic = False

        self.assertEqual(self.alias.getEncodableAttributes(self.obj), {})

    def test_dynamic(self):
        self.alias.compile()

        self.assertEqual(self.alias.encodable_properties, None)
        self.obj.foo = 'bar'
        self.obj.bar = 'foo'

        attrs = self.alias.getEncodableAttributes(self.obj)
        self.assertEqual(attrs, {'foo': 'bar', 'bar': 'foo'})

    def test_proxy(self):
        from pyamf import flex

        c = pyamf.get_encoder(pyamf.AMF3)

        self.alias.proxy_attrs = ('foo', 'bar')
        self.alias.compile()

        self.assertEqual(self.alias.proxy_attrs, ['bar', 'foo'])

        self.obj.foo = ['bar', 'baz']
        self.obj.bar = {'foo': 'gak'}

        attrs = self.alias.getEncodableAttributes(self.obj, c)

        k = attrs.keys()

        k.sort()

        self.assertEqual(k, ['bar', 'foo'])

        self.assertTrue(isinstance(attrs['foo'], flex.ArrayCollection))
        self.assertEqual(attrs['foo'], ['bar', 'baz'])

        self.assertTrue(isinstance(attrs['bar'], flex.ObjectProxy))
        self.assertEqual(attrs['bar']._amf_object, {'foo': 'gak'})

    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)

        self.obj.foo = 'bar'
        self.obj.spam = 'eggs'

        ret = self.alias.getEncodableAttributes(self.obj)

        self.assertEquals(ret, {'bar': 'bar', 'spam': 'eggs'})
Esempio n. 39
0
 def setUp(self):
     self.alias = ClassAlias(Spam, 'foo', defer=True)
     self.obj = Spam()
Esempio n. 40
0
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'})
Esempio n. 41
0
 def setUp(self):
     self.alias = ClassAlias(Spam, 'foo', defer=True)
     self.obj = Spam()
Esempio n. 42
0
    def test_external(self):
        class A(object):
            pass

        class B:
            pass

        self.assertRaises(AttributeError, ClassAlias, A, external=True)
        self.assertRaises(AttributeError, ClassAlias, B, external=True)

        A.__readamf__ = None
        B.__readamf__ = None

        self.assertRaises(AttributeError, ClassAlias, A, external=True)
        self.assertRaises(AttributeError, ClassAlias, B, external=True)

        A.__readamf__ = lambda x: None
        B.__readamf__ = lambda x: None

        self.assertRaises(AttributeError, ClassAlias, A, external=True)
        self.assertRaises(AttributeError, ClassAlias, B, external=True)

        A.__writeamf__ = 'foo'
        B.__writeamf__ = 'bar'

        self.assertRaises(TypeError, ClassAlias, A, external=True)
        self.assertRaises(TypeError, ClassAlias, B, external=True)

        A.__writeamf__ = lambda x: None
        B.__writeamf__ = lambda x: None

        a = ClassAlias(A, external=True)
        b = ClassAlias(B, external=True)

        self.assertEqual(a.readonly_attrs, None)
        self.assertEqual(a.static_attrs, [])
        self.assertEqual(a.decodable_properties, None)
        self.assertEqual(a.encodable_properties, None)
        self.assertEqual(a.exclude_attrs, None)

        self.assertTrue(a.anonymous)
        self.assertTrue(a.external)
        self.assertTrue(a._compiled)

        self.assertEqual(a.klass, A)
        self.assertEqual(a.alias, '')

        # now b

        self.assertEqual(b.readonly_attrs, None)
        self.assertEqual(b.static_attrs, [])
        self.assertEqual(b.decodable_properties, None)
        self.assertEqual(b.encodable_properties, None)
        self.assertEqual(b.exclude_attrs, None)

        self.assertTrue(b.anonymous)
        self.assertTrue(b.external)
        self.assertTrue(b._compiled)

        self.assertEqual(b.klass, B)
        self.assertEqual(b.alias, '')
Esempio n. 43
0
 def test_amf3(self):
     x = ClassAlias(Spam, amf3=True)
     self.assertTrue(x.amf3)
Esempio n. 44
0
class GetEncodableAttributesTestCase(unittest.TestCase):
    """
    Tests for L{ClassAlias.getEncodableAttributes}
    """
    def setUp(self):
        self.alias = ClassAlias(Spam, 'foo', defer=True)
        self.obj = Spam()

    def test_empty(self):
        attrs = self.alias.getEncodableAttributes(self.obj)

        self.assertEqual(attrs, {})

    def test_static(self):
        self.alias.static_attrs = ['foo', 'bar']
        self.alias.compile()

        self.obj.foo = 'bar'
        # leave self.obj.bar
        self.assertFalse(hasattr(self.obj, 'bar'))

        attrs = self.alias.getEncodableAttributes(self.obj)

        self.assertEqual(attrs, {'foo': 'bar', 'bar': pyamf.Undefined})

    def test_not_dynamic(self):
        self.alias.compile()
        self.alias.dynamic = False

        self.assertEqual(self.alias.getEncodableAttributes(self.obj), {})

    def test_dynamic(self):
        self.alias.compile()

        self.assertEqual(self.alias.encodable_properties, None)
        self.obj.foo = 'bar'
        self.obj.bar = 'foo'

        attrs = self.alias.getEncodableAttributes(self.obj)
        self.assertEqual(attrs, {'foo': 'bar', 'bar': 'foo'})

    def test_proxy(self):
        from pyamf import flex

        c = pyamf.get_encoder(pyamf.AMF3)

        self.alias.proxy_attrs = ('foo', 'bar')
        self.alias.compile()

        self.assertEqual(self.alias.proxy_attrs, ['bar', 'foo'])

        self.obj.foo = ['bar', 'baz']
        self.obj.bar = {'foo': 'gak'}

        attrs = self.alias.getEncodableAttributes(self.obj, c)

        k = list(attrs.keys())

        k.sort()

        self.assertEqual(k, ['bar', 'foo'])

        self.assertTrue(isinstance(attrs['foo'], flex.ArrayCollection))
        self.assertEqual(attrs['foo'], ['bar', 'baz'])

        self.assertTrue(isinstance(attrs['bar'], flex.ObjectProxy))
        self.assertEqual(attrs['bar']._amf_object, {'foo': 'gak'})

    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)

        self.obj.foo = 'bar'
        self.obj.spam = 'eggs'

        ret = self.alias.getEncodableAttributes(self.obj)

        self.assertEqual(ret, {'bar': 'bar', 'spam': 'eggs'})
Esempio n. 45
0
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'})