Example #1
0
 def _thread_is_not_in_session(self, event):
     with pytest.raises(InvalidRequestError):
         User(name='bob').save()
     with self.session:
         User(name='bob').save()
         self.session.flush()
         assert User.query.count() == 1
     event.set()
     return 'ok'
Example #2
0
    def test_get_or_create(self):
        with self.session:
            a, a_created = User.get_or_create(name='bob')
            assert a_created

        with self.session:
            b, b_created = User.get_or_create(name='bob')
            assert not b_created

        assert a.id == b.id
Example #3
0
    def test_nested_rollback(self):
        with pytest.raises(ValueError):
            with self.session:
                User(name='bob').save()
                with self.session:
                    User(name='fred').save()
                    self.session.flush()
                    raise ValueError

        assert not self.session._registry()._depth
Example #4
0
    def test_object_remains_associated_across_transactions(self):
        with self.session:
            bob = User(name='bob').save()

        with self.session:
            assert User.query.count() == 1
            User(name='fred', friend=bob).save()

        with self.session:
            assert User.query.count() == 2
Example #5
0
    def test_get_or_create(self):
        with self.session:
            a, a_created = User.get_or_create(name='bob')
            assert a_created

        with self.session:
            b, b_created = User.get_or_create(name='bob')
            assert not b_created

        assert a.id == b.id
Example #6
0
    def test_explicit_rollback(self):
        with self.session:
            User(name='bob').save()
            self.session.rollback()

        with self.session:
            assert User.query.filter_by(name='bob').count() == 0
Example #7
0
    def test_can_query_object_from_parent_transaction_when_nested(self):
        with self.session as session:
            User(name='bob').save()

            with session:
                assert User.query.count() == 1

        with self.session:
            assert User.query.count() == 1
Example #8
0
    def test_can_query_within_context_manager(self):
        with self.session:
            User(name='bob').save()

        with self.session:
            assert User.query.count() == 1
Example #9
0
    def test_can_not_query_outside_context_manager(self):
        with self.session:
            User(name='bob').save()

        with pytest.raises(InvalidRequestError):
            User.query.count()
Example #10
0
 def test_can_not_save_outside_context_manager(self):
     with pytest.raises(InvalidRequestError):
         User(name='bob').save()