def test_add_new_vehicle_invalid_new_miles(self): v_negative = Vehicle('Car', -100) v_alpha = Vehicle('Car', 'abc') with self.assertRaises(MileageError): self.db.insert(v_negative) with self.assertRaises(MileageError): self.db.insert(v_alpha)
def test_error_increase_miles_for_vehicle_not_in_db(self): v1 = Vehicle('Purple Car', 100) self.db.insert(v1) expected = {'Purple Car': 100} v_not_in_db = Vehicle('Blue Car') with self.assertRaises(MileageError): self.db.increase_miles(v_not_in_db, 50) self.compare_db_to_expected(expected)
def test_increase_miles_invalid_new_miles(self): v_negative = Vehicle('Green Car', -100) v_alpha = Vehicle('Green Car', 'abc') v1 = Vehicle('Green Car', 100) self.db.insert(v1) with self.assertRaises(MileageError): self.db.increase_miles(v_negative, -100) with self.assertRaises(MileageError): self.db.increase_miles(v_alpha, 'pizza')
def test_will_not_add_duplicate_vehicle(self): v1 = Vehicle('Blue Car', 100) self.db.insert(v1) expected = {'Blue Car': 100} self.compare_db_to_expected(expected) with self.assertRaises(MileageError): v2 = Vehicle('Blue Car', 50) self.db.insert(v2) expected = {'Blue Car': 100} # Will not modify DB self.compare_db_to_expected(expected)
def test_add_new_vehicle(self): v1 = Vehicle('Blue Car', 100) self.db.insert(v1) expected = {'Blue Car': 100} self.compare_db_to_expected(expected) v2 = Vehicle('Green Car', 50) self.db.insert(v2) expected = {'Blue Car': 100, 'Green Car': 50} self.compare_db_to_expected(expected) v3 = Vehicle('Red Car', 0) self.db.insert(v3) expected = {'Blue Car': 100, 'Green Car': 50, 'Red Car': 0} self.compare_db_to_expected(expected)
def test_update_one_vehicle(self, mock_input, mock_positive_float): mock_input.return_value = 'Blue Car' mock_positive_float.return_value = 100 # re-import view here so when it imports other things, specfically the view_util # library, it will get the view_util library with the mock input_positive_float importlib.reload( view) # Re-import view to ensure it uses the mock imports # Make mock (fake, pretend) ViewModel with a mock insert method # We can make the mock here since it's not replacing any part of the # program code - it's a self-contained mock object. Use this to figure # out if the view calls the expected method with the correct data. mock_view_model = MagicMock() mock_view_model.increase_miles = MagicMock() # Give the new View the mock ViewModel test_view = view.View(mock_view_model) # The patched inputs will return 'Blue Car' and 100 so the expected vehicle is like this, vehicle = Vehicle('Blue Car', 100) # Call the method under test! test_view.update_one_vehicle() # And assert the mock is called with the correct vehicle. mock_view_model.increase_miles.assert_called_with(vehicle, 100)
def get_one_new_vehicle(self): name = input('Enter new vehicle name to insert, or enter to quit: ') if not name: return miles = input_positive_float(f'Enter new miles driven for {name}: ') vehicle = Vehicle(name, miles) try: self.view_model.insert(vehicle) return vehicle except MileageError as e: print(str(e))
def test_increase_miles_for_vehicle(self): # v1 = Vehicle('Blue Car', 100) # self.db.insert(v1) # expected = { 'Blue Car' : 100 } # self.compare_db_to_expected(expected) # self.db.increase_miles(v1, 50) # expected['Blue Car'] = 100 + 50 # self.compare_db_to_expected(expected) v1 = Vehicle('Pie', 0) self.db.insert(v1) self.db.increase_miles(v1, 30)
def test_insert_one_vehicle(self, mock_input, mock_positive_float): """ Need to replace the ViewModel with a fake view model so the data is not sent to a real database. This test cares that the update_one_vehicle is calling the increase_miles method in the view_model with the right data. The two patch decorators: Replace the input_positive_float with a mock function that returns a pre-configured value. Otherwise the test will stop and wait for input. Update one vehicle also uses the builtin input function, so replace that with a mock too, again, returning a pre-configured value. Patching replaces the parts of the program with mocks just for the duration of this test. When this test is done (passes, fails, errors, whatever) the original methods/functions are restored. """ mock_input.return_value = 'Green Car' mock_positive_float.return_value = 3 # re-import view here so when it imports other things, specfically the view_util # library, it will get the view_util library with the mock input_positive_float importlib.reload(view) # Make mock (fake, pretend) ViewModel with a mock insert method # We can make the mock here since it's not replacing any part of the # program code - it's a self-contained mock object. Use this to figure # out if the view calls the expected method with the correct data. mock_view_model = MagicMock() mock_view_model.insert = MagicMock() # Give the new View the mock ViewModel test_view = view.View(mock_view_model) # The patched inputs will return 'Green Car' and 3 so the expected vehicle is like this, vehicle = Vehicle('Green Car', 3) # Call the method under test! test_view.get_one_new_vehicle() # And assert the mock is called with the correct vehicle. mock_view_model.insert.assert_called_with(vehicle)
def update_one_vehicle(self): name = input( 'Enter existing vehicle name or enter to stop updating vehicles: ') if not name: return miles = input_positive_float(f'Enter new miles driven for {name}: ') vehicle = Vehicle(name, miles) # Can substitute a Van for a Vehicle - code all still works, # although DB would need to be updated to store the extra field. # seats = int(input('Enter seats for van: ')) # vehicle = Van(name, seats) try: self.view_model.increase_miles(vehicle, miles) return vehicle except MileageError as e: print(str(e))
def get_all(self): con = sqlite3.connect(db) vehicles_cursor = con.execute('SELECT * FROM MILES') vehicles = [ Vehicle(*row) for row in vehicles_cursor.fetchall() ] con.close() return vehicles
def test_add_new_vehicle_no_vehicle(self): v_no_name = Vehicle(None) with self.assertRaises(MileageError): self.db.insert(v_no_name)
def test_increase_miles_no_vehicle(self): v_no_name = Vehicle(None) with self.assertRaises(MileageError): self.db.increase_miles(v_no_name, 100)
def get_all(self): response = requests.get(host).json() return [Vehicle(v['vehicle'], v['total_miles']) for v in response]