def test_delete_room(self): """ Tests that rooms can be deleted """ expected = self.db.session.query(Room).count() - 1 # check if deletion successful RoomService.delete_room(self.test_room.id) self.assertEqual(self.db.session.query(Room).count(), expected)
def test_invalid_add_resident_to_room(self): """ Tests that non-resident users cannot be added to a room """ test_user_info = ('*****@*****.**', 'Bob', 'Ross', 'ADMIN') test_user = UserService.create_user(*test_user_info) with self.assertRaises(ValueError): RoomService.add_resident_to_room(test_user.email, self.test_room.number) resident = ResidentService.get_resident_by_id(test_user.id) self.assertIsNone(resident) self.assertFalse(resident in self.test_room.occupants)
def test_add_resident_to_room(self): """ Tests that residents can be added to a room """ test_user_info = ('*****@*****.**', 'Bob', 'Ross', 'RESIDENT') test_user = UserService.create_user(*test_user_info) resident = ResidentService.get_resident_by_id(test_user.id) old_room = RoomService.get_room_by_number('') self.assertTrue(resident in old_room.occupants) RoomService.add_resident_to_room(test_user.email, self.test_room.number) self.assertEqual(resident.room_number, self.test_room.number) self.assertTrue(resident in self.test_room.occupants) self.assertFalse(resident in old_room.occupants)
def edit_resident(user_id, email, first_name, last_name, room_number): """ Edits an existing resident identified by user_id. Args: user_id: Unique user id. email: New email. first_name: New first name. last_name: New last name. room_number: New room number. """ from ovs.services.user_service import UserService from ovs.services.room_service import RoomService UserService.edit_user(user_id, email, first_name, last_name) RoomService.add_resident_to_room(email, room_number)
def test_edit_room(self): """ Tests that rooms can be edited""" self.assertTrue( RoomService.edit_room(self.test_room.id, '6', 'Bad', 'Double')) self.assertEqual(self.test_room.number, '6') self.assertEqual(self.test_room.status, 'Bad') self.assertEqual(self.test_room.type, 'Double')
def test_create_resident(self): """ Tests that residents can be created """ resident = Resident.query.filter( Resident.user_id == self.test_user.id).first() self.assertIsNotNone(resident) old_room = RoomService.get_room_by_number('') self.assertTrue(resident in old_room.occupants)
def create_test_resident(self): """ Creates a test resident for use in testing """ self.test_resident_info = ('*****@*****.**', 'Bob', 'Ross', 'RESIDENT') self.test_admin_info = ('*****@*****.**', 'Bob', 'Ross', 'ADMIN') self.test_user = UserService.create_user(*self.test_resident_info) self.test_admin = UserService.create_user(*self.test_admin_info) self.test_resident = ResidentService.get_resident_by_id( self.test_user.id) self.test_room = RoomService.create_room('1', 'Good', 'Single')
def validate_room_number(form, field): # pylint: disable=no-self-argument """ Checks whether room_number is unique. Args: form: The ManageRoomForm that was submitted. field: The room_number field. Raises: ValidationError: If the room_number already exists. """ room = RoomService.get_room_by_id(form.room_id.data) if room: number = room.number if (field.data != number) and RoomService.get_room_by_number( field.data) is not None: raise ValidationError('A room with that number already exists') else: raise ValidationError('Room does not exist')
def validate_room_number(form, field): # pylint: disable=no-self-argument, no-self-use """ Validates that room number does not already exist Args: form: The RegisterRoomForm that was submitted. field: The room_number field. Raises: ValidationError: If room number already exists. """ if RoomService.get_room_by_number(field.data) is not None: raise ValidationError('A room with that number already exists')
def validate_room_number(form, field): # pylint: disable=no-self-argument, no-self-use """ Checks whether room number exists. Args: form: The ManageResidentsForm that was submitted. field: The room_number field. Raises: ValidationError: If room_number does not exist. """ if field.data != '' and RoomService.get_room_by_number( field.data) is None: raise ValidationError("Room doesn't exist")
def test_edit_room_invalid(self): """ Tests that rooms can be edited""" self.assertRaises(AttributeError, RoomService.edit_room, self.test_room.id + 3, '6', 'Bad', 'Double') self.assertIsNone(RoomService.get_room_by_number('6'))
def create_test_room(self): """ Creates a room for use in testing """ self.test_room_info = ('5', 'Good', 'Single') self.test_room = RoomService.create_room(*self.test_room_info)
def test_invalid_get_room_by_id(self): """ Tests that get_room_by_id returns None for an invalid room id """ room = RoomService.get_room_by_id(5) self.assertIsNone(room)
def test_get_room_by_number(self): """ Tests that get_room_by_number returns the correct room """ room = RoomService.get_room_by_number(self.test_room.number) self.assertEqual((room.number, room.status, room.type), self.test_room_info)
def test_get_all_rooms_multiple(self): """ Tests getting all rooms with 2 entries""" RoomService.create_room('6', 'Bad', 'Double') self.assertEqual(len(RoomService.get_all_rooms()), 2)
def test_get_all_rooms(self): """ Tests getting all rooms with 1 entry """ self.assertEqual(len(RoomService.get_all_rooms()), 1)
def test_invalid_get_room_by_number(self): """ Tests that get_room_by_number returns None for an invalid room number """ room = RoomService.get_room_by_number('429') self.assertIsNone(room)