Пример #1
0
    def testPutMulti(self):
        ent1 = model.Model(key=model.Key('MyModel', 1))
        ent2 = model.Model(key=model.Key('MyModel', 2))
        ent3 = model.Model(key=model.Key('MyModel', 3))

        res = model.put_multi((ent1, ent2, ent3))
        self.assertEqual(res, [ent1.key, ent2.key, ent3.key])
Пример #2
0
 def testIncompleteKey(self):
     m = model.Model()
     k = model.Key(flat=['Model', None])
     m.key = k
     pb = m.ToPb()
     m2 = model.Model()
     m2.FromPb(pb)
     self.assertEqual(m2, m)
Пример #3
0
    def testGetMulti(self):
        ent1 = model.Model(key=model.Key('MyModel', 1))
        ent2 = model.Model(key=model.Key('MyModel', 2))
        ent3 = model.Model(key=model.Key('MyModel', 3))
        key1 = ent1.put()
        key2 = ent2.put()
        key3 = ent3.put()

        res = model.get_multi((key1, key2, key3))
        self.assertEqual(res, [ent1, ent2, ent3])
Пример #4
0
    def testIdAndParent(self):
        # id
        m = model.Model(id='bar')
        self.assertEqual(m.put(), model.Key('Model', 'bar'))

        # id + parent
        p = model.Key('ParentModel', 'foo')
        m = model.Model(id='bar', parent=p)
        self.assertEqual(m.put(),
                         model.Key('ParentModel', 'foo', 'Model', 'bar'))
Пример #5
0
 def create_entities(self):
     key0 = model.Key(flat=['Foo', None])
     ent1 = model.Model(key=key0)
     ent2 = model.Model(key=key0)
     ent3 = model.Model(key=key0)
     fut1 = self.ctx.put(ent1)
     fut2 = self.ctx.put(ent2)
     fut3 = self.ctx.put(ent3)
     key1 = yield fut1
     key2 = yield fut2
     key3 = yield fut3
     raise tasklets.Return([key1, key2, key3])
Пример #6
0
    def testPutMultiAsync(self):
        ent1 = model.Model(key=model.Key('MyModel', 1))
        ent2 = model.Model(key=model.Key('MyModel', 2))
        ent3 = model.Model(key=model.Key('MyModel', 3))

        @tasklets.tasklet
        def foo():
            ents = yield model.put_multi_async([ent1, ent2, ent3])
            raise tasklets.Return(ents)

        res = foo().get_result()
        self.assertEqual(res, [ent1.key, ent2.key, ent3.key])
Пример #7
0
    def testGetMultiAsync(self):
        ent1 = model.Model(key=model.Key('MyModel', 1))
        ent2 = model.Model(key=model.Key('MyModel', 2))
        ent3 = model.Model(key=model.Key('MyModel', 3))
        key1 = ent1.put()
        key2 = ent2.put()
        key3 = ent3.put()

        @tasklets.tasklet
        def foo():
            ents = yield model.get_multi_async([key1, key2, key3])
            raise tasklets.Return(ents)

        res = foo().get_result()
        self.assertEqual(res, [ent1, ent2, ent3])
Пример #8
0
    def testOrphanProperties(self):
        class Tag(model.Model):
            names = model.StringProperty(repeated=True)
            ratings = model.IntegerProperty(repeated=True)

        class Address(model.Model):
            line = model.StringProperty(repeated=True)
            city = model.StringProperty()
            zip = model.IntegerProperty()
            tags = model.StructuredProperty(Tag)

        class Person(model.Model):
            address = model.StructuredProperty(Address)
            age = model.IntegerProperty(repeated=True)
            name = model.StringProperty()
            k = model.KeyProperty()

        k = model.Key(flat=['Person', 42])
        p = Person(name='White House',
                   k=k,
                   age=[210, 211],
                   address=Address(
                       line=['1600 Pennsylvania', 'Washington, DC'],
                       tags=Tag(names=['a', 'b'], ratings=[1, 2]),
                       zip=20500))
        p.key = k
        pb = p.ToPb()
        q = model.Model()
        q.FromPb(pb)
        qb = q.ToPb()
        linesp = str(pb).splitlines(True)
        linesq = str(qb).splitlines(True)
        lines = difflib.unified_diff(linesp, linesq, 'Expected', 'Actual')
        self.assertEqual(pb, qb, ''.join(lines))
Пример #9
0
    def testDeleteMulti(self):
        ent1 = model.Model(key=model.Key('MyModel', 1))
        ent2 = model.Model(key=model.Key('MyModel', 2))
        ent3 = model.Model(key=model.Key('MyModel', 3))
        key1 = ent1.put()
        key2 = ent2.put()
        key3 = ent3.put()

        self.assertEqual(key1.get(), ent1)
        self.assertEqual(key2.get(), ent2)
        self.assertEqual(key3.get(), ent3)

        res = model.delete_multi((key1, key2, key3))

        self.assertEqual(key1.get(), None)
        self.assertEqual(key2.get(), None)
        self.assertEqual(key3.get(), None)
Пример #10
0
 def testKey(self):
     m = model.Model()
     self.assertEqual(m.key, None)
     k = model.Key(flat=['ParentModel', 42, 'Model', 'foobar'])
     m.key = k
     self.assertEqual(m.key, k)
     del m.key
     self.assertEqual(m.key, None)
Пример #11
0
    def testDeleteMultiAsync(self):
        ent1 = model.Model(key=model.Key('MyModel', 1))
        ent2 = model.Model(key=model.Key('MyModel', 2))
        ent3 = model.Model(key=model.Key('MyModel', 3))
        key1 = ent1.put()
        key2 = ent2.put()
        key3 = ent3.put()

        self.assertEqual(key1.get(), ent1)
        self.assertEqual(key2.get(), ent2)
        self.assertEqual(key3.get(), ent3)

        @tasklets.tasklet
        def foo():
            ents = yield model.delete_multi_async([key1, key2, key3])
            raise tasklets.Return(ents)

        res = foo().get_result()
        self.assertEqual(key1.get(), None)
        self.assertEqual(key2.get(), None)
        self.assertEqual(key3.get(), None)
Пример #12
0
    def testIdAndParent(self):
        p = model.Key('ParentModel', 'foo')

        # key name
        m = model.Model(id='bar')
        m2 = model.Model._from_pb(m._to_pb())
        self.assertEqual(m2.key, model.Key('Model', 'bar'))

        # key name + parent
        m = model.Model(id='bar', parent=p)
        m2 = model.Model._from_pb(m._to_pb())
        self.assertEqual(m2.key, model.Key('ParentModel', 'foo', 'Model',
                                           'bar'))

        # key id
        m = model.Model(id=42)
        m2 = model.Model._from_pb(m._to_pb())
        self.assertEqual(m2.key, model.Key('Model', 42))

        # key id + parent
        m = model.Model(id=42, parent=p)
        m2 = model.Model._from_pb(m._to_pb())
        self.assertEqual(m2.key, model.Key('ParentModel', 'foo', 'Model', 42))

        # parent
        m = model.Model(parent=p)
        m2 = model.Model._from_pb(m._to_pb())
        self.assertEqual(m2.key, model.Key('ParentModel', 'foo', 'Model',
                                           None))

        # not key -- invalid
        self.assertRaises(datastore_errors.BadValueError,
                          model.Model,
                          key='foo')

        # wrong key kind -- invalid
        k = model.Key('OtherModel', 'bar')
        self.assertRaises(model.KindError, model.Model, key=k)

        # incomplete parent -- invalid
        p2 = model.Key('ParentModel', None)
        self.assertRaises(datastore_errors.BadArgumentError,
                          model.Model,
                          parent=p2)
        self.assertRaises(datastore_errors.BadArgumentError,
                          model.Model,
                          id='bar',
                          parent=p2)

        # key + id -- invalid
        k = model.Key('Model', 'bar')
        self.assertRaises(datastore_errors.BadArgumentError,
                          model.Model,
                          key=k,
                          id='bar')

        # key + parent -- invalid
        k = model.Key('Model', 'bar', parent=p)
        self.assertRaises(datastore_errors.BadArgumentError,
                          model.Model,
                          key=k,
                          parent=p)

        # key + id + parent -- invalid
        self.assertRaises(datastore_errors.BadArgumentError,
                          model.Model,
                          key=k,
                          id='bar',
                          parent=p)