예제 #1
0
 def __create_value_for_mongo_value(self, mongo_value):
     if isinstance(mongo_value, Binary):
         return datastore_types.Blob(str(mongo_value))
     if isinstance(mongo_value, types.DictType):
         if mongo_value['class'] == 'rating':
             return datastore_types.Rating(int(mongo_value["rating"]))
         if mongo_value['class'] == 'category':
             return datastore_types.Category(mongo_value["category"])
         if mongo_value['class'] == 'key':
             return self.__key_for_id(mongo_value['path'])
         if mongo_value['class'] == 'list':
             return [
                 self.__create_value_for_mongo_value(v)
                 for v in mongo_value['list']
             ]
         if mongo_value['class'] == 'user':
             return users.User(email=mongo_value["email"])
         if mongo_value['class'] == 'text':
             return datastore_types.Text(mongo_value['string'])
         if mongo_value['class'] == 'im':
             return datastore_types.IM(mongo_value['protocol'],
                                       mongo_value['address'])
         if mongo_value['class'] == 'geopt':
             return datastore_types.GeoPt(mongo_value['lat'],
                                          mongo_value['lon'])
         if mongo_value['class'] == 'email':
             return datastore_types.Email(mongo_value['value'])
         if mongo_value['class'] == 'bytes':
             return datastore_types.ByteString(mongo_value['value'])
         if mongo_value['class'] == 'blobkey':
             return datastore_types.BlobKey(mongo_value['value'])
     return mongo_value
    def testDatastoreTypes(self):
        """Puts and gets different basic datastore types."""

        entity = datastore.Entity('TestKind')

        entity.update({
            'rating':
            datastore_types.Rating(1),
            'category':
            datastore_types.Category('bugs'),
            'key':
            datastore_types.Key.from_path('foo', 'bar'),
            'user':
            users.User('*****@*****.**'),
            'text':
            datastore_types.Text('some text'),
            'blob':
            datastore_types.Blob('data'),
            'bytestring':
            datastore_types.ByteString('data'),
            'im':
            datastore_types.IM('http://example.com/', 'Larry97'),
            'geopt':
            datastore_types.GeoPt(1.1234, -1.1234),
            'email':
            datastore_types.Email('*****@*****.**'),
            'blobkey':
            datastore_types.BlobKey('27f5a7'),
        })

        datastore.Put(entity)
        e = datastore.Get(entity)
        datastore.Delete(entity)
    def testVariousPropertiyTypes(self):
        """Tests various property types."""
        class Note(db.Model):
            timestamp = db.DateTimeProperty(auto_now=True)
            description = db.StringProperty()
            author_email = db.EmailProperty()
            location = db.GeoPtProperty()
            user = db.UserProperty()

        Note(description="My first note.",
             author_email="*****@*****.**",
             location="52.518,13.408",
             user=users.get_current_user()).put()

        query = db.GqlQuery("SELECT * FROM Note ORDER BY timestamp DESC")
        self.assertEqual(1, query.count())

        query = db.GqlQuery("SELECT * FROM Note WHERE timestamp <= :1",
                            datetime.datetime.now())

        self.assertEqual(1, query.count())

        note = query.get()

        self.assertEqual("My first note.", note.description)

        self.assertEqual(db.Email("*****@*****.**"), note.author_email)
        self.assertEqual("*****@*****.**", note.author_email)

        self.assertEqual(
            datastore_types.GeoPt(52.518000000000001, 13.407999999999999),
            note.location)
        self.assertEqual("52.518,13.408", note.location)

        del note

        query = Note.all().filter(
            'location =',
            datastore_types.GeoPt(52.518000000000001, 13.407999999999999))
        self.assertEqual(1, query.count())

        query = Note.all().filter('location =', db.GeoPt("52.518,13.408"))
        self.assertEqual(1, query.count())
예제 #4
0
    def test_encode(self):
        point = datastore_types.GeoPt(lat=1.23456, lon=-23.9876)

        self.assertEncodes(
            point, (b'\x10\x00*google.appengine.api.datastore_types.GeoPt', (
                b'\x00\x03lat\x00?\xf3\xc0\xc1\xfc\x8f28',
                b'\x00\x03lon\x00\xc07\xfc\xd3Z\x85\x87\x94',
            ), b'\x00\x00\t'),
            encoding=pyamf.AMF0)

        self.assertEncodes(point,
                           (b'\n#Ugoogle.appengine.api.datastore_types.GeoPt'
                            b'\x07lat\x07lon'
                            b'\x05?\xf3\xc0\xc1\xfc\x8f28\x05'
                            b'\xc07\xfc\xd3Z\x85\x87\x94'),
                           encoding=pyamf.AMF3)
 def parse(self, value):
     return datastore_types.GeoPt(value)
 def __CastGeoPt(self, values):
     """Cast input to GeoPt() class using 2 input parameters."""
     if len(values) != 2:
         self.__CastError('GEOPT', values, 'requires 2 input parameters')
     return datastore_types.GeoPt(*values)
예제 #7
0
    def test_entity_from_json_data(self):
        """Create a datastore.Entity instance from JSON data."""

        from datetime import datetime
        from gaesynkit import handlers
        from google.appengine.api import datastore_types

        data = {
            'kind': u'A',
            'key': u'dGVzdEBkZWZhdWx0ISFBCGE=',
            'id': 2,
            'properties': {
                'string': {
                    'type': 'string',
                    'value': 'A string.'
                },
                'boolean': {
                    'type': 'bool',
                    'value': True
                },
                'int': {
                    'type': 'int',
                    'value': 42
                },
                'float': {
                    'type': 'float',
                    'value': 1.8200000000000001
                },
                'key': {
                    'type': 'key',
                    'value': 'agR0ZXN0cg4LEgRLaW5kIgRuYW1lDA'
                },
                'byte_string': {
                    'type': 'byte_string',
                    'value': 'Byte String'
                },
                'date': {
                    'type': 'gd:when',
                    'value': '2011/01/06 00:00:00'
                },
                'list': {
                    'type': 'int',
                    'value': [1, 2, 3, 4]
                },
                'email': {
                    'type': 'gd:email',
                    'value': u'*****@*****.**'
                },
                'location': {
                    'type': 'georss:point',
                    'value': u'52.5,13.3'
                },
                'category': {
                    'type': 'atom:category',
                    'value': u'coding'
                },
                'url': {
                    'type': 'atom:link',
                    'value': u'http://www.google.com'
                },
                'im': {
                    'type': 'gd:im',
                    'value': u'sip foobar'
                },
                'phone': {
                    'type': 'gd:phonenumber',
                    'value': u'1 (206) 555-1212'
                },
                'address': {
                    'type': 'gd:postaladdress',
                    'value': u'Address'
                },
                'rating': {
                    'type': 'gd:rating',
                    'value': 99L
                }
            }
        }

        entity = handlers.entity_from_json_data(data)

        self.assertEqual(entity['string'], u'A string.')
        self.assertEqual(entity['boolean'], True)
        self.assertEqual(entity['int'], 42)
        self.assertEqual(entity['float'], 1.8200000000000001)
        self.assertEqual(
            entity['key'],
            datastore_types.Key.from_path(u'Kind', u'name', _app=u'test'))
        self.assertEqual(entity['byte_string'], 'Byte String')
        self.assertEqual(entity['date'], datetime(2011, 1, 6, 0, 0))
        self.assertEqual(entity['list'], [1, 2, 3, 4])
        self.assertEqual(entity['email'], u'*****@*****.**')
        self.assertEqual(entity['location'],
                         datastore_types.GeoPt(52.5, 13.300000000000001))
        self.assertEqual(entity['category'], u'coding')
        self.assertEqual(entity['url'], u'http://www.google.com')
        self.assertEqual(entity['im'], datastore_types.IM(u'sip', u'foobar'))
        self.assertEqual(entity['phone'], u'1 (206) 555-1212')
        self.assertEqual(entity['address'], u'Address')
        self.assertEqual(entity['rating'], 99)
예제 #8
0
                        'type': 'gd:im',
                        'value': datastore_types.IM('sip', 'foobar')
                    },
                    'email': {
                        'type': 'gd:email',
                        'value': u'*****@*****.**'
                    },
                    'user': {
                        'type': 'user',
                        'value': 'tester'
                    },
                    'location': {
                        'type':
                        'georss:point',
                        'value':
                        datastore_types.GeoPt(52.500556000000003, 13.398889)
                    }
                },
                'id': 3
            })

        entity = datastore.Entity("A", name="a")
        self.assertEqual(handlers.json_data_from_entity(entity), {
            'kind': u'A',
            'properties': {},
            'name': u'a'
        })

    def test_entity_from_json_data(self):
        """Create a datastore.Entity instance from JSON data."""