Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use pytest-insta to manage snapshots #299

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions projects/extension/requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ python-dotenv==1.0.1
fastapi==0.112.0
fastapi-cli==0.0.5
psycopg[binary]==3.2.1
pytest-insta==0.3.0
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
DROP DATABASE
CREATE DATABASE
You are now connected to database "toc" as user "postgres".
CREATE EXTENSION
Objects in extension "ai"
Object description
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
DROP DATABASE
CREATE DATABASE
You are now connected to database "toc" as user "postgres".
CREATE EXTENSION
Objects in extension "ai"
Object description
Expand Down
21 changes: 13 additions & 8 deletions projects/extension/tests/contents/test_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,28 @@ def host_dir() -> Path:
return Path(__file__).parent.absolute()


def init(version: int) -> None:
def init() -> str:
cmd = " ".join(
[
"psql",
f'''-d "{db_url("postgres", "postgres")}"''',
"-v ON_ERROR_STOP=1",
"-X",
f"-o {docker_dir()}/output{version}.actual",
f"-f {docker_dir()}/init.sql",
]
)
if where_am_i() != "docker":
cmd = f"docker exec -w {docker_dir()} pgai-ext {cmd}"
subprocess.run(cmd, check=True, shell=True, env=os.environ, cwd=str(host_dir()))
result = subprocess.run(
cmd,
check=True,
shell=True,
env=os.environ,
cwd=str(host_dir()),
text=True,
capture_output=True,
)
return result.stdout


def major_version() -> int:
Expand All @@ -56,9 +64,6 @@ def major_version() -> int:
return int(version[0:2])


def test_contents() -> None:
def test_contents(snapshot) -> None:
version = major_version()
init(version)
actual = host_dir().joinpath(f"output{version}.actual").read_text()
expected = host_dir().joinpath(f"output{version}.expected").read_text()
assert actual == expected
assert snapshot(f"pg{version}.txt") == init()
2 changes: 0 additions & 2 deletions projects/extension/tests/privileges/function.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
\set users {bob,fred,alice,jill}

-- check function privileges
\! rm -f function.actual
select
f.prokind
, u as "user"
Expand All @@ -14,4 +13,3 @@ inner join pg_namespace n on (n.nspname = any(array['ai']))
inner join pg_proc f on (n.oid = f.pronamespace)
cross join unnest(array['execute']) p
order by n.nspname, 6, p, u
\g (format=aligned) function.actual
2 changes: 0 additions & 2 deletions projects/extension/tests/privileges/schema.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
\set users {bob,fred,alice,jill}

-- check schema privileges
\! rm -f schema.actual
select
n as "schema"
, u as "user"
Expand All @@ -11,4 +10,3 @@ from unnest(:'users'::text[]) u
cross join unnest(array['ai', 'wiki']) n
cross join unnest(array['create', 'usage']) p
order by n, p, u
\g (format=aligned) schema.actual
2 changes: 0 additions & 2 deletions projects/extension/tests/privileges/sequence.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
\set users {bob,fred,alice,jill}

-- check sequence privileges
\! rm -f sequence.actual
select
n.nspname as "schema"
, k.relname as "table"
Expand All @@ -13,4 +12,3 @@ inner join pg_namespace n on (n.nspname = any(array['ai', 'wiki']))
inner join pg_class k on (n.oid = k.relnamespace and k.relkind in ('S'))
cross join unnest(array['select', 'update']) p
order by n.nspname, k.relname, u, p
\g (format=aligned) sequence.actual
2 changes: 0 additions & 2 deletions projects/extension/tests/privileges/table.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
\set users {bob,fred,alice,jill}

-- check table privileges
\! rm -f schema.actual
select
n.nspname as "schema"
, k.relname as "table"
Expand All @@ -13,4 +12,3 @@ inner join pg_namespace n on (n.nspname = any(array['ai', 'wiki']))
inner join pg_class k on (n.oid = k.relnamespace and k.relkind in ('r', 'p'))
cross join unnest(array['select', 'insert', 'update', 'delete']) p
order by n.nspname, k.relname, u, p
\g (format=aligned) table.actual
33 changes: 21 additions & 12 deletions projects/extension/tests/privileges/test_privileges.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def read_file(filename: str) -> str:
return f.read()


def psql_file(user, dbname, file: str) -> None:
def psql_file(user, dbname, file: str) -> str:
cmd = " ".join(
[
"psql",
Expand All @@ -47,7 +47,16 @@ def psql_file(user, dbname, file: str) -> None:
)
if where_am_i() != "docker":
cmd = f"docker exec -w {docker_dir()} pgai-ext {cmd}"
subprocess.run(cmd, check=True, shell=True, env=os.environ, cwd=str(host_dir()))
result = subprocess.run(
cmd,
check=True,
text=True,
capture_output=True,
shell=True,
env=os.environ,
cwd=str(host_dir()),
)
return result.stdout


@pytest.fixture(scope="module", autouse=True)
Expand All @@ -63,24 +72,24 @@ def run_test(kind: str) -> None:
assert actual == expected


def test_schema_privileges():
run_test("schema")
def test_schema_privileges(snapshot):
assert snapshot() == psql_file("postgres", "privs", "schema.sql")


def test_table_privileges():
run_test("table")
def test_table_privileges(snapshot):
assert snapshot() == psql_file("postgres", "privs", "table.sql")


def test_sequence_privileges():
run_test("sequence")
def test_sequence_privileges(snapshot):
assert snapshot() == psql_file("postgres", "privs", "sequence.sql")


def test_view_privileges():
run_test("view")
def test_view_privileges(snapshot):
assert snapshot() == psql_file("postgres", "privs", "view.sql")


def test_function_privileges():
run_test("function")
def test_function_privileges(snapshot):
assert snapshot() == psql_file("postgres", "privs", "function.sql")


def test_jill_privileges():
Expand Down
2 changes: 0 additions & 2 deletions projects/extension/tests/privileges/view.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
\set users {bob,fred,alice,jill}

-- check view privileges
\! rm -f view.actual
select
n.nspname as "schema"
, k.relname as "view"
Expand All @@ -13,4 +12,3 @@ inner join pg_namespace n on (n.nspname = any(array['ai', 'wiki']))
inner join pg_class k on (n.oid = k.relnamespace and k.relkind in ('v'))
cross join unnest(array['select']) p
order by n.nspname, k.relname, u, p
\g (format=aligned) view.actual
Loading