Skip to content

Commit b29cc37

Browse files
committed
add files
1 parent 38987cc commit b29cc37

File tree

9 files changed

+388
-0
lines changed

9 files changed

+388
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# from django.db import transaction
2+
3+
4+
class Transaction:
5+
def __init__(self):
6+
# Initialize account balance
7+
self.balance = 500 # Starting balance
8+
9+
def execute_transaction(self, amount):
10+
try:
11+
# Creating a transaction context
12+
# with transaction.atomic():
13+
14+
# Check if there are enough funds on the account
15+
if amount <= self.balance:
16+
# Execute a transaction - write off funds
17+
self.balance -= amount
18+
19+
# Commit a transaction to the database
20+
# transaction.commit()
21+
22+
return "Transaction successful"
23+
else:
24+
return "Insufficient funds"
25+
except Exception as e:
26+
# If an error occurs, rollback the transaction and display an error message
27+
# transaction.rollback()
28+
29+
return "Error: {}".format(str(e))

test 06 transaction/Lib/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
from Lib.Transaction import Transaction
3+
4+
5+
class TransactionTest(unittest.TestCase):
6+
def test_transaction_success(self):
7+
# Create an instance of the Transaction class
8+
transaction = Transaction()
9+
# Execute transaction
10+
result = transaction.execute_transaction(100) # pass the transaction amount
11+
# Check if the transaction was successful
12+
self.assertEqual(result, "Transaction successful")
13+
14+
def test_transaction_insufficient_funds(self):
15+
# Create an instance of the Transaction class
16+
transaction = Transaction()
17+
# Execute a transaction with insufficient funds on the account
18+
result = transaction.execute_transaction(1000) # Pass the transaction amount
19+
# Check if the transaction failed due to lack of funds
20+
self.assertEqual(result, "Insufficient funds")
21+
22+
23+
if __name__ == '__main__':
24+
unittest.main()

test 07 shop/Lib/Product.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Product:
2+
def __init__(self, name, price, category):
3+
self.name = name
4+
self.price = price
5+
self.category = category
6+
self.discount_applied = False
7+
8+
# Method for calculating the amount of goods, taking into account the quantity
9+
def calculate_total_price(self, quantity):
10+
return self.price * quantity
11+
12+
# Method for calculating the amount of goods including tax 20%
13+
def calculate_total_price_with_vat(self, quantity):
14+
total_price = self.price * quantity
15+
vat = total_price * 0.2
16+
return total_price + vat
17+
18+
# Method for calculating the amount of goods, taking into account a 10% discount
19+
def calculate_total_price_with_discount(self, quantity):
20+
total_price = self.price * quantity
21+
discount = total_price * 0.1
22+
return total_price - discount

test 07 shop/Lib/Purchase.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from Lib.Product import *
2+
3+
4+
class Purchase:
5+
# Method for placing an order
6+
@staticmethod
7+
def place_order(product, quantity, payment_method):
8+
# Checking if the product has already applied a discount, then it is not allowed to apply a discount by a promotional code
9+
if product.discount_applied:
10+
raise ValueError("Product discount has already been applied, the use of a promotional code is not allowed")
11+
12+
total_price = product.calculate_total_price(quantity)
13+
14+
# Check available payment methods
15+
if payment_method not in ['cash', 'credit_card']:
16+
raise ValueError("Invalid payment method")
17+
18+
# If the payment method is 'cash', then add 10% commission
19+
if payment_method == 'cash':
20+
total_price *= 1.1
21+
22+
# Placing an order and returning the total amount of the order
23+
return total_price
24+
25+
26+
# Method for applying discount by promo code
27+
@staticmethod
28+
def apply_discount_by_promocode(product, quantity, promocode):
29+
# Calculation of the total cost of the goods
30+
total_price = product.calculate_total_price(quantity)
31+
32+
# Checking if the product has already applied a discount, then it is not allowed to apply a discount by a promotional code
33+
if product.discount_applied:
34+
raise ValueError("Product discount already applied, promo code cannot be used")
35+
36+
# Checking the promotional code and applying the discount
37+
if promocode == 'DISCOUNT10':
38+
discount = total_price * 0.1
39+
total_price -= discount
40+
product.discount_applied = True
41+
42+
# Return the total discounted amount
43+
return total_price

test 07 shop/Lib/__init__.py

Whitespace-only changes.

test 07 shop/testShop.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import unittest
2+
from parameterized import parameterized
3+
from Lib.Purchase import Purchase
4+
from Lib.Product import Product
5+
6+
7+
class TestProductPurchase(unittest.TestCase):
8+
# Method for setting test data
9+
def setUp(self):
10+
# Create products for each category
11+
self.laptops = [Product("Laptop1", 1000, "laptops"),
12+
Product("Laptop2", 1500, "laptops"),
13+
Product("Laptop3", 1200, "laptops")]
14+
15+
self.phones = [Product("Phone1", 800, "phones"),
16+
Product("Phone2", 900, "phones"),
17+
Product("Phone3", 1000, "phones")]
18+
19+
# Method for testing checkout
20+
def test_place_order(self):
21+
# Create a product object
22+
product = Product("Laptop1", 1000, "laptops")
23+
# Call the method under test
24+
total_price = Purchase.place_order(product, 2, 'cash')
25+
# Check result
26+
self.assertEqual(total_price, 2200) # Changed expected result to 2200
27+
# Checking that the product discount has not been applied
28+
self.assertFalse(product.discount_applied)
29+
30+
# Method for testing the calculation of the amount of goods, taking into account the quantity
31+
def test_calculate_total_price(self):
32+
# Create a product object
33+
product = Product("Laptop1", 1000, "laptops")
34+
# Call the method under test
35+
total_price = product.calculate_total_price(2)
36+
# Check result
37+
self.assertEqual(total_price, 2000)
38+
39+
# Method for testing the calculation of the amount of goods, taking into account a 10% discount
40+
def test_calculate_total_price_with_discount(self):
41+
# Checking the calculation of the amount, taking into account the discount for each category of goods
42+
for product in self.laptops:
43+
total_price = product.calculate_total_price_with_discount(2)
44+
self.assertEqual(total_price, 1800)
45+
46+
for product in self.phones:
47+
total_price = product.calculate_total_price_with_discount(3)
48+
self.assertEqual(total_price, 1680)
49+
50+
# Method for testing the application of a promo code discount
51+
def test_apply_discount_by_promocode(self):
52+
# Create a product object
53+
product = Product("Laptop1", 1000, "laptops")
54+
# Call the method under test
55+
total_price = Purchase.apply_discount_by_promocode(product, 2, 'DISCOUNT10')
56+
# Check result
57+
self.assertEqual(total_price, 1800)
58+
59+
# Check that the promo code discount cannot be applied again if there is already a discount on the product
60+
product_with_discount = Product("Laptop2", 1500, "laptops")
61+
Purchase.apply_discount_by_promocode(product_with_discount, 1, 'DISCOUNT10')
62+
with self.assertRaises(ValueError):
63+
Purchase.apply_discount_by_promocode(product_with_discount, 1, 'DISCOUNT10')
64+
65+
# Method for testing the calculation of the amount of goods, taking into account a 10% discount
66+
def test_calculate_total_price_with_discount(self):
67+
# Create a product object
68+
product = Product("Laptop1", 1000, "laptops")
69+
# Call the method under test, passing the quantity argument
70+
total_price = product.calculate_total_price_with_discount(2)
71+
# Check result
72+
self.assertEqual(total_price, 1800)
73+
74+
# Method to test place_order method with wrong payment method
75+
def test_place_order_with_wrong_payment_method(self):
76+
# Create a product object
77+
product = Product("Laptop1", 1000, "laptops")
78+
# Check for an error with an incorrect payment method
79+
with self.assertRaises(ValueError):
80+
Purchase.place_order(product, 2, 'paypal')
81+
82+
# Method for testing the calculation of the amount of the goods, taking into account the tax of 20%
83+
def test_calculate_total_price_with_vat(self):
84+
# Checking the calculation of the amount including tax for each category of goods
85+
for product in self.laptops + self.phones:
86+
# Call the method under test
87+
total_price_with_vat = product.calculate_total_price_with_vat(2)
88+
# Expected Result
89+
expected_total_price_with_vat = product.price * 2 * 1.2
90+
# Check result
91+
self.assertEqual(total_price_with_vat, expected_total_price_with_vat)
92+
93+
# Testing the place_order method using parameterization
94+
@parameterized.expand([
95+
# Test data: product, quantity, payment_method, expected_total_price
96+
(Product("Phone1", 1000, "phones"), 2, "cash", 2200),
97+
(Product("Laptop1", 2000, "laptops"), 3, "credit_card", 6000),
98+
(Product("Phone2", 1000, "phones"), 1, "cash", 1100)
99+
])
100+
def test_place_order_with_parameterization(self, product, quantity, payment_method, expected_total_price):
101+
# Call the method under test
102+
total_price = Purchase.place_order(product, quantity, payment_method)
103+
104+
# Round expected and actual amount to 2 decimal places
105+
expected_total_price = round(expected_total_price, 2)
106+
total_price = round(total_price, 2)
107+
108+
# Check for equality of expected and actual amount
109+
self.assertEqual(total_price, expected_total_price)
110+
111+
# Testing the apply_discount_by_promocode method using parameterization
112+
@parameterized.expand([
113+
# Test data: product, quantity, promocode, expected_total_price
114+
(Product("Phone1", 1000, "phones"), 2, "DISCOUNT10", 1800),
115+
(Product("Laptop1", 2000, "laptops"), 3, "DISCOUNT10", 5400),
116+
(Product("Phone2", 1500, "phones"), 1, "DISCOUNT10", 1350)
117+
])
118+
def test_apply_discount_by_promocode_with_parameterization(self, product, quantity, promocode, expected_total_price):
119+
# Call the method under test
120+
total_price = Purchase.apply_discount_by_promocode(product, quantity, promocode)
121+
# Checking if the expected value matches
122+
self.assertEqual(total_price, expected_total_price)
123+
124+
# Create a test suite
125+
# def suite():
126+
# suite = unittest.TestSuite()
127+
# suite.addTest(TestProductPurchase("test_place_order"))
128+
# suite.addTest(TestProductPurchase("test_apply_discount_by_promocode"))
129+
# suite.addTest(TestProductPurchase("test_calculate_total_price_with_discount"))
130+
# suite.addTest(TestProductPurchase("test_calculate_total_price_with_vat"))
131+
# suite.addTest(TestProductPurchase("test_calculate_total_price_with_quantity"))
132+
# suite.addTest(TestProductPurchase("test_place_order_with_wrong_payment_method"))
133+
# return suite
134+
135+
136+
if __name__ == '__main__':
137+
unittest.main()
138+
# runner = unittest.TextTestRunner()
139+
# runner.run(suite())

test 08 customers/Lib/Customers.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import sqlite3
2+
3+
4+
class Customers:
5+
def __init__(self):
6+
# Initialize the class object, connect to the database and create a cursor
7+
self.connection = sqlite3.connect('customers.db')
8+
self.cursor = self.connection.cursor()
9+
10+
# Create the "customers" table if it doesn't already exist
11+
self.cursor.execute('''
12+
CREATE TABLE IF NOT EXISTS customers (
13+
id INTEGER PRIMARY KEY AUTOINCREMENT,
14+
first_name TEXT,
15+
last_name TEXT,
16+
email TEXT,
17+
phone TEXT,
18+
country TEXT
19+
)
20+
''')
21+
self.connection.commit()
22+
23+
def add_customer(self, first_name, last_name, email, phone, country):
24+
# Method for adding a new client to the database
25+
self.cursor.execute('''
26+
INSERT INTO customers (first_name, last_name, email, phone, country)
27+
VALUES (?, ?, ?, ?, ?)
28+
''', (first_name, last_name, email, phone, country))
29+
self.connection.commit()
30+
print("Customer added successfully.")
31+
32+
def find_customers_by_name(self, first_name, last_name):
33+
# Method to search for clients by name in the database
34+
self.cursor.execute('''
35+
SELECT *
36+
FROM customers
37+
WHERE first_name LIKE ? AND last_name LIKE ?
38+
ORDER BY first_name, last_name
39+
''', ('%' + first_name + '%', '%' + last_name + '%'))
40+
rows = self.cursor.fetchall()
41+
if rows:
42+
result = []
43+
for row in rows:
44+
result.append(row)
45+
return result
46+
else:
47+
print("No customers found by name.")
48+
return [] # Return an empty list if no clients are found
49+
50+
def find_customers_by_country(self, country):
51+
# Method to search for customers by country in the database using a generator
52+
self.cursor.execute('''
53+
SELECT *
54+
FROM customers
55+
WHERE country LIKE ?
56+
ORDER BY first_name, last_name
57+
''', ('%' + country + '%',))
58+
rows = self.cursor.fetchall()
59+
if rows:
60+
result = []
61+
for row in rows:
62+
result.append(row)
63+
return result # Return a list with found clients
64+
else:
65+
print("No customers found by country.")
66+
return [] # Return an empty list if no clients are found
67+
68+
def __del__(self):
69+
# Closing the cursor and the database connection when the class object is deleted
70+
self.cursor.close()
71+
self.connection.close()

0 commit comments

Comments
 (0)