1
1
import pytest
2
+ from sqlalchemy import text
2
3
3
4
from alembic_utils .exceptions import SQLParseFailure
4
5
from alembic_utils .pg_function import PGFunction
9
10
10
11
@pytest .fixture (scope = "function" )
11
12
def sql_setup (engine ):
12
- conn = engine
13
- conn .execute (
13
+ with engine .begin () as connection :
14
+ connection .execute (
15
+ text (
16
+ """
17
+ create table public.account (
18
+ id serial primary key,
19
+ email text not null
20
+ );
14
21
"""
15
- create table public.account (
16
- id serial primary key,
17
- email text not null
18
- );
19
- """
20
- )
22
+ )
23
+ )
21
24
22
25
yield
23
- conn .execute ("drop table public.account cascade" )
26
+ with engine .begin () as connection :
27
+ connection .execute (text ("drop table public.account cascade" ))
24
28
25
29
26
30
FUNC = PGFunction .from_sql (
@@ -45,7 +49,8 @@ def sql_setup(engine):
45
49
46
50
47
51
def test_create_revision (sql_setup , engine ) -> None :
48
- engine .execute (FUNC .to_sql_statement_create ())
52
+ with engine .begin () as connection :
53
+ connection .execute (FUNC .to_sql_statement_create ())
49
54
50
55
register_entities ([FUNC , TRIG ], entity_types = [PGTrigger ])
51
56
run_alembic_command (
@@ -71,8 +76,9 @@ def test_create_revision(sql_setup, engine) -> None:
71
76
72
77
73
78
def test_trig_update_revision (sql_setup , engine ) -> None :
74
- engine .execute (FUNC .to_sql_statement_create ())
75
- engine .execute (TRIG .to_sql_statement_create ())
79
+ with engine .begin () as connection :
80
+ connection .execute (FUNC .to_sql_statement_create ())
81
+ connection .execute (TRIG .to_sql_statement_create ())
76
82
77
83
UPDATED_TRIG = PGTrigger (
78
84
schema = "public" ,
@@ -113,8 +119,9 @@ def test_trig_update_revision(sql_setup, engine) -> None:
113
119
114
120
115
121
def test_noop_revision (sql_setup , engine ) -> None :
116
- engine .execute (FUNC .to_sql_statement_create ())
117
- engine .execute (TRIG .to_sql_statement_create ())
122
+ with engine .begin () as connection :
123
+ connection .execute (FUNC .to_sql_statement_create ())
124
+ connection .execute (TRIG .to_sql_statement_create ())
118
125
119
126
register_entities ([FUNC , TRIG ], entity_types = [PGTrigger ])
120
127
@@ -141,8 +148,9 @@ def test_noop_revision(sql_setup, engine) -> None:
141
148
142
149
def test_drop (sql_setup , engine ) -> None :
143
150
# Manually create a SQL function
144
- engine .execute (FUNC .to_sql_statement_create ())
145
- engine .execute (TRIG .to_sql_statement_create ())
151
+ with engine .begin () as connection :
152
+ connection .execute (FUNC .to_sql_statement_create ())
153
+ connection .execute (TRIG .to_sql_statement_create ())
146
154
147
155
# Register no functions locally
148
156
register_entities ([], schemas = ["public" ], entity_types = [PGTrigger ])
0 commit comments