Example #1
0
    def test_expando(self):
        class Author(db.Expando):
            name = db.StringProperty()

        class Novel(db.Expando):
            title = db.StringProperty()
            author = db.ReferenceProperty(Author)

        a = Author(name='Jane Austen')
        a.put()
        k = str(a.key())

        b = Novel(title='Sense and Sensibility', author=a)

        self.assertEquals(b.author, a)

        try:
            encoder = pyamf.get_encoder(pyamf.AMF0)

            encoder.writeElement(b)
            self.assertEquals(encoder.stream.getvalue(),
                '\x03\x00\x04_key\x05\x00\x06author\x03\x00\x04_key\x02%s%s'
                '\x00\x04name\x02\x00\x0bJane Austen\x00\x00\t\x00\x05title'
                '\x02\x00\x15Sense and Sensibility\x00\x00\t' % (
                    struct.pack('>H', len(k)), k))

            encoder = pyamf.get_encoder(pyamf.AMF3)

            encoder.writeElement(b)
            self.assertEquals(encoder.stream.getvalue(),
                '\n;\x01\t_key\rauthor\x0btitle\x01\n+\x01\x00\tname\x06%s%s'
                '\x06\x17Jane Austen\x01\x06+Sense and Sensibility\x01' % (
                    amf3._encode_int(len(k) << 1 | amf3.REFERENCE_BIT), k))

            # now test with aliases ..
            pyamf.register_class(Author, 'Author')
            pyamf.register_class(Novel, 'Novel')

            encoder = pyamf.get_encoder(pyamf.AMF0)

            encoder.writeElement(b)
            self.assertEquals(encoder.stream.getvalue(), '\x10\x00\x05Novel'
                '\x00\x04_key\x05\x00\x06author\x10\x00\x06Author\x00\x04_key'
                '\x02%s%s\x00\x04name\x02\x00\x0bJane Austen\x00\x00\t\x00'
                '\x05title\x02\x00\x15Sense and Sensibility\x00\x00\t' % (
                    struct.pack('>H', len(k)), k))

            encoder = pyamf.get_encoder(pyamf.AMF3)

            encoder.writeElement(b)
            self.assertEquals(encoder.stream.getvalue(),
                '\n;\x0bNovel\t_key\rauthor\x0btitle\x01\n+\rAuthor\x02\tname'
                '\x06%s%s\x06\x17Jane Austen\x01\x06+Sense and Sensibility'
                '\x01' % (amf3._encode_int(len(k) << 1 | amf3.REFERENCE_BIT), k))
        except:
            a.delete()
            raise

        a.delete()
    def test_amf3(self):
        encoder = pyamf.get_encoder(pyamf.AMF3)
        context = encoder.context
        stream = encoder.stream
        s = str(self.jessica.key())

        self.assertFalse(hasattr(context, "gae_objects"))

        encoder.writeObject(self.jessica)

        self.assertTrue(hasattr(context, "gae_objects"))
        self.assertEquals(context.gae_objects, {PetModel: {s: self.jessica}})
        self.assertEquals(
            stream.getvalue(),
            "\nk\x01\t_key!weight_in_pounds"
            "\ttype\tname\x13birthdate%%spayed_or_neutered\x06%s%s\x04\x05\x06"
            "\x07cat\x06\x0fJessica\x08\x01B^\xc4\xae\xaa"
            "\x00\x00\x00\x02\x01" % (amf3._encode_int(len(s) << 1 | amf3.REFERENCE_BIT), s),
        )

        stream.truncate()
        encoder.writeObject(self.jessica2)

        self.assertTrue(hasattr(context, "gae_objects"))
        self.assertEquals(context.gae_objects, {PetModel: {s: self.jessica}})
        self.assertEquals(stream.getvalue(), "\n\x00")
    def test_amf3(self):
        d = pyamf.get_decoder(pyamf.AMF3)
        b = d.stream

        b.write(
            "\n\x0b\x07Pet\tname\x06\x0fJessica\t_key\x06%s%s\x13birthdate"
            "\x08\x01B^\xc4\xae\xaa\x00\x00\x00!weight_in_pounds\x04\x05\x07"
            "foo\x06\x07bar\ttype\x06\x07cat%%spayed_or_neutered\x02\x01"
            % (amf3._encode_int(len(self.key) << 1 | amf3.REFERENCE_BIT), self.key)
        )

        b.seek(0)
        x = d.readElement()

        self.assertTrue(isinstance(x, PetExpando))
        self.assertEquals(x.__class__, PetExpando)

        self.assertEquals(x.type, self.jessica.type)
        self.assertEquals(x.weight_in_pounds, self.jessica.weight_in_pounds)
        self.assertEquals(x.birthdate, datetime.date(1986, 10, 2))
        self.assertEquals(x.spayed_or_neutered, self.jessica.spayed_or_neutered)

        # now check db.Expando internals
        self.assertEquals(x.key(), self.jessica.key())
        self.assertEquals(x.kind(), self.jessica.kind())
        self.assertEquals(x.parent(), self.jessica.parent())
        self.assertEquals(x.parent_key(), self.jessica.parent_key())
        self.assertTrue(x.is_saved())
Example #4
0
    def test_amf3(self):
        d = pyamf.get_decoder(pyamf.AMF3)
        b = d.stream

        b.write(
            '\n\x0b\x07Pet\tname\x06\x0fJessica\t_key\x06%s%s\x13birthdate'
            '\x08\x01B^\xc4\xae\xaa\x00\x00\x00!weight_in_pounds\x04\x05\x07'
            'foo\x06\x07bar\ttype\x06\x07cat%%spayed_or_neutered\x02\x01' %
            (amf3._encode_int(len(self.key) << 1 | amf3.REFERENCE_BIT),
             self.key))

        b.seek(0)
        x = d.readElement()

        self.assertTrue(isinstance(x, PetExpando))
        self.assertEquals(x.__class__, PetExpando)

        self.assertEquals(x.type, self.jessica.type)
        self.assertEquals(x.weight_in_pounds, self.jessica.weight_in_pounds)
        self.assertEquals(x.birthdate, datetime.date(1986, 10, 2))
        self.assertEquals(x.spayed_or_neutered,
                          self.jessica.spayed_or_neutered)

        # now check db.Expando internals
        self.assertEquals(x.key(), self.jessica.key())
        self.assertEquals(x.kind(), self.jessica.kind())
        self.assertEquals(x.parent(), self.jessica.parent())
        self.assertEquals(x.parent_key(), self.jessica.parent_key())
        self.assertTrue(x.is_saved())
Example #5
0
    def test_amf3(self):
        encoder = pyamf.get_encoder(pyamf.AMF3)
        context = encoder.context
        stream = encoder.stream
        s = str(self.jessica.key())

        self.assertFalse(hasattr(context, 'gae_objects'))

        encoder.writeObject(self.jessica)

        self.assertTrue(hasattr(context, 'gae_objects'))
        self.assertEquals(context.gae_objects, {PetModel: {s: self.jessica}})
        self.assertEquals(
            stream.getvalue(), '\nk\x01\t_key!weight_in_pounds'
            '\ttype\tname\x13birthdate%%spayed_or_neutered\x06%s%s\x04\x05\x06'
            '\x07cat\x06\x0fJessica\x08\x01B^\xc4\xae\xaa'
            '\x00\x00\x00\x02\x01' %
            (amf3._encode_int(len(s) << 1 | amf3.REFERENCE_BIT), s))

        stream.truncate()
        encoder.writeObject(self.jessica2)

        self.assertTrue(hasattr(context, 'gae_objects'))
        self.assertEquals(context.gae_objects, {PetModel: {s: self.jessica}})
        self.assertEquals(stream.getvalue(), '\n\x00')
    def test_save_amf3(self):
        self.jessica.put()

        k = str(self.jessica.key())
        encoder = pyamf.get_encoder(pyamf.AMF3)
        encoder.writeElement(self.jessica)

        self.assertEquals(
            encoder.stream.getvalue(),
            "\nk\x01\t_key!weight_in_pounds\ttype\tname\x13birthdate%%"
            "spayed_or_neutered\x06%s%s\x04\x05\x06\x07cat\x06\x0fJessica"
            "\x08\x01B^\xc4\xae\xaa\x00\x00\x00\x02\x01" % (amf3._encode_int(len(k) << 1 | amf3.REFERENCE_BIT), k),
        )
Example #7
0
    def test_save_amf3(self):
        self.jessica.put()

        k = str(self.jessica.key())
        encoder = pyamf.get_encoder(pyamf.AMF3)
        encoder.writeElement(self.jessica)

        self.assertEquals(
            encoder.stream.getvalue(),
            '\nk\x01\t_key!weight_in_pounds\ttype\tname\x13birthdate%%'
            'spayed_or_neutered\x06%s%s\x04\x05\x06\x07cat\x06\x0fJessica'
            '\x08\x01B^\xc4\xae\xaa\x00\x00\x00\x02\x01' %
            (amf3._encode_int(len(k) << 1 | amf3.REFERENCE_BIT), k))
    def test_model(self):
        class Author(db.Model):
            name = db.StringProperty()

        class Novel(db.Model):
            title = db.StringProperty()
            author = db.ReferenceProperty(Author)

        a = Author(name="Jane Austen")
        a.put()
        k = str(a.key())

        b = Novel(title="Sense and Sensibility", author=a)

        self.assertEquals(b.author, a)

        try:
            encoder = pyamf.get_encoder(pyamf.AMF0)

            encoder.writeElement(b)
            self.assertEquals(
                encoder.stream.getvalue(),
                "\x03\x00\x04_key\x05\x00\x06author\x03\x00\x04_key\x02%s%s"
                "\x00\x04name\x02\x00\x0bJane Austen\x00\x00\t\x00\x05title"
                "\x02\x00\x15Sense and Sensibility\x00\x00\t" % (struct.pack(">H", len(k)), k),
            )

            encoder = pyamf.get_encoder(pyamf.AMF3)

            encoder.writeElement(b)
            self.assertEquals(
                encoder.stream.getvalue(),
                "\n;\x01\t_key\rauthor\x0btitle\x01\n+\x01\x00\tname\x06%s%s"
                "\x06\x17Jane Austen\x01\x06+Sense and Sensibility\x01"
                % (amf3._encode_int(len(k) << 1 | amf3.REFERENCE_BIT), k),
            )

            # now test with aliases ..
            pyamf.register_class(Author, "Author")
            pyamf.register_class(Novel, "Novel")

            encoder = pyamf.get_encoder(pyamf.AMF0)

            encoder.writeElement(b)
            self.assertEquals(
                encoder.stream.getvalue(),
                "\x10\x00\x05Novel"
                "\x00\x04_key\x05\x00\x06author\x10\x00\x06Author\x00\x04_key"
                "\x02%s%s\x00\x04name\x02\x00\x0bJane Austen\x00\x00\t\x00"
                "\x05title\x02\x00\x15Sense and Sensibility\x00\x00\t" % (struct.pack(">H", len(k)), k),
            )

            encoder = pyamf.get_encoder(pyamf.AMF3)

            encoder.writeElement(b)
            self.assertEquals(
                encoder.stream.getvalue(),
                "\n;\x0bNovel\t_key"
                "\rauthor\x0btitle\x01\n+\rAuthor\x02\tname\x06%s%s\x06\x17"
                "Jane Austen\x01\x06+Sense and Sensibility\x01"
                % (amf3._encode_int(len(k) << 1 | amf3.REFERENCE_BIT), k),
            )
        except:
            a.delete()
            raise

        a.delete()
Example #9
0
    def test_expando(self):
        class Author(db.Expando):
            name = db.StringProperty()

        class Novel(db.Expando):
            title = db.StringProperty()
            author = db.ReferenceProperty(Author)

        a = Author(name='Jane Austen')
        a.put()
        k = str(a.key())

        b = Novel(title='Sense and Sensibility', author=a)

        self.assertEquals(b.author, a)

        try:
            encoder = pyamf.get_encoder(pyamf.AMF0)

            encoder.writeElement(b)
            self.assertEquals(
                encoder.stream.getvalue(),
                '\x03\x00\x06author\x03\x00\x04_key\x02%s%s\x00\x04name\x02'
                '\x00\x0bJane Austen\x00\x00\t\x00\x05title\x02\x00\x15'
                'Sense and Sensibility\x00\x00\t' %
                (struct.pack('>H', len(k)), k))

            encoder = pyamf.get_encoder(pyamf.AMF3)

            encoder.writeElement(b)
            self.assertEquals(
                encoder.stream.getvalue(),
                '\n+\x01\rauthor\x0btitle\n+\x01\t_key\tname\x06%s%s\x06\x17'
                'Jane Austen\x01\x06+Sense and Sensibility\x01' %
                (amf3._encode_int(len(k) << 1 | amf3.REFERENCE_BIT), k))

            # now test with aliases ..
            pyamf.register_class(Author, 'Author')
            pyamf.register_class(Novel, 'Novel')

            encoder = pyamf.get_encoder(pyamf.AMF0)

            encoder.writeElement(b)
            self.assertEquals(
                encoder.stream.getvalue(), '\x10\x00\x05Novel'
                '\x00\x06author\x10\x00\x06Author\x00\x04_key\x02%s%s\x00\x04name'
                '\x02\x00\x0bJane Austen\x00\x00\t\x00\x05title\x02\x00\x15'
                'Sense and Sensibility\x00\x00\t' %
                (struct.pack('>H', len(k)), k))

            encoder = pyamf.get_encoder(pyamf.AMF3)

            encoder.writeElement(b)
            self.assertEquals(
                encoder.stream.getvalue(),
                '\n+\x0bNovel\rauthor\x0btitle\n+\rAuthor\t_key\tname\x06%s%s'
                '\x06\x17Jane Austen\x01\x06+Sense and Sensibility\x01' %
                (amf3._encode_int(len(k) << 1 | amf3.REFERENCE_BIT), k))
        except:
            a.delete()
            raise

        a.delete()