Add the schema, the dependencies and an end-to-end test suite

The schema existed only inside the queries, so a clean clone could not run:
nothing created the tables that database.py and main.py expect, and there was
no declaration of what to install.

- schema.sql: the seven tables, their primary and foreign keys, and the
  administrator the code requires. Reconstructed from the SQL the code issues
  and typed for it: the code reads rows by position (SELECT *), so the column
  order is part of the contract, and users.userid has to be a 4-byte integer
  because message_all() counts rows with pg_column_size(userid)/4.
- scripts/db.py, Makefile, docker-compose.yml: a throwaway PostgreSQL and the
  targets up/schema/seed/run/test/down. Both passwords come from the
  environment (NETFLOX_DB_PASSWORD, NETFLOX_DEMO_PASSWORD); nothing is stored
  in the repository, and schema.sql receives the administrator password
  through a session setting.
- requirements.txt and requirements-dev.txt: psycopg2-binary, plus pytest for
  the suite.
- tests/: 30 end-to-end tests that recreate the schema and drive the real
  functions of database.py. They check the column order and the keys against
  information_schema and walk every main flow. The defects found while mapping
  the code are pinned by test_known_bug_* and xfail instead of fixed, since
  fixing the application was not part of this change. The suite also guards the
  credential fix: database.py must refuse to import without its variable and no
  tracked file may contain the value.
- README.md: the sections shared with the other repositories, with a real
  session.
- The report moved to docs/relatorio-BD.pdf: it had accents and spaces in the
  filename.
This commit is contained in:
Andre Neto
2026-10-08 14:26:47 +01:00
parent c2a253071e
commit 4ef5a67343
12 changed files with 1117 additions and 13 deletions
+40
View File
@@ -0,0 +1,40 @@
"""Shared pytest setup for the Netflox suite.
Every test starts from a known database: the `public` schema is recreated and
schema.sql is applied (that is where the administrator required by the code comes
from), then the demo rows are loaded. The database itself is the throwaway
PostgreSQL started by `docker compose` (`make up`).
"""
from __future__ import annotations
import os
import pathlib
import sys
import pytest
ROOT = pathlib.Path(__file__).resolve().parent.parent
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from scripts import db as dbctl # noqa: E402
def pytest_configure(config):
for name in ("NETFLOX_DB_PASSWORD", "NETFLOX_DEMO_PASSWORD"):
if not os.environ.get(name):
raise pytest.UsageError(
f"{name} is not set; `make test` exports both, see README (Tests)"
)
@pytest.fixture(autouse=True)
def fresh_database():
"""Recreate the schema and reload the demo rows before every test."""
conn = dbctl.connect()
try:
dbctl.apply_schema(conn)
dbctl.seed(conn)
finally:
conn.close()
yield