class BookingMethodsTests(TestCase): def setUp(self): self.restaurant = Restaurant.objects.create( name='da mario', city='vignola', address='via baracchini, 95', n_places=50, booking_duration=120) self.client_user = User.objects.create(first_name='paolo', last_name='verdi', email='*****@*****.**', username='******', user_type=User.TYPES[0][0]) self.client_user.set_password('password') self.restaurant_user = User.objects.create( first_name='mario', last_name='rossi', email='*****@*****.**', username='******', user_type=User.TYPES[1][0], restaurant_information=self.restaurant) self.restaurant_user.set_password('password') restaurant_position = get_coordinates(self.restaurant.city + ', ' + self.restaurant.address) self.restaurant.latitude = restaurant_position['lat'] self.restaurant.longitude = restaurant_position['lng'] self.booking = Booking( client=self.client_user, restaurant=self.restaurant, n_places=2, start_time=timezone.make_aware( datetime.now(), timezone.get_current_timezone()).replace(microsecond=0), state=Booking.STATES[0][0]) self.booking.end_time = self.booking.calculate_end_time() def test_end_is_after_start(self): self.assertEqual(self.booking.end_time <= self.booking.start_time, False)
class CheckAvailabilityWiewTests(TestCase): def setUp(self): self.url = reverse('booking:check_availability') self.client = Client() self.password = '******' self.datetime = timezone.make_aware( datetime.now(), timezone.get_current_timezone()).replace(microsecond=0) self.restaurant = Restaurant.objects.create( name='da mario', city='vignola', address='via baracchini, 95', n_places=50, booking_duration=120) restaurant_position = get_coordinates(self.restaurant.city + ', ' + self.restaurant.address) self.restaurant.latitude = restaurant_position['lat'] self.restaurant.longitude = restaurant_position['lng'] self.restaurant.save() self.client_user = User.objects.create(first_name='paolo', last_name='verdi', email='*****@*****.**', username='******', user_type=User.TYPES[0][0]) self.client_user.set_password(self.password) self.client_user.save() self.restaurant_user = User.objects.create( first_name='mario', last_name='rossi', email='*****@*****.**', username='******', user_type=User.TYPES[1][0], restaurant_information=self.restaurant) self.restaurant_user.set_password(self.password) self.restaurant_user.save() self.booking = Booking(client=self.client_user, restaurant=self.restaurant, start_time=self.datetime, n_places=2, state=Booking.STATES[1][0]) self.booking.end_time = self.booking.calculate_end_time() def test_client_logged_ajax_call(self): response = self.client.post(self.url, {}, HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(response.status_code, 200) def test_client_logged_no_ajax_call(self): response = self.client.post(self.url, {}) self.assertEqual(response.status_code, 404) def test_client_logged_ajax_call_with_data(self): self.booking.save() self.client.login(username=self.client_user.username, password=self.password) data = { 'restaurant_id': self.restaurant.id, 'n_places': 10, 'start_time': self.datetime.strftime("%Y-%m-%d-%H-%M-%S") } data_result = {'result': 'success', 'state': Booking.STATES[1][0]} response = self.client.post(self.url, data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(response.status_code, 200) self.assertJSONEqual(str(response.content, encoding='utf8'), data_result) def test_client_logged_ajax_call_without_data(self): data = { 'restaurant_id': 0, 'n_places': 10, 'start_time': self.datetime.strftime("%Y-%m-%d-%H-%M-%S") } data_result = {'result': 'error'} response = self.client.post(self.url, data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(response.status_code, 200) self.assertJSONEqual(str(response.content, encoding='utf8'), data_result)
class ClientBookingsWiewTests(TestCase): def setUp(self): self.url = reverse('booking:client_bookings') self.client = Client() self.password = '******' self.datetime = timezone.make_aware( datetime.now(), timezone.get_current_timezone()).replace(microsecond=0) self.restaurant = Restaurant.objects.create( name='da mario', city='vignola', address='via baracchini, 95', n_places=50, booking_duration=120) restaurant_position = get_coordinates(self.restaurant.city + ', ' + self.restaurant.address) self.restaurant.latitude = restaurant_position['lat'] self.restaurant.longitude = restaurant_position['lng'] self.restaurant.save() self.client_user = User.objects.create(first_name='paolo', last_name='verdi', email='*****@*****.**', username='******', user_type=User.TYPES[0][0]) self.client_user.set_password(self.password) self.client_user.save() self.restaurant_user = User.objects.create( first_name='mario', last_name='rossi', email='*****@*****.**', username='******', user_type=User.TYPES[1][0], restaurant_information=self.restaurant) self.restaurant_user.set_password(self.password) self.restaurant_user.save() self.booking = Booking(client=self.client_user, restaurant=self.restaurant, start_time=self.datetime, n_places=2, state=Booking.STATES[1][0]) self.booking.end_time = self.booking.calculate_end_time() def test_not_logged_user(self): response = self.client.get(self.url) expected_url = reverse('login') + '?next=' + self.url self.assertRedirects(response, expected_url, status_code=302, target_status_code=200) def test_restaurant_logged(self): self.restaurant_user.client_information = self.restaurant self.restaurant_user.save() self.client.login(username=self.restaurant_user.username, password=self.password) response = self.client.get(self.url) expected_url = reverse('login') + '?next=' + self.url self.assertRedirects(response, expected_url, status_code=302, target_status_code=200) def test_client_logged_without_results(self): self.client.login(username=self.client_user.username, password=self.password) response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertQuerysetEqual(response.context['booking_list'], []) def test_client_logged_with_results(self): self.booking.save() self.client.login(username=self.client_user.username, password=self.password) response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['booking_list'][0], self.booking) def test_booking_before_now(self): self.booking.start_time = self.datetime - timedelta(minutes=1) self.booking.save() self.client.login(username=self.client_user.username, password=self.password) response = self.client.get(self.url) self.assertEqual(response.status_code, 200) self.assertQuerysetEqual(response.context['booking_list'], [])