Files
s2p_banking_system/cli/test_database_generator.py
2024-06-21 02:47:58 +02:00

73 lines
3.0 KiB
Python

# Lucas Mathews - Fontys Student ID: 5023572
# Banking System Test Database Generator
# This program generates dumy data for the banking system.
# This adds 50 clients, each with 2 accounts. Each account has 40 transactions.
from faker import Faker
from connection import add_client, add_account, add_transaction
import random
import datetime
import hashlib
import time
def generate_hash(): # Creates a hash for a password
seed = str(random.random()).encode('utf-8')
return hashlib.sha512(seed).hexdigest()
def timestamp(): # Returns the current timestamp
return (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
def generate_test_database(client_id, password):
fake = Faker() # Create a Faker instance
all_account_ids = [] # List to store all account IDs
for i in range(50): # Generate 50 clients
password = generate_hash()
name = fake.name()
birthdate = fake.date_of_birth(minimum_age=18, maximum_age=90)
address = fake.address()
phone_number = fake.phone_number()
email = fake.email()
notes = fake.text(max_nb_chars=50)
client_response = add_client(name, birthdate, address, phone_number, email, password, notes)
client_id = client_response['message']
for j in range(2): # Each client has 2 accounts
balance = 1000 # Initialize balance to 1000
account_type = random.choice(['Spending', 'Savings'])
account_notes = fake.text(max_nb_chars=50)
account_response = add_account(client_id=client_id, description=fake.text(max_nb_chars=15), account_type=account_type, notes=account_notes)
response_dict = account_response
account_id = response_dict['message']
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_description = fake.text(max_nb_chars=50)
recipient_account_id = random.choice(all_account_ids)
transaction_response = add_transaction(amount=amount, account_id=account_id, recipient_account_id=recipient_account_id, otp_code=123456, description=transaction_description)
print(f"Transaction response: {transaction_response}")
time.sleep(1)
all_account_ids.append(account_id)
print("Test database generated successfully.")