def test_history_populated_passive_return_never_set(self):
     User, Address, sess, a1 = self._u_ad_fixture(True)
     eq_(
         Address.user.impl.get_history(
             attributes.instance_state(a1),
             attributes.instance_dict(a1),
             passive=attributes.PASSIVE_RETURN_NEVER_SET,
         ),
         ((), [User(name="ed")], ()),
     )
 def test_history_populated_passive_no_initialize(self):
     User, Address, sess, a1 = self._u_ad_fixture(True)
     eq_(
         Address.user.impl.get_history(
             attributes.instance_state(a1),
             attributes.instance_dict(a1),
             passive=attributes.PASSIVE_NO_INITIALIZE,
         ),
         attributes.HISTORY_BLANK,
     )
     assert "user_id" not in a1.__dict__
     assert "user" not in a1.__dict__
 def test_get_empty_passive_no_initialize(self):
     User, Address, sess, a1 = self._u_ad_fixture(False)
     eq_(
         Address.user.impl.get(
             attributes.instance_state(a1),
             attributes.instance_dict(a1),
             passive=attributes.PASSIVE_NO_INITIALIZE,
         ),
         attributes.PASSIVE_NO_RESULT,
     )
     assert "user_id" not in a1.__dict__
     assert "user" not in a1.__dict__
 def test_history_empty_passive_return_never_set(self):
     User, Address, sess, a1 = self._u_ad_fixture(False)
     eq_(
         Address.user.impl.get_history(
             attributes.instance_state(a1),
             attributes.instance_dict(a1),
             passive=attributes.PASSIVE_RETURN_NEVER_SET,
         ),
         ((), (), ()),
     )
     assert "user_id" not in a1.__dict__
     assert "user" not in a1.__dict__
    def test_basic(self):
        for base in (object, MyBaseClass, MyClass):

            class User(base):
                pass

            register_class(User)
            attributes.register_attribute(User,
                                          "user_id",
                                          uselist=False,
                                          useobject=False)
            attributes.register_attribute(User,
                                          "user_name",
                                          uselist=False,
                                          useobject=False)
            attributes.register_attribute(User,
                                          "email_address",
                                          uselist=False,
                                          useobject=False)

            u = User()
            u.user_id = 7
            u.user_name = "john"
            u.email_address = "*****@*****.**"

            eq_(u.user_id, 7)
            eq_(u.user_name, "john")
            eq_(u.email_address, "*****@*****.**")
            attributes.instance_state(u)._commit_all(
                attributes.instance_dict(u))
            eq_(u.user_id, 7)
            eq_(u.user_name, "john")
            eq_(u.email_address, "*****@*****.**")

            u.user_name = "heythere"
            u.email_address = "*****@*****.**"
            eq_(u.user_id, 7)
            eq_(u.user_name, "heythere")
            eq_(u.email_address, "*****@*****.**")
    def test_history(self):
        for base in (object, MyBaseClass, MyClass):

            class Foo(base):
                pass

            class Bar(base):
                pass

            register_class(Foo)
            register_class(Bar)
            attributes.register_attribute(Foo,
                                          "name",
                                          uselist=False,
                                          useobject=False)
            attributes.register_attribute(Foo,
                                          "bars",
                                          uselist=True,
                                          trackparent=True,
                                          useobject=True)
            attributes.register_attribute(Bar,
                                          "name",
                                          uselist=False,
                                          useobject=False)

            f1 = Foo()
            f1.name = "f1"

            eq_(
                attributes.get_state_history(attributes.instance_state(f1),
                                             "name"),
                (["f1"], (), ()),
            )

            b1 = Bar()
            b1.name = "b1"
            f1.bars.append(b1)
            eq_(
                attributes.get_state_history(attributes.instance_state(f1),
                                             "bars"),
                ([b1], [], []),
            )

            attributes.instance_state(f1)._commit_all(
                attributes.instance_dict(f1))
            attributes.instance_state(b1)._commit_all(
                attributes.instance_dict(b1))

            eq_(
                attributes.get_state_history(attributes.instance_state(f1),
                                             "name"),
                ((), ["f1"], ()),
            )
            eq_(
                attributes.get_state_history(attributes.instance_state(f1),
                                             "bars"),
                ((), [b1], ()),
            )

            f1.name = "f1mod"
            b2 = Bar()
            b2.name = "b2"
            f1.bars.append(b2)
            eq_(
                attributes.get_state_history(attributes.instance_state(f1),
                                             "name"),
                (["f1mod"], (), ["f1"]),
            )
            eq_(
                attributes.get_state_history(attributes.instance_state(f1),
                                             "bars"),
                ([b2], [b1], []),
            )
            f1.bars.remove(b1)
            eq_(
                attributes.get_state_history(attributes.instance_state(f1),
                                             "bars"),
                ([b2], [], [b1]),
            )
    def test_deferred(self):
        for base in (object, MyBaseClass, MyClass):

            class Foo(base):
                pass

            data = {"a": "this is a", "b": 12}

            def loader(state, keys):
                for k in keys:
                    state.dict[k] = data[k]
                return attributes.ATTR_WAS_SET

            manager = register_class(Foo)
            manager.deferred_scalar_loader = loader
            attributes.register_attribute(Foo,
                                          "a",
                                          uselist=False,
                                          useobject=False)
            attributes.register_attribute(Foo,
                                          "b",
                                          uselist=False,
                                          useobject=False)

            if base is object:
                assert Foo not in (
                    instrumentation._instrumentation_factory._state_finders)
            else:
                assert Foo in (
                    instrumentation._instrumentation_factory._state_finders)

            f = Foo()
            attributes.instance_state(f)._expire(attributes.instance_dict(f),
                                                 set())
            eq_(f.a, "this is a")
            eq_(f.b, 12)

            f.a = "this is some new a"
            attributes.instance_state(f)._expire(attributes.instance_dict(f),
                                                 set())
            eq_(f.a, "this is a")
            eq_(f.b, 12)

            attributes.instance_state(f)._expire(attributes.instance_dict(f),
                                                 set())
            f.a = "this is another new a"
            eq_(f.a, "this is another new a")
            eq_(f.b, 12)

            attributes.instance_state(f)._expire(attributes.instance_dict(f),
                                                 set())
            eq_(f.a, "this is a")
            eq_(f.b, 12)

            del f.a
            eq_(f.a, None)
            eq_(f.b, 12)

            attributes.instance_state(f)._commit_all(
                attributes.instance_dict(f))
            eq_(f.a, None)
            eq_(f.b, 12)