continue development
This commit is contained in:
@@ -1,14 +1,7 @@
|
||||
# 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.
|
||||
|
||||
ADMIN_EMAIL = "lmath56@hotmail.com" # Email address of the administrator account
|
||||
|
||||
ADMIN_EMAIL = "lmath56@hotmail.com"
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
@@ -19,97 +12,113 @@ from class_transaction import Transaction
|
||||
from faker import Faker
|
||||
import random
|
||||
import datetime
|
||||
import hashlib
|
||||
import hashlib
|
||||
import uuid
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
def generate_hash(): # Creates a hash for a password
|
||||
|
||||
def generate_hash():
|
||||
seed = str(random.random()).encode('utf-8')
|
||||
return hashlib.sha512(seed).hexdigest()
|
||||
|
||||
def generate_uuid(): # Generates a unique identifier for transactions
|
||||
def generate_uuid():
|
||||
return str(uuid.uuid4())
|
||||
|
||||
def generate_uuid_short(): # Generates a short uuid for accounts and clients
|
||||
def generate_uuid_short():
|
||||
return str(uuid.uuid4())[:8]
|
||||
|
||||
def timestamp(): # Returns the current timestamp
|
||||
return (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
||||
def current_timestamp():
|
||||
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
engine = create_engine('sqlite:///test_database.db')# Create a new engine for the test database
|
||||
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
|
||||
def random_date(start, end):
|
||||
return start + timedelta(seconds=random.randint(0, int((end - start).total_seconds())))
|
||||
|
||||
fake = Faker() # Create a Faker instance
|
||||
def timestamp_this_year():
|
||||
start = datetime(datetime.now().year, 1, 1)
|
||||
end = datetime(datetime.now().year, 12, 31, 23, 59, 59)
|
||||
return random_date(start, end).strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
all_account_ids = [] # List to store all account IDs
|
||||
def timestamp_this_century():
|
||||
start = datetime(2000, 1, 1)
|
||||
end = datetime(2099, 12, 31, 23, 59, 59)
|
||||
return random_date(start, end).strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
for i in range(50): # Generate 50 clients
|
||||
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"
|
||||
|
||||
engine = create_engine('sqlite:///test_database.db')
|
||||
Base.metadata.create_all(engine)
|
||||
Session = sessionmaker(bind=engine)
|
||||
session = Session()
|
||||
|
||||
fake = Faker()
|
||||
all_account_ids = []
|
||||
|
||||
for i in range(50):
|
||||
is_administrator = 1 if i == 0 else 0
|
||||
password_hash = "7835062ec36ed529fe22cc63baf3ec18d347dacb21c9801da8ba0848cc18efdf1e51717dd5b1240f7556aca3947aa0722452858be6002c1d46b1f1c311b0e9d8" if i == 0 else generate_hash()
|
||||
client_id = generate_uuid_short()
|
||||
all_account_ids.append(client_id) # Add the client ID to the list of account IDs
|
||||
client = Client(
|
||||
client_id=client_id,
|
||||
name="ADMIN" if i == 0 else fake.name(),
|
||||
birthdate="ADMIN" if i == 0 else fake.date_of_birth(minimum_age=18, maximum_age=90),
|
||||
opening_timestamp=timestamp() if i == 0 else fake.date_this_century(),
|
||||
address="ADMIN" if i == 0 else fake.address(),
|
||||
phone_number="ADMIN" if i == 0 else fake.phone_number(),
|
||||
email=ADMIN_EMAIL if i == 0 else fake.email(),
|
||||
administrator=is_administrator,
|
||||
name="ADMIN" if i == 0 else fake.name(),
|
||||
birthdate="ADMIN" if i == 0 else timestamp_this_century(),
|
||||
opening_timestamp=current_timestamp() if i == 0 else timestamp_this_century(),
|
||||
address="ADMIN" if i == 0 else fake.address(),
|
||||
phone_number="ADMIN" if i == 0 else fake.phone_number(),
|
||||
email=ADMIN_EMAIL if i == 0 else fake.email(),
|
||||
administrator=is_administrator,
|
||||
hash=password_hash,
|
||||
notes=fake.text(max_nb_chars=50), # Generate fake notes
|
||||
enabled=1,
|
||||
accounts=[]) # Empty list for accounts, you can add accounts later)
|
||||
notes=fake.text(max_nb_chars=50),
|
||||
enabled=1,
|
||||
accounts=[]
|
||||
)
|
||||
session.add(client)
|
||||
|
||||
for j in range(2):# Each client has 2 accounts
|
||||
for j in range(2):
|
||||
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
|
||||
balance = 1000
|
||||
|
||||
for k in range(40):
|
||||
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
|
||||
if transaction_type == 'Withdrawal' and balance - amount < 0:
|
||||
continue
|
||||
|
||||
if transaction_type == 'Deposit': # Update balance based on transaction type
|
||||
if transaction_type == 'Deposit':
|
||||
balance += amount
|
||||
elif transaction_type == 'Withdrawal':
|
||||
else:
|
||||
balance -= amount
|
||||
|
||||
transaction = Transaction(
|
||||
transaction_id=generate_uuid(),
|
||||
account_id=account_id,
|
||||
recipient_account_id=random.choice(all_account_ids),
|
||||
recipient_account_id=random.choice(all_account_ids) if all_account_ids else account_id,
|
||||
transaction_type=transaction_type,
|
||||
amount=amount,
|
||||
timestamp=fake.date_this_year(),
|
||||
description=fake.text(max_nb_chars=50)
|
||||
timestamp=timestamp_this_year(),
|
||||
description=fake.text(max_nb_chars=20)
|
||||
)
|
||||
session.add(transaction)
|
||||
|
||||
account = Account(
|
||||
account_id=account_id,
|
||||
client_id=client_id,
|
||||
description=fake.text(max_nb_chars=200),
|
||||
open_timestamp=fake.date_this_century(),
|
||||
description=fake.text(max_nb_chars=20),
|
||||
open_timestamp=timestamp_this_year(),
|
||||
account_type=random.choice(['Spending', 'Savings']),
|
||||
balance=balance, # Set balance to calculated balance
|
||||
balance=balance,
|
||||
enabled=1,
|
||||
notes=fake.text(max_nb_chars=50),
|
||||
transactions=[])
|
||||
transactions=[]
|
||||
)
|
||||
session.add(account)
|
||||
all_account_ids.append(account_id)
|
||||
|
||||
session.commit() # Commit the session to write the test data to the database
|
||||
session.commit()
|
||||
|
||||
print(f"The client_id of the administrator account of this test database is: {all_account_ids[0]}. The password is: Happymeal1")
|
||||
# Retrieve the client_id of the administrator account from the session
|
||||
admin_client_id = session.query(Client.client_id).filter(Client.administrator == 1).first()[0]
|
||||
|
||||
session.close() # Close the session
|
||||
# Print the client_id of the administrator account
|
||||
print(f"The client_id of the administrator account of this test database is: {admin_client_id}. The password is: Happymeal1")
|
||||
|
||||
session.close()
|
||||
Reference in New Issue
Block a user