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

📄 properties.py

📁 Python的一个ORM,现在很火
💻 PY
📖 第 1 页 / 共 3 页
字号:
        self.obj.prop2 = []        self.obj_info.checkpoint()        self.obj_info.event.hook("changed", changed)        self.assertEquals(self.obj.prop1, [])        self.assertEquals(changes, [])        self.obj.prop1.append("a")        self.assertEquals(changes, [])        # Check "flush" event. Notice that the other variable wasn't        # listed, since it wasn't changed.        self.obj_info.event.emit("flush")        self.assertEquals(changes, [(self.variable1, None, ["a"], False)])        del changes[:]        # Check "object-deleted" event. Notice that the other variable        # wasn't listed again, since it wasn't changed.        del self.obj        self.assertEquals(changes, [(self.variable1, None, ["a"], False)])    def test_list(self):        self.setup(List, default_factory=list, allow_none=False)        self.assertTrue(isinstance(self.column1, Column))        self.assertTrue(isinstance(self.column2, Column))        self.assertEquals(self.column1.name, "column1")        self.assertEquals(self.column1.table, self.SubClass)        self.assertEquals(self.column2.name, "prop2")        self.assertEquals(self.column2.table, self.SubClass)        self.assertTrue(isinstance(self.variable1, ListVariable))        self.assertTrue(isinstance(self.variable2, ListVariable))        self.assertEquals(self.obj.prop1, [])        self.assertRaises(NoneError, setattr, self.obj, "prop1", None)        self.obj.prop2 = None        self.assertEquals(self.obj.prop2, None)        self.obj.prop1 = ["a"]        self.assertEquals(self.obj.prop1, ["a"])        self.obj.prop1.append("b")        self.assertEquals(self.obj.prop1, ["a", "b"])    def test_list_events(self):        self.setup(List, default_factory=list, allow_none=False)        changes = []        def changed(owner, variable, old_value, new_value, fromdb):            changes.append((variable, old_value, new_value, fromdb))        self.obj_info.checkpoint()        self.obj_info.event.hook("changed", changed)        self.assertEquals(self.obj.prop1, [])        self.assertEquals(changes, [])        self.obj.prop1.append("a")        self.assertEquals(changes, [])        # Check "flush" event. Notice that the other variable wasn't        # listed, since it wasn't changed.        self.obj_info.event.emit("flush")        self.assertEquals(changes, [(self.variable1, None, ["a"], False)])        del changes[:]        # Check "object-deleted" event. Notice that the other variable        # wasn't listed again, since it wasn't changed.        del self.obj        self.assertEquals(changes, [(self.variable1, None, ["a"], False)])    def test_variable_factory_arguments(self):        class Class(object):            __storm_table__ = "test"        for func, cls, value in [                               (Bool, BoolVariable, True),                               (Int, IntVariable, 1),                               (Float, FloatVariable, 1.1),                               (Chars, CharsVariable, "str"),                               (Unicode, UnicodeVariable, u"unicode"),                               (DateTime, DateTimeVariable, datetime.now()),                               (Date, DateVariable, date.today()),                               (Time, TimeVariable, datetime.now().time()),                               (Pickle, PickleVariable, {}),                                     ]:            # Test no default and allow_none=True.            prop = func(name="name")            column = prop.__get__(None, Class)            self.assertEquals(column.name, "name")            self.assertEquals(column.table, Class)            variable = column.variable_factory()            self.assertTrue(isinstance(variable, cls))            self.assertEquals(variable.get(), None)            variable.set(None)            self.assertEquals(variable.get(), None)            # Test default and allow_none=False.            prop = func(name="name", default=value, allow_none=False)            column = prop.__get__(None, Class)            self.assertEquals(column.name, "name")            self.assertEquals(column.table, Class)            variable = column.variable_factory()            self.assertTrue(isinstance(variable, cls))            self.assertRaises(NoneError, variable.set, None)            self.assertEquals(variable.get(), value)            # Test default_factory.            prop = func(name="name", default_factory=lambda:value)            column = prop.__get__(None, Class)            self.assertEquals(column.name, "name")            self.assertEquals(column.table, Class)            variable = column.variable_factory()            self.assertTrue(isinstance(variable, cls))            self.assertEquals(variable.get(), value)class PropertyRegistryTest(TestHelper):    def setUp(self):        TestHelper.setUp(self)        class Class(object):            __storm_table__ = "table"            prop1 = Property("column1", primary=True)            prop2 = Property()        class SubClass(Class):            __storm_table__ = "subtable"        self.Class = Class        self.SubClass = SubClass        self.AnotherClass = type("Class", (Class,), {})        self.registry = PropertyRegistry()    def test_get_empty(self):        self.assertRaises(PropertyPathError, self.registry.get, "unexistent")    def test_get(self):        self.registry.add_class(self.Class)        prop1 = self.registry.get("prop1")        prop2 = self.registry.get("prop2")        self.assertTrue(prop1 is self.Class.prop1)        self.assertTrue(prop2 is self.Class.prop2)    def test_get_with_class_name(self):        self.registry.add_class(self.Class)        prop1 = self.registry.get("Class.prop1")        prop2 = self.registry.get("Class.prop2")        self.assertTrue(prop1 is self.Class.prop1)        self.assertTrue(prop2 is self.Class.prop2)    def test_get_with_two_classes(self):        self.registry.add_class(self.Class)        self.registry.add_class(self.SubClass)        prop1 = self.registry.get("Class.prop1")        prop2 = self.registry.get("Class.prop2")        self.assertTrue(prop1 is self.Class.prop1)        self.assertTrue(prop2 is self.Class.prop2)        prop1 = self.registry.get("SubClass.prop1")        prop2 = self.registry.get("SubClass.prop2")        self.assertTrue(prop1 is self.SubClass.prop1)        self.assertTrue(prop2 is self.SubClass.prop2)    def test_get_ambiguous(self):        self.AnotherClass.__module__ += ".foo"        self.registry.add_class(self.Class)        self.registry.add_class(self.SubClass)        self.registry.add_class(self.AnotherClass)        self.assertRaises(PropertyPathError, self.registry.get, "Class.prop1")        self.assertRaises(PropertyPathError, self.registry.get, "Class.prop2")        prop1 = self.registry.get("SubClass.prop1")        prop2 = self.registry.get("SubClass.prop2")        self.assertTrue(prop1 is self.SubClass.prop1)        self.assertTrue(prop2 is self.SubClass.prop2)    def test_get_ambiguous_but_different_path(self):        self.AnotherClass.__module__ += ".foo"        self.registry.add_class(self.Class)        self.registry.add_class(self.SubClass)        self.registry.add_class(self.AnotherClass)        prop1 = self.registry.get("properties.Class.prop1")        prop2 = self.registry.get("properties.Class.prop2")        self.assertTrue(prop1 is self.Class.prop1)        self.assertTrue(prop2 is self.Class.prop2)        prop1 = self.registry.get("SubClass.prop1")        prop2 = self.registry.get("SubClass.prop2")        self.assertTrue(prop1 is self.SubClass.prop1)        self.assertTrue(prop2 is self.SubClass.prop2)        prop1 = self.registry.get("foo.Class.prop1")        prop2 = self.registry.get("foo.Class.prop2")        self.assertTrue(prop1 is self.AnotherClass.prop1)        self.assertTrue(prop2 is self.AnotherClass.prop2)    def test_get_ambiguous_but_different_path_with_namespace(self):        self.AnotherClass.__module__ += ".foo"        self.registry.add_class(self.Class)        self.registry.add_class(self.SubClass)        self.registry.add_class(self.AnotherClass)        prop1 = self.registry.get("Class.prop1", "tests.properties")        prop2 = self.registry.get("Class.prop2", "tests.properties.bar")        self.assertTrue(prop1 is self.Class.prop1)        self.assertTrue(prop2 is self.Class.prop2)        prop1 = self.registry.get("Class.prop1", "tests.properties.foo")        prop2 = self.registry.get("Class.prop2", "tests.properties.foo.bar")        self.assertTrue(prop1 is self.AnotherClass.prop1)        self.assertTrue(prop2 is self.AnotherClass.prop2)    def test_class_is_collectable(self):        self.AnotherClass.__module__ += ".foo"        self.registry.add_class(self.Class)        self.registry.add_class(self.AnotherClass)        del self.AnotherClass        gc.collect()        prop1 = self.registry.get("prop1")        prop2 = self.registry.get("prop2")        self.assertTrue(prop1 is self.Class.prop1)        self.assertTrue(prop2 is self.Class.prop2)    def test_add_property(self):        self.registry.add_property(self.Class, self.Class.prop1, "custom_name")        prop1 = self.registry.get("Class.custom_name")        self.assertEquals(prop1, self.Class.prop1)        self.assertRaises(PropertyPathError, self.registry.get, "Class.prop1")class PropertyPublisherMetaTest(TestHelper):    def setUp(self):        TestHelper.setUp(self)        class Base(object):            __metaclass__ = PropertyPublisherMeta        class Class(Base):            __storm_table__ = "table"            prop1 = Property("column1", primary=True)            prop2 = Property()        class SubClass(Class):            __storm_table__ = "subtable"        self.Class = Class        self.SubClass = SubClass        class Class(Class):            __module__ += ".foo"            prop3 = Property("column3")        self.AnotherClass = Class        self.registry = Base._storm_property_registry    def test_get_empty(self):        self.assertRaises(PropertyPathError, self.registry.get, "unexistent")    def test_get_subclass(self):        prop1 = self.registry.get("SubClass.prop1")        prop2 = self.registry.get("SubClass.prop2")        self.assertTrue(prop1 is self.SubClass.prop1)        self.assertTrue(prop2 is self.SubClass.prop2)    def test_get_ambiguous(self):        self.assertRaises(PropertyPathError, self.registry.get, "Class.prop1")        self.assertRaises(PropertyPathError, self.registry.get, "Class.prop2")

⌨️ 快捷键说明

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