Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/build-main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
sudo apt install libpq-dev
pip install poetry
- run: poetry install --with dev
- run: poetry run isort --profile black --diff .
- run: poetry run isort --profile black --diff dblinter/ tests/
- run: poetry run black --check .
- run: poetry run ruff check .
- run: poetry run pylint dblinter tests
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
sudo apt install libpq-dev
pip install poetry
- run: poetry install --with dev
- run: poetry run isort --profile black --diff .
- run: poetry run isort --profile black --diff dblinter/ tests/
- run: poetry run black --check .
- run: poetry run ruff check .
- run: poetry run pylint dblinter tests
Expand Down
8 changes: 0 additions & 8 deletions dblinter.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ table:
message: "No primary key on table {0}.{1}.{2}."
fixes:
- create a primary key.
- name: TableWithoutIndex
ruleid: T002
enabled: True
context:
desc: table without index.
message: "No index on table {0}.{1}.{2}."
fixes:
- check if it's necessary to create index.
- name: TableWithRedundantIndex
ruleid: T003
enabled: True
Expand Down
8 changes: 0 additions & 8 deletions dblinter/default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,6 @@ table:
message: "No primary key on table {0}.{1}.{2}."
fixes:
- create a primary key.
- name: TableWithoutIndex
ruleid: T002
enabled: True
context:
desc: table without index.
message: "No index on table {0}.{1}.{2}."
fixes:
- check if it's necessary to create index.
- name: TableWithRedundantIndex
ruleid: T003
enabled: True
Expand Down
Empty file.
26 changes: 0 additions & 26 deletions dblinter/rules/T002/TableWithoutIndex.py

This file was deleted.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,5 @@ extension-pkg-whitelist = "pydantic"
requires = ["poetry-core"]
build-backend = "poetry-core.masonry.api"

[tool.black]
extend-exclude = "dblinter.cfg"
89 changes: 89 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,92 @@ def remove_container():
}
wait_for_postgres(uri, 30)
return uri


@pytest.fixture(scope="function", autouse=True)
def cleanup_database(request):
"""Clean up database after each test to prevent test pollution.

This fixture runs automatically for all tests that use postgres databases.
"""
# Only run cleanup if the test uses a postgres fixture
uses_pg = "postgres_instance_args" in request.fixturenames
uses_pg12 = "postgres12_instance_args" in request.fixturenames

if not uses_pg and not uses_pg12:
yield
return

yield
# Cleanup after test

# Determine which instance to clean
instance_args = None
if uses_pg:
instance_args = request.getfixturevalue("postgres_instance_args")
elif uses_pg12:
instance_args = request.getfixturevalue("postgres12_instance_args")

if not instance_args:
return

try:
conn = psycopg2.connect(
user=instance_args["user"],
password=instance_args["password"],
host=instance_args["host"],
port=instance_args["port"],
dbname=instance_args["dbname"],
sslmode=instance_args["sslmode"],
connect_timeout=10,
)
conn.autocommit = True
cur = conn.cursor()

# Drop all user-created schemas (except public)
cur.execute(
"""
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name NOT IN ('pg_catalog', 'information_schema', 'public',
'pg_toast', '_timescaledb_catalog', '_timescaledb_config',
'_timescaledb_internal', '_timescaledb_cache', '_timescaledb_functions',
'timescaledb', 'pgaudit')
"""
)
schemas = cur.fetchall()
for (schema,) in schemas:
cur.execute(f'DROP SCHEMA IF EXISTS "{schema}" CASCADE')

# Drop all tables in public schema
cur.execute(
"""
SELECT tablename
FROM pg_tables
WHERE schemaname = 'public'
"""
)
tables = cur.fetchall()
for (table,) in tables:
cur.execute(f'DROP TABLE IF EXISTS public."{table}" CASCADE')

# Drop all user-created roles
cur.execute(
"""
SELECT rolname
FROM pg_roles
WHERE rolname NOT LIKE 'pg_%'
AND rolname != 'postgres'
"""
)
roles = cur.fetchall()
for (role,) in roles:
# Revoke all privileges first
cur.execute(f'REASSIGN OWNED BY "{role}" TO postgres')
cur.execute(f'DROP OWNED BY "{role}" CASCADE')
cur.execute(f'DROP ROLE IF EXISTS "{role}"')

cur.close()
conn.close()
except Exception as e: # pylint: disable=broad-exception-caught
print(f"Cleanup warning: {e}")
8 changes: 0 additions & 8 deletions tests/data/good_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ table:
message: "No primary key on table {0}.{1}.{2}."
fixes:
- create a primary key.
- name: TableWithoutIndex
ruleid: T002
enabled: True
context:
desc: table without index.
message: "No index on table {0}.{1}.{2}."
fixes:
- check if it's necessary to create index.
- name: TableWithRedundantIndex
ruleid: T003
enabled: True
Expand Down
46 changes: 0 additions & 46 deletions tests/rules/T002/test_TableWithoutIndex.py

This file was deleted.

36 changes: 6 additions & 30 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,22 +299,10 @@ def test_main_with_include_table(postgres_instance_args) -> None:
sarif_document=sarif_document,
include="e_table%",
)
assert (
sarif_document.sarif_doc.runs[0].results[0].message.text
== "No primary key on table postgres.schema1.e_table1."
)
assert (
sarif_document.sarif_doc.runs[0].results[1].message.text
== "No index on table postgres.schema1.e_table1."
)
assert (
sarif_document.sarif_doc.runs[0].results[3].message.text
== "No primary key on table postgres.schema1.e_table2."
)
assert (
sarif_document.sarif_doc.runs[0].results[4].message.text
== "No index on table postgres.schema1.e_table2."
)
# Check that results contain expected messages (order not guaranteed)
result_messages = [r.message.text for r in sarif_document.sarif_doc.runs[0].results]
assert "No primary key on table postgres.schema1.e_table1." in result_messages
assert "No primary key on table postgres.schema1.e_table2." in result_messages


def test_main_with_include_table_and_schema(postgres_instance_args) -> None:
Expand Down Expand Up @@ -347,17 +335,9 @@ def test_main_with_include_table_and_schema(postgres_instance_args) -> None:
== "No primary key on table postgres.schema1.e_table1."
)
assert (
sarif_document.sarif_doc.runs[0].results[1].message.text
== "No index on table postgres.schema1.e_table1."
)
assert (
sarif_document.sarif_doc.runs[0].results[3].message.text
sarif_document.sarif_doc.runs[0].results[2].message.text
== "No primary key on table postgres.schema1.e_table2."
)
assert (
sarif_document.sarif_doc.runs[0].results[4].message.text
== "No index on table postgres.schema1.e_table2."
)


def test_main_with_exclude_table(postgres_instance_args) -> None:
Expand Down Expand Up @@ -387,10 +367,6 @@ def test_main_with_exclude_table(postgres_instance_args) -> None:
sarif_document.sarif_doc.runs[0].results[0].message.text
== "No primary key on table postgres.schema1.e_table1."
)
assert (
sarif_document.sarif_doc.runs[0].results[1].message.text
== "No index on table postgres.schema1.e_table1."
)


def test_main_with_schema_and_role_without_default_role_nok(
Expand Down Expand Up @@ -501,7 +477,7 @@ def test_main_with_schema_and_role_not_exist_nok(postgres_instance_args) -> None

assert (
"No role grantee on table postgres.schema1.e_table1. It means that except owner."
in sarif_document.sarif_doc.runs[0].results[2].message.text
in sarif_document.sarif_doc.runs[0].results[1].message.text
)


Expand Down