From c2064a4f761da33af128d10c813bc02d3e87a202 Mon Sep 17 00:00:00 2001 From: minhtrannhat Date: Fri, 1 Mar 2024 14:40:59 -0500 Subject: [PATCH] feat(test): generate fake test data --- backend/pyproject.toml | 3 +- backend/src/neo_neo_todo/migrations/0.py | 1 + .../src/neo_neo_todo/migrations/test_data.py | 60 +++++++++++++++++++ backend/src/neo_neo_todo/utils/database.py | 3 + 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 backend/src/neo_neo_todo/migrations/test_data.py diff --git a/backend/pyproject.toml b/backend/pyproject.toml index db3adc0..1fe55ba 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -66,4 +66,5 @@ dev = {cmd = "uvicorn neo_neo_todo.main:app --reload", env_file = "development.e recreate-db-base = "python3 src/neo_neo_todo/utils/database.py" migration-base = "python3 src/neo_neo_todo/migrations/0.py" recreate-db = {composite = ["recreate-db-base", "migration-base"], env_file = "development.env"} -test = {composite = ["recreate-db-base", "migration-base", "pytest tests/"], env_file = "testing.env"} +generate-test-data = "python3 src/neo_neo_todo/migrations/test_data.py" +test = {composite = ["recreate-db-base", "migration-base", "generate-test-data", "pytest tests/"], env_file = "testing.env"} diff --git a/backend/src/neo_neo_todo/migrations/0.py b/backend/src/neo_neo_todo/migrations/0.py index 60c0ef3..141adca 100644 --- a/backend/src/neo_neo_todo/migrations/0.py +++ b/backend/src/neo_neo_todo/migrations/0.py @@ -66,6 +66,7 @@ def migrate() -> None: conn.commit() print("Finished PostgreSQL migration number 0") + print("=================================================================") if __name__ == "__main__": diff --git a/backend/src/neo_neo_todo/migrations/test_data.py b/backend/src/neo_neo_todo/migrations/test_data.py new file mode 100644 index 0000000..8ad86ad --- /dev/null +++ b/backend/src/neo_neo_todo/migrations/test_data.py @@ -0,0 +1,60 @@ +import os +from urllib.parse import urlparse + +import psycopg +from psycopg import sql + + +def generate_test_data() -> None: + """ + Generate the test data in PostgreSQL database + + Watch for PostgreSQL version changes + """ + url_parts = urlparse(os.environ["TODO_DB_DATABASE_URL"]) + + # Extract connection parameters + dbname = url_parts.path[1:] + user_name = url_parts.username + password = url_parts.password + host = url_parts.hostname + port = url_parts.port + + print( + f"The database name is {dbname}, the username is {user_name}, " + f"the password is {password}, the host is {host}, the port is {port}" + ) + + with psycopg.connect( + dbname=dbname, user=user_name, password=password, host=host, port=port + ) as conn: + with conn.cursor() as cur: + # create the members table + cur.execute( + sql.SQL( + """ + INSERT INTO members (email, password_hash) + VALUES ('member@todo.test', + '$2b$14$6yXjNza30kPCg3LhzZJfqeCWOLM.zyTiQFD4rdWlFHBTfYzzKJMJe') + """ + ) + ) + + cur.execute( + sql.SQL( + """ + INSERT INTO todos (member_id, task) + VALUES (1, 'Test Task') + """ + ) + ) + + # Commit the changes + conn.commit() + + print("Finished PostgreSQL test data generation") + print("=================================================================") + + +if __name__ == "__main__": + generate_test_data() diff --git a/backend/src/neo_neo_todo/utils/database.py b/backend/src/neo_neo_todo/utils/database.py index d4d2c78..f1e939b 100644 --- a/backend/src/neo_neo_todo/utils/database.py +++ b/backend/src/neo_neo_todo/utils/database.py @@ -20,6 +20,8 @@ def recreate_db() -> None: host = url_parts.hostname port = url_parts.port + print("=================================================================") + print( "Running database recreation script, " f"The database name is {dbname}, the username is {user_name}, " @@ -96,6 +98,7 @@ def recreate_db() -> None: conn.commit() print("Finished PostgreSQL (re)creatation") + print("=================================================================") if __name__ == "__main__":