class TestBatteryBase(TestCase): def setUp(self): self.init_charge = 50 self.batt = Battery(100, self.init_charge, 32) def test_charge_negative_voltage(self): with self.assertRaises(ValueError): self.batt.charge(32, -240, 5) def test_charge_negative_period(self): with self.assertRaises(ValueError): self.batt.charge(32, 240, -5) def test_valid_reset_default(self): self.batt.charge(16, 240, 5) self.batt.reset() self.assertEqual(self.batt.current_charging_power, 0) self.assertEqual(self.batt._current_charge, self.init_charge) def test_valid_reset(self): self.batt.charge(16, 240, 5) self.batt.reset(45) self.assertEqual(self.batt.current_charging_power, 0) self.assertEqual(self.batt._current_charge, 45) def test_reset_over_capacity(self): with self.assertRaises(ValueError): self.batt.reset(110)
class TestBattery(TestBatteryBase): def setUp(self): self.batt = Battery(100, 50, 7.68) def test_valid_charge(self): rate = self.batt.charge(16, 240, 5) self.assertEqual(rate, 16) self.assertEqual(self.batt.current_charging_power, 3.84) self.assertAlmostEqual(self.batt._current_charge, 50.32) def test_charge_over_max_rate(self): rate = self.batt.charge(55, 240, 5) self.assertEqual(rate, 32) self.assertEqual(self.batt.current_charging_power, 7.68) self.assertAlmostEqual(self.batt._current_charge, 50.64) def test_charge_over_capacity(self): self.batt = Battery(50, 49.8, 7.68) rate = self.batt.charge(32, 240, 5) self.assertAlmostEqual(rate, 10) self.assertAlmostEqual(self.batt.current_charging_power, 2.4) self.assertAlmostEqual(self.batt._current_charge, 50)