def sql_function_entities(): sql_function_entities = [] for p in Path(str(Path().resolve()) + "/src/db/sql/functions").rglob('*.sql'): pg_function_entitity = PGFunction.from_sql(p.read_text()) sql_function_entities.append(pg_function_entitity) return sql_function_entities
def test_pg_function_from_sql_file_valid() -> None: SQL = """ CREATE OR REPLACE FUNCTION public.to_upper(some_text text) RETURNS TEXT AS $$ SELECT upper(some_text) $$ language SQL; """ func = PGFunction.from_sql(SQL) assert func.schema == "public"
conn = engine conn.execute(""" create table public.account ( id serial primary key, email text not null ); """) yield conn.execute("drop table public.account cascade") FUNC = PGFunction.from_sql( """create function public.downcase_email() returns trigger as $$ begin return new; end; $$ language plpgsql; """) TRIG = PGTrigger( schema="public", signature="lower_account_EMAIL", on_entity="public.account", definition=""" BEFORE INSERT ON public.account FOR EACH ROW EXECUTE PROCEDURE public.downcase_email() """, )
def test_pg_function_from_sql_file_invalid() -> None: SQL = """ NO VALID SQL TO BE FOUND HERE """ with pytest.raises(SQLParseFailure): PGFunction.from_sql(SQL)