def test_remove_room(self): room_controller = RoomController() room_controller.create_room("New Room Controller", "Description") room_by_name = room_controller.get_room_by_name("New Room Controller") self.assertTrue(room_by_name) room_controller.delete_room_by_id(room_by_name.id) with self.assertRaises(RoomNotFoundError): room_controller.get_room_by_id(1)
def put(self, _id): description = request.json.get("description") try: RoomController().alter_description_by_id(_id, description) except RoomNotFoundError: return "", 204 return "", 200
def get(self, name): try: room = RoomController().get_room_by_name(name) except RoomNotFoundError: return "", 204 return { "name": room.name, "id": room.id, "description": room.description }, 200
def test_alter_room(self): room_controller = RoomController() room_controller.create_room("New Room Controller", "Description") room_by_name = room_controller.get_room_by_name("New Room Controller") self.assertEqual(room_by_name.description, "Description") room_controller.alter_description_by_id(room_by_name.id, "New Description") room_by_name = room_controller.get_room_by_name("New Room Controller") self.assertEqual(room_by_name.description, "New Description")
def test_post(self): response = self.client.post("/room/", json={ "name": "Sala", "description": "New" }) room = RoomController().get_room_by_id(1) self.assertEqual(room.name, "Sala") self.assertEqual(room.description, "New") self.assertEqual(response.status_code, 200)
def test_common(self): room_controller = RoomController() room_controller.create_room("New Room Controller", "Description") room_by_name = room_controller.get_room_by_name("New Room Controller") self.assertEqual(room_by_name.name, "New Room Controller") room_by_id = room_controller.get_room_by_id(1) self.assertEqual(room_by_id.id, 1)
def __init__(self): self.room_controller = RoomController() self.user_controller = UserController()
class ScheduleController: def __init__(self): self.room_controller = RoomController() self.user_controller = UserController() def create_schedule(self, date, user_name, room_name, description): current_app.logger.info("Trying to create a new schedule to date: {}.".format(str(date))) try: date = datetime.strptime(date, "%Y-%m-%d") except ValueError as error: current_app.logger.warning( "Problem with date parser when create a new schedule. The date is: {} ".format(str(date)) ) raise error user = self.user_controller.get_user_by_name(user_name) if not user: current_app.logger.warning("User {} not found when trying to create a new schedule.".format(user_name)) raise UserNotFoundError() room = self.room_controller.get_room_by_name(room_name) if not room: current_app.logger.warning("Room {} not found when trying to create a new schedule.".format(room_name)) raise RoomNotFoundError() schedule = Schedule(date=date, user=user, room=room, description=description) db.session.add(schedule) db.session.commit() current_app.logger.info("Schedule create.") def get_schedule_by_id(self, _id): schedule = Schedule.query.filter(Schedule.id == _id).first() if schedule: return schedule raise ScheduleNotFoundError() def remove_schedule_by_id(self, _id): current_app.logger.info("Removing schedule with id: {}.".format(_id)) schedule = Schedule.query.filter(Schedule.id == _id).first() if schedule: db.session.delete(schedule) db.session.commit() return raise ScheduleNotFoundError() def get_schedule_by_date(self, date): current_app.logger.info("Geting all schedules with date: {}.".format(str(date))) try: date = datetime.strptime(date, "%Y-%m-%d") except ValueError as error: raise error return Schedule.query.filter(Schedule.date == date.date()).all() def get_schedule_by_room_name(self, room_name): current_app.logger.info("Geting all schedules for room: {}.".format(room_name)) room = self.room_controller.get_room_by_name(room_name) return room.schedules def alter_description_by_id(self, _id, description): current_app.logger.info("Trying to alter description of schedule with id: {}.".format(_id)) schedule = Schedule.query.filter(Schedule.id == _id).first() if schedule: schedule.description = description db.session.commit() return raise ScheduleNotFoundError()
def post(self): name = request.json.get("name") description = request.json.get("description") RoomController().create_room(name, description) return "", 200
def delete(self, _id): try: RoomController().delete_room_by_id(_id) except RoomNotFoundError: return "", 204 return "", 200
def test_alter_room_that_not_exist(self): room_controller = RoomController() with self.assertRaises(RoomNotFoundError): room_controller.alter_description_by_id(1, "test")
def test_remove_room_that_not_exist(self): room_controller = RoomController() with self.assertRaises(RoomNotFoundError): room_controller.delete_room_by_id(1)
def test_get_room_by_name_that_not_exist(self): room_controller = RoomController() with self.assertRaises(RoomNotFoundError): room_controller.get_room_by_name("Bla")