def test_typed_object(self): pyamf.register_class(Spam, alias="org.pyamf.spam") x = Spam() x.baz = "hello" self.assertEncoded(x, "\x10\x00\x0eorg.pyamf.spam\x00\x03baz" "\x02\x00\x05hello\x00\x00\t")
def test_loadInstanceFromDatastore(self): # not a class type self.assertRaises(TypeError, adapter_db.loadInstanceFromDatastore, chr, '') # not a subclass of db.Model/db.Expando self.assertRaises(TypeError, adapter_db.loadInstanceFromDatastore, Spam, '') # not a valid key type self.assertRaises(TypeError, adapter_db.loadInstanceFromDatastore, GettableModelStub, 2) self.assertEquals(GettableModelStub.gets, []) adapter_db.loadInstanceFromDatastore(GettableModelStub, 'foo', codec=None) self.assertEquals(GettableModelStub.gets, [[('foo', ), {}]]) codec = Spam() codec.context = Spam() GettableModelStub.gets = [] self.assertFalse(hasattr(codec.context, 'gae_objects')) adapter_db.loadInstanceFromDatastore(GettableModelStub, 'foo', codec=codec) self.assertTrue(hasattr(codec.context, 'gae_objects')) self.assertEquals(GettableModelStub.gets, [[('foo', ), {}]]) gae_objects = codec.context.gae_objects self.assertTrue( isinstance(gae_objects, adapter_db.GAEReferenceCollection)) self.assertEquals(gae_objects, {GettableModelStub: {'foo': None}})
def test_typed_object(self): pyamf.register_class(Spam, alias='org.pyamf.spam') x = Spam() x.baz = 'hello' self.assertEncoded(x, '\x10\x00\x0eorg.pyamf.spam\x00\x03baz' '\x02\x00\x05hello\x00\x00\t')
def test_force_amf3(self): alias = pyamf.register_class(Spam, 'spam.eggs') alias.amf3 = True x = Spam() x.x = 'y' self.assertEncoded(x, '\x11\n\x0b\x13spam.eggs\x03x\x06\x03y\x01')
def test_anonymous(self): pyamf.register_class(Spam) x = Spam() x.spam = "eggs" x.hello = "world" self.assertEncoded(x, "\x03", ("\x00\x05hello\x02\x00\x05world", "\x00\x04spam\x02\x00\x04eggs"), "\x00\x00\t")
def test_force_amf3(self): alias = pyamf.register_class(Spam, "spam.eggs") alias.amf3 = True x = Spam() x.x = "y" self.assertEncoded(x, "\x11\n\x0b\x13spam.eggs\x03x\x06\x03y\x01")
def test_force_amf3(self): pyamf.register_class(Spam, 'spam.eggs', metadata=['amf3']) x = Spam() x.x = 'y' self._run([ (x, '\x11\n\x0b\x13spam.eggs\x03x\x06\x03y\x01')])
def test_anonymous(self): pyamf.register_class(Spam) x = Spam() x.spam = 'eggs' x.hello = 'world' self._run([ (x, '\x03\x00\x05hello\x02\x00\x05world\x00\x04spam\x02\x00\x04eggs\x00\x00\t')])
def test_anonymous(self): pyamf.register_class(Spam) x = Spam() x.spam = 'eggs' x.hello = 'world' self.assertEncoded(x, '\x03', ('\x00\x05hello\x02\x00\x05world', '\x00\x04spam\x02\x00\x04eggs'), '\x00\x00\t')
def test_typed_object(self): pyamf.register_class(Spam, alias='org.pyamf.spam') x = Spam() x.baz = 'hello' self.assertEncoded( x, '\x10\x00\x0eorg.pyamf.spam\x00\x03baz' '\x02\x00\x05hello\x00\x00\t')
def test_dynamic(self): def attr_func(obj): self.assertTrue(isinstance(obj, Spam)) return ['hello'] x = Spam() x.foo = 'bar' x.hello = 'world' pyamf.register_class(Spam, attr_func=attr_func, metadata=['dynamic']) self._run([(x, '\x03\x00\x05hello\x02\x00\x05world\x00\x00\t')]) pyamf.unregister_class(Spam) # try duplicate attributes pyamf.register_class(Spam, attrs=['hello'], attr_func=attr_func, metadata=['dynamic']) self._run([(x, '\x03\x00\x05hello\x02\x00\x05world\x00\x00\t')]) pyamf.unregister_class(Spam) pyamf.register_class(Spam, attrs=['foo'], attr_func=attr_func, metadata=['dynamic']) self._run([(x, '\x03\x00\x03foo\x02\x00\x03bar\x00\x05hello\x02\x00' '\x05world\x00\x00\t')]) pyamf.unregister_class(Spam) # and now typedobject pyamf.register_class(Spam, 'x', attr_func=attr_func, metadata=['dynamic']) self._run([(x, '\x10\x00\x01x\x00\x05hello\x02\x00\x05world\x00\x00\t') ]) pyamf.unregister_class(Spam) pyamf.register_class(Spam, 'x', attrs=['hello'], attr_func=attr_func, metadata=['dynamic']) self._run([(x, '\x10\x00\x01x\x00\x05hello\x02\x00\x05world\x00\x00\t') ]) pyamf.unregister_class(Spam) pyamf.register_class(Spam, 'x', attrs=['foo'], attr_func=attr_func, metadata=['dynamic']) self._run([(x, '\x10\x00\x01x\x00\x03foo\x02\x00\x03bar\x00\x05hello' '\x02\x00\x05world\x00\x00\t')])
def test_anonymous(self): pyamf.register_class(Spam) x = Spam() x.spam = 'eggs' x.hello = 'world' self._run([(x, ('\x03', ('\x00\x05hello\x02\x00\x05world', '\x00\x04spam\x02\x00\x04eggs'), '\x00\x00\t'))])
def test_typed_object(self): pyamf.register_class(Spam, alias='org.pyamf.spam') x = Spam() x.baz = 'hello' self.encoder.writeElement(x) self.assertEquals(self.buf.getvalue(), '\x10\x00\x0eorg.pyamf.spam\x00\x03baz\x02\x00\x05hello\x00\x00\t')
def test_object(self): self.assertEncoded({'a': u'spam', 'b': 5}, '\n\x0b\x01\x03a\x06\tspam\x03b\x04\x05\x01') pyamf.register_class(Spam, 'org.pyamf.spam') obj = Spam() obj.baz = 'hello' self.assertEncoded(obj, '\n\x0b\x1dorg.pyamf.spam\x07baz\x06\x0bhello\x01')
def test_object(self): self.assertEncoded({'a': 'spam', 'b': 5}, '\n\x0b\x01\x03a\x06\tspam\x03b\x04\x05\x01') pyamf.register_class(Spam, 'org.pyamf.spam') obj = Spam() obj.baz = 'hello' self.assertEncoded(obj, '\n\x0b\x1dorg.pyamf.spam\x07baz\x06\x0bhello\x01')
def test_anonymous(self): pyamf.register_class(Spam) x = Spam() x.spam = 'eggs' x.hello = 'world' self.assertEncoded( x, b'\x03', (b'\x00\x05hello\x02\x00\x05world', b'\x00\x04spam\x02\x00\x04eggs'), b'\x00\x00\t' )
def test_dynamic(self): def attr_func(obj): self.assertTrue(isinstance(obj, Spam)) return ['hello'] x = Spam() x.foo = 'bar' x.hello = 'world' pyamf.register_class(Spam, attr_func=attr_func, metadata=['dynamic']) self._run([(x, '\x03\x00\x05hello\x02\x00\x05world\x00\x00\t')]) pyamf.unregister_class(Spam) # try duplicate attributes pyamf.register_class(Spam, attrs=['hello'], attr_func=attr_func, metadata=['dynamic']) self._run([(x, '\x03\x00\x05hello\x02\x00\x05world\x00\x00\t')]) pyamf.unregister_class(Spam) pyamf.register_class(Spam, attrs=['foo'], attr_func=attr_func, metadata=['dynamic']) self._run([ (x, ('\x03', ( '\x00\x03foo\x02\x00\x03bar', '\x00\x05hello\x02\x00\x05world' ), '\x00\x00\t'))]) pyamf.unregister_class(Spam) # and now typedobject pyamf.register_class(Spam, 'x', attr_func=attr_func, metadata=['dynamic']) self._run([(x, '\x10\x00\x01x\x00\x05hello\x02\x00\x05world\x00\x00\t')]) pyamf.unregister_class(Spam) pyamf.register_class(Spam, 'x', attrs=['hello'], attr_func=attr_func, metadata=['dynamic']) self._run([(x, '\x10\x00\x01x\x00\x05hello\x02\x00\x05world\x00\x00\t')]) pyamf.unregister_class(Spam) pyamf.register_class(Spam, 'x', attrs=['foo'], attr_func=attr_func, metadata=['dynamic']) self._run([ (x, ('\x10\x00\x01x', ( '\x00\x03foo\x02\x00\x03bar', '\x00\x05hello\x02\x00\x05world' ), '\x00\x00\t'))])
def test_dynamic_static(self): x = Spam() x.foo = "bar" x.hello = "world" alias = pyamf.register_class(Spam) alias.static_attrs = ["hello"] alias.compile() self.assertTrue(alias.dynamic) self.assertEncoded(x, "\x03", ("\x00\x05hello\x02\x00\x05world", "\x00\x03foo\x02\x00\x03bar"), "\x00\x00\t")
def test_dynamic(self): x = Spam() x.foo = 'bar' x.hello = 'world' alias = pyamf.register_class(Spam) alias.exclude_attrs = ['foo'] alias.compile() self.assertTrue(alias.dynamic) self.assertEncoded(x, '\x03\x00\x05hello\x02\x00\x05world\x00\x00\t')
def test_dynamic_static(self): x = Spam() x.foo = 'bar' x.hello = 'world' alias = pyamf.register_class(Spam) alias.static_attrs = ['hello'] alias.compile() self.assertTrue(alias.dynamic) self.assertEncoded(x, '\x03', ('\x00\x05hello\x02\x00\x05world', '\x00\x03foo\x02\x00\x03bar'), '\x00\x00\t')
def test_dynamic_registered(self): x = Spam() x.foo = "bar" x.hello = "world" alias = pyamf.register_class(Spam, "x") alias.exclude_attrs = ["foo"] alias.compile() self.assertTrue(alias.dynamic) self.assertEncoded(x, "\x10\x00\x01x", "\x00\x05hello\x02\x00\x05world", "\x00\x00\t")
def test_object(self): self._run([ ({'a': u'spam', 'b': 5}, '\n\x0b\x01\x03a\x06\tspam\x03b\x04\x05\x01')]) pyamf.register_class(Spam, 'org.pyamf.spam') obj = Spam() obj.baz = 'hello' self.encoder.writeElement(obj) self.assertEqual(self.buf.getvalue(), '\n\x0b\x1dorg.pyamf.spam\x07baz' '\x06\x0bhello\x01') pyamf.unregister_class(Spam)
def test_dynamic(self): pyamf.register_class(Spam, 'abc.xyz') x = Spam({'spam': 'eggs'}) self.encoder.writeElement(x) self.assertEqual(self.buf.getvalue(), '\n\x0b\x0fabc.xyz\tspam\x06\teggs\x01')
def test_get_attrs(self): pyamf.register_class(Spam) alias = pyamf.get_class_alias(Spam) x = Spam() self.assertEquals(alias.getAttrs(x), (None, None)) pyamf.unregister_class(Spam) pyamf.register_class(Spam, attrs=['foo']) alias = pyamf.get_class_alias(Spam) x = Spam() self.assertEquals(alias.getAttrs(x), (['foo'], [])) pyamf.unregister_class(Spam) pyamf.register_class(Spam, metadata=['dynamic']) alias = pyamf.get_class_alias(Spam) x = Spam() self.assertEquals(alias.getAttrs(x), (None, None)) pyamf.unregister_class(Spam) def af(x): self.assertEquals(self._obj, x) return ['bar'] pyamf.register_class(Spam, attr_func=af, metadata=['dynamic']) alias = pyamf.get_class_alias(Spam) self._obj = Spam() self.assertEquals(alias.getAttrs(self._obj), ([], ['bar'])) pyamf.unregister_class(Spam) pyamf.register_class(Spam, attrs=['foo', 'bar'], attr_func=af, metadata=['dynamic']) alias = pyamf.get_class_alias(Spam) self._obj = Spam() self.assertEquals(alias.getAttrs(self._obj), (['foo', 'bar'], []))
def test_class_references(self): alias = pyamf.register_class(Spam, 'abc.xyz') x = Spam({'spam': 'eggs'}) y = Spam({'foo': 'bar'}) self.encoder.writeElement(x) cd = self.context.getClass(Spam) self.assertTrue(cd.alias is alias) self.assertEqual(self.buf.getvalue(), '\n\x0b\x0fabc.xyz\tspam\x06\teggs\x01') pos = self.buf.tell() self.encoder.writeElement(y) self.assertEqual(self.buf.getvalue()[pos:], '\n\x01\x07foo\x06\x07bar\x01')
def test_dynamic_static(self): x = Spam() x.foo = 'bar' x.hello = 'world' alias = pyamf.register_class(Spam) alias.static_attrs = ['hello'] alias.compile() self.assertTrue(alias.dynamic) self.assertEncoded( x, '\x03', ('\x00\x05hello\x02\x00\x05world', '\x00\x03foo\x02\x00\x03bar'), '\x00\x00\t')
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'})
def test_getGAEObjects(self): context = Spam() self.assertFalse(hasattr(context, 'gae_objects')) x = adapter_db.getGAEObjects(context) self.assertTrue(isinstance(x, adapter_db.GAEReferenceCollection)) self.assertTrue(hasattr(context, 'gae_objects')) self.assertEquals(id(x), id(context.gae_objects))
def test_static(self): alias = pyamf.register_class(Spam, 'abc.xyz') alias.dynamic = False x = Spam({'spam': 'eggs'}) self.encoder.writeElement(x) self.assertEqual(self.buf.getvalue(), '\n\x03\x0fabc.xyz') pyamf.unregister_class(Spam) self.buf.truncate() self.encoder.context.clear() alias = pyamf.register_class(Spam, 'abc.xyz') alias.dynamic = False alias.static_attrs = ['spam'] x = Spam({'spam': 'eggs', 'foo': 'bar'}) self.encoder.writeElement(x) self.assertEqual(self.buf.getvalue(), '\n\x13\x0fabc.xyz\tspam\x06\teggs')
def test_combined(self): alias = pyamf.register_class(Spam, 'abc.xyz') alias.static_attrs = ['spam'] x = Spam({'spam': 'foo', 'eggs': 'bar'}) self.encoder.writeElement(x) buf = self.buf.getvalue() self.assertEqual(buf, '\n\x1b\x0fabc.xyz\tspam\x06\x07foo\teggs\x06\x07bar\x01')
def test_dynamic(self): x = Spam() x.foo = 'bar' x.hello = 'world' alias = pyamf.register_class(Spam) alias.exclude_attrs = ['foo'] alias.compile() self.assertTrue(alias.dynamic) self._run([(x, '\x03\x00\x05hello\x02\x00\x05world\x00\x00\t')]) pyamf.unregister_class(Spam) # try duplicate attributes alias = pyamf.register_class(Spam) alias.static_attrs = ['hello'] alias.compile() self.assertTrue(alias.dynamic) self._run([(x, '\x03\x00\x05hello\x02\x00\x05world\x00\x03foo\x02' '\x00\x03bar\x00\x00\t')]) pyamf.unregister_class(Spam) # and now typedobject alias = pyamf.register_class(Spam, 'x') alias.exclude_attrs = ['foo'] alias.compile() self.assertTrue(alias.dynamic) self._run([(x, '\x10\x00\x01x\x00\x05hello\x02\x00\x05world\x00\x00\t')])
def test_dynamic(self): x = Spam() x.foo = 'bar' x.hello = 'world' alias = pyamf.register_class(Spam) alias.exclude_attrs = ['foo'] alias.compile() self.assertTrue(alias.dynamic) self._run([(x, '\x03\x00\x05hello\x02\x00\x05world\x00\x00\t')]) pyamf.unregister_class(Spam) # try duplicate attributes alias = pyamf.register_class(Spam) alias.static_attrs = ['hello'] alias.compile() self.assertTrue(alias.dynamic) self._run([(x, '\x03\x00\x05hello\x02\x00\x05world\x00\x03foo\x02' '\x00\x03bar\x00\x00\t')]) pyamf.unregister_class(Spam) # and now typedobject alias = pyamf.register_class(Spam, 'x') alias.exclude_attrs = ['foo'] alias.compile() self.assertTrue(alias.dynamic) self._run([(x, '\x10\x00\x01x\x00\x05hello\x02\x00\x05world\x00\x00\t') ])
def test_loadInstanceFromDatastore(self): # not a class type self.assertRaises(TypeError, adapter_db.loadInstanceFromDatastore, chr, "") # not a subclass of db.Model/db.Expando self.assertRaises(TypeError, adapter_db.loadInstanceFromDatastore, Spam, "") # not a valid key type self.assertRaises(TypeError, adapter_db.loadInstanceFromDatastore, GettableModelStub, 2) self.assertEquals(GettableModelStub.gets, []) adapter_db.loadInstanceFromDatastore(GettableModelStub, "foo", codec=None) self.assertEquals(GettableModelStub.gets, [[("foo",), {}]]) codec = Spam() codec.context = Spam() GettableModelStub.gets = [] self.assertFalse(hasattr(codec.context, "gae_objects")) adapter_db.loadInstanceFromDatastore(GettableModelStub, "foo", codec=codec) self.assertTrue(hasattr(codec.context, "gae_objects")) self.assertEquals(GettableModelStub.gets, [[("foo",), {}]]) gae_objects = codec.context.gae_objects self.assertTrue(isinstance(gae_objects, adapter_db.GAEReferenceCollection)) self.assertEquals(gae_objects, {GettableModelStub: {"foo": None}})
def test_external(self): alias = pyamf.register_class(Spam, 'abc.xyz') alias.external = True x = Spam({'spam': 'eggs'}) self.encoder.writeElement(x) buf = self.buf.getvalue() # an inline object with and inline class-def, encoding = 0x01, 1 attr self.assertEqual(buf[:2], '\x0a\x07') # class alias name self.assertEqual(buf[2:10], '\x0fabc.xyz') self.assertEqual(len(buf), 10)
def setUp(self): self.alias = ClassAlias(Spam, 'foo') self.obj = Spam()
def setUp(self): self.alias = ClassAlias(Spam, 'foo', defer=True) self.obj = Spam()
def test_anonymous(self): pyamf.register_class(Spam) x = Spam({'spam': 'eggs'}) self.assertEncoded(x, '\n\x0b\x01\x09spam\x06\x09eggs\x01')
def test_amf3(self): self.encoder.use_amf3 = True o = Spam() self.assertEncoded(o, '\x11\n\x0b\x01\x01')