Coverage for test/test_battery/test_battery.py: 98%
54 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-09 21:08 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-09 21:08 +0000
1"""Unit Tests for the Base Battery and Derived Nubbin and Spindler Batteries
2"""
4import unittest
5from abc import ABC
6from datetime import datetime
7from battery.base_battery import Battery
8from battery.nubbin import NubbinBattery
9from battery.spindler import SpindlerBattery
11from utils.input_validator import validate_date
14class TestBattery(unittest.TestCase):
15 """Tests for the Correct Implementation the Battery Class.
17 This tests that the Battery base class is correctly implemented according to specification.
18 """
19 def test_battery_is_subclass_of_abstract_base_class(self):
20 """Test that the Battery class is an instance of the ABC class"""
21 self.assertTrue(issubclass(Battery, ABC))
23 def test_battery_needs_service(self):
24 """Test that the Battery class has a needs_service method"""
25 self.assertTrue(hasattr(Battery, 'needs_service'))
28class TestNubbinBattery(unittest.TestCase):
29 """Tests for the NubbinBattery Class.
31 This tests that the NubbinBattery class is correctly implemented.
32 """
33 def test_nubbin_battery_is_subclass_of_battery(self):
34 """Test that the NubbinBattery class is an instance of the Battery class."""
35 self.assertTrue(issubclass(NubbinBattery, Battery))
37 def test_nubbin_battery_needs_service(self):
38 """Test that the NubbinBattery class has a needs_service method."""
39 self.assertTrue(hasattr(NubbinBattery, 'needs_service'))
41 def test_nubbin_battery_needs_service_returns_bool(self):
42 """Test that the needs_service method returns a boolean."""
43 current_date = validate_date(datetime.now())
44 last_service_date = validate_date(datetime.now())
45 self.assertIsInstance(NubbinBattery(last_service_date, current_date).needs_service(), bool)
47 def test_nubbin_battery_needs_service_returns_true(self):
48 """Test that the needs_service method returns True when the battery needs service."""
49 current_date = validate_date(datetime.now())
50 base_year = 2024
51 current_year = datetime.now().year
52 year_difference = 6 + ((current_year - base_year) // 3)
53 last_service_date = current_date.replace(year=current_date.year - year_difference)
54 self.assertTrue(NubbinBattery(last_service_date, current_date).needs_service())
56 def test_nubbin_battery_needs_service_returns_false(self):
57 """Test that the needs_service method returns
58 False when the battery does not need service."""
59 current_date = validate_date(datetime.now())
60 last_service_date = validate_date(datetime.now())
61 self.assertFalse(NubbinBattery(last_service_date, current_date).needs_service())
64class TestSpindlerBattery(unittest.TestCase):
65 """Tests for the Correct Implemetation of SpindlerBattery Class.
67 This tests that the SpindlerBattery class is correctly implemented.
68 """
69 def test_spindler_battery_is_subclass_of_battery(self):
70 """Test that the SpindlerBattery class is an
71 instance of the Battery class"""
72 self.assertTrue(issubclass(SpindlerBattery, Battery))
74 def test_spindler_battery_needs_service(self):
75 """Test that the SpindlerBattery class has a needs_service method"""
76 self.assertTrue(hasattr(SpindlerBattery, 'needs_service'))
78 def test_spindler_battery_needs_service_returns_bool(self):
79 """Test that the needs_service method returns a boolean"""
80 current_date = validate_date(datetime.now())
81 last_service_date = validate_date(datetime.now())
82 self.assertIsInstance(SpindlerBattery(last_service_date,
83 current_date).needs_service(), bool)
85 def test_spindler_battery_needs_service_returns_true(self):
86 """Test that the needs_service method returns
87 True when the battery needs service."""
88 current_date = validate_date(datetime.now())
89 base_year = 2024
90 current_year = datetime.now().year
91 year_difference = 3 + ((current_year - base_year) // 3)
92 last_service_date = current_date.replace(year=current_date.year - year_difference)
93 self.assertTrue(SpindlerBattery(last_service_date, current_date).needs_service())
95 def test_spindler_battery_needs_service_returns_false(self):
96 """Test that the needs_service method
97 returns False when the battery does not need service"""
98 current_date = validate_date(datetime.now())
99 last_service_date = validate_date(datetime.now())
100 self.assertFalse(SpindlerBattery(last_service_date, current_date).needs_service())
103if __name__ == '__main__':
104 unittest.main()