⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 base.py

📁 Python的一个ORM,现在很火
💻 PY
📖 第 1 页 / 共 5 页
字号:
## Copyright (c) 2006, 2007 Canonical## Written by Gustavo Niemeyer <gustavo@niemeyer.net>## This file is part of Storm Object Relational Mapper.## Storm is free software; you can redistribute it and/or modify# it under the terms of the GNU Lesser General Public License as# published by the Free Software Foundation; either version 2.1 of# the License, or (at your option) any later version.## Storm is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the# GNU Lesser General Public License for more details.## You should have received a copy of the GNU Lesser General Public License# along with this program.  If not, see <http://www.gnu.org/licenses/>.#import gcfrom storm.references import Reference, ReferenceSet, Proxyfrom storm.database import Resultfrom storm.properties import Int, Float, Chars, Unicode, Property, Picklefrom storm.properties import PropertyPublisherMetafrom storm.expr import Asc, Desc, Select, Func, LeftJoin, SQLfrom storm.variables import Variable, UnicodeVariable, IntVariablefrom storm.info import get_obj_info, ClassAliasfrom storm.exceptions import *from storm.store import *from tests.helper import run_thisclass Foo(object):    __storm_table__ = "foo"    id = Int(primary=True)    title = Unicode()class Bar(object):    __storm_table__ = "bar"    id = Int(primary=True)    title = Unicode()    foo_id = Int()    foo = Reference(foo_id, Foo.id)class Blob(object):    __storm_table__ = "bin"    id = Int(primary=True)    bin = Chars()class Link(object):    __storm_table__ = "link"    __storm_primary__ = "foo_id", "bar_id"    foo_id = Int()    bar_id = Int()class FooRef(Foo):    bar = Reference(Foo.id, Bar.foo_id)class FooRefSet(Foo):    bars = ReferenceSet(Foo.id, Bar.foo_id)class FooRefSetOrderID(Foo):    bars = ReferenceSet(Foo.id, Bar.foo_id, order_by=Bar.id)class FooRefSetOrderTitle(Foo):    bars = ReferenceSet(Foo.id, Bar.foo_id, order_by=Bar.title)class FooIndRefSet(Foo):    bars = ReferenceSet(Foo.id, Link.foo_id, Link.bar_id, Bar.id)class FooIndRefSetOrderID(Foo):    bars = ReferenceSet(Foo.id, Link.foo_id, Link.bar_id, Bar.id,                        order_by=Bar.id)class FooIndRefSetOrderTitle(Foo):    bars = ReferenceSet(Foo.id, Link.foo_id, Link.bar_id, Bar.id,                        order_by=Bar.title)class BarProxy(object):    __storm_table__ = "bar"    id = Int(primary=True)    title = Unicode()    foo_id = Int()    foo = Reference(foo_id, Foo.id)    foo_title = Proxy(foo, Foo.title)class DecorateVariable(Variable):    def _parse_get(self, value, to_db):        return u"to_%s(%s)" % (to_db and "db" or "py", value)    def _parse_set(self, value, from_db):        return u"from_%s(%s)" % (from_db and "db" or "py", value)class FooVariable(Foo):    title = Property(variable_class=DecorateVariable)class Wrapper(object):    def __init__(self, obj):        self.obj = objWrapper.__object_info = property(lambda self: self.obj.__object_info)class StoreTest(object):    def setUp(self):        self.create_database()        self.drop_tables()        self.create_tables()        self.create_sample_data()        self.create_store()        def tearDown(self):        self.drop_store()        self.drop_sample_data()        self.drop_tables()        self.drop_database()    def create_database(self):        raise NotImplementedError    def create_tables(self):        raise NotImplementedError    def create_sample_data(self):        connection = self.database.connect()        connection.execute("INSERT INTO foo VALUES (10, 'Title 30')")        connection.execute("INSERT INTO foo VALUES (20, 'Title 20')")        connection.execute("INSERT INTO foo VALUES (30, 'Title 10')")        connection.execute("INSERT INTO bar VALUES (100, 10, 'Title 300')")        connection.execute("INSERT INTO bar VALUES (200, 20, 'Title 200')")        connection.execute("INSERT INTO bar VALUES (300, 30, 'Title 100')")        connection.execute("INSERT INTO bin VALUES (10, 'Blob 30')")        connection.execute("INSERT INTO bin VALUES (20, 'Blob 20')")        connection.execute("INSERT INTO bin VALUES (30, 'Blob 10')")        connection.execute("INSERT INTO link VALUES (10, 100)")        connection.execute("INSERT INTO link VALUES (10, 200)")        connection.execute("INSERT INTO link VALUES (10, 300)")        connection.execute("INSERT INTO link VALUES (20, 100)")        connection.execute("INSERT INTO link VALUES (20, 200)")        connection.execute("INSERT INTO link VALUES (30, 300)")        connection.commit()    def create_store(self):        self.store = Store(self.database)    def drop_store(self):        self.store.rollback()        # Closing the store is needed because testcase objects are all        # instantiated at once, and thus connections are kept open.        self.store.close()    def drop_sample_data(self):        pass    def drop_tables(self):        for table in ["foo", "bar", "bin", "link"]:            connection = self.database.connect()            try:                connection.execute("DROP TABLE %s" % table)                connection.commit()            except:                connection.rollback()    def drop_database(self):        pass    def get_items(self):        # Bypass the store to avoid flushing.        connection = self.store._connection        result = connection.execute("SELECT * FROM foo ORDER BY id")        return list(result)    def get_committed_items(self):        connection = self.database.connect()        result = connection.execute("SELECT * FROM foo ORDER BY id")        return list(result)    def test_execute(self):        result = self.store.execute("SELECT 1")        self.assertTrue(isinstance(result, Result))        self.assertEquals(result.get_one(), (1,))                result = self.store.execute("SELECT 1", noresult=True)        self.assertEquals(result, None)    def test_execute_params(self):        result = self.store.execute("SELECT ?", [1])        self.assertTrue(isinstance(result, Result))        self.assertEquals(result.get_one(), (1,))    def test_execute_flushes(self):        foo = self.store.get(Foo, 10)        foo.title = u"New Title"        result = self.store.execute("SELECT title FROM foo WHERE id=10")        self.assertEquals(result.get_one(), ("New Title",))    def test_close(self):        store = Store(self.database)        store.close()        self.assertRaises(ClosedError, store.execute, "SELECT 1")    def test_get(self):        foo = self.store.get(Foo, 10)        self.assertEquals(foo.id, 10)        self.assertEquals(foo.title, "Title 30")        foo = self.store.get(Foo, 20)        self.assertEquals(foo.id, 20)        self.assertEquals(foo.title, "Title 20")        foo = self.store.get(Foo, 40)        self.assertEquals(foo, None)    def test_get_cached(self):        foo = self.store.get(Foo, 10)        self.assertTrue(self.store.get(Foo, 10) is foo)    def test_wb_get_cached_doesnt_need_connection(self):        foo = self.store.get(Foo, 10)        connection = self.store._connection        self.store._connection = None        self.store.get(Foo, 10)        self.store._connection = connection    def test_cache_cleanup(self):        foo = self.store.get(Foo, 10)        foo.taint = True        del foo        gc.collect()        foo = self.store.get(Foo, 10)        self.assertFalse(getattr(foo, "taint", False))    def test_add_returns_object(self):        """        Store.add() returns the object passed to it.  This allows this        kind of code:        thing = Thing()        store.add(thing)        return thing        to be simplified as:        return store.add(Thing())        """        foo = Foo()        self.assertEquals(self.store.add(foo), foo)    def test_add_and_stop_referencing(self):        # After adding an object, no references should be needed in        # python for it still to be added to the database.        foo = Foo()        foo.title = u"live"        self.store.add(foo)        del foo        gc.collect()        self.assertTrue(self.store.find(Foo, title=u"live").one())    def test_obj_info_with_deleted_object(self):        # Let's try to put Storm in trouble by killing the object        # while still holding a reference to the obj_info.        class MyFoo(Foo):            loaded = False            def __storm_loaded__(self):                self.loaded = True        foo = self.store.get(MyFoo, 20)        foo.tainted = True        obj_info = get_obj_info(foo)        del foo        gc.collect()                self.assertEquals(obj_info.get_obj(), None)        foo = self.store.find(MyFoo, id=20).one()        self.assertTrue(foo)        self.assertFalse(getattr(foo, "tainted", False))        # The object was rebuilt, so the loaded hook must have run.        self.assertTrue(foo.loaded)    def test_obj_info_with_deleted_object_with_get(self):        # Same thing, but using get rather than find.        foo = self.store.get(Foo, 20)        foo.tainted = True        obj_info = get_obj_info(foo)        del foo        gc.collect()                self.assertEquals(obj_info.get_obj(), None)        foo = self.store.get(Foo, 20)        self.assertTrue(foo)        self.assertFalse(getattr(foo, "tainted", False))    def test_delete_object_when_obj_info_is_dirty(self):        # Object should stay in memory if dirty.        foo = self.store.get(Foo, 20)        foo.title = u"Changed"        foo.tainted = True        obj_info = get_obj_info(foo)        del foo        gc.collect()                self.assertTrue(obj_info.get_obj())    def test_get_tuple(self):        class MyFoo(Foo):            __storm_primary__ = "title", "id"        foo = self.store.get(MyFoo, (u"Title 30", 10))        self.assertEquals(foo.id, 10)        self.assertEquals(foo.title, "Title 30")        foo = self.store.get(MyFoo, (u"Title 20", 10))        self.assertEquals(foo, None)    def test_of(self):        foo = self.store.get(Foo, 10)        self.assertEquals(Store.of(foo), self.store)        self.assertEquals(Store.of(Foo()), None)        self.assertEquals(Store.of(object()), None)    def test_find_iter(self):        result = self.store.find(Foo)        lst = [(foo.id, foo.title) for foo in result]        lst.sort()        self.assertEquals(lst, [                          (10, "Title 30"),                          (20, "Title 20"),                          (30, "Title 10"),                         ])    def test_find_from_cache(self):        foo = self.store.get(Foo, 10)        self.assertTrue(self.store.find(Foo, id=10).one() is foo)    def test_find_expr(self):        result = self.store.find(Foo, Foo.id == 20,                                 Foo.title == u"Title 20")        self.assertEquals([(foo.id, foo.title) for foo in result], [                          (20, "Title 20"),                         ])        result = self.store.find(Foo, Foo.id == 10,                                 Foo.title == u"Title 20")        self.assertEquals([(foo.id, foo.title) for foo in result], [                         ])    def test_find_sql(self):        foo = self.store.find(Foo, SQL("foo.id = 20")).one()        self.assertEquals(foo.title, "Title 20")    def test_find_str(self):        foo = self.store.find(Foo, "foo.id = 20").one()        self.assertEquals(foo.title, "Title 20")    def test_find_keywords(self):        result = self.store.find(Foo, id=20, title=u"Title 20")        self.assertEquals([(foo.id, foo.title) for foo in result], [                          (20, u"Title 20")                         ])        result = self.store.find(Foo, id=10, title=u"Title 20")        self.assertEquals([(foo.id, foo.title) for foo in result], [                         ])    def test_find_order_by(self, *args):        result = self.store.find(Foo).order_by(Foo.title)        lst = [(foo.id, foo.title) for foo in result]        self.assertEquals(lst, [                          (30, "Title 10"),                          (20, "Title 20"),                          (10, "Title 30"),                         ])    def test_find_order_asc(self, *args):        result = self.store.find(Foo).order_by(Asc(Foo.title))        lst = [(foo.id, foo.title) for foo in result]        self.assertEquals(lst, [                          (30, "Title 10"),                          (20, "Title 20"),                          (10, "Title 30"),                         ])

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -