def test_listens_for_not_initialized(self, app, db):
        with create_pgevents() as pg:

            class Widget(db.Model):
                __tablename__ = "widget"
                id = db.Column(db.Integer, primary_key=True)

            create_all(db)

            @pg.listens_for(Widget, {"insert"})
            def callback(row_id, identifier):
                pass

            assert "public.widget" in pg._triggers
            assert len(pg._triggers["public.widget"]) == 1
            trigger = pg._triggers["public.widget"][0]
            assert not trigger.installed
            assert trigger.target == Widget
            assert trigger.events == {"insert"}
            assert trigger.callback == callback

            with create_connection(db, raw=True) as conn:
                trigger_installed_ = trigger_installed(conn, "widget")
                assert not trigger_installed_

            pg.init_app(app)

            assert trigger.installed

            with create_connection(db, raw=True) as conn:
                trigger_installed_ = trigger_installed(conn, "widget")
                assert trigger_installed_
Beispiel #2
0
    def test_listens_for_not_initialized(self, app, db):
        with create_pgevents() as pg:

            class Widget(db.Model):
                __tablename__ = 'widget'
                id = db.Column(db.Integer, primary_key=True)

            create_all(db)

            @pg.listens_for(Widget, {'insert'})
            def callback(row_id, identifier):
                pass

            assert ('public.widget' in pg._triggers)
            assert (len(pg._triggers['public.widget']) == 1)
            trigger = pg._triggers['public.widget'][0]
            assert (trigger.installed == False)
            assert (trigger.target == Widget)
            assert (trigger.events == {'insert'})
            assert (trigger.callback == callback)

            with create_connection(db, raw=True) as conn:
                trigger_installed_ = trigger_installed(conn, 'widget')
                assert (trigger_installed_ == False)

            pg.init_app(app)

            assert (trigger.installed == True)

            with create_connection(db, raw=True) as conn:
                trigger_installed_ = trigger_installed(conn, 'widget')
                assert (trigger_installed_ == True)
    def test_listen_mixed(self, app, db):
        class Widget(db.Model):
            __tablename__ = "widget"
            id = db.Column(db.Integer, primary_key=True)

        db.session.execute(CreateSchema("private"))
        db.session.commit()

        class Gadget(db.Model):
            __tablename__ = "gadget"
            __table_args__ = {"schema": "private"}
            id = db.Column(db.Integer, primary_key=True)

        create_all(db)

        def widget_callback(row_id, identifier):
            pass

        def gadget_callback(row_id, identifier):
            pass

        with create_pgevents() as pg:
            pg.listen(Widget, {"insert"}, widget_callback)

            assert "public.widget" in pg._triggers
            assert len(pg._triggers["public.widget"]) == 1

            widget_trigger = pg._triggers["public.widget"][0]
            assert not widget_trigger.installed
            assert widget_trigger.target == Widget
            assert widget_trigger.events == {"insert"}
            assert widget_trigger.callback == widget_callback

            with create_connection(db, raw=True) as conn:
                trigger_installed_ = trigger_installed(conn, "widget")
                assert not trigger_installed_

            pg.init_app(app)

            assert widget_trigger.installed

            with create_connection(db, raw=True) as conn:
                trigger_installed_ = trigger_installed(conn, "widget")
                assert trigger_installed_

            pg.listen(Gadget, {"delete"}, gadget_callback)

            assert "private.gadget" in pg._triggers
            assert len(pg._triggers["private.gadget"]) == 1

            trigger = pg._triggers["private.gadget"][0]
            assert trigger.installed
            assert trigger.target == Gadget
            assert trigger.events == {"delete"}
            assert trigger.callback == gadget_callback

            with create_connection(db, raw=True) as conn:
                trigger_installed_ = trigger_installed(conn, "gadget", schema="private")
                assert trigger_installed_
Beispiel #4
0
    def test_listens_for_mixed(self, app, db):
        with create_pgevents() as pg:

            class Widget(db.Model):
                __tablename__ = 'widget'
                id = db.Column(db.Integer, primary_key=True)

            db.session.execute(CreateSchema('private'))
            db.session.commit()

            class Gadget(db.Model):
                __tablename__ = 'gadget'
                __table_args__ = {'schema': 'private'}
                id = db.Column(db.Integer, primary_key=True)

            create_all(db)

            @pg.listens_for(Widget, {'insert'})
            def widget_callback(row_id, identifier):
                pass

            assert ('public.widget' in pg._triggers)
            assert (len(pg._triggers['public.widget']) == 1)
            trigger = pg._triggers['public.widget'][0]
            assert (trigger.installed == False)
            assert (trigger.target == Widget)
            assert (trigger.events == {'insert'})
            assert (trigger.callback == widget_callback)

            with create_connection(db, raw=True) as conn:
                trigger_installed_ = trigger_installed(conn, 'widget')
                assert (trigger_installed_ == False)

            pg.init_app(app)

            assert (trigger.installed == True)

            with create_connection(db, raw=True) as conn:
                trigger_installed_ = trigger_installed(conn, 'widget')
                assert (trigger_installed_ == True)

            @pg.listens_for(Gadget, {'delete'})
            def gadget_callback(row_id, identifier):
                pass

            assert ('private.gadget' in pg._triggers)
            assert (len(pg._triggers['private.gadget']) == 1)

            trigger = pg._triggers['private.gadget'][0]
            assert (trigger.installed == True)
            assert (trigger.target == Gadget)
            assert (trigger.events == {'delete'})
            assert (trigger.callback == gadget_callback)

            with create_connection(db, raw=True) as conn:
                trigger_installed_ = trigger_installed(conn,
                                                       'gadget',
                                                       schema='private')
                assert (trigger_installed_ == True)
    def test_listen_one_model_multiple_triggers(self, app, db):
        class Widget(db.Model):
            __tablename__ = "widget"
            id = db.Column(db.Integer, primary_key=True)

        create_all(db)

        def upsert_callback(row_id, identifier):
            pass

        def delete_callback(row_id, identifier):
            pass

        with create_pgevents(app) as pg:
            pg.listen(Widget, {"insert", "update"}, upsert_callback)
            pg.listen(Widget, {"delete"}, delete_callback)

            assert "public.widget" in pg._triggers
            assert len(pg._triggers["public.widget"]) == 2

            trigger = pg._triggers["public.widget"][0]
            assert trigger.installed
            assert trigger.target == Widget
            assert trigger.events == {"insert", "update"}
            assert trigger.callback == upsert_callback

            trigger = pg._triggers["public.widget"][1]
            assert trigger.installed
            assert trigger.target == Widget
            assert trigger.events == {"delete"}
            assert trigger.callback == delete_callback

            with create_connection(db, raw=True) as conn:
                trigger_installed_ = trigger_installed(conn, "widget")
                assert trigger_installed_
Beispiel #6
0
    def test_listen_one_model_multiple_triggers(self, app, db):
        class Widget(db.Model):
            __tablename__ = 'widget'
            id = db.Column(db.Integer, primary_key=True)

        create_all(db)

        def upsert_callback(row_id, identifier):
            pass

        def delete_callback(row_id, identifier):
            pass

        with create_pgevents(app) as pg:
            pg.listen(Widget, {'insert', 'update'}, upsert_callback)
            pg.listen(Widget, {'delete'}, delete_callback)

            assert ('public.widget' in pg._triggers)
            assert (len(pg._triggers['public.widget']) == 2)

            trigger = pg._triggers['public.widget'][0]
            assert (trigger.installed == True)
            assert (trigger.target == Widget)
            assert (trigger.events == {'insert', 'update'})
            assert (trigger.callback == upsert_callback)

            trigger = pg._triggers['public.widget'][1]
            assert (trigger.installed == True)
            assert (trigger.target == Widget)
            assert (trigger.events == {'delete'})
            assert (trigger.callback == delete_callback)

            with create_connection(db, raw=True) as conn:
                trigger_installed_ = trigger_installed(conn, 'widget')
                assert (trigger_installed_ == True)
Beispiel #7
0
    def test_install_trigger_for_model(self, app, db):
        class Widget(db.Model):
            __tablename__ = "widget"
            id = db.Column(db.Integer, primary_key=True)

        create_all(db)

        with create_pgevents(app) as pg:
            with create_connection(db, raw=True) as conn:
                trigger_installed_ = trigger_installed(conn, "widget")
                assert not trigger_installed_

                pg._install_trigger_for_model(Widget)

                trigger_installed_ = trigger_installed(conn, "widget")
                assert trigger_installed_