Advertisement
MNNM2021

Untitled

Apr 2nd, 2024
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.26 KB | None | 0 0
  1. from project.truck_driver import TruckDriver
  2.  
  3. from unittest import TestCase, main
  4.  
  5.  
  6. class TestTruckDriver(TestCase):
  7.     def setUp(self) -> None:
  8.         self.truck_driver = TruckDriver("Pesho", 20.0)
  9.  
  10.     def test_init(self):
  11.         self.assertEqual(self.truck_driver.name, "Pesho")
  12.         self.assertEqual(self.truck_driver.money_per_mile, 20.0)
  13.         self.assertEqual(self.truck_driver.available_cargos, {})
  14.         self.assertEqual(self.truck_driver.earned_money, 0.0)
  15.         self.assertEqual(self.truck_driver.miles, 0)
  16.  
  17.     def test_earn_money_value_error(self):
  18.         with self.assertRaises(ValueError) as ve:
  19.             self.truck_driver.earned_money = -1.0
  20.         self.assertEqual("Pesho went bankrupt.", str(ve.exception))
  21.  
  22.     def test_earn_money_value_no_error(self):
  23.         earn_money = 100.0
  24.         self.truck_driver.earned_money = earn_money
  25.         self.assertEqual(self.truck_driver.earned_money, earn_money)
  26.  
  27.     def test_bankrupt(self):
  28.         self.truck_driver.money_per_mile = 0.01
  29.         self.truck_driver.add_cargo_offer("Novi Pazar", 2000)
  30.  
  31.         with self.assertRaises(ValueError) as ve:
  32.             self.truck_driver.drive_best_cargo_offer()
  33.  
  34.         self.assertEqual(str(ve.exception), f"{self.truck_driver.name} went bankrupt.")
  35.  
  36.     def test_add_cargo_offer_if_cargo_location_not_in_available_cargos(self):
  37.         cargo_location = "Sofia"
  38.         cargo_miles = 100
  39.  
  40.         result = self.truck_driver.add_cargo_offer(cargo_location, cargo_miles)
  41.  
  42.         self.assertEqual(self.truck_driver.available_cargos[cargo_location], cargo_miles)
  43.  
  44.         expected_message = f"Cargo for {cargo_miles} to {cargo_location} was added as an offer."
  45.         self.assertEqual(result, expected_message)
  46.  
  47.     def test_add_cargo_offer_if_cargo_location_is_in_available_cargos_already(self):
  48.         cargo_location = "Sofia"
  49.         cargo_miles = 100
  50.  
  51.         self.truck_driver.add_cargo_offer(cargo_location, cargo_miles)
  52.  
  53.         with self.assertRaises(Exception) as ex:
  54.             self.truck_driver.add_cargo_offer(cargo_location, cargo_miles)
  55.  
  56.         self.assertEqual("Cargo offer is already added.", str(ex.exception))
  57.  
  58.     def test_drive_best_cargo_offer_no_cargo_available(self):
  59.         self.truck_driver.available_cargos = {}
  60.  
  61.         result = self.truck_driver.drive_best_cargo_offer()
  62.  
  63.         expected_message = "There are no offers available."
  64.         self.assertEqual(result, expected_message)
  65.  
  66.     def test_drive_best_cargo_offer_with_available_cargo(self):
  67.         self.truck_driver.available_cargos = {"Sofia": 100, "Plovdiv": 200, "Varna": 150}
  68.  
  69.         result = self.truck_driver.drive_best_cargo_offer()
  70.  
  71.         expected_cargo_location = "Plovdiv"
  72.         expected_cargo_miles = 200
  73.         expected_result = f"{self.truck_driver.name} is driving {expected_cargo_miles} to {expected_cargo_location}."
  74.         self.assertEqual(result, expected_result)
  75.  
  76.     def test_eat_deducts_money(self):
  77.         self.truck_driver.earned_money = 50.0
  78.  
  79.         self.truck_driver.eat(250)
  80.  
  81.         expected_earned_money = 50.0 - 20.0
  82.         self.assertEqual(self.truck_driver.earned_money, expected_earned_money)
  83.  
  84.     def test_eat_does_not_deduct_money(self):
  85.         self.truck_driver.earned_money = 10.0
  86.  
  87.         self.truck_driver.eat(200)
  88.  
  89.         self.assertEqual(self.truck_driver.earned_money, 10.0)
  90.  
  91.     def test_sleep_deducts_money(self):
  92.         self.truck_driver.earned_money = 100.0
  93.  
  94.         self.truck_driver.sleep(1000)
  95.  
  96.         expected_earned_money = 100.0 - 45.0
  97.         self.assertEqual(self.truck_driver.earned_money, expected_earned_money)
  98.  
  99.     def test_sleep_does_not_deduct_money(self):
  100.         self.truck_driver.earned_money = 40.0
  101.  
  102.         self.truck_driver.sleep(2001)
  103.  
  104.         self.assertEqual(self.truck_driver.earned_money, 40.0)
  105.  
  106.     def test_pump_gas_deducts_money(self):
  107.         self.truck_driver.earned_money = 1000.0
  108.  
  109.         self.truck_driver.pump_gas(1500)
  110.  
  111.         expected_earned_money = 1000.0 - 500.0
  112.         self.assertEqual(self.truck_driver.earned_money, expected_earned_money)
  113.  
  114.     def test_pump_gas_does_not_deduct_money(self):
  115.         self.truck_driver.earned_money = 400.0
  116.  
  117.         self.truck_driver.pump_gas(2001)
  118.  
  119.         self.assertEqual(self.truck_driver.earned_money, 400.0)
  120.  
  121.     def test_repair_truck_deduct_money(self):
  122.         self.truck_driver.earned_money = 10000.0
  123.  
  124.         self.truck_driver.repair_truck(20000)
  125.         deducted_earned_money = 10000.0 - 7500.0
  126.  
  127.         self.assertEqual(deducted_earned_money, self.truck_driver.earned_money)
  128.  
  129.     def test_repair_truck_does_not_deduct_money(self):
  130.         self.truck_driver.earned_money = 6000.0
  131.  
  132.         self.truck_driver.repair_truck(18500)
  133.  
  134.         self.assertEqual(self.truck_driver.earned_money, 6000.0)
  135.  
  136.     def test_check_for_activities(self):
  137.         self.truck_driver.earned_money = 20000.0
  138.  
  139.         self.truck_driver.check_for_activities(250)
  140.  
  141.         expected_final_earned_money = 20000.0 - 20.0 - 0.0 - 0.0
  142.  
  143.         self.assertEqual(self.truck_driver.earned_money, expected_final_earned_money)
  144.  
  145.     def test_repr(self):
  146.         expected_repr = "Pesho has 0 miles behind his back."
  147.         self.assertEqual(repr(self.truck_driver), expected_repr)
  148.  
  149.  
  150. if __name__ == "__main__":
  151.     main()
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement