Updated and tested database_generator.py

This commit is contained in:
Lucas Mathews
2024-05-23 16:03:43 +02:00
parent 41db324c7f
commit 8dfee22dfc
3 changed files with 65 additions and 48 deletions

View File

@@ -1,5 +1,5 @@
[database] [database]
name=bank.db name=test_database.db
[api_file] [api_file]
name=api.yml name=api.yml

Binary file not shown.

View File

@@ -1,8 +1,17 @@
# Lucas Mathews - Fontys Student ID: 5023572
# Banking System Test Database Generator
# This program generates a test database for the banking system. The database contains 50 clients, each with 2 accounts. Each account has 40 transactions.
# The first client is an administrator. The password for the administrator account is "Happymeal1". The program uses the Faker library to generate fake
# data for the clients, accounts, and transactions. The random library is used to generate random data for the accounts and transactions. The program
# creates a new SQLite database called test_database.db and writes the test data to the database. The client ID of the administrator account and the
# password for the administrator account.
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from class_base import Base from class_base import Base
import hashlib # hashlib for password hashing import hashlib
import uuid # uuid for unique identifiers import uuid
from class_account import Account from class_account import Account
from class_client import Client from class_client import Client
from class_transaction import Transaction from class_transaction import Transaction
@@ -16,30 +25,24 @@ def generate_hash(): # Creates a hash for a password
def generate_uuid(): # Generates a unique identifier for transactions def generate_uuid(): # Generates a unique identifier for transactions
return str(uuid.uuid4()) return str(uuid.uuid4())
def generate_uuid_short(): # Generates a short uuid def generate_uuid_short(): # Generates a short uuid for accounts and clients
return str(uuid.uuid4())[:8] return str(uuid.uuid4())[:8]
# Create a new engine for the test database engine = create_engine('sqlite:///test_database.db')# Create a new engine for the test database
engine = create_engine('sqlite:///test_database.db') Base.metadata.create_all(engine) # Create all tables in the test database
Session = sessionmaker(bind=engine) # Create a new sessionmaker bound to the test engine
session = Session() # Create a new session
# Create all tables in the test database fake = Faker() # Create a Faker instance
Base.metadata.create_all(engine)
# Create a new sessionmaker bound to the test engine all_account_ids = [] # List to store all account IDs
Session = sessionmaker(bind=engine)
# Create a new session for i in range(50): # Generate 50 clients
session = Session() is_administrator = 1 if i == 0 else 0 # Set the first client as an administrator
# Set the password hash for the first account so that the password is "Happymeal1"
# Create a Faker instance password_hash = "7835062ec36ed529fe22cc63baf3ec18d347dacb21c9801da8ba0848cc18efdf1e51717dd5b1240f7556aca3947aa0722452858be6002c1d46b1f1c311b0e9d8" if i == 0 else generate_hash()
fake = Faker()
# List to store all account IDs
all_account_ids = []
# Generate 50 clients
for i in range(50):
client_id = generate_uuid_short() client_id = generate_uuid_short()
all_account_ids.append(client_id) # Add the client ID to the list of account IDs
client = Client( client = Client(
client_id=client_id, client_id=client_id,
name=fake.name(), name=fake.name(),
@@ -48,44 +51,58 @@ for i in range(50):
address=fake.address(), address=fake.address(),
phone_number=fake.phone_number(), phone_number=fake.phone_number(),
email=fake.email(), email=fake.email(),
administrator=random.choice([0, 1]), administrator=is_administrator,
hash=generate_hash(), # Replace with appropriate value hash=password_hash,
notes=fake.text(max_nb_chars=50), # Generate fake notes notes=fake.text(max_nb_chars=50), # Generate fake notes
enabled=1, enabled=1,
accounts=[]) # Empty list for accounts, you can add accounts later) accounts=[]) # Empty list for accounts, you can add accounts later)
session.add(client) session.add(client)
# Each client has 2 accounts for j in range(2):# Each client has 2 accounts
for j in range(2):
account_id = generate_uuid_short() account_id = generate_uuid_short()
balance = 1000 # Initialize balance to 1000
for k in range(40): # Each account has 40 transactions
if not all_account_ids: # Skip creating a transaction if there are no accounts yet
continue
transaction_type = random.choice(['Deposit', 'Withdrawal'])
amount = random.randint(1, 200)
if transaction_type == 'Withdrawal' and balance - amount < 0: # Skip withdrawal if it would make balance negative
continue
if transaction_type == 'Deposit': # Update balance based on transaction type
balance += amount
elif transaction_type == 'Withdrawal':
balance -= amount
transaction = Transaction(
transaction_id=generate_uuid(),
account_id=account_id,
recipient_account_id=random.choice(all_account_ids),
transaction_type=transaction_type,
amount=amount,
timestamp=fake.date_this_year(),
description=fake.text(max_nb_chars=50)
)
session.add(transaction)
account = Account( account = Account(
account_id=account_id, account_id=account_id,
client_id=client_id, client_id=client_id,
description=fake.text(max_nb_chars=200), description=fake.text(max_nb_chars=200),
open_timestamp=fake.date_this_century(), open_timestamp=fake.date_this_century(),
account_type=random.choice(['Spending', 'Savings']), account_type=random.choice(['Spending', 'Savings']),
balance=random.randint(100, 10000), balance=balance, # Set balance to calculated balance
enabled=1, enabled=1,
notes=fake.text(max_nb_chars=50), notes=fake.text(max_nb_chars=50),
transactions=[]) transactions=[])
session.add(account) session.add(account)
all_account_ids.append(account_id) all_account_ids.append(account_id)
# Each account has 40 transactions session.commit() # Commit the session to write the test data to the database
for k in range(40):
transaction = Transaction(
transaction_id=generate_uuid(), # Call the function here
account_id=account_id,
recipient_account_id=random.choice(all_account_ids),
transaction_type=random.choice(['Deposit', 'Withdrawal']),
amount=random.randint(1, 20),
timestamp=fake.date_this_year(),
description=fake.text(max_nb_chars=50)
)
session.add(transaction)
# Commit the session to write the test data to the database print(f"The client_id of the administrator account of this test database is: {all_account_ids[0]}. The password is: Happymeal1")
session.commit()
# Close the session session.close() # Close the session
session.close()