Skip to content

Commit

Permalink
fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vcai122 committed Nov 19, 2023
1 parent cd9c80e commit 4403781
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 52 deletions.
16 changes: 8 additions & 8 deletions backend/tests/gsr_booking/test_gsr_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,21 @@ def test_recent(self):
self.assertIn("image_url", res_json[0])
self.assertNotEqual(res_json[0]["id"], res_json[1]["id"])

@mock.patch("gsr_booking.views.BW.is_wharton", is_wharton_false)
@mock.patch("gsr_booking.api_wrapper.WhartonBookingWrapper.is_wharton", is_wharton_false)
def test_get_wharton_false(self):
response = self.client.get(reverse("is-wharton"))
res_json = json.loads(response.content)
self.assertEqual(1, len(res_json))
self.assertFalse(res_json["is_wharton"])

@mock.patch("gsr_booking.views.BW.is_wharton", is_wharton_true)
@mock.patch("gsr_booking.api_wrapper.WhartonBookingWrapper.is_wharton", is_wharton_true)
def test_get_wharton_true(self):
response = self.client.get(reverse("is-wharton"))
res_json = json.loads(response.content)
self.assertEqual(1, len(res_json))
self.assertTrue(res_json["is_wharton"])

@mock.patch("gsr_booking.views.BW.get_availability", libcal_availability)
@mock.patch("gsr_booking.api_wrapper.BookingHandler.get_availability", libcal_availability)
def test_availability_libcal(self):
response = self.client.get(reverse("availability", args=["1086", "1889"]))
res_json = json.loads(response.content)
Expand All @@ -120,7 +120,7 @@ def test_availability_libcal(self):
self.assertIn("id", room)
self.assertIn("availability", room)

@mock.patch("gsr_booking.views.BW.get_availability", wharton_availability)
@mock.patch("gsr_booking.api_wrapper.BookingHandler.get_availability", wharton_availability)
def test_availability_wharton(self):
response = self.client.get(reverse("availability", args=["JMHH", "1"]))
res_json = json.loads(response.content)
Expand All @@ -134,7 +134,7 @@ def test_availability_wharton(self):
self.assertIn("id", room)
self.assertIn("availability", room)

@mock.patch("gsr_booking.views.BW.book_room", book_cancel_room)
@mock.patch("gsr_booking.api_wrapper.BookingHandler.book_room", book_cancel_room)
def test_book_libcal(self):
payload = {
"start_time": "2021-11-21T18:30:00-05:00",
Expand All @@ -150,7 +150,7 @@ def test_book_libcal(self):
self.assertEqual(1, len(res_json))
self.assertEqual("success", res_json["detail"])

@mock.patch("gsr_booking.views.BW.book_room", book_cancel_room)
@mock.patch("gsr_booking.api_wrapper.BookingHandler.book_room", book_cancel_room)
def test_book_wharton(self):
payload = {
"start_time": "2021-11-21T18:30:00-05:00",
Expand All @@ -166,7 +166,7 @@ def test_book_wharton(self):
self.assertEqual(1, len(res_json))
self.assertEqual("success", res_json["detail"])

@mock.patch("gsr_booking.views.BW.cancel_room", book_cancel_room)
@mock.patch("gsr_booking.api_wrapper.BookingHandler.cancel_room", book_cancel_room)
def test_cancel_room(self):
payload = {"booking_id": "booking id"}
response = self.client.post(
Expand All @@ -176,7 +176,7 @@ def test_cancel_room(self):
self.assertEqual(1, len(res_json))
self.assertEqual("success", res_json["detail"])

@mock.patch("gsr_booking.views.BW.get_reservations", reservations)
@mock.patch("gsr_booking.api_wrapper.BookingHandler.get_reservations", reservations)
def test_reservations(self):
response = self.client.get(reverse("reservations"))
res_json = json.loads(response.content)
Expand Down
79 changes: 35 additions & 44 deletions backend/tests/gsr_booking/test_gsr_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from django.test import TestCase
from rest_framework.test import APIClient

from gsr_booking.api_wrapper import BookingWrapper
from gsr_booking.group_logic import GroupBook
from gsr_booking.api_wrapper import GSRBooker, WhartonGSRBooker
from gsr_booking.models import GSR, Group, GroupMembership, GSRBooking, Reservation


Expand Down Expand Up @@ -51,11 +50,6 @@ def json(self):
return Mock(json.load(data), 200)


def mock_check_credits(self, user):
wharton_lids = GSR.objects.filter(kind=GSR.KIND_WHARTON).values_list("lid", flat=True)
return {lid: 90 for lid in wharton_lids}


class TestBookingWrapper(TestCase):
def setUp(self):
call_command("load_gsrs")
Expand All @@ -65,94 +59,91 @@ def setUp(self):
)
self.client = APIClient()
self.client.force_authenticate(user=self.user)
self.bw = BookingWrapper()
self.gb = GroupBook()
self.group = Group.objects.create(owner=self.group_user, name="Penn Labs", color="blue")

@mock.patch("gsr_booking.api_wrapper.WhartonLibWrapper.request", mock_requests_get)
@mock.patch("gsr_booking.api_wrapper.WhartonBookingWrapper.request", mock_requests_get)
def test_is_wharton(self):
self.assertFalse(self.bw.is_wharton(self.user))
self.assertFalse(WhartonGSRBooker.is_wharton(self.user))

@mock.patch("gsr_booking.api_wrapper.WhartonLibWrapper.request", mock_requests_get)
@mock.patch("gsr_booking.api_wrapper.WhartonBookingWrapper.request", mock_requests_get)
def test_wharton_availability(self):
availability = self.bw.get_availability("JMHH", 1, "2021-01-07", "2022-01-08", self.user)
availability = GSRBooker.get_availability("JMHH", 1, "2021-01-07", "2022-01-08", self.user)
self.assertIn("name", availability)
self.assertIn("gid", availability)
self.assertIn("rooms", availability)
self.assertIn("room_name", availability["rooms"][0])
self.assertIn("id", availability["rooms"][0])
self.assertIn("availability", availability["rooms"][0])

@mock.patch("gsr_booking.api_wrapper.WhartonLibWrapper.check_credits", mock_check_credits)
@mock.patch("gsr_booking.api_wrapper.WhartonLibWrapper.request", mock_requests_get)
# @mock.patch("gsr_booking.api_wrapper.WhartonBookingWrapper.check_credits", mock_check_credits)
@mock.patch("gsr_booking.api_wrapper.WhartonBookingWrapper.request", mock_requests_get)
def test_book_wharton(self):
book_wharton = self.bw.book_room(
book_wharton = GSRBooker.book_room(
1, 94, "241", "2021-12-05T16:00:00-05:00", "2021-12-05T16:30:00-05:00", self.user
)
self.assertEquals("241", book_wharton.room_name)
self.assertEquals("241", book_wharton.gsrbooking_set.first().room_name)

@mock.patch("gsr_booking.api_wrapper.WhartonLibWrapper.request", mock_requests_get)
@mock.patch("gsr_booking.api_wrapper.WhartonBookingWrapper.request", mock_requests_get)
def test_wharton_reservations(self):
reservations = self.bw.WLW.get_reservations(self.user)
reservations = WhartonGSRBooker.get_reservations(self.user)
self.assertTrue(isinstance(reservations, list))
self.assertIn("booking_id", reservations[0])
self.assertIn("gsr", reservations[0])
self.assertIn("gid", reservations[0])

@mock.patch("gsr_booking.api_wrapper.WhartonLibWrapper.request", mock_requests_get)
@mock.patch("gsr_booking.api_wrapper.WhartonBookingWrapper.request", mock_requests_get)
def test_cancel_wharton(self):
cancel = self.bw.cancel_room("987654", self.user)
cancel = GSRBooker.cancel_room("987654", self.user)
self.assertIsNone(cancel)

@mock.patch("gsr_booking.api_wrapper.LibCalWrapper.request", mock_requests_get)
@mock.patch("gsr_booking.api_wrapper.LibCalBookingWrapper.request", mock_requests_get)
def test_libcal_availability(self):
availability = self.bw.get_availability("1086", 1889, "2021-01-07", "2022-01-08", self.user)
availability = GSRBooker.get_availability("1086", 1889, "2021-01-07", "2022-01-08", self.user)
self.assertIn("name", availability)
self.assertIn("gid", availability)
self.assertIn("rooms", availability)
self.assertIn("room_name", availability["rooms"][0])
self.assertIn("id", availability["rooms"][0])
self.assertIn("availability", availability["rooms"][0])

@mock.patch("gsr_booking.api_wrapper.LibCalWrapper.request", mock_requests_get)
@mock.patch("gsr_booking.api_wrapper.LibCalBookingWrapper.request", mock_requests_get)
def test_book_libcal(self):
book_libcal = self.bw.book_room(
book_libcal = GSRBooker.book_room(
1889,
7192,
"VP WIC Booth 01",
"2021-12-05T16:00:00-05:00",
"2021-12-05T16:30:00-05:00",
self.user,
)
self.assertEquals("VP WIC Booth 01", book_libcal.room_name)
print('here', book_libcal)
self.assertEquals("VP WIC Booth 01", book_libcal.gsrbooking_set.first().room_name)

@mock.patch(
"gsr_booking.api_wrapper.WhartonLibWrapper.request", mock_requests_get
"gsr_booking.api_wrapper.WhartonBookingWrapper.request", mock_requests_get
) # purposefully wharton request here
def test_libcal_reservations(self):
reservations = self.bw.get_reservations(self.user)
reservations = GSRBooker.get_reservations(self.user)
self.assertTrue(isinstance(reservations, list))
self.assertIn("booking_id", reservations[0])
self.assertIn("gsr", reservations[0])

@mock.patch("gsr_booking.api_wrapper.LibCalWrapper.request", mock_requests_get)
@mock.patch("gsr_booking.api_wrapper.LibCalBookingWrapper.request", mock_requests_get)
def test_cancel_libcal(self):
group = Group.objects.create(owner=self.user)
reservation = Reservation.objects.create(creator=self.user, group=group)
GSRBooking.objects.create(
reservation=reservation,
user=self.user,
booking_id="123",
gsr=GSR.objects.all().first(),
gsr=GSR.objects.filter(kind="LIBCAL").first(),
room_id=1,
room_name="room",
)
cancel = self.bw.cancel_room("123", self.user)
cancel = GSRBooker.cancel_room("123", self.user)
self.assertIsNone(cancel)

@mock.patch(
"gsr_booking.api_wrapper.WhartonLibWrapper.check_credits", mock_check_credits
) # purposefully use mock_check_credits to mock being wharton user with credits
@mock.patch("gsr_booking.api_wrapper.WhartonLibWrapper.request", mock_requests_get)

@mock.patch("gsr_booking.api_wrapper.WhartonBookingWrapper.request", mock_requests_get)
def test_group_book_wharton(self):
# make sure group_user is treated as a wharton user so they
# are returned in list of wharton users in gb.book_room
Expand All @@ -166,7 +157,7 @@ def test_group_book_wharton(self):
)

# reservation under user with group
reservation = self.gb.book_room(
reservation = GSRBooker.book_room(
1,
94,
"241",
Expand All @@ -177,22 +168,22 @@ def test_group_book_wharton(self):
)

bookings = list(reservation.gsrbooking_set.all())
self.assertEqual(len(bookings), 4)
bookings.sort(key=lambda x: x.start)
# check bookings cover entire range and enough time
for i in range(len(bookings) - 1):
self.assertEqual(bookings[i].end, bookings[i + 1].start)
for booking in bookings:
self.assertEqual(booking.end - booking.start, timedelta(minutes=30))
total_time = sum([booking.end - booking.start for booking in bookings], timedelta())
self.assertEqual(total_time, timedelta(hours=2))

# check reservation exists
self.assertIsNotNone(Reservation.objects.get(pk=reservation.id))

@mock.patch("gsr_booking.api_wrapper.LibCalWrapper.request", mock_requests_get)
@mock.patch("gsr_booking.api_wrapper.LibCalBookingWrapper.request", mock_requests_get)
def test_group_book_libcal(self):
# add user to the group
GroupMembership.objects.create(user=self.user, group=self.group, accepted=True)

reservation = self.gb.book_room(
reservation = GSRBooker.book_room(
1889,
7192,
"VP WIC Booth 01",
Expand All @@ -207,7 +198,7 @@ def test_group_book_libcal(self):
# check bookings cover entire range and enough time
for i in range(len(bookings) - 1):
self.assertEqual(bookings[i].end, bookings[i + 1].start)
for booking in bookings:
self.assertEqual(booking.end - booking.start, timedelta(minutes=30))
total_time = sum([booking.end - booking.start for booking in bookings], timedelta())
self.assertEqual(total_time, timedelta(hours=2))
# check reservation exists
self.assertIsNotNone(Reservation.objects.get(pk=reservation.id))

0 comments on commit 4403781

Please sign in to comment.