feat(test): generate fake test data
This commit is contained in:
parent
573d7ead6c
commit
c2064a4f76
@ -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"}
|
||||
|
@ -66,6 +66,7 @@ def migrate() -> None:
|
||||
conn.commit()
|
||||
|
||||
print("Finished PostgreSQL migration number 0")
|
||||
print("=================================================================")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
60
backend/src/neo_neo_todo/migrations/test_data.py
Normal file
60
backend/src/neo_neo_todo/migrations/test_data.py
Normal file
@ -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()
|
@ -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__":
|
||||
|
Loading…
x
Reference in New Issue
Block a user