Skip to content

Commit

Permalink
Laundry testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-Jess committed Nov 15, 2024
1 parent 69ca6c5 commit d65045f
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 58 deletions.
1 change: 1 addition & 0 deletions backend/laundry/data/laundry_data.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
room_id,room_name,room_description,room_location,count_washers,count_dryers
14089,English House,English House,14146,3,3
14082,"Gregory College House: Class of 1925""",Gregory College House: Class of 1925,14138,4,4
14085,Gregory College House: Van Pelt,Gregory College House: Van Pelt,14141,6,6
Expand Down
22 changes: 10 additions & 12 deletions backend/laundry/management/commands/load_laundry_rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ class Command(BaseCommand):
def handle(self, *args, **kwargs):

with open("laundry/data/laundry_data.csv") as data:
reader = csv.reader(data)

for i, row in enumerate(reader):
room_id, name, location, location_id, total_washers, total_dryers = row

LaundryRoom.objects.create(
room_id=room_id,
name=name,
location=location,
location_id=location_id,
total_washers=total_washers,
total_dryers=total_dryers,
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,
)

Expand Down
20 changes: 8 additions & 12 deletions backend/laundry/management/commands/update_laundry_rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
from requests.exceptions import HTTPError


def write_file(laundry_rooms):
with open("laundry/data/laundry_data.csv", "w") as f:
writer = csv.DictWriter(f, laundry_rooms[0].keys())
writer.writeheader()
writer.writerows(laundry_rooms)


class Command(BaseCommand):
help = "Update laundry rooms csv from server"

Expand Down Expand Up @@ -65,15 +72,4 @@ def handle(self, *args, **kwargs):
room["count_washers"] = count_washers
room["count_dryers"] = count_dryers

# write to csv
keys = [
"room_id",
"room_name",
"room_description",
"room_location",
"count_washers",
"count_dryers",
]
with open("laundry/data/laundry_data.csv", "w") as f:
writer = csv.DictWriter(f, keys)
writer.writerows(laundry_rooms)
write_file(laundry_rooms)
2 changes: 1 addition & 1 deletion backend/laundry/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class LaundryRoom(models.Model):
room_id = models.IntegerField(default=0)
room_id = models.IntegerField(default=0, primary_key=True)
name = models.CharField(max_length=255)
location = models.CharField(max_length=255)
location_id = models.IntegerField(default=0)
Expand Down
8 changes: 4 additions & 4 deletions backend/tests/laundry/test_api_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

from laundry.api_wrapper import all_status, room_status, save_data
from laundry.models import LaundryRoom, LaundrySnapshot
from tests.laundry.test_commands import fakeLaundryGet
from tests.laundry.test_commands import mock_laundry_get


ALL_URL = f"{settings.LAUNDRY_URL}/?location="


@mock.patch("requests.get", fakeLaundryGet)
@mock.patch("requests.get", mock_laundry_get)
class TestAllStatus(TestCase):
def setUp(self):
LaundryRoom.objects.get_or_create(
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_all_status(self):
self.assertTrue(hall[machine]["offline"] >= 0)


@mock.patch("requests.get", fakeLaundryGet)
@mock.patch("requests.get", mock_laundry_get)
class TestHallStatus(TestCase):
def setUp(self):
LaundryRoom.objects.get_or_create(
Expand Down Expand Up @@ -86,7 +86,7 @@ def test_all_status(self):
self.assertIn("status", machine)


@mock.patch("requests.get", fakeLaundryGet)
@mock.patch("requests.get", mock_laundry_get)
class TestSaveData(TestCase):
def setUp(self):
LaundryRoom.objects.get_or_create(
Expand Down
91 changes: 68 additions & 23 deletions backend/tests/laundry/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from laundry.models import LaundryRoom, LaundrySnapshot


def fakeLaundryGet(url, *args, **kwargs):
def mock_laundry_get(url, *args, **kwargs):
# TODO: should we do this with regex? using split bc I think it's cleaner
split = url.split("/")
if "/".join(split[0:3]) == settings.LAUNDRY_URL:
Expand All @@ -25,21 +25,35 @@ def fakeLaundryGet(url, *args, **kwargs):
raise NotImplementedError


@mock.patch("requests.get", fakeLaundryGet)
@mock.patch("requests.get", mock_laundry_get)
class TestGetSnapshot(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
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_db_populate(self):
Expand All @@ -50,10 +64,10 @@ def test_db_populate(self):
self.assertEqual("Captured snapshots!\n", out.getvalue())

# asserts that all rooms have been snapshotted
self.assertEqual(LaundrySnapshot.objects.all().count(), 4)
self.assertEqual(LaundrySnapshot.objects.all().count(), 3)


@mock.patch("requests.get", fakeLaundryGet)
@mock.patch("requests.get", mock_laundry_get)
class TestLaundryRoomMigration(TestCase):
def test_db_populate(self):
out = StringIO()
Expand All @@ -62,25 +76,56 @@ def test_db_populate(self):
# tests the value of the output
self.assertEqual("Uploaded Laundry Rooms!\n", out.getvalue())

# asserts that the number of LaundryRooms created was 53
self.assertEqual(LaundryRoom.objects.all().count(), 53)

# compute expected number of rooms
with open("laundry/data/laundry_data.csv") as data:
reader = csv.reader(data)
# subtract 1 to account for header
num_rooms = sum(1 for _ in reader) - 1

for i, row in enumerate(reader):
hall_id, hall_name, location, uuid, total_washers, total_dryers = row
# asserts that the number of LaundryRooms created was 53
self.assertEqual(LaundryRoom.objects.all().count(), num_rooms)

room = LaundryRoom.objects.get(hall_id=hall_id)
with open("laundry/data/laundry_data.csv") as data:
reader = csv.DictReader(data)

# asserts that all fields of LaundryRoom are same
self.assertEqual(room.name, hall_name)
self.assertEqual(room.location, location)
self.assertEqual(str(room.uuid), uuid)
self.assertEqual(room.total_washers, int(total_washers))
self.assertEqual(room.total_dryers, int(total_dryers))
for row in reader:
room = LaundryRoom.objects.get(room_id=int(row["room_id"]))

# asserts that all fields of LaundryRoom are same
self.assertEqual(room.name, row["room_name"])
self.assertEqual(room.location, row["room_description"])
self.assertEqual(room.location_id, int(row["room_location"]))
self.assertEqual(room.total_washers, int(row["count_washers"]))
self.assertEqual(room.total_dryers, int(row["count_dryers"]))
call_command("load_laundry_rooms")

# asserts that LaundryRooms do not recreate itself
self.assertEqual(LaundryRoom.objects.all().count(), 53)


@mock.patch("requests.get", 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):
call_command("update_laundry_rooms")
expected = [
{
"room_id": 14089,
"room_name": "English House",
"room_description": "English House",
"room_location": 14146,
},
{
"room_id": 14099,
"room_name": "Harnwell 10th Floor",
"room_description": "Harnwell College House",
"room_location": 14150,
},
{
"room_id": 14100,
"room_name": "Harnwell 12th Floor",
"room_description": "Harnwell College House",
"room_location": 14150,
},
]
# assert that the mock was called with this
mock_laundry_write.assert_called_with(expected)
12 changes: 6 additions & 6 deletions backend/tests/laundry/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
from rest_framework.test import APIClient

from laundry.models import LaundryRoom, LaundrySnapshot
from tests.laundry.test_commands import fakeLaundryGet
from tests.laundry.test_commands import mock_laundry_get


User = get_user_model()


@mock.patch("requests.get", fakeLaundryGet)
@mock.patch("requests.get", mock_laundry_get)
class HallIdViewTestCase(TestCase):
def setUp(self):
LaundryRoom.objects.get_or_create(
Expand Down Expand Up @@ -42,7 +42,7 @@ def test_response(self):
)


@mock.patch("requests.get", fakeLaundryGet)
@mock.patch("requests.get", mock_laundry_get)
class HallInfoViewTestCase(TestCase):
def setUp(self):
LaundryRoom.objects.get_or_create(
Expand Down Expand Up @@ -74,7 +74,7 @@ def test_hall_error(self):
self.assertEqual(404, response.status_code)


@mock.patch("requests.get", fakeLaundryGet)
@mock.patch("requests.get", mock_laundry_get)
class MultipleHallInfoViewTestCase(TestCase):
def setUp(self):
LaundryRoom.objects.get_or_create(
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_hall_error(self):
self.assertEqual(404, response.status_code)


@mock.patch("requests.get", fakeLaundryGet)
@mock.patch("requests.get", mock_laundry_get)
class HallUsageViewTestCase(TestCase):
def setUp(self):
LaundryRoom.objects.get_or_create(
Expand Down Expand Up @@ -148,7 +148,7 @@ def test_response(self):
self.assertEqual(self.snapshot.available_dryers, res_json["dryer_data"][str(hour)])


@mock.patch("requests.get", fakeLaundryGet)
@mock.patch("requests.get", mock_laundry_get)
class PreferencesTestCase(TestCase):
def setUp(self):
LaundryRoom.objects.get_or_create(
Expand Down
Empty file added backend/utils/__init__.py
Empty file.

0 comments on commit d65045f

Please sign in to comment.