def test_should_return_name_of_the_cheapper_hotel_when_pass_two_hotels_with_same_price( self): hotels = [ Hotel('LakeWood', 10, 20, 20, 25, 1), Hotel('Wood', 10, 20, 20, 25, 2) ] calculator = Calculator(hotels) self.assertEqual(calculator.getCheappest(customerType, period), 'Wood')
def test_should_return_name_of_the_second_hotel_because_its_cheaper(self): hotels = [ Hotel('Wood', 10, 20, 20, 25, 1), Hotel('LakeWood', 8, 10, 20, 25, 1) ] calculator = Calculator(hotels) self.assertEqual(calculator.getCheappest(customerType, period), 'LakeWood')
def setUp(self): self.weekday_regular_price = 110 self.weekday_reward_price = 80 self.weekend_regular_price = 90 self.weekend_reward_price = 80 self.hotel = Hotel( "Lakewood", 3, { "weekday": { "Regular": self.weekday_regular_price, "Reward": self.weekday_reward_price }, "weekend": { "Regular": self.weekend_regular_price, "Reward": self.weekend_reward_price } }) pass
def load_from_file(cls, hotels_config_file='hotels.json'): """Instance a hotel chain from a configuration file path """ hotels = [] with open(hotels_config_file) as f: hotels_config = json.load(f) for hotel in hotels_config: hotels.append(Hotel(**hotel)) hotel_chain = cls() hotel_chain._hotels = hotels return hotel_chain
def setUp(self): self.hotel_a = Hotel( "Lakewood", 3, { "weekday": { "Regular": 110, "Reward": 80, }, "weekend": { "Regular": 90, "Reward": 80 } }) self.hotel_b = Hotel( "Bridgewood", 4, { "weekday": { "Regular": 160, "Reward": 110, }, "weekend": { "Regular": 60, "Reward": 50 } }) self.hotel_c = Hotel( "Ridgewood", 5, { "weekday": { "Regular": 220, "Reward": 100, }, "weekend": { "Regular": 150, "Reward": 40 } }) self.hotel_chain = HotelChain( [self.hotel_a, self.hotel_b, self.hotel_c])
def process_task(self, city_id, hotel_id): url = self.domain + "/Hotel_Review-g{}-d{}".format( city_id, hotel_id) soup = ReviewGenerator.get_soup(url) name = soup.find("h1", attrs={ "class": "ui_header", "id": "HEADING"}) name = name.text if name else None rating = soup.find("span", attrs={ "class": "ui_bubble_rating"}) if rating: rating = rating.get("class")[1] rating = re.search(r"bubble_(\d\d)", rating) rating = rating.group(1) if rating else None else: rating = None reviews = list(ReviewGenerator(city_id, hotel_id, self.domain)) reviews = list(set(reviews)) if reviews and name: return Hotel(name, rating, reviews, url)
class TestHotelChain(unittest.TestCase): def setUp(self): self.hotel_a = Hotel( "Lakewood", 3, { "weekday": { "Regular": 110, "Reward": 80, }, "weekend": { "Regular": 90, "Reward": 80 } }) self.hotel_b = Hotel( "Bridgewood", 4, { "weekday": { "Regular": 160, "Reward": 110, }, "weekend": { "Regular": 60, "Reward": 50 } }) self.hotel_c = Hotel( "Ridgewood", 5, { "weekday": { "Regular": 220, "Reward": 100, }, "weekend": { "Regular": 150, "Reward": 40 } }) self.hotel_chain = HotelChain( [self.hotel_a, self.hotel_b, self.hotel_c]) def test_get_lowest_offer_empty_current_offer(self): customer_request = CustomerRequest('Regular', []) self.assertEqual( HotelChain.get_lowest_offer( None, self.hotel_a.get_offer(customer_request)), self.hotel_a.get_offer(customer_request)) def test_get_lowest_offer_empty_contender_offer(self): customer_request = CustomerRequest('Regular', []) self.assertEqual( HotelChain.get_lowest_offer( self.hotel_a.get_offer(customer_request), None), self.hotel_a.get_offer(customer_request)) def test_get_lowest_offer_contender_offer_lower_price(self): customer_request = CustomerRequest('Regular', ['16Mar2009(mon)']) self.assertEqual( HotelChain.get_lowest_offer( self.hotel_b.get_offer(customer_request), self.hotel_a.get_offer(customer_request), ), self.hotel_a.get_offer(customer_request)) def test_get_lowest_offer_current_offer_lower_price(self): customer_request = CustomerRequest('Regular', ['16Mar2009(mon)']) self.assertEqual( HotelChain.get_lowest_offer( self.hotel_a.get_offer(customer_request), self.hotel_b.get_offer(customer_request), ), self.hotel_a.get_offer(customer_request)) def test_get_lowest_offer_same_price_contender_higher_ranking(self): customer_request = CustomerRequest( 'Reward', ['15Mar2009(sun)', '16Mar2009(mon)']) self.assertEqual( HotelChain.get_lowest_offer( self.hotel_a.get_offer(customer_request), self.hotel_b.get_offer(customer_request), ), self.hotel_b.get_offer(customer_request)) def test_get_lowest_offer_same_price_contender_lower_ranking(self): customer_request = CustomerRequest( 'Reward', ['15Mar2009(sun)', '16Mar2009(mon)']) self.assertEqual( HotelChain.get_lowest_offer( self.hotel_b.get_offer(customer_request), self.hotel_a.get_offer(customer_request), ), self.hotel_b.get_offer(customer_request)) # testing find best offer def test_find_best_offer_empty_dates(self): customer_request = CustomerRequest('Regular', []) self.assertEqual(self.hotel_chain.find_best_offer(customer_request), self.hotel_c.name) def test_find_best_offer_testcase_1(self): customer_request = CustomerRequest( 'Regular', ['16Mar2009(mon)', '17Mar2009(tues)', '18Mar2009(wed)']) self.assertEqual(self.hotel_chain.find_best_offer(customer_request), self.hotel_a.name) def test_find_best_offer_testcase_2(self): customer_request = CustomerRequest( 'Regular', ['20Mar2009(fri)', '21Mar2009(sat)', '22Mar2009(sun)']) self.assertEqual(self.hotel_chain.find_best_offer(customer_request), self.hotel_b.name) def test_find_best_offer_testcase_3(self): customer_request = CustomerRequest( 'Reward', ['26Mar2009(thur)', '27Mar2009(fri)', '28Mar2009(sat)']) self.assertEqual(self.hotel_chain.find_best_offer(customer_request), self.hotel_c.name)
def test_return_0_when_get_price_for_period(self): lakeWood = Hotel('LakeWood', 8, 10, 20, 25, 1) hotelPrice = lakeWood.getPriceForPeriod('regular',['SUN','MON','TUE']) self.assertEqual(hotelPrice,26)
def test_if_day_is_a_weekend_day(self): day = 'FRI' lakeWood = Hotel('LakeWood', 8, 10, 20, 25, 1) self.assertEqual(lakeWood.isWeekend(day), False)
class TestHotel(unittest.TestCase): def setUp(self): self.weekday_regular_price = 110 self.weekday_reward_price = 80 self.weekend_regular_price = 90 self.weekend_reward_price = 80 self.hotel = Hotel( "Lakewood", 3, { "weekday": { "Regular": self.weekday_regular_price, "Reward": self.weekday_reward_price }, "weekend": { "Regular": self.weekend_regular_price, "Reward": self.weekend_reward_price } }) pass def test_calculate_regular_empty(self): customer_request = CustomerRequest('Regular', []) self.assertEqual(self.hotel.calculate_price(customer_request), 0) def test_calculate_regular_1_weekday(self): customer_request = CustomerRequest('Regular', ['16Mar2009(mon)']) self.assertEqual(self.hotel.calculate_price(customer_request), self.weekday_regular_price) def test_calculate_regular_5_weekday(self): customer_request = CustomerRequest('Regular', [ '16Mar2009(mon)', '17Mar2009(tues)', '18Mar2009(wes)', '19Mar2009(thur)', '20Mar2009(fri)', ]) self.assertEqual(self.hotel.calculate_price(customer_request), self.weekday_regular_price * 5) def test_calculate_reward_1_weekday(self): customer_request = CustomerRequest('Reward', ['16Mar2009(mon)']) self.assertEqual(self.hotel.calculate_price(customer_request), self.weekday_reward_price) def test_calculate_reward_5_weekday(self): customer_request = CustomerRequest('Reward', [ '16Mar2009(mon)', '17Mar2009(tues)', '18Mar2009(wes)', '19Mar2009(thur)', '20Mar2009(fri)', ]) self.assertEqual(self.hotel.calculate_price(customer_request), self.weekday_reward_price * 5) def test_calculate_regular_1_weekend(self): customer_request = CustomerRequest('Regular', ['15Mar2009(sun)']) self.assertEqual(self.hotel.calculate_price(customer_request), self.weekend_regular_price) def test_calculate_regular_2_weekend(self): customer_request = CustomerRequest( 'Regular', ['14Mar2009(sat)', '15Mar2009(sun)']) self.assertEqual(self.hotel.calculate_price(customer_request), self.weekend_regular_price * 2) def test_calculate_reward_1_weekend(self): customer_request = CustomerRequest('Reward', ['14Mar2009(sat)']) self.assertEqual(self.hotel.calculate_price(customer_request), self.weekend_reward_price) def test_calculate_reward_2_weekend(self): customer_request = CustomerRequest( 'Reward', ['14Mar2009(sat)', '15Mar2009(sun)']) self.assertEqual(self.hotel.calculate_price(customer_request), self.weekend_reward_price * 2) def test_calculate_regular_2_weekday_2_weekend(self): customer_request = CustomerRequest('Regular', [ '14Mar2009(sat)', '15Mar2009(sun)', '16Mar2009(mon)', '17Mar2009(tues)', ]) self.assertEqual( self.hotel.calculate_price(customer_request), self.weekend_regular_price * 2 + self.weekday_regular_price * 2) def test_calculate_reward_2_weekday_2_weekend(self): customer_request = CustomerRequest('Reward', [ '14Mar2009(sat)', '15Mar2009(sun)', '16Mar2009(mon)', '17Mar2009(tues)', ]) self.assertEqual( self.hotel.calculate_price(customer_request), self.weekend_reward_price * 2 + self.weekday_reward_price * 2) def test_get_offer(self): customer_request = CustomerRequest('Reward', [ '14Mar2009(sat)', '15Mar2009(sun)', '16Mar2009(mon)', '17Mar2009(tues)' ]) self.assertDictEqual( self.hotel.get_offer(customer_request), { 'hotel': self.hotel, 'price': self.weekend_reward_price * 2 + self.weekday_reward_price * 2 })
def test_should_return_name_of_the_cheapper_hotel_when_pass_one_hotel( self): hotels = [Hotel('LakeWood', 8, 10, 20, 25, 1)] calculator = Calculator(hotels) self.assertEqual(calculator.getCheappest(customerType, period), 'LakeWood')