diff --git a/seeding/cartsFaker.py b/seeding/cartsFaker.py new file mode 100644 index 0000000000000000000000000000000000000000..e0e28400d6dc5e86af27c0d1a9b280053db403f6 --- /dev/null +++ b/seeding/cartsFaker.py @@ -0,0 +1,47 @@ +from templateFaker import * + +# Constant +CARTS_COUNT = 10000 +USERS_LIMIT = 200 +PRODUCTS_LIMIT = 200 + +# Get data from database +select_users = '''SELECT id + FROM users + ORDER BY rand() + LIMIT ''' + str(USERS_LIMIT) +cursor.execute(select_users) +users = cursor.fetchall() + +select_products = '''SELECT id, stock + FROM products + ORDER BY rand() + LIMIT ''' + str(PRODUCTS_LIMIT) +cursor.execute(select_products) +products = cursor.fetchall() + +select_carts = '''SELECT user_id, product_id + FROM carts''' +cursor.execute(select_carts) +existing_carts = cursor.fetchall() + +for i in range(CARTS_COUNT): + carts_sql = '''INSERT INTO carts(user_id, product_id, quantity) + VALUES(%s, %s, %s)''' + + user_index = randint(0, USERS_LIMIT - 1) + product_index = randint(0, PRODUCTS_LIMIT - 1) + while((users[user_index][0], products[product_index][0]) in existing_carts) : + user_id = randint(0, USERS_LIMIT - 1) + product_id = randint(0, PRODUCTS_LIMIT - 1) + + user_id = users[user_index][0] + product_id = products[product_index][0] + quantity = randint(1, products[product_index][1]) + + carts_val = (user_id, product_id, quantity) + + cursor.execute(carts_sql, carts_val) + +db.commit() +print("Insertion success") \ No newline at end of file diff --git a/seeding/categoriesFaker.py b/seeding/categoriesFaker.py new file mode 100644 index 0000000000000000000000000000000000000000..746f9f1e33514c6789cdbea704b2db89f25234f2 --- /dev/null +++ b/seeding/categoriesFaker.py @@ -0,0 +1,16 @@ +from templateFaker import * + +CATEGORIES_COUNT = 10000 + +for i in range(CATEGORIES_COUNT): + category_sql = '''INSERT INTO categories(name) + VALUES(%s)''' + + category_name = "category-" + str(i + 1) + + category_val = (category_name,) + + cursor.execute(category_sql, category_val) + +db.commit() +print("Insertion success") \ No newline at end of file diff --git a/seeding/productsFaker.py b/seeding/productsFaker.py new file mode 100644 index 0000000000000000000000000000000000000000..59c275864c898a5c27ece54dae56f20d2a0dac73 --- /dev/null +++ b/seeding/productsFaker.py @@ -0,0 +1,52 @@ +from templateFaker import * + +# Constant +PRODUCTS_COUNT = 10000 +CATEGORIES_LIMIT = 20 +PRICE_LOW = 1 +PRICE_HIGH = 1000 +STOCK_LIMIT = 100 + +# Get data from folder +image_path = root_path + "\\src\\storage\\image\\" +images = os.listdir(image_path) +image_count = len(images) + +# Get data from database +select_categories = '''SELECT id + FROM categories + ORDER BY rand() + LIMIT ''' + str(CATEGORIES_LIMIT) +cursor.execute(select_categories) +categories = cursor.fetchall() + +print("Data retrieval success") + +for i in range(PRODUCTS_COUNT): + product_sql = '''INSERT INTO products(category_id, name, description, price, stock) + VALUES(%s, %s, %s, %s, %s)''' + + category_index = randint(0, CATEGORIES_LIMIT - 1) + category_id = categories[category_index][0] + name = faker.name() + "-" + str(i) + description = faker.text() + price = randint(PRICE_LOW, PRICE_HIGH) + stock = randint(0, STOCK_LIMIT) + + product_val = (category_id, name, description, price, stock) + + cursor.execute(product_sql, product_val) + product_id = cursor.lastrowid + + file_sql = '''INSERT INTO product_files(product_id, file_name, file_extension) + VALUES(%s, %s, %s)''' + + image_index = randint(0, image_count - 1) + file_name = images[image_index] + file_extension = splitext(file_name)[1] + file_val = (product_id, file_name, file_extension) + + cursor.execute(file_sql, file_val) + +db.commit() +print("Insertion success") diff --git a/seeding/templateFaker.py b/seeding/templateFaker.py new file mode 100644 index 0000000000000000000000000000000000000000..22f8dcce074a19cecbd715647f33457c852fe66d --- /dev/null +++ b/seeding/templateFaker.py @@ -0,0 +1,30 @@ +# imports +from faker import Faker +from random import randint +import mysql.connector +import os +from os.path import abspath, dirname, splitext + +# database +db = mysql.connector.connect( + host="localhost", + port=3307, + user="root", + password="rootpw", + database="tubes-db", +) +cursor = db.cursor() + +# faker +faker = Faker() + +# os path +file_path = abspath(__file__) +root_path = dirname(dirname(file_path)) + +# functions +def clear_table(table_name : str): + delete_sql = "DELETE FROM " + table_name + cursor.execute(delete_sql) + db.commit() + print("Deletion success") diff --git a/seeding/userFaker.py b/seeding/userFaker.py new file mode 100644 index 0000000000000000000000000000000000000000..dd652bbc020c5d1b349c72e0934a3b9ef64151bd --- /dev/null +++ b/seeding/userFaker.py @@ -0,0 +1,21 @@ +from templateFaker import * + +USER_COUNT = 1000 + +for i in range(USER_COUNT): + user_sql = '''INSERT INTO users(email, username, name, password, description, category) + VALUES(%s, %s, %s, %s, %s, %s)''' + + name = faker.name() + username = name + "-" + str(i) + email = name + "@gmail.com" + password = name + description = faker.text() + category = "user" + + user_val = (email, username, name, password, description, category) + + cursor.execute(user_sql, user_val) + +db.commit() +print("Insertion success") \ No newline at end of file