class CityTests(unittest.TestCase): def setUp(self): """Set up a new city object for testing.""" self.id = 1 self.x_coord = 538 self.y_coord = 732 self.city = City(self.id, self.x_coord, self.y_coord) def test_create_new_city(self): """Test that city is created correctly. city should be an instance of City class. city should have id that was passed to constructor city should have x and y coordinates stored as a tuple """ self.assertIsInstance(self.city, City) self.assertEqual(self.city.id, self.id) x, y = self.city.coordinates self.assertEqual(x, self.x_coord) self.assertEqual(y, self.y_coord) def test_distance(self): """Test that city calculates distance between itself and another city correctly. """ second_city = City(2, 349, 265) distance = self.city.distance(second_city) self.assertEqual(distance, 504)
class SalesmanTests(unittest.TestCase): def setUp(self): """Set up a new salesman instance for testing.""" self.city = City(1, 538, 732) self.salesman = Salesman(self.city, mock_selector) def test_create_new_salesman(self): """Test that salesman object instantiated correctly. Should be an instance of Salesman class. Should have a list of cities visited named trip containing only initial city. Should have trip_length of 0 """ self.assertIsInstance(self.salesman, Salesman) self.assertEqual(1, len(self.salesman.trip)) self.assertEqual(self.salesman.trip[0], self.city) self.assertEqual(self.salesman.trip_length, 0) def test_trip_length(self): """Test that trip_length property returns correct value.""" city2 = City(2, 349, 265) self.assertEqual(self.salesman.trip_length, 0) length = self.city.distance(city2) * 2 self.salesman.trip.append(city2) self.assertEqual(self.salesman.trip_length, length) def test_next_city(self): """Test next_city method. next_city method should append new city returned from mock_selector to trip trip length should now be equal to the length between the two cities (there and back) a salesman instance that is not passed a selector method should raise a NotImplementedError """ roads = [] prob_calc = None params = (1, 0.5, 0.5, 1) selector_params = (roads, prob_calc, params) self.salesman.next_city_selector(selector_params) new_city = self.salesman.trip[-1] self.assertEqual(len(self.salesman.trip), 2) self.assertEqual(new_city.id, 2) self.assertEqual(new_city.coordinates, (349, 265)) new_salesman = Salesman(new_city) self.assertRaises(NotImplementedError, lambda: new_salesman.next_city_selector(selector_params))