Advertisement
go6odn28

Test_code

Dec 18th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.75 KB | None | 0 0
  1. from project.second_hand_car import SecondHandCar
  2. from unittest import TestCase, main
  3.  
  4.  
  5. class SecondHandCarTest(TestCase):
  6.  
  7.     def test_01_init(self):
  8.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  9.         self.assertEqual(second_hand_car.model, "VW")
  10.         self.assertEqual(second_hand_car.car_type, "Golf")
  11.         self.assertEqual(second_hand_car.mileage, 100000)
  12.         self.assertEqual(second_hand_car.price, 10000.0)
  13.         self.assertEqual([], second_hand_car.repairs)
  14.  
  15.     def test_02_price_equal_to_one(self):
  16.         with self.assertRaises(ValueError) as ve:
  17.             second_hand_car = SecondHandCar("VW", "Golf", 100000, 1.0)
  18.  
  19.         expected_message = 'Price should be greater than 1.0!'
  20.         output = str(ve.exception)
  21.         self.assertEqual(expected_message, output)
  22.  
  23.     def test_03_price_equal_to_zero(self):
  24.         with self.assertRaises(ValueError) as ve:
  25.             second_hand_car = SecondHandCar("VW", "Golf", 100000, 0.0)
  26.  
  27.         expected_message = 'Price should be greater than 1.0!'
  28.         output = str(ve.exception)
  29.         self.assertEqual(expected_message, output)
  30.  
  31.     def test_04_negative_price(self):
  32.         with self.assertRaises(ValueError) as ve:
  33.             second_hand_car = SecondHandCar("VW", "Golf", 100000, -1.0)
  34.  
  35.         expected_message = 'Price should be greater than 1.0!'
  36.         output = str(ve.exception)
  37.         self.assertEqual(expected_message, output)
  38.  
  39.     def test_05_price_greater_than_one(self):
  40.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  41.         self.assertEqual(second_hand_car.price, 10000.0)
  42.  
  43.     def test_06_mileage_equal_to_100(self):
  44.         with self.assertRaises(ValueError) as ve:
  45.             second_hand_car = SecondHandCar("VW", "Golf", 100, 10000.0)
  46.  
  47.         expected_message = 'Please, second-hand cars only! Mileage must be greater than 100!'
  48.         output = str(ve.exception)
  49.         self.assertEqual(expected_message, output)
  50.  
  51.     def test_07_mileage_less_than_100(self):
  52.         with self.assertRaises(ValueError) as ve:
  53.             second_hand_car = SecondHandCar("VW", "Golf", 90, 10000.0)
  54.  
  55.         expected_message = 'Please, second-hand cars only! Mileage must be greater than 100!'
  56.         output = str(ve.exception)
  57.         self.assertEqual(expected_message, output)
  58.  
  59.     def test_08_negative_mileage(self):
  60.         with self.assertRaises(ValueError) as ve:
  61.             second_hand_car = SecondHandCar("VW", "Golf", -20, 10000.0)
  62.  
  63.         expected_message = 'Please, second-hand cars only! Mileage must be greater than 100!'
  64.         output = str(ve.exception)
  65.         self.assertEqual(expected_message, output)
  66.  
  67.     def test_09_mileage_greater_than_100(self):
  68.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  69.         self.assertEqual(second_hand_car.mileage, 100000)
  70.  
  71.     def test_10_set_promotional_price_with_new_price_greater_than_the_initial_price(self):
  72.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  73.  
  74.         with self.assertRaises(ValueError) as ve:
  75.             second_hand_car.set_promotional_price(20000.0)
  76.         expected_error = 'You are supposed to decrease the price!'
  77.         self.assertEqual(str(ve.exception), expected_error)
  78.  
  79.     def test_11_set_promotional_price_with_new_price_equal_to_the_initial_price(self):
  80.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  81.  
  82.         with self.assertRaises(ValueError) as ve:
  83.             second_hand_car.set_promotional_price(10000.0)
  84.         expected_error = 'You are supposed to decrease the price!'
  85.         self.assertEqual(str(ve.exception), expected_error)
  86.  
  87.     def test_12_set_promotional_price_with_new_price_less_than_the_initial_price(self):
  88.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  89.  
  90.         res = second_hand_car.set_promotional_price(5000.0)
  91.         expected_message = 'The promotional price has been successfully set.'
  92.         self.assertEqual(res, expected_message)
  93.  
  94.     def test_13_need_to_repair_with_bigger_repair_price(self):
  95.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  96.  
  97.         res = second_hand_car.need_repair(6000.0, "Fuel pomp")
  98.         expected_message = 'Repair is impossible!'
  99.         self.assertEqual(res, expected_message)
  100.  
  101.     def test_14_need_to_repair_increase_self_price(self):
  102.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  103.  
  104.         second_hand_car.need_repair(1000.0, "Fuel pomp")
  105.         self.assertEqual(11000.0, second_hand_car.price)
  106.  
  107.     def test_15_need_to_repair_increase_self_repairs_append_repair_description(self):
  108.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  109.  
  110.         second_hand_car.need_repair(1000.0, "Fuel pomp")
  111.         self.assertEqual(["Fuel pomp"], second_hand_car.repairs)
  112.  
  113.     def test_16_need_to_repair_with_proper_price_expected_message(self):
  114.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  115.         res = second_hand_car.need_repair(1000.0, "Fuel pomp")
  116.         expected_message = 'Price has been increased due to repair charges.'
  117.         self.assertEqual(expected_message, res)
  118.  
  119.     def test_17__gt__method_with_not_the_same_type(self):
  120.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  121.         second_hand_car_2 = SecondHandCar("Audi", "Quatro", 200000, 9000.0)
  122.         res = second_hand_car.__gt__(second_hand_car_2)
  123.         expected_message = 'Cars cannot be compared. Type mismatch!'
  124.         self.assertEqual(res, expected_message)
  125.  
  126.     def test_18__gt__method_with_price_greater_than_self_price_same_car_type(self):
  127.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  128.         second_hand_car_2 = SecondHandCar("VW", "Golf", 200000, 11000.0)
  129.         res = second_hand_car.__gt__(second_hand_car_2)
  130.         self.assertFalse(res)
  131.  
  132.     def test_19__gt__method_with_price_less_than_self_price_same_car_type(self):
  133.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  134.         second_hand_car_2 = SecondHandCar("VW", "Golf", 200000, 9000.0)
  135.         res = second_hand_car.__gt__(second_hand_car_2)
  136.         self.assertTrue(res)
  137.  
  138.     def test_20__gt__method_with_price_equal_to_self_price_same_car_type(self):
  139.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  140.         second_hand_car_2 = SecondHandCar("VW", "Golf", 200000, 10000.0)
  141.         res = second_hand_car.__gt__(second_hand_car_2)
  142.         self.assertFalse(res)
  143.  
  144.     def test_21__str__method(self):
  145.         second_hand_car = SecondHandCar("VW", "Golf", 100000, 10000.0)
  146.         output = "Model VW | Type Golf | Milage 100000km\nCurrent price: 10000.00 | Number of Repairs: 0"
  147.         self.assertEqual(second_hand_car.__str__(), output)
  148.  
  149.  
  150. if __name__ == "__main__":
  151.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement