|
| 1 | +import pytest |
| 2 | +from pytest_mock_resources import create_postgres_fixture |
| 3 | +from sqlalchemy import text |
| 4 | +from sqlalchemy.exc import ProgrammingError |
| 5 | + |
| 6 | +from sqlalchemy_declarative_extensions import ( |
| 7 | + Functions, |
| 8 | + declarative_database, |
| 9 | + register_sqlalchemy_events, |
| 10 | +) |
| 11 | +from sqlalchemy_declarative_extensions.sqlalchemy import declarative_base |
| 12 | + |
| 13 | +_Base = declarative_base() |
| 14 | + |
| 15 | + |
| 16 | +@declarative_database |
| 17 | +class BaseIncludeOnly(_Base): # type: ignore |
| 18 | + __abstract__ = True |
| 19 | + |
| 20 | + functions = Functions(include=["test_*"]) |
| 21 | + |
| 22 | + |
| 23 | +@declarative_database |
| 24 | +class BaseExcludeOnly(_Base): # type: ignore |
| 25 | + __abstract__ = True |
| 26 | + |
| 27 | + functions = Functions(ignore=["ignore_*"]) |
| 28 | + |
| 29 | + |
| 30 | +@declarative_database |
| 31 | +class BaseIncludeAndExclude(_Base): # type: ignore |
| 32 | + __abstract__ = True |
| 33 | + |
| 34 | + functions = Functions(include=["test_*", "keep_*"], ignore=["*_ignore"]) |
| 35 | + |
| 36 | + |
| 37 | +register_sqlalchemy_events(BaseIncludeOnly.metadata, functions=True) |
| 38 | +register_sqlalchemy_events(BaseExcludeOnly.metadata, functions=True) |
| 39 | +register_sqlalchemy_events(BaseIncludeAndExclude.metadata, functions=True) |
| 40 | + |
| 41 | +pg_include = create_postgres_fixture(engine_kwargs={"echo": True}, session=True) |
| 42 | +pg_exclude = create_postgres_fixture(engine_kwargs={"echo": True}, session=True) |
| 43 | +pg_both = create_postgres_fixture(engine_kwargs={"echo": True}, session=True) |
| 44 | + |
| 45 | + |
| 46 | +def test_include_only(pg_include): |
| 47 | + # Matches the include pattern, thus dropped because it's not declared. |
| 48 | + pg_include.execute( |
| 49 | + text( |
| 50 | + "CREATE FUNCTION test_func() RETURNS INTEGER language sql as $$ select 1 $$;" |
| 51 | + ) |
| 52 | + ) |
| 53 | + # Doesn't match the include pattern, thus kept because it's unmanaged. |
| 54 | + pg_include.execute( |
| 55 | + text( |
| 56 | + "CREATE FUNCTION other_func() RETURNS INTEGER language sql as $$ select 2 $$;" |
| 57 | + ) |
| 58 | + ) |
| 59 | + pg_include.commit() |
| 60 | + |
| 61 | + BaseIncludeOnly.metadata.create_all(bind=pg_include.connection()) |
| 62 | + pg_include.commit() |
| 63 | + |
| 64 | + with pytest.raises(ProgrammingError): |
| 65 | + pg_include.execute(text("SELECT test_func()")).scalar() |
| 66 | + pg_include.rollback() |
| 67 | + |
| 68 | + result = pg_include.execute(text("SELECT other_func()")).scalar() |
| 69 | + assert result == 2 |
| 70 | + |
| 71 | + |
| 72 | +def test_exclude_only(pg_exclude): |
| 73 | + # Matches the exclude pattern, thus kept because it's being ignored. |
| 74 | + pg_exclude.execute( |
| 75 | + text( |
| 76 | + "CREATE FUNCTION ignore_this() RETURNS INTEGER language sql as $$ select 1 $$;" |
| 77 | + ) |
| 78 | + ) |
| 79 | + # Doesn't match the exclude pattern, thus dropped because it's not being ignored. |
| 80 | + pg_exclude.execute( |
| 81 | + text( |
| 82 | + "CREATE FUNCTION manage_this() RETURNS INTEGER language sql as $$ select 2 $$;" |
| 83 | + ) |
| 84 | + ) |
| 85 | + pg_exclude.commit() |
| 86 | + |
| 87 | + BaseExcludeOnly.metadata.create_all(bind=pg_exclude.connection()) |
| 88 | + pg_exclude.commit() |
| 89 | + |
| 90 | + result = pg_exclude.execute(text("SELECT ignore_this()")).scalar() |
| 91 | + assert result == 1 |
| 92 | + |
| 93 | + with pytest.raises(ProgrammingError): |
| 94 | + pg_exclude.execute(text("SELECT manage_this()")).scalar() |
| 95 | + |
| 96 | + |
| 97 | +def test_include_and_exclude_interaction(pg_both): |
| 98 | + """Test the interaction between include and exclude. |
| 99 | +
|
| 100 | + A function that matches include becomes managed, but can become unmanaged if also matching the |
| 101 | + exclude. |
| 102 | + """ |
| 103 | + pg_both.execute( |
| 104 | + text( |
| 105 | + "CREATE FUNCTION test_func() RETURNS INTEGER language sql as $$ select 1 $$;" |
| 106 | + ) |
| 107 | + ) |
| 108 | + pg_both.execute( |
| 109 | + text( |
| 110 | + "CREATE FUNCTION test_ignore() RETURNS INTEGER language sql as $$ select 2 $$;" |
| 111 | + ) |
| 112 | + ) |
| 113 | + pg_both.execute( |
| 114 | + text( |
| 115 | + "CREATE FUNCTION keep_this() RETURNS INTEGER language sql as $$ select 3 $$;" |
| 116 | + ) |
| 117 | + ) |
| 118 | + pg_both.execute( |
| 119 | + text( |
| 120 | + "CREATE FUNCTION other_func() RETURNS INTEGER language sql as $$ select 4 $$;" |
| 121 | + ) |
| 122 | + ) |
| 123 | + |
| 124 | + pg_both.commit() |
| 125 | + |
| 126 | + BaseIncludeAndExclude.metadata.create_all(bind=pg_both.connection()) |
| 127 | + pg_both.commit() |
| 128 | + |
| 129 | + with pytest.raises(ProgrammingError): |
| 130 | + pg_both.execute(text("SELECT test_func()")).scalar() |
| 131 | + pg_both.rollback() |
| 132 | + |
| 133 | + result = pg_both.execute(text("SELECT test_ignore()")).scalar() |
| 134 | + assert result == 2 |
| 135 | + |
| 136 | + with pytest.raises(ProgrammingError): |
| 137 | + pg_both.execute(text("SELECT keep_this()")).scalar() |
| 138 | + pg_both.rollback() |
| 139 | + |
| 140 | + result = pg_both.execute(text("SELECT other_func()")).scalar() |
| 141 | + assert result == 4 |
| 142 | + |
| 143 | + |
| 144 | +def test_include_with_schema_patterns(pg_include): |
| 145 | + pg_include.execute(text("CREATE SCHEMA foo")) |
| 146 | + pg_include.execute(text("CREATE SCHEMA bar")) |
| 147 | + |
| 148 | + pg_include.execute( |
| 149 | + text( |
| 150 | + "CREATE FUNCTION test_one() RETURNS INTEGER language sql as $$ select 1 $$;" |
| 151 | + ) |
| 152 | + ) |
| 153 | + pg_include.execute( |
| 154 | + text( |
| 155 | + "CREATE FUNCTION foo.test_two() RETURNS INTEGER language sql as $$ select 2 $$;" |
| 156 | + ) |
| 157 | + ) |
| 158 | + pg_include.execute( |
| 159 | + text( |
| 160 | + "CREATE FUNCTION bar.other() RETURNS INTEGER language sql as $$ select 3 $$;" |
| 161 | + ) |
| 162 | + ) |
| 163 | + |
| 164 | + pg_include.commit() |
| 165 | + |
| 166 | + BaseIncludeOnly.metadata.create_all(bind=pg_include.connection()) |
| 167 | + pg_include.commit() |
| 168 | + |
| 169 | + with pytest.raises(ProgrammingError): |
| 170 | + pg_include.execute(text("SELECT test_one()")).scalar() |
| 171 | + pg_include.rollback() |
| 172 | + |
| 173 | + result = pg_include.execute(text("SELECT foo.test_two()")).scalar() |
| 174 | + assert result == 2 |
| 175 | + |
| 176 | + result = pg_include.execute(text("SELECT bar.other()")).scalar() |
| 177 | + assert result == 3 |
0 commit comments