class HouseEnergyCostTestCase(unittest.TestCase): """Testing energy cost calculation method""" def setUp(self): self.house = House(1) self.house.devices_settings = OrderedDict({ 'energy_src': 'battery', 'cooling_lvl': 1.0, 'heating_lvl': 1.0, 'light_lvl': 1.0, 'curtains_lvl': 1.0 }) self.house.battery['current'] = 0.001 def test_change_source_if_not_enough_energy_in_battery(self): self.house._calculate_cost_and_update_energy_source() self.assertTrue(self.house.devices_settings['energy_src'] == 'grid') def test_update_battery_should_take_energy_from_it(self): self.house.battery['current'] = 100 self.house._calculate_cost_and_update_energy_source() self.assertLess(self.house.battery['current'], 100) def test_energy_cost_not_negative(self): self.house.devices_settings = OrderedDict({ 'energy_src': 'grid', 'cooling_lvl': 0, 'heating_lvl': 0, 'light_lvl': 0, 'curtains_lvl': 0 }) cost = self.house._calculate_cost_and_update_energy_source() self.assertTrue( cost >= 0, "Energy cost should not be a negative number!\n" "Settings: \n{}".format(self.house.devices_settings)) for i in range(100): self.house.devices_settings = OrderedDict({ 'energy_src': 'grid', 'cooling_lvl': random.random(), 'heating_lvl': random.random(), 'light_lvl': random.random(), 'curtains_lvl': random.random() }) cost = self.house._calculate_cost_and_update_energy_source() self.assertTrue( cost >= 0, "Energy cost should not be a negative number!\n" "Settings:\n{}".format(self.house.devices_settings))