-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_recommended_product_functions.py
129 lines (97 loc) · 8.04 KB
/
test_recommended_product_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import unittest
from recommended_product_functions import *
import os
import sqlite3
import database_commands.database_commands as database_commands
from datetime import datetime
current_year = datetime.now().year
current_month = datetime.now().month
current_day = datetime.now().day
class TestRecommendedProductFunctions(unittest.TestCase):
@classmethod
def setUpClass(self):
self.recommended_product_functions = recommended_product_functions()
self.connection = sqlite3.connect("database.db")
self.cursor = self.connection.cursor()
self.dataBase = database_commands.DataBase()
self.dataBase.drop_table(self.connection, self.cursor, "products")
self.dataBase.drop_table(self.connection, self.cursor, "sales")
self.dataBase.drop_table(self.connection, self.cursor, "recommended_products")
self.dataBase.create_table(self.connection, self.cursor, "products", "id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, price REAL, description TEXT, count INTEGER, sales INTEGER")
self.dataBase.create_table(self.connection, self.cursor, "sales", "id INTEGER PRIMARY KEY AUTOINCREMENT, product_id INTEGER, sale_time TEXT, count INTEGER")
self.dataBase.create_table(self.connection, self.cursor, "recommended_products", "id INTEGER PRIMARY KEY AUTOINCREMENT, product_id INTEGER, sales_last_day INTEGER")
@classmethod
def tearDownClass(self):
self.connection.close()
def setUp(self):
#clear tables
self.dataBase.clear_table(self.connection, self.cursor, "products")
self.dataBase.clear_table(self.connection, self.cursor, "sales")
self.dataBase.clear_table(self.connection, self.cursor, "recommended_products")
#generate test data
self.dataBase.insert_data(self.connection, self.cursor, "products", "name, price, description, count, sales", ("product1", 1.0, "description1", 1, 1,))
self.dataBase.insert_data(self.connection, self.cursor, "products", "name, price, description, count, sales", ("product2", 1.0, "description2", 1, 1,))
self.dataBase.insert_data(self.connection, self.cursor, "products", "name, price, description, count, sales", ("product3", 1.0, "description3", 1, 1,))
self.dataBase.insert_data(self.connection, self.cursor, "products", "name, price, description, count, sales", ("product4", 1.0, "description4", 1, 1,))
self.dataBase.insert_data(self.connection, self.cursor, "products", "name, price, description, count, sales", ("product5", 1.0, "description5", 1, 1,))
self.dataBase.insert_data(self.connection, self.cursor, "sales", "product_id, sale_time, count", (1, f"{current_year}-{current_month}-{current_day} 00:10:00", 1,))
self.dataBase.insert_data(self.connection, self.cursor, "sales", "product_id, sale_time, count", (1, f"{current_year}-{current_month}-{current_day} 00:30:00", 1,))
self.dataBase.insert_data(self.connection, self.cursor, "sales", "product_id, sale_time, count", (1, f"{current_year}-{current_month}-{current_day} 00:50:00", 1,))
self.dataBase.insert_data(self.connection, self.cursor, "sales", "product_id, sale_time, count", (1, f"{current_year}-{current_month}-{current_day} 00:00:00", 1,))
self.dataBase.insert_data(self.connection, self.cursor, "sales", "product_id, sale_time, count", (2, f"{current_year}-{current_month}-{current_day} 00:20:00", 1,))
self.dataBase.insert_data(self.connection, self.cursor, "sales", "product_id, sale_time, count", (2, f"{current_year}-{current_month}-{current_day} 00:30:00", 1,))
self.dataBase.insert_data(self.connection, self.cursor, "sales", "product_id, sale_time, count", (2, f"{current_year}-{current_month}-{current_day} 00:50:00", 1,))
self.dataBase.insert_data(self.connection, self.cursor, "sales", "product_id, sale_time, count", (2, f"{current_year}-{current_month}-{current_day} 00:40:00", 1,))
self.dataBase.insert_data(self.connection, self.cursor, "sales", "product_id, sale_time, count", (3, f"{current_year}-{current_month}-{current_day} 00:00:00", 1,))
self.dataBase.insert_data(self.connection, self.cursor, "sales", "product_id, sale_time, count", (3, f"{current_year}-{current_month}-{current_day} 00:20:00", 1,))
self.dataBase.insert_data(self.connection, self.cursor, "sales", "product_id, sale_time, count", (3, f"{current_year}-{current_month}-{current_day} 00:10:00", 1,))
self.dataBase.insert_data(self.connection, self.cursor, "sales", "product_id, sale_time, count", (4, f"{current_year}-{current_month}-{current_day} 00:40:00", 1,))
self.dataBase.insert_data(self.connection, self.cursor, "sales", "product_id, sale_time, count", (4, f"{current_year}-{current_month}-{current_day} 00:30:00", 1,))
def test_generate_recommended_products(self):
self.recommended_product_functions.generate_recommended_products(self.connection, self.cursor, 5)
self.cursor.execute("SELECT * FROM recommended_products")
dataBase_response = self.cursor.fetchall()
self.assertEqual(dataBase_response[0][1], 1)
self.assertEqual(dataBase_response[1][1], 2)
self.assertEqual(dataBase_response[2][1], 3)
self.assertEqual(dataBase_response[3][1], 4)
self.assertEqual(dataBase_response[4][1], 5)
#test if the sales_last_day is correct
self.cursor.execute("SELECT * FROM recommended_products WHERE product_id = 1")
dataBase_response = self.cursor.fetchall()
self.assertEqual(dataBase_response[0][2], 4)
self.cursor.execute("SELECT * FROM recommended_products WHERE product_id = 2")
dataBase_response = self.cursor.fetchall()
self.assertEqual(dataBase_response[0][2], 4)
self.cursor.execute("SELECT * FROM recommended_products WHERE product_id = 3")
dataBase_response = self.cursor.fetchall()
self.assertEqual(dataBase_response[0][2], 3)
#check if the id is unigue
self.cursor.execute("SELECT * FROM recommended_products")
dataBase_response = self.cursor.fetchall()
self.assertEqual(dataBase_response[0][0], 1)
self.assertEqual(dataBase_response[1][0], 2)
self.assertEqual(dataBase_response[2][0], 3)
self.assertEqual(dataBase_response[3][0], 4)
# check if the generate_recommended_products throws an error if the product_count is bigger than the number of products
with self.assertRaises(IndexError):
self.recommended_product_functions.generate_recommended_products(self.connection, self.cursor, 6)
# check if the generate_recommended_products throws an error if the product_count is smaller than 1
with self.assertRaises(IndexError):
self.recommended_product_functions.generate_recommended_products(self.connection, self.cursor, 0)
# check if the generate_recommended_products throws an error if the product_count is not an integer
with self.assertRaises(TypeError):
self.recommended_product_functions.generate_recommended_products(self.connection, self.cursor, "test")
def test_get_sales_last_day(self):
sales_last_day = self.recommended_product_functions.get_sales_last_day(self.connection, self.cursor, 1)
self.assertEqual(sales_last_day, 4)
sales_last_day = self.recommended_product_functions.get_sales_last_day(self.connection, self.cursor, 2)
self.assertEqual(sales_last_day, 4)
sales_last_day = self.recommended_product_functions.get_sales_last_day(self.connection, self.cursor, 3)
self.assertEqual(sales_last_day, 3)
sales_last_day = self.recommended_product_functions.get_sales_last_day(self.connection, self.cursor, 4)
self.assertEqual(sales_last_day, 2)
sales_last_day = self.recommended_product_functions.get_sales_last_day(self.connection, self.cursor, 5)
self.assertEqual(sales_last_day, 0)
if __name__ == '__main__':
unittest.main()