diff --git a/backend/laundry/api_wrapper.py b/backend/laundry/api_wrapper.py index fe12928f..1637bd38 100644 --- a/backend/laundry/api_wrapper.py +++ b/backend/laundry/api_wrapper.py @@ -10,6 +10,22 @@ def get_room_url(room_id: int): return f"{settings.LAUNDRY_URL}/rooms/{room_id}/machines?raw=true" +def get_validated(url): + """ + Makes a request to the given URL and returns the JSON response if the request is successful. + Uses headers specific to the laundry API and should not be used for other requests. + @param url: The URL to make the request to. + @return: The JSON response if the request is successful, otherwise None. + """ + try: + request = requests.get(url, timeout=60, headers=settings.LAUNDRY_HEADERS) + request.raise_for_status() + return request.json() + except HTTPError as e: + print(f"Error: {e}") + return None + + def update_machine_object(machine, machine_type_data): """ Updates Machine status and time remaining @@ -20,7 +36,7 @@ def update_machine_object(machine, machine_type_data): # TODO: need to update if we identify other codes, especially error status = machine["currentStatus"]["statusId"] if status == "IN_USE": - time_remaining = machine[3].getText().split(" ")[0] + time_remaining = machine["currentStatus"]["remainingSeconds"] machine_type_data["running"] += 1 try: machine_type_data["time_remaining"].append(int(time_remaining)) @@ -52,14 +68,10 @@ def parse_a_room(room_request_link): detailed = [] - try: - request = requests.get(room_request_link, timeout=60, headers=settings.LAUNDRY_HEADERS) - request.raise_for_status() - except HTTPError: + request_json = get_validated(room_request_link) + if request_json is None: return {"washers": washers, "dryers": dryers, "details": detailed} - request_json = request.json() - [ update_machine_object(machine, washers) if machine["isWasher"] else None for machine in request_json @@ -89,15 +101,8 @@ def check_is_working(): Returns True if the wash alert web interface seems to be working properly, or False otherwise. """ - try: - all_rooms_request = requests.get( - f"{settings.LAUNDRY_URL}/geoBoundaries/5610?raw=true", - timeout=60, - headers=settings.LAUNDRY_HEADERS, - ) - all_rooms_request.raise_for_status() - - except HTTPError: + all_rooms_request = get_validated(f"{settings.LAUNDRY_URL}/geoBoundaries/5610?raw=true") + if all_rooms_request is None: return False return True diff --git a/backend/laundry/management/commands/load_laundry_rooms.py b/backend/laundry/management/commands/load_laundry_rooms.py index 4784d2c8..65af9c0d 100644 --- a/backend/laundry/management/commands/load_laundry_rooms.py +++ b/backend/laundry/management/commands/load_laundry_rooms.py @@ -7,19 +7,29 @@ class Command(BaseCommand): def handle(self, *args, **kwargs): - + count = 0 with open("laundry/data/laundry_data.csv") as data: reader = csv.DictReader(data) - for row in reader: LaundryRoom.objects.get_or_create( room_id=int(row["room_id"]), - name=row["room_name"], - location=row["room_description"], - location_id=int(row["room_location"]), - total_washers=int(row["count_washers"]), - total_dryers=int(row["count_dryers"]), - new=True, + defaults={ + "name": row["room_name"], + "location": row["room_description"], + "location_id": int(row["room_location"]), + "total_washers": int(row["count_washers"]), + "total_dryers": int(row["count_dryers"]), + }, ) + count += 1 self.stdout.write("Uploaded Laundry Rooms!") + ( + self.stdout.write( + f"Warning: There are {LaundryRoom.objects.all().count() - count} rooms in the " + f"database but not in the data file. If they are no longer supported by Penn's " + f"servers, consider deleting them." + ) + if count < LaundryRoom.objects.all().count() + else None + ) diff --git a/backend/laundry/management/commands/update_laundry_rooms.py b/backend/laundry/management/commands/update_laundry_rooms.py index 90916cfe..70abcb3b 100644 --- a/backend/laundry/management/commands/update_laundry_rooms.py +++ b/backend/laundry/management/commands/update_laundry_rooms.py @@ -1,9 +1,9 @@ import csv -import requests from django.conf import settings from django.core.management.base import BaseCommand -from requests.exceptions import HTTPError + +from laundry.api_wrapper import get_validated def write_file(laundry_rooms): @@ -18,29 +18,19 @@ class Command(BaseCommand): def handle(self, *args, **kwargs): # Pull initial request with everything - try: - headers = { - "x-api-key": settings.LAUNDRY_X_API_KEY, - "alliancels-auth-token": settings.LAUNDRY_ALLIANCELS_API_KEY, - } - all_rooms_request = requests.get( - f"{settings.LAUNDRY_URL}/geoBoundaries/5610?raw=true", timeout=60, headers=headers - ) - all_rooms_request.raise_for_status() - - except HTTPError as e: - self.stdout.write(f"Error: {e}") + all_rooms_request_json = get_validated( + f"{settings.LAUNDRY_URL}/geoBoundaries/5610?raw=true" + ) + if all_rooms_request_json is None: return - - all_rooms_request_json = all_rooms_request.json() locations = all_rooms_request_json["geoBoundaries"][0]["geoBoundaries"] laundry_rooms = [ { - "room_id": room["id"], + "room_id": int(room["id"]), "room_name": room["roomName"], "room_description": location["description"], - "room_location": location["id"], + "room_location": int(location["id"]), } for location in locations for room in location["rooms"] @@ -49,18 +39,11 @@ def handle(self, *args, **kwargs): # for each room, send a request to find number of washers and dryers # TODO: This is really inefficient, but may require change in frontend code to update for room in laundry_rooms: - try: - room_request = requests.get( - f"{settings.LAUNDRY_URL}/rooms/{room['room_id']}/machines?raw=true", - timeout=60, - headers=headers, - ) - room_request.raise_for_status() - except HTTPError as e: - self.stdout.write(f"Error: {e}") + room_request_json = get_validated( + f"{settings.LAUNDRY_URL}/rooms/{room['room_id']}/machines?raw=true" + ) + if room_request_json is None: return - - room_request_json = room_request.json() # count washers and dryers count_washers = 0 count_dryers = 0 diff --git a/backend/laundry/migrations/0004_alter_laundryroom_room_id.py b/backend/laundry/migrations/0004_alter_laundryroom_room_id.py new file mode 100644 index 00000000..5ddb2df5 --- /dev/null +++ b/backend/laundry/migrations/0004_alter_laundryroom_room_id.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.2 on 2024-11-17 01:04 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("laundry", "0003_rename_hall_id_laundryroom_room_id_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="laundryroom", + name="room_id", + field=models.IntegerField(default=0, unique=True), + ), + ] diff --git a/backend/laundry/migrations/0005_remove_laundryroom_new.py b/backend/laundry/migrations/0005_remove_laundryroom_new.py new file mode 100644 index 00000000..699e3786 --- /dev/null +++ b/backend/laundry/migrations/0005_remove_laundryroom_new.py @@ -0,0 +1,17 @@ +# Generated by Django 5.0.2 on 2024-11-17 09:12 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("laundry", "0004_alter_laundryroom_room_id"), + ] + + operations = [ + migrations.RemoveField( + model_name="laundryroom", + name="new", + ), + ] diff --git a/backend/laundry/models.py b/backend/laundry/models.py index d2a657cf..cd52d185 100644 --- a/backend/laundry/models.py +++ b/backend/laundry/models.py @@ -3,18 +3,15 @@ class LaundryRoom(models.Model): - room_id = models.IntegerField(default=0, primary_key=True) + room_id = models.IntegerField(default=0, unique=True) name = models.CharField(max_length=255) location = models.CharField(max_length=255) location_id = models.IntegerField(default=0) total_washers = models.IntegerField(default=0) total_dryers = models.IntegerField(default=0) - # laundry api update 9/29/2024; new indicates it is new, added to avoid nuking old info - new = models.BooleanField(default=False) - def __str__(self): - return f"Hall {self.name} | {self.location}" + return f"Room {self.name} | {self.location}" class LaundrySnapshot(models.Model): @@ -24,4 +21,4 @@ class LaundrySnapshot(models.Model): available_dryers = models.IntegerField() def __str__(self): - return f"Hall {self.room.name} | {self.date.date()}" + return f"Room {self.room.name} | {self.date.date()}" diff --git a/backend/laundry/serializers.py b/backend/laundry/serializers.py index 5c4513c3..59581969 100644 --- a/backend/laundry/serializers.py +++ b/backend/laundry/serializers.py @@ -4,6 +4,9 @@ class LaundryRoomSerializer(serializers.ModelSerializer): + # tech debt but we're rewriting at some point and this maintains frontend-compatibility + hall_id = serializers.IntegerField(source="room_id") + class Meta: model = LaundryRoom - fields = ("name", "room_id", "location") + fields = ("name", "hall_id", "location") diff --git a/backend/laundry/urls.py b/backend/laundry/urls.py index b05861d1..0fa26807 100644 --- a/backend/laundry/urls.py +++ b/backend/laundry/urls.py @@ -6,10 +6,10 @@ urlpatterns = [ - path("hall//", cache_page(Cache.MINUTE)(HallInfo.as_view()), name="hall-info"), - path("usage//", cache_page(Cache.MINUTE)(HallUsage.as_view()), name="hall-usage"), + path("hall//", cache_page(Cache.MINUTE)(HallInfo.as_view()), name="hall-info"), + path("usage//", cache_page(Cache.MINUTE)(HallUsage.as_view()), name="hall-usage"), path( - "rooms/", + "rooms/", cache_page(Cache.MINUTE)(MultipleHallInfo.as_view()), name="multiple-hall-info", ), diff --git a/backend/laundry/views.py b/backend/laundry/views.py index 94a7582b..781ed4b8 100644 --- a/backend/laundry/views.py +++ b/backend/laundry/views.py @@ -135,8 +135,8 @@ def compute_usage(room_id): return content - def get(self, request, hall_id): - return Response(HallUsage.compute_usage(hall_id)) + def get(self, request, room_id): + return Response(HallUsage.compute_usage(room_id)) class Preferences(APIView): @@ -155,9 +155,9 @@ def get(self, request): cached_preferences = cache.get(key) if cached_preferences is None: preferences = request.user.profile.laundry_preferences.all() - cached_preferences = preferences.values_list("hall_id", flat=True) + cached_preferences = preferences.values_list("room_id", flat=True) # get all laundries with one of these - valid_rooms = LaundryRoom.objects.filter(room_id__in=cached_preferences, new=True) + valid_rooms = LaundryRoom.objects.filter(room_id__in=cached_preferences) cached_preferences = valid_rooms.values_list("room_id", flat=True) cache.set(key, cached_preferences, Cache.MONTH) @@ -169,12 +169,10 @@ def post(self, request): preferences = profile.laundry_preferences if "rooms" not in request.data: return Response({"success": False, "error": "No rooms provided"}, status=400) - halls = [ - get_object_or_404(LaundryRoom, room_id=int(hall_id)) - for hall_id in request.data["rooms"] + get_object_or_404(LaundryRoom, room_id=int(room_id)) + for room_id in request.data["rooms"] ] - # clears all previous preferences in many-to-many preferences.clear() preferences.add(*halls) diff --git a/backend/tests/laundry/mock_geoboundaries_request.json b/backend/tests/laundry/mock_geoboundaries_request.json index 8e04e8c6..14dd62bf 100644 --- a/backend/tests/laundry/mock_geoboundaries_request.json +++ b/backend/tests/laundry/mock_geoboundaries_request.json @@ -186,11 +186,11 @@ "signupBonusExpirationDate": null, "maxRedemptions": null, "ordinalValue": 1 - }, + } ], "ordinalValue": 4, "isRetail": false - }, + } ], "ordinalValue": 0, "isRetail": false diff --git a/backend/tests/laundry/test_api_wrapper.py b/backend/tests/laundry/test_api_wrapper.py index a835995b..bb5b8c11 100644 --- a/backend/tests/laundry/test_api_wrapper.py +++ b/backend/tests/laundry/test_api_wrapper.py @@ -1,7 +1,5 @@ -''' from unittest import mock -from django.conf import settings from django.test import TestCase from laundry.api_wrapper import all_status, room_status, save_data @@ -9,30 +7,39 @@ from tests.laundry.test_commands import mock_laundry_get -ALL_URL = f"{settings.LAUNDRY_URL}/?location=" - - -@mock.patch("requests.get", mock_laundry_get) +@mock.patch("laundry.api_wrapper.get_validated", mock_laundry_get) class TestAllStatus(TestCase): def setUp(self): LaundryRoom.objects.get_or_create( - hall_id=0, name="Bishop White", location="Quad", total_washers=9, total_dryers=9 - ) - LaundryRoom.objects.get_or_create( - hall_id=1, name="Chestnut Butcher", location="Quad", total_washers=11, total_dryers=11 + room_id=14089, + name="English House", + location="English House", + location_id=14146, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=2, name="Class of 1928 Fisher", location="Quad", total_washers=8, total_dryers=8 + room_id=14099, + name="Harnwell 10th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=3, name="Craig", location="Quad", total_washers=3, total_dryers=3 + room_id=14100, + name="Harnwell 12th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) def test_all_status(self): data = all_status() - self.assertEqual(len(data), 4) + self.assertEqual(len(data), 3) for room in LaundryRoom.objects.all(): self.assertIn(room.name, data) @@ -55,20 +62,32 @@ def test_all_status(self): self.assertTrue(hall[machine]["offline"] >= 0) -@mock.patch("requests.get", mock_laundry_get) +@mock.patch("laundry.api_wrapper.get_validated", mock_laundry_get) class TestHallStatus(TestCase): def setUp(self): LaundryRoom.objects.get_or_create( - hall_id=0, name="Bishop White", location="Quad", total_washers=9, total_dryers=9 + room_id=14089, + name="English House", + location="English House", + location_id=14146, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=1, name="Chestnut Butcher", location="Quad", total_washers=11, total_dryers=11 + room_id=14099, + name="Harnwell 10th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=2, name="Class of 1928 Fisher", location="Quad", total_washers=8, total_dryers=8 - ) - LaundryRoom.objects.get_or_create( - hall_id=3, name="Craig", location="Quad", total_washers=3, total_dryers=3 + room_id=14100, + name="Harnwell 12th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) def test_all_status(self): @@ -87,20 +106,32 @@ def test_all_status(self): self.assertIn("status", machine) -@mock.patch("requests.get", mock_laundry_get) +@mock.patch("laundry.api_wrapper.get_validated", mock_laundry_get) class TestSaveData(TestCase): def setUp(self): LaundryRoom.objects.get_or_create( - hall_id=0, name="Bishop White", location="Quad", total_washers=9, total_dryers=9 - ) - LaundryRoom.objects.get_or_create( - hall_id=1, name="Chestnut Butcher", location="Quad", total_washers=11, total_dryers=11 + room_id=14089, + name="English House", + location="English House", + location_id=14146, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=2, name="Class of 1928 Fisher", location="Quad", total_washers=8, total_dryers=8 + room_id=14099, + name="Harnwell 10th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=3, name="Craig", location="Quad", total_washers=3, total_dryers=3 + room_id=14100, + name="Harnwell 12th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) def test_save_data(self): @@ -110,21 +141,20 @@ def test_save_data(self): save_data() # checks that all rooms have been accounted for - self.assertEqual(LaundrySnapshot.objects.all().count(), 4) + self.assertEqual(LaundrySnapshot.objects.all().count(), 3) # # adds all id's to a list - hall_ids = LaundryRoom.objects.all().values_list("hall_id", flat=True) + room_ids = LaundryRoom.objects.all().values_list("room_id", flat=True) - self.assertEqual(len(hall_ids), 4) + self.assertEqual(len(room_ids), 3) # asserts that fields are correct, and that all snapshots # have been accounted for for snapshot in LaundrySnapshot.objects.all(): - self.assertIn(snapshot.room.hall_id, hall_ids) + self.assertIn(snapshot.room.room_id, room_ids) self.assertTrue(snapshot.available_washers >= 0) self.assertTrue(snapshot.available_dryers >= 0) save_data() - self.assertEqual(LaundrySnapshot.objects.all().count(), 8) -''' \ No newline at end of file + self.assertEqual(LaundrySnapshot.objects.all().count(), 6) diff --git a/backend/tests/laundry/test_commands.py b/backend/tests/laundry/test_commands.py index e7600ab9..dd7dd602 100644 --- a/backend/tests/laundry/test_commands.py +++ b/backend/tests/laundry/test_commands.py @@ -1,5 +1,5 @@ -''' import csv +import json from io import StringIO from unittest import mock @@ -16,17 +16,15 @@ def mock_laundry_get(url, *args, **kwargs): if "/".join(split[0:3]) == settings.LAUNDRY_URL: if split[3] == "rooms": with open(f"tests/laundry/mock_rooms_request_{split[4]}.json", "rb") as f: - m = mock.MagicMock(content=f.read()) - return m + return json.load(f) elif split[3] == "geoBoundaries": with open("tests/laundry/mock_geoboundaries_request.json", "rb") as f: - m = mock.MagicMock(content=f.read()) - return m + return json.load(f) else: raise NotImplementedError -@mock.patch("requests.get", mock_laundry_get) +@mock.patch("laundry.api_wrapper.get_validated", mock_laundry_get) class TestGetSnapshot(TestCase): def setUp(self): # populates database with LaundryRooms @@ -68,7 +66,7 @@ def test_db_populate(self): self.assertEqual(LaundrySnapshot.objects.all().count(), 3) -@mock.patch("requests.get", mock_laundry_get) +@mock.patch("laundry.api_wrapper.get_validated", mock_laundry_get) class TestLaundryRoomMigration(TestCase): def test_db_populate(self): out = StringIO() @@ -101,9 +99,10 @@ def test_db_populate(self): call_command("load_laundry_rooms") # asserts that LaundryRooms do not recreate itself + self.assertEqual(LaundryRoom.objects.all().count(), num_rooms) -@mock.patch("requests.get", mock_laundry_get) +@mock.patch("laundry.management.commands.update_laundry_rooms.get_validated", mock_laundry_get) class TestUpdateLaundryRooms(TestCase): @mock.patch("laundry.management.commands.update_laundry_rooms.write_file") def test_update_laundry_rooms(self, mock_laundry_write): @@ -114,20 +113,25 @@ def test_update_laundry_rooms(self, mock_laundry_write): "room_name": "English House", "room_description": "English House", "room_location": 14146, + "count_washers": 3, + "count_dryers": 3, }, { "room_id": 14099, "room_name": "Harnwell 10th Floor", "room_description": "Harnwell College House", "room_location": 14150, + "count_washers": 3, + "count_dryers": 3, }, { "room_id": 14100, "room_name": "Harnwell 12th Floor", "room_description": "Harnwell College House", "room_location": 14150, + "count_washers": 3, + "count_dryers": 3, }, ] # assert that the mock was called with this mock_laundry_write.assert_called_with(expected) -''' \ No newline at end of file diff --git a/backend/tests/laundry/test_models.py b/backend/tests/laundry/test_models.py index 95d24bdb..3c8e2878 100644 --- a/backend/tests/laundry/test_models.py +++ b/backend/tests/laundry/test_models.py @@ -1,4 +1,3 @@ -''' from django.test import TestCase from laundry.models import LaundryRoom, LaundrySnapshot @@ -8,46 +7,53 @@ class LaundrySnapshotTestCase(TestCase): def setUp(self): # populates database with LaundryRooms LaundryRoom.objects.get_or_create( - hall_id=0, name="Bishop White", location="Quad", total_washers=9, total_dryers=9 + room_id=14089, + name="English House", + location="English House", + location_id=14146, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=1, name="Chestnut Butcher", location="Quad", total_washers=11, total_dryers=11 + room_id=14099, + name="Harnwell 10th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=2, name="Class of 1928 Fisher", location="Quad", total_washers=8, total_dryers=8 + room_id=14100, + name="Harnwell 12th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) - LaundryRoom.objects.get_or_create( - hall_id=3, name="Craig", location="Quad", total_washers=3, total_dryers=3 - ) - self.laundry_room = LaundryRoom.objects.get(hall_id=0, name="Bishop White", location="Quad") + self.laundry_room = LaundryRoom.objects.get(room_id=14089) self.snapshot = LaundrySnapshot.objects.create( - room=self.laundry_room, available_washers=10, available_dryers=10 + room=self.laundry_room, available_washers=3, available_dryers=3 ) def test_str(self): self.assertEqual( str(self.snapshot), - f"Hall No. {self.snapshot.room.hall_id} | {self.snapshot.date.date()}", + f"Room {self.snapshot.room.name} | {self.snapshot.date.date()}", ) class LaundryRoomTestCase(TestCase): def setUp(self): # populates database with LaundryRooms - LaundryRoom.objects.get_or_create( - hall_id=0, name="Bishop White", location="Quad", total_washers=9, total_dryers=9 - ) - LaundryRoom.objects.get_or_create( - hall_id=1, name="Chestnut Butcher", location="Quad", total_washers=11, total_dryers=11 - ) - LaundryRoom.objects.get_or_create( - hall_id=2, name="Class of 1928 Fisher", location="Quad", total_washers=8, total_dryers=8 - ) - LaundryRoom.objects.get_or_create( - hall_id=3, name="Craig", location="Quad", total_washers=3, total_dryers=3 + + self.room = LaundryRoom.objects.create( + room_id=14089, + name="English House", + location="English House", + location_id=14146, + total_washers=3, + total_dryers=3, ) - self.room = LaundryRoom.objects.create(hall_id=1, name="test hall", location="location") def test_str(self): - self.assertEqual(str(self.room), f"Hall No. {self.room.hall_id} | {self.room.name}") -''' \ No newline at end of file + self.assertEqual(str(self.room), f"Room {self.room.name} | {self.room.location}") diff --git a/backend/tests/laundry/test_views.py b/backend/tests/laundry/test_views.py index 2aa99667..31ee5607 100644 --- a/backend/tests/laundry/test_views.py +++ b/backend/tests/laundry/test_views.py @@ -1,4 +1,3 @@ -""" import json from unittest import mock @@ -15,20 +14,32 @@ User = get_user_model() -@mock.patch("requests.get", mock_laundry_get) +@mock.patch("laundry.api_wrapper.get_validated", mock_laundry_get) class HallIdViewTestCase(TestCase): def setUp(self): LaundryRoom.objects.get_or_create( - hall_id=0, name="Bishop White", location="Quad", total_washers=9, total_dryers=9 + room_id=14089, + name="English House", + location="English House", + location_id=14146, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=1, name="Chestnut Butcher", location="Quad", total_washers=11, total_dryers=11 + room_id=14099, + name="Harnwell 10th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=2, name="Class of 1928 Fisher", location="Quad", total_washers=8, total_dryers=8 - ) - LaundryRoom.objects.get_or_create( - hall_id=3, name="Craig", location="Quad", total_washers=3, total_dryers=3 + room_id=14100, + name="Harnwell 12th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) self.client = APIClient() @@ -38,31 +49,43 @@ def test_response(self): for hall in res_json: self.assertTrue( LaundryRoom.objects.filter( - name=hall["name"], hall_id=hall["hall_id"], location=hall["location"] + name=hall["name"], room_id=hall["hall_id"], location=hall["location"] ) ) -@mock.patch("requests.get", mock_laundry_get) +@mock.patch("laundry.api_wrapper.get_validated", mock_laundry_get) class HallInfoViewTestCase(TestCase): def setUp(self): LaundryRoom.objects.get_or_create( - hall_id=0, name="Bishop White", location="Quad", total_washers=9, total_dryers=9 - ) - LaundryRoom.objects.get_or_create( - hall_id=1, name="Chestnut Butcher", location="Quad", total_washers=11, total_dryers=11 + room_id=14089, + name="English House", + location="English House", + location_id=14146, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=2, name="Class of 1928 Fisher", location="Quad", total_washers=8, total_dryers=8 + room_id=14099, + name="Harnwell 10th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=3, name="Craig", location="Quad", total_washers=3, total_dryers=3 + room_id=14100, + name="Harnwell 12th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) - self.laundry_room = LaundryRoom.objects.get(hall_id=0, name="Bishop White", location="Quad") + self.laundry_room = LaundryRoom.objects.get(room_id=14089) self.client = APIClient() def test_response(self): - response = self.client.get(reverse("hall-info", args=[self.laundry_room.hall_id])) + response = self.client.get(reverse("hall-info", args=[self.laundry_room.room_id])) res_json = json.loads(response.content) if response.status_code == 200: self.assertEqual(self.laundry_room.name, res_json["hall_name"]) @@ -75,32 +98,44 @@ def test_hall_error(self): self.assertEqual(404, response.status_code) -@mock.patch("requests.get", mock_laundry_get) +@mock.patch("laundry.api_wrapper.get_validated", mock_laundry_get) class MultipleHallInfoViewTestCase(TestCase): def setUp(self): LaundryRoom.objects.get_or_create( - hall_id=0, name="Bishop White", location="Quad", total_washers=9, total_dryers=9 - ) - LaundryRoom.objects.get_or_create( - hall_id=1, name="Chestnut Butcher", location="Quad", total_washers=11, total_dryers=11 + room_id=14089, + name="English House", + location="English House", + location_id=14146, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=2, name="Class of 1928 Fisher", location="Quad", total_washers=8, total_dryers=8 + room_id=14099, + name="Harnwell 10th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=3, name="Craig", location="Quad", total_washers=3, total_dryers=3 + room_id=14100, + name="Harnwell 12th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) - self.laundry_room = LaundryRoom.objects.get(hall_id=0, name="Bishop White", location="Quad") + self.laundry_room = LaundryRoom.objects.get(room_id=14089) self.client = APIClient() def test_response(self): - response = self.client.get(reverse("multiple-hall-info", args=["0,1,2,3"])) + response = self.client.get(reverse("multiple-hall-info", args=["0,1,2"])) res_json = json.loads(response.content) if response.status_code == 200: rooms = res_json["rooms"] for room in rooms: - self.assertTrue(LaundryRoom.objects.filter(hall_id=room["id"])) + self.assertTrue(LaundryRoom.objects.filter(room_id=room["id"])) self.assertIn("machines", room) self.assertIn("washers", room["machines"]) self.assertIn("dryers", room["machines"]) @@ -117,29 +152,41 @@ def test_hall_error(self): self.assertEqual(404, response.status_code) -@mock.patch("requests.get", mock_laundry_get) +@mock.patch("laundry.api_wrapper.get_validated", mock_laundry_get) class HallUsageViewTestCase(TestCase): def setUp(self): LaundryRoom.objects.get_or_create( - hall_id=0, name="Bishop White", location="Quad", total_washers=9, total_dryers=9 + room_id=14089, + name="English House", + location="English House", + location_id=14146, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=1, name="Chestnut Butcher", location="Quad", total_washers=11, total_dryers=11 + room_id=14099, + name="Harnwell 10th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=2, name="Class of 1928 Fisher", location="Quad", total_washers=8, total_dryers=8 + room_id=14100, + name="Harnwell 12th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) - LaundryRoom.objects.get_or_create( - hall_id=3, name="Craig", location="Quad", total_washers=3, total_dryers=3 - ) - self.laundry_room = LaundryRoom.objects.get(hall_id=0, name="Bishop White", location="Quad") + self.laundry_room = LaundryRoom.objects.get(room_id=14089) self.snapshot = LaundrySnapshot.objects.create( - room=self.laundry_room, available_washers=5, available_dryers=10 + room=self.laundry_room, available_washers=3, available_dryers=3 ) self.client = APIClient() def test_response(self): - response = self.client.get(reverse("hall-usage", args=[self.laundry_room.hall_id])) + response = self.client.get(reverse("hall-usage", args=[self.laundry_room.room_id])) res_json = json.loads(response.content) time = timezone.localtime() @@ -149,27 +196,37 @@ def test_response(self): self.assertEqual(self.snapshot.available_dryers, res_json["dryer_data"][str(hour)]) -@mock.patch("requests.get", mock_laundry_get) +@mock.patch("laundry.api_wrapper.get_validated", mock_laundry_get) class PreferencesTestCase(TestCase): def setUp(self): LaundryRoom.objects.get_or_create( - hall_id=0, name="Bishop White", location="Quad", total_washers=9, total_dryers=9 + room_id=14089, + name="English House", + location="English House", + location_id=14146, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=1, name="Chestnut Butcher", location="Quad", total_washers=11, total_dryers=11 + room_id=14099, + name="Harnwell 10th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) LaundryRoom.objects.get_or_create( - hall_id=2, name="Class of 1928 Fisher", location="Quad", total_washers=8, total_dryers=8 - ) - LaundryRoom.objects.get_or_create( - hall_id=3, name="Craig", location="Quad", total_washers=3, total_dryers=3 + room_id=14100, + name="Harnwell 12th Floor", + location="Harnwell College House", + location_id=14150, + total_washers=3, + total_dryers=3, ) self.client = APIClient() self.test_user = User.objects.create_user("user", "user@a.com", "user") - self.laundry_room = LaundryRoom.objects.get(hall_id=0, name="Bishop White", location="Quad") - self.other_laundry_room = LaundryRoom.objects.get( - hall_id=1, name="Chestnut Butcher", location="Quad" - ) + self.laundry_room = LaundryRoom.objects.get(room_id=14089) + self.other_laundry_room = LaundryRoom.objects.get(room_id=14099) self.test_user.profile.laundry_preferences.add(self.laundry_room) def test_get(self): @@ -177,14 +234,14 @@ def test_get(self): response = self.client.get(reverse("preferences")) res_json = json.loads(response.content) - self.assertIn(self.laundry_room.hall_id, res_json["rooms"]) + self.assertIn(self.laundry_room.room_id, res_json["rooms"]) def test_post(self): self.client.force_authenticate(user=self.test_user) - self.client.post(reverse("preferences"), {"rooms": [self.other_laundry_room.hall_id]}) + payload = json.dumps({"rooms": [self.other_laundry_room.room_id]}) + self.client.post(reverse("preferences"), payload, content_type="application/json") response = self.client.get(reverse("preferences")) res_json = json.loads(response.content) - self.assertIn(self.other_laundry_room.hall_id, res_json["rooms"]) -""" + self.assertIn(self.other_laundry_room.room_id, res_json["rooms"])