import unittest
def calculate_total_cost(items, tax_rate):
# Assume this function calculates the total cost of a list of items, including tax
total = sum(item["price"] for item in items)
total += total * tax_rate
return total
def calculate_item_cost(item, tax_rate):
# Assume this function calculates the cost of a single item, including tax
cost = item["price"] * (1 + tax_rate)
return cost
def fetch_items_from_database(query):
# Assume this function fetches a list of items from a database
items = [{"name": "item1", "price": 10.0},
{"name": "item2", "price": 20.0},
{"name": "item3", "price": 30.0}]
return items
class TestUnit(unittest.TestCase):
# These unit tests check how individual functions work in isolation
def test_calculate_item_cost(self):
item = {"name": "item1", "price": 10.0}
tax_rate = 0.1
expected_cost = 11.0
result = calculate_item_cost(item, tax_rate)
self.assertAlmostEqual(result, expected_cost)
def test_calculate_total_cost(self):
items = [{"name": "item1", "price": 10.0},
{"name": "item2", "price": 20.0},
{"name": "item3", "price": 30.0}]
tax_rate = 0.1
expected_total = 66.0
result = calculate_total_cost(items, tax_rate)
self.assertAlmostEqual(result, expected_total)
def test_fetch_items_from_database(self):
query = "SELECT * FROM items"
expected_items = [{"name": "item1", "price": 10.0},
{"name": "item2", "price": 20.0},
{"name": "item3", "price": 30.0}]
result = fetch_items_from_database(query)
self.assertEqual(result, expected_items)
class TestIntegration(unittest.TestCase):
# This integration test checks how fetch_items_from_database interacts with calculate_total_cost
def test_calculate_total_cost_with_fetch_items(self):
query = "SELECT * FROM items"
items = fetch_items_from_database(query)
tax_rate = 0.1
expected_total = 66.0
result = calculate_total_cost(items, tax_rate)
self.assertAlmostEqual(result, expected_total)
class TestSystem(unittest.TestCase):
# Since our system is so small, this system test is pretty much just our integration test.
# Assume all parts of the system would be in this test case.
def test_calculate_total_cost_with_large_input(self):
items = fetch_items_from_database("SELECT * FROM items") * 1000
tax_rate = 0.1
result = calculate_total_cost(items, tax_rate)
self.assertIsNotNone(result)
def test_calculate_total_cost_with_large_tax_rate(self):
items = fetch_items_from_database("SELECT * FROM items")
tax_rate = 0.9
result = calculate_total_cost(items, tax_rate)
self.assertIsNotNone(result)
class TestAcceptance(unittest.TestCase):
# This acceptance test is supposed to test the software as a user would use it.
def test_calculate_total_cost_with_real_world_input(self):
items = fetch_items_from_database("SELECT * FROM items")
tax_rate = 0.1
result = calculate_total_cost(items, tax_rate)
self.assertAlmostEqual(result, (10.0 + 20.0 + 30.0) * (1 + tax_rate))
def test_calculate_total_cost_with_real_world_tax_rate(self):
items = fetch_items_from_database("SELECT * FROM items")
tax_rate = 0.08
result = calculate_total_cost(items, tax_rate)
self.assertAlmostEqual(result, 64.8)
if __name__ == '__main__':
unittest.main()