示例#1
0
 def test_key_item_saved(self):
     # make sure that an instance of this model is saved at least
     # once before retrieving the key
     item = TestModel()
     self.assertRaises(db.NotSavedError, item.key)
     item.put()
     item.key()
 def test_key_item_saved(self):
     # make sure that an instance of this model is saved at least
     # once before retrieving the key
     item = TestModel()
     self.assertRaises(db.NotSavedError, item.key)
     item.put()
     item.key()
示例#3
0
 def setUp(self):
     self._test = TestModel(xstring="foo")
     self._test.put()
     self._key = self._test.key()
     self._test_name = TestModel(xstring="foo", key_name="fookey")
     self._test_name.put()
     self._key_name = self._test_name.key()
 def test_get_or_insert(self):
     item1 = TestModel.get_or_insert('test1', xstring='foo')
     self.assert_(isinstance(item1, TestModel))
     test = TestModel.get_or_insert('test1')
     self.assertEqual(item1, test)
     self.assertEqual(item1.xstring, 'foo')
     item1.delete()
示例#5
0
 def setUp(self):
     self._test = TestModel(xstring="foo")
     self._test.put()
     self._key = self._test.key()
     self._test_name = TestModel(xstring="foo", key_name="fookey")
     self._test_name.put()
     self._key_name = self._test_name.key()
示例#6
0
 def test_get_or_insert(self):
     item1 = TestModel.get_or_insert('test1', xstring='foo')
     self.assert_(isinstance(item1, TestModel))
     test = TestModel.get_or_insert('test1')
     self.assertEqual(item1, test)
     self.assertEqual(item1.xstring, 'foo')
     item1.delete()
示例#7
0
class QueryTest(unittest.TestCase):

    def setUp(self):
        TestModel.all().delete()
        self.item1 = TestModel(key_name='foo1')
        self.item1.put()

    def tearDown(self):
        self.item1.delete()
        self.item1 = None

    def test_refkey(self):
        # Regression for issue23.
        i1 = TestModel2()
        i1.put()
        i2 = TestModel()
        i2.ref = i1
        i2.put()
        q = TestModel.all()
        q.filter('ref =', i1.key())
        res = list(q.fetch(1000))
        self.assertEqual(len(res), 1)
        self.assertEqual(str(res[0].key()), str(i2.key()))

    def test_key_only(self):
        q = TestModel.all(keys_only=True)
        res = q.fetch(1000)
        self.assertEqual(len(res), 1)
        self.assertEqual(res[0], self.item1.id)
 def test_filter_unicode(self):  # issue22
     # This test passes with Python >= 2.6 either way.
     obj = TestModel()
     obj.xstring = 'foo'
     obj.save()
     query = db.GqlQuery((u'SELECT * FROM RegressionTestModel'
                          u' WHERE xstring = :foo'), foo=u'foo')
     self.assertEqual(query.count(), 1)
 def test_get_value_for_datastore(self):
     m = TestModel()
     m2 = TestModel2()
     m2.put()
     m.ref = m2
     m.put()
     self.assert_(isinstance(TestModel.ref.get_value_for_datastore(m),
                             db.Key))
     self.assertEqual(TestModel.ref.get_value_for_datastore(m), m2.key())
示例#10
0
 def test_protected_attr(self):
     m = TestModel()
     m2 = TestModel2()
     m2.put()
     m.ref = m2
     m.put()
     self.assert_(hasattr(m, '_ref'))
     self.assert_(isinstance(m._ref, db.Key))
     self.assertEqual(m._ref, m2.key())
示例#11
0
 def test_filter(self):
     q = TestModel.all()
     self.assert_(isinstance(q, db.Query))
     q = q.filter('xstring =', 'foo1')
     self.assert_(isinstance(q, db.Query))
     self.assertEqual(len(list(q)), 1)
     q = TestModel.all().filter('xstring =', 'foo')
     self.assert_(isinstance(q, db.Query))
     self.assertEqual(len(list(q)), 0)
 def test_gql(self):
     item1 = TestModel.get_or_insert('test1', xstring='foo')
     item2 = TestModel.get_or_insert('test2', xstring='foo')
     results = TestModel.gql('WHERE xstring = \'foo\'')
     self.assertEqual(results.count(), 2)
     self.assert_(item1 in results)
     self.assert_(item2 in results)
     item1.delete()
     item2.delete()
示例#13
0
 def test_get_value_for_datastore(self):
     m = TestModel()
     m2 = TestModel2()
     m2.put()
     m.ref = m2
     m.put()
     self.assert_(isinstance(TestModel.ref.get_value_for_datastore(m),
                             db.Key))
     self.assertEqual(TestModel.ref.get_value_for_datastore(m), m2.key())
示例#14
0
 def test_filter(self):
     q = TestModel.all()
     self.assert_(isinstance(q, db.Query))
     q = q.filter('xstring =', 'foo1')
     self.assert_(isinstance(q, db.Query))
     self.assertEqual(len(list(q)), 1)
     q = TestModel.all().filter('xstring =', 'foo')
     self.assert_(isinstance(q, db.Query))
     self.assertEqual(len(list(q)), 0)
示例#15
0
 def test_filter_unicode(self):  # issue22
     # This test passes with Python >= 2.6 either way.
     obj = TestModel()
     obj.xstring = 'foo'
     obj.save()
     query = db.GqlQuery((u'SELECT * FROM RegressionTestModel'
                          u' WHERE xstring = :foo'),
                         foo=u'foo')
     self.assertEqual(query.count(), 1)
示例#16
0
 def test_gql(self):
     item1 = TestModel.get_or_insert('test1', xstring='foo')
     item2 = TestModel.get_or_insert('test2', xstring='foo')
     results = TestModel.gql('WHERE xstring = \'foo\'')
     self.assertEqual(results.count(), 2)
     self.assert_(item1 in results)
     self.assert_(item2 in results)
     item1.delete()
     item2.delete()
示例#17
0
 def test_auto_current_user_add(self):
     c = Client()
     c.login(username='******', password='******')
     response = c.get('/')
     user = response.context['user']
     self.assert_(user is not None)
     obj = TestModel()
     obj.save()
     self.assertEqual(obj.xuser, user)
 def test_protected_attr(self):
     m = TestModel()
     m2 = TestModel2()
     m2.put()
     m.ref = m2
     m.put()
     self.assert_(hasattr(m, '_ref'))
     self.assert_(isinstance(m._ref, db.Key))
     self.assertEqual(m._ref, m2.key())
 def test_auto_current_user_add(self):
     c = Client()
     c.login(username='******', password='******')
     response = c.get('/')
     user = response.context['user']
     self.assert_(user is not None)
     obj = TestModel()
     obj.save()
     self.assertEqual(obj.xuser, user)
示例#20
0
 def test_user_property_patched(self):
     c = Client()
     c.login(username='******', password='******')
     response = c.get('/')
     user = response.context['user']
     obj = TestModel()
     obj.save()
     self.assert_(callable(obj.xuser.email))
     self.assert_(hasattr(obj.xuser, 'nickname'))
     self.assert_(callable(obj.xuser.nickname))
 def test_user_property_patched(self):
     c = Client()
     c.login(username='******', password='******')
     response = c.get('/')
     user = response.context['user']
     obj = TestModel()
     obj.save()
     self.assert_(callable(obj.xuser.email))
     self.assert_(hasattr(obj.xuser, 'nickname'))
     self.assert_(callable(obj.xuser.nickname))
示例#22
0
 def test_ancestor(self):
     grandpa = TestModel.get_or_insert('grandpa')
     dad = TestModel.get_or_insert('dad', parent=grandpa)
     TestModel.get_or_insert('junior', parent=dad)
     TestModel.get_or_insert('unrelated')
     q = TestModel.all()
     q.ancestor(grandpa)
     self.assertEqual(len(q), 2)
     q2 = TestModel.all()
     q2.ancestor(dad)
     self.assertEqual(len(q2), 1)
     # ancestor() accepts keys too
     q = TestModel.all()
     q.ancestor(grandpa.key())
     self.assertEqual(len(q), 2)
     q2 = TestModel.all()
     q2.ancestor(dad.key())
     self.assertEqual(len(q2), 1)
 def test_properties(self):
     props = TestModel.properties()
     self.assert_('xstring' in props)
     self.assert_(isinstance(props['xstring'], db.StringProperty))
     self.assert_('gae_parent_id' not in props)
     # From a App Engine perspective this is wrong. Choices should
     # be ('foo', 'bar'). For Django this is right.
     self.assertEqual(props['choices'].choices,
                      [('foo', 'foo'), ('bar', 'bar')])
 def test_ancestor(self):
     grandpa = TestModel.get_or_insert('grandpa')
     dad = TestModel.get_or_insert('dad', parent=grandpa)
     TestModel.get_or_insert('junior', parent=dad)
     TestModel.get_or_insert('unrelated')
     q = TestModel.all()
     q.ancestor(grandpa)
     self.assertEqual(len(q), 2)
     q2 = TestModel.all()
     q2.ancestor(dad)
     self.assertEqual(len(q2), 1)
     # ancestor() accepts keys too
     q = TestModel.all()
     q.ancestor(grandpa.key())
     self.assertEqual(len(q), 2)
     q2 = TestModel.all()
     q2.ancestor(dad.key())
     self.assertEqual(len(q2), 1)
示例#25
0
 def test_query_listproperty(self):
     obj = TestModel()
     obj.xlist = ['foo', 'bar', 'baz']
     obj.save()
     query = db.GqlQuery(('SELECT * FROM RegressionTestModel'
                          ' WHERE xlist = :1'), 'foo')
     self.assertEqual([obj], list(query))
     self.assertEqual(query.count(), 1)
     tobj = query.get()
     self.assertEqual(tobj.xlist, ['foo', 'bar', 'baz'])
     query = db.GqlQuery(('SELECT * FROM RegressionTestModel'
                          ' WHERE xlist = :1'), 'nomatch')
     self.assertEqual([], list(query))
     self.assertEqual(query.count(), 0)
     tobj.xlist = ['bar', 'baz']
     tobj.save()
     query = db.GqlQuery(('SELECT * FROM RegressionTestModel'
                          ' WHERE xlist = :1'), 'foo')
     self.assertEqual([], list(query))
     self.assertEqual(query.count(), 0)
示例#26
0
 def test_query_listproperty(self):
     obj = TestModel()
     obj.xlist = ['foo', 'bar', 'baz']
     obj.save()
     query = db.GqlQuery(('SELECT * FROM RegressionTestModel'
                          ' WHERE xlist = :1'), 'foo')
     self.assertEqual([obj], list(query))
     self.assertEqual(query.count(), 1)
     tobj = query.get()
     self.assertEqual(tobj.xlist, ['foo', 'bar', 'baz'])
     query = db.GqlQuery(('SELECT * FROM RegressionTestModel'
                          ' WHERE xlist = :1'), 'nomatch')
     self.assertEqual([], list(query))
     self.assertEqual(query.count(), 0)
     tobj.xlist = ['bar', 'baz']
     tobj.save()
     query = db.GqlQuery(('SELECT * FROM RegressionTestModel'
                          ' WHERE xlist = :1'), 'foo')
     self.assertEqual([], list(query))
     self.assertEqual(query.count(), 0)
示例#27
0
 def test_listproperty_save_restore(self):
     obj = TestModel()
     obj.xlist = ["foo", "bar", "baz"]
     obj.save()
     tobj = TestModel.get_by_id(obj.key().id())
     self.assertEqual(tobj, obj)
     self.assertEqual(tobj.xlist, ["foo", "bar", "baz"])
 def test_listproperty_save_restore(self):
     obj = TestModel()
     obj.xlist = ["foo", "bar", "baz"]
     obj.save()
     tobj = TestModel.get_by_id(obj.key().id())
     self.assertEqual(tobj, obj)
     self.assertEqual(tobj.xlist, ["foo", "bar", "baz"])
 def test_blobproperty_save_restore(self):
     obj = TestModel()
     obj.blob = db.Blob("test")
     obj.save()
     tobj = TestModel.get_by_id(obj.key().id())
     self.assertEqual(tobj, obj)
     self.assertEqual(obj.blob, "test")
     self.assertEqual(tobj.blob, "test")
     self.assert_(isinstance(tobj.blob, db.Blob))
示例#30
0
 def test_blobproperty_save_restore(self):
     obj = TestModel()
     obj.blob = db.Blob("test")
     obj.save()
     tobj = TestModel.get_by_id(obj.key().id())
     self.assertEqual(tobj, obj)
     self.assertEqual(obj.blob, "test")
     self.assertEqual(tobj.blob, "test")
     self.assert_(isinstance(tobj.blob, db.Blob))
示例#31
0
 def test_refkey(self):
     # Regression for issue23.
     i1 = TestModel2()
     i1.put()
     i2 = TestModel()
     i2.ref = i1
     i2.put()
     q = TestModel.all()
     q.filter('ref =', i1.key())
     res = list(q.fetch(1000))
     self.assertEqual(len(res), 1)
     self.assertEqual(str(res[0].key()), str(i2.key()))
 def test_refkey(self):
     # Regression for issue23.
     i1 = TestModel2()
     i1.put()
     i2 = TestModel()
     i2.ref = i1
     i2.put()
     q = TestModel.all()
     q.filter('ref =', i1.key())
     res = list(q.fetch(1000))
     self.assertEqual(len(res), 1)
     self.assertEqual(str(res[0].key()), str(i2.key()))
示例#33
0
class KeyTest(unittest.TestCase):
    def setUp(self):
        self._test = TestModel(xstring="foo")
        self._test.put()
        self._key = self._test.key()
        self._test_name = TestModel(xstring="foo", key_name="fookey")
        self._test_name.put()
        self._key_name = self._test_name.key()

    def tearDown(self):
        self._test.delete()
        self._test_name.delete()
        self._test = self._key = None
        self._test_name = self._key_name = None

    # constructor

    def test_constructor(self):
        self.assertRaises(TypeError, db.Key, ('foo', ), {'should': 'fail'})
        self.assertRaises(db.BadArgumentError, db.Key, (123, ))
        self.assertRaises(db.BadArgumentError, db.Key, (range(2), ))

    # class methods

    def test_from_path(self):
        self.assertRaises(db.BadArgumentError,
                          db.Key.from_path, ('foo', 1),
                          should='fail')
        self.assertRaises(db.BadArgumentError, db.Key.from_path, ('foo', ))
        self.assertEqual(
            db.Key.from_path('RegressionTestModel', self._test.id), self._key)
        k = db.Key.from_path('RegressionTestModel', 'fookey')
        self.assertEqual(db.Key.from_path('RegressionTestModel', 'fookey'),
                         self._key_name)

    # instance methods

    def test_app(self):
        self.assertEqual(self._key.app(), 'gae2django')

    def test_kind(self):
        self.assertEqual(self._key.kind(), 'RegressionTestModel')

    def test_id(self):
        self.assertEqual(self._key.id(), self._test.id)
        self.assertEqual(self._key_name.id(), None)

    def test_name(self):
        self.assertEqual(self._key_name.name(), 'fookey')
        self.assertEqual(self._key.name(), None)

    def test_id_or_name(self):
        self.assertEqual(self._key.id_or_name(), self._test.id)
        self.assertEqual(self._key_name.id_or_name(), 'fookey')

    def test_has_id_or_name(self):
        self.assertEqual(self._key.has_id_or_name(), True)
        self.assertEqual(self._key_name.has_id_or_name(), True)

    def test_parent(self):
        t1 = TestModel(xstring="child", parent=self._test)
        t1.put()
        self.assertEqual(t1.key().parent(), self._key)
        t1.delete()
        t2 = TestModel(xstring="child", parent=self._test_name)
        t2.put()
        self.assertEqual(t2.key().parent(), self._key_name)
        t2.delete()

    # internals

    def test_compare(self):
        # see issue8
        k1 = db.Key('foo1')
        k2 = db.Key('foo1')
        self.assert_(k1 == k2, '%r doesn\'t compare equal to %r' % (k1, k2))
        self.assert_(k2 in set([k1]), 'set comparsion failed')
        self.assert_(not k1 is k2, 'is comparsion failed')

    def test__str__(self):
        k1 = db.Key('foo1')
        self.assertEqual('foo1', str(k1))
示例#34
0
 def test_parent(self):
     t1 = TestModel(xstring="child", parent=self._test)
     t1.put()
     self.assertEqual(t1.key().parent(), self._key)
     t1.delete()
     t2 = TestModel(xstring="child", parent=self._test_name)
     t2.put()
     self.assertEqual(t2.key().parent(), self._key_name)
     t2.delete()
 def test_get(self):
     item1 = TestModel(key_name='test1')
     item1.put()
     self.assertEqual(TestModel.get('test1'), item1)
     self.assertEqual(TestModel.get('foo'), None)
     self.assertEqual(TestModel.get(['test1', 'test2']), [item1, None])
     item2 = TestModel(key_name='test2')
     item2.put()
     self.assertEqual(TestModel.get(['test1', 'test2']), [item1, item2])
     item1.delete()
     item2.delete()
示例#36
0
 def test_is_saved(self):
     item = TestModel()
     self.assertFalse(item.is_saved())
     item.put()
     self.assertTrue(item.is_saved())
 def test_properties(self):
     props = TestModel.properties()
     self.assert_('xstring' in props)
     self.assert_(isinstance(props['xstring'], db.StringProperty))
     self.assert_('gae_parent_id' not in props)
 def test_get_by_id(self):
     item1 = TestModel(xstring='test1')
     item1.put()
     self.assertEqual(TestModel.get_by_id(item1.key().id()), item1)
     self.assertEqual(TestModel.get_by_id(-1), None)
     self.assertEqual(TestModel.get_by_id([item1.key().id(), -1]),
                      [item1, None])
     item2 = TestModel(xstring='test2')
     item2.put()
     self.assertEqual(TestModel.get_by_id([item1.key().id(),
                                           item2.key().id()]),
                      [item1, item2])
     item1.delete()
     item2.delete()
 def test_is_saved(self):
     item = TestModel()
     self.assertFalse(item.is_saved())
     item.put()
     self.assertTrue(item.is_saved())
示例#40
0
 def test_get_by_key_name(self):
     self.assertEqual(TestModel.get_by_key_name('foo'), None)
     self.assertEqual(TestModel.get_by_key_name(['foo', 'bar']),
                      [None, None])
     item1 = TestModel(key_name='test1')
     item1.put()
     self.assertEqual(TestModel.get_by_key_name('test1'), item1)
     self.assertEqual(TestModel.get_by_key_name(['test1', 'test2']),
                      [item1, None])
     item2 = TestModel(key_name='test2')
     item2.put()
     self.assertEqual(TestModel.get_by_key_name(['test1', 'test2']),
                      [item1, item2])
     self.assertEqual(TestModel.get_by_key_name(['test1']),
                      [item1])
     item1.delete()
     item2.delete()
示例#41
0
 def test_get(self):
     item1 = TestModel(key_name='test1')
     item1.put()
     self.assertEqual(TestModel.get('test1'), item1)
     self.assertEqual(TestModel.get('foo'), None)
     self.assertEqual(TestModel.get(['test1', 'test2']), [item1, None])
     item2 = TestModel(key_name='test2')
     item2.put()
     self.assertEqual(TestModel.get(['test1', 'test2']), [item1, item2])
     item1.delete()
     item2.delete()
示例#42
0
class DatastoreModelTest(unittest.TestCase):

    def setUp(self):
        self.item1 = TestModel(key_name='foo1')
        self.item1.put()

    def tearDown(self):
        self.item1.delete()
        self.item1 = None

    # Class methods

    def test_get(self):
        item1 = TestModel(key_name='test1')
        item1.put()
        self.assertEqual(TestModel.get('test1'), item1)
        self.assertEqual(TestModel.get('foo'), None)
        self.assertEqual(TestModel.get(['test1', 'test2']), [item1, None])
        item2 = TestModel(key_name='test2')
        item2.put()
        self.assertEqual(TestModel.get(['test1', 'test2']), [item1, item2])
        item1.delete()
        item2.delete()

    def test_get_by_id(self):
        item1 = TestModel(xstring='test1')
        item1.put()
        self.assertEqual(TestModel.get_by_id(item1.key().id()), item1)
        self.assertEqual(TestModel.get_by_id(-1), None)
        self.assertEqual(TestModel.get_by_id([item1.key().id(), -1]),
                         [item1, None])
        item2 = TestModel(xstring='test2')
        item2.put()
        self.assertEqual(TestModel.get_by_id([item1.key().id(),
                                              item2.key().id()]),
                         [item1, item2])
        item1.delete()
        item2.delete()


    def test_get_by_key_name(self):
        self.assertEqual(TestModel.get_by_key_name('foo'), None)
        self.assertEqual(TestModel.get_by_key_name(['foo', 'bar']),
                         [None, None])
        item1 = TestModel(key_name='test1')
        item1.put()
        self.assertEqual(TestModel.get_by_key_name('test1'), item1)
        self.assertEqual(TestModel.get_by_key_name(['test1', 'test2']),
                         [item1, None])
        item2 = TestModel(key_name='test2')
        item2.put()
        self.assertEqual(TestModel.get_by_key_name(['test1', 'test2']),
                         [item1, item2])
        self.assertEqual(TestModel.get_by_key_name(['test1']),
                         [item1])
        item1.delete()
        item2.delete()

    def test_get_or_insert(self):
        item1 = TestModel.get_or_insert('test1', xstring='foo')
        self.assert_(isinstance(item1, TestModel))
        test = TestModel.get_or_insert('test1')
        self.assertEqual(item1, test)
        self.assertEqual(item1.xstring, 'foo')
        item1.delete()

    def test_all(self):
        self.assertEqual(len(TestModel.all()), 1)
        item1 = TestModel.get_or_insert('test1')
        self.assertEqual(len(TestModel.all()), 2)
        self.assert_(item1 in TestModel.all())
        item1.delete()

    def test_ancestor(self):
        grandpa = TestModel.get_or_insert('grandpa')
        dad = TestModel.get_or_insert('dad', parent=grandpa)
        TestModel.get_or_insert('junior', parent=dad)
        TestModel.get_or_insert('unrelated')
        q = TestModel.all()
        q.ancestor(grandpa)
        self.assertEqual(len(q), 2)
        q2 = TestModel.all()
        q2.ancestor(dad)
        self.assertEqual(len(q2), 1)
        # ancestor() accepts keys too
        q = TestModel.all()
        q.ancestor(grandpa.key())
        self.assertEqual(len(q), 2)
        q2 = TestModel.all()
        q2.ancestor(dad.key())
        self.assertEqual(len(q2), 1)

    def test_gql(self):
        item1 = TestModel.get_or_insert('test1', xstring='foo')
        item2 = TestModel.get_or_insert('test2', xstring='foo')
        results = TestModel.gql('WHERE xstring = \'foo\'')
        self.assertEqual(results.count(), 2)
        self.assert_(item1 in results)
        self.assert_(item2 in results)
        item1.delete()
        item2.delete()

    def test_kind(self):
        self.assertEqual(TestModel.kind(), TestModel._meta.db_table)

    def test_properties(self):
        props = TestModel.properties()
        self.assert_('xstring' in props)
        self.assert_(isinstance(props['xstring'], db.StringProperty))
        self.assert_('gae_parent_id' not in props)

    # Instance methods

    def test_key(self):
        key = self.item1.key()
        self.assert_(isinstance(key, db.Key))

    def test_key_multiple_calls(self):
        # make sure multiple calls to key() return the same instance of Key
        # see issue8
        key1 = self.item1.key()
        key2 = self.item1.key()
        self.assertEqual(id(key1), id(key2),
                         ('Multiple calls to Model.key() returned different'
                          ' objects: %s %s.' % (id(key1), id(key2))))

    def test_key_item_saved(self):
        # make sure that an instance of this model is saved at least
        # once before retrieving the key
        item = TestModel()
        self.assertRaises(db.NotSavedError, item.key)
        item.put()
        item.key()

    def test_is_saved(self):
        item = TestModel()
        self.assertFalse(item.is_saved())
        item.put()
        self.assertTrue(item.is_saved())
 def setUp(self):
     TestModel.all().delete()
     self.item1 = TestModel(key_name='foo1')
     self.item1.put()
 def test_kind(self):
     self.assertEqual(TestModel.kind(), TestModel._meta.db_table)
 def test_all(self):
     self.assertEqual(len(TestModel.all()), 1)
     item1 = TestModel.get_or_insert('test1')
     self.assertEqual(len(TestModel.all()), 2)
     self.assert_(item1 in TestModel.all())
     item1.delete()
示例#46
0
 def test_parent(self):
     t1 = TestModel(xstring="child", parent=self._test)
     t1.put()
     self.assertEqual(t1.key().parent(), self._key)
     t1.delete()
     t2 = TestModel(xstring="child", parent=self._test_name)
     t2.put()
     self.assertEqual(t2.key().parent(), self._key_name)
     t2.delete()
示例#47
0
 def test_all(self):
     self.assertEqual(len(TestModel.all()), 1)
     item1 = TestModel.get_or_insert('test1')
     self.assertEqual(len(TestModel.all()), 2)
     self.assert_(item1 in TestModel.all())
     item1.delete()
示例#48
0
 def setUp(self):
     TestModel.all().delete()
     self.item1 = TestModel(key_name='foo1')
     self.item1.put()
 def test_get_by_key_name(self):
     self.assertEqual(TestModel.get_by_key_name('foo'), None)
     self.assertEqual(TestModel.get_by_key_name(['foo', 'bar']),
                      [None, None])
     item1 = TestModel(key_name='test1')
     item1.put()
     self.assertEqual(TestModel.get_by_key_name('test1'), item1)
     self.assertEqual(TestModel.get_by_key_name(['test1', 'test2']),
                      [item1, None])
     item2 = TestModel(key_name='test2')
     item2.put()
     self.assertEqual(TestModel.get_by_key_name(['test1', 'test2']),
                      [item1, item2])
     self.assertEqual(TestModel.get_by_key_name(['test1']),
                      [item1])
     item1.delete()
     item2.delete()
示例#50
0
 def setUp(self):
     self.item1 = TestModel(key_name='foo1')
     self.item1.put()
示例#51
0
 def test_key_only(self):
     q = TestModel.all(keys_only=True)
     res = q.fetch(1000)
     self.assertEqual(len(res), 1)
     self.assertEqual(res[0], self.item1.id)
示例#52
0
 def test_get_by_id(self):
     item1 = TestModel(xstring='test1')
     item1.put()
     self.assertEqual(TestModel.get_by_id(item1.key().id()), item1)
     self.assertEqual(TestModel.get_by_id(-1), None)
     self.assertEqual(TestModel.get_by_id([item1.key().id(), -1]),
                      [item1, None])
     item2 = TestModel(xstring='test2')
     item2.put()
     self.assertEqual(TestModel.get_by_id([item1.key().id(),
                                           item2.key().id()]),
                      [item1, item2])
     item1.delete()
     item2.delete()
示例#53
0
 def test_kind(self):
     self.assertEqual(TestModel.kind(), TestModel._meta.db_table)
示例#54
0
class KeyTest(unittest.TestCase):

    def setUp(self):
        self._test = TestModel(xstring="foo")
        self._test.put()
        self._key = self._test.key()
        self._test_name = TestModel(xstring="foo", key_name="fookey")
        self._test_name.put()
        self._key_name = self._test_name.key()

    def tearDown(self):
        self._test.delete()
        self._test_name.delete()
        self._test = self._key = None
        self._test_name = self._key_name = None

    # constructor

    def test_constructor(self):
        self.assertRaises(TypeError, db.Key, ('foo',), {'should': 'fail'})
        self.assertRaises(db.BadArgumentError, db.Key, (123,))
        self.assertRaises(db.BadArgumentError, db.Key, (range(2),))

    # class methods

    def test_from_path(self):
        self.assertRaises(db.BadArgumentError, db.Key.from_path,
                          ('foo', 1), should='fail')
        self.assertRaises(db.BadArgumentError, db.Key.from_path,
                          ('foo',))
        self.assertEqual(db.Key.from_path('RegressionTestModel',
                                          self._test.id), self._key)
        k = db.Key.from_path('RegressionTestModel', 'fookey')
        self.assertEqual(db.Key.from_path('RegressionTestModel', 'fookey'),
                         self._key_name)

    # instance methods

    def test_app(self):
        self.assertEqual(self._key.app(), 'gae2django')

    def test_kind(self):
        self.assertEqual(self._key.kind(), 'RegressionTestModel')

    def test_id(self):
        self.assertEqual(self._key.id(), self._test.id)
        self.assertEqual(self._key_name.id(), None)

    def test_name(self):
        self.assertEqual(self._key_name.name(), 'fookey')
        self.assertEqual(self._key.name(), None)

    def test_id_or_name(self):
        self.assertEqual(self._key.id_or_name(), self._test.id)
        self.assertEqual(self._key_name.id_or_name(), 'fookey')

    def test_has_id_or_name(self):
        self.assertEqual(self._key.has_id_or_name(), True)
        self.assertEqual(self._key_name.has_id_or_name(), True)

    def test_parent(self):
        t1 = TestModel(xstring="child", parent=self._test)
        t1.put()
        self.assertEqual(t1.key().parent(), self._key)
        t1.delete()
        t2 = TestModel(xstring="child", parent=self._test_name)
        t2.put()
        self.assertEqual(t2.key().parent(), self._key_name)
        t2.delete()

    # internals

    def test_compare(self):
        # see issue8
        k1 = db.Key('foo1')
        k2 = db.Key('foo1')
        self.assert_(k1 == k2, '%r doesn\'t compare equal to %r' % (k1, k2))
        self.assert_(k2 in set([k1]), 'set comparsion failed')
        self.assert_(not k1 is k2, 'is comparsion failed')

    def test__str__(self):
        k1 = db.Key('foo1')
        self.assertEqual('foo1', str(k1))
示例#55
0
 def test_properties(self):
     props = TestModel.properties()
     self.assert_('xstring' in props)
     self.assert_(isinstance(props['xstring'], db.StringProperty))
     self.assert_('gae_parent_id' not in props)
 def setUp(self):
     self.item1 = TestModel(key_name='foo1')
     self.item1.put()
class DatastoreModelTest(unittest.TestCase):

    def setUp(self):
        self.item1 = TestModel(key_name='foo1')
        self.item1.put()

    def tearDown(self):
        self.item1.delete()
        self.item1 = None

    # Class methods

    def test_get(self):
        item1 = TestModel(key_name='test1')
        item1.put()
        self.assertEqual(TestModel.get('test1'), item1)
        self.assertEqual(TestModel.get('foo'), None)
        self.assertEqual(TestModel.get(['test1', 'test2']), [item1, None])
        item2 = TestModel(key_name='test2')
        item2.put()
        self.assertEqual(TestModel.get(['test1', 'test2']), [item1, item2])
        item1.delete()
        item2.delete()

    def test_get_by_id(self):
        item1 = TestModel(xstring='test1')
        item1.put()
        self.assertEqual(TestModel.get_by_id(item1.key().id()), item1)
        self.assertEqual(TestModel.get_by_id(-1), None)
        self.assertEqual(TestModel.get_by_id([item1.key().id(), -1]),
                         [item1, None])
        item2 = TestModel(xstring='test2')
        item2.put()
        self.assertEqual(TestModel.get_by_id([item1.key().id(),
                                              item2.key().id()]),
                         [item1, item2])
        item1.delete()
        item2.delete()


    def test_get_by_key_name(self):
        self.assertEqual(TestModel.get_by_key_name('foo'), None)
        self.assertEqual(TestModel.get_by_key_name(['foo', 'bar']),
                         [None, None])
        item1 = TestModel(key_name='test1')
        item1.put()
        self.assertEqual(TestModel.get_by_key_name('test1'), item1)
        self.assertEqual(TestModel.get_by_key_name(['test1', 'test2']),
                         [item1, None])
        item2 = TestModel(key_name='test2')
        item2.put()
        self.assertEqual(TestModel.get_by_key_name(['test1', 'test2']),
                         [item1, item2])
        self.assertEqual(TestModel.get_by_key_name(['test1']),
                         [item1])
        item1.delete()
        item2.delete()

    def test_get_or_insert(self):
        item1 = TestModel.get_or_insert('test1', xstring='foo')
        self.assert_(isinstance(item1, TestModel))
        test = TestModel.get_or_insert('test1')
        self.assertEqual(item1, test)
        self.assertEqual(item1.xstring, 'foo')
        item1.delete()

    def test_all(self):
        self.assertEqual(len(TestModel.all()), 1)
        item1 = TestModel.get_or_insert('test1')
        self.assertEqual(len(TestModel.all()), 2)
        self.assert_(item1 in TestModel.all())
        item1.delete()

    def test_ancestor(self):
        grandpa = TestModel.get_or_insert('grandpa')
        dad = TestModel.get_or_insert('dad', parent=grandpa)
        TestModel.get_or_insert('junior', parent=dad)
        TestModel.get_or_insert('unrelated')
        q = TestModel.all()
        q.ancestor(grandpa)
        self.assertEqual(len(q), 2)
        q2 = TestModel.all()
        q2.ancestor(dad)
        self.assertEqual(len(q2), 1)
        # ancestor() accepts keys too
        q = TestModel.all()
        q.ancestor(grandpa.key())
        self.assertEqual(len(q), 2)
        q2 = TestModel.all()
        q2.ancestor(dad.key())
        self.assertEqual(len(q2), 1)

    def test_gql(self):
        item1 = TestModel.get_or_insert('test1', xstring='foo')
        item2 = TestModel.get_or_insert('test2', xstring='foo')
        results = TestModel.gql('WHERE xstring = \'foo\'')
        self.assertEqual(results.count(), 2)
        self.assert_(item1 in results)
        self.assert_(item2 in results)
        item1.delete()
        item2.delete()

    def test_kind(self):
        self.assertEqual(TestModel.kind(), TestModel._meta.db_table)

    def test_properties(self):
        props = TestModel.properties()
        self.assert_('xstring' in props)
        self.assert_(isinstance(props['xstring'], db.StringProperty))
        self.assert_('gae_parent_id' not in props)
        # From a App Engine perspective this is wrong. Choices should
        # be ('foo', 'bar'). For Django this is right.
        self.assertEqual(props['choices'].choices,
                         [('foo', 'foo'), ('bar', 'bar')])

    def test_property_attribute_access(self):
        # model classes allow accessing properties as attributes
        self.assert_(isinstance(TestModel.xstring, db.StringProperty))

    # Instance methods

    def test_key(self):
        key = self.item1.key()
        self.assert_(isinstance(key, db.Key))

    def test_key_multiple_calls(self):
        # make sure multiple calls to key() return the same instance of Key
        # see issue8
        key1 = self.item1.key()
        key2 = self.item1.key()
        self.assertEqual(id(key1), id(key2),
                         ('Multiple calls to Model.key() returned different'
                          ' objects: %s %s.' % (id(key1), id(key2))))

    def test_key_item_saved(self):
        # make sure that an instance of this model is saved at least
        # once before retrieving the key
        item = TestModel()
        self.assertRaises(db.NotSavedError, item.key)
        item.put()
        item.key()

    def test_is_saved(self):
        item = TestModel()
        self.assertFalse(item.is_saved())
        item.put()
        self.assertTrue(item.is_saved())