def test_end_time(self):
     r = Reservation()
     r.set_end_time(2016, 2, 29, 12)
     self.assertEqual(r.get_end_time(), datetime.datetime(2016, 2, 29, 12))
     with self.assertRaises(ValueError):
         r.set_end_time(2015, 2, 29, 12)
     with self.assertRaises(ValueError):
         r.set_end_time(2100, 2, 29, 12)
    def test_add_save_conflict(self):
        rda = ReservationDataAccess("test.ini")
        rda.load()
        r = Reservation()
        r.set_id("id3")
        r.set_reserver_name("reserver4")
        r.set_reserved_room_id("room3")
        r.set_start_time(2015, 6, 11, 12)
        r.set_end_time(2015, 6, 12, 16)
        self.assertFalse(rda.add_reservation(r))
        rda.save()

        with open("test.ini", "rt") as f:
            self.assertEqual(f.readline(), "[id1]\n")
            self.assertEqual(f.readline(), "start_time = 06/10/15 12:00:00\n")
            self.assertEqual(f.readline(), "end_time = 06/10/15 16:00:00\n")
            self.assertEqual(f.readline(), "reserver = reserver1\n")
            self.assertEqual(f.readline(), "room_id = room1\n")

            self.assertEqual(f.readline(), "\n")

            self.assertEqual(f.readline(), "[id2]\n")
            self.assertEqual(f.readline(), "start_time = 06/10/15 12:00:00\n")
            self.assertEqual(f.readline(), "end_time = 06/10/15 16:00:00\n")
            self.assertEqual(f.readline(), "reserver = reserver2\n")
            self.assertEqual(f.readline(), "room_id = room2\n")

            self.assertEqual(f.readline(), "\n")
            
            self.assertEqual(f.readline(), "[id3]\n")
            self.assertEqual(f.readline(), "start_time = 06/11/15 12:00:00\n")
            self.assertEqual(f.readline(), "end_time = 06/12/15 16:00:00\n")
            self.assertEqual(f.readline(), "reserver = reserver3\n")
            self.assertEqual(f.readline(), "room_id = room3\n")

            self.assertEqual(f.readline(), "\n")
            self.assertEqual(f.readline(), "")
    def test_update_with_conflict_then_list(self):
        rda = ReservationDataAccess("test.ini")
        rda.load()
        r = Reservation()
        r.set_id("id1")
        r.set_start_time(2015, 6, 11, 1)
        r.set_end_time(2015, 6, 12, 3)
        r.set_reserved_room_id("room3")
        r.set_reserver_name("reserver1")
        self.assertFalse(rda.update_reservation(r))
        r.set_id("id4")
        r.set_start_time(2015, 6, 11, 1)
        r.set_end_time(2015, 6, 12, 3)
        r.set_reserved_room_id("room0")
        r.set_reserver_name("reserver4")
        self.assertFalse(rda.update_reservation(r))
        r.set_id("id1")
        r.set_start_time(2015, 6, 11, 1)
        r.set_end_time(2015, 6, 12, 3)
        r.set_reserved_room_id("room0")
        r.set_reserver_name("reserver3")
        self.assertFalse(rda.update_reservation(r))
        rlist = rda.list_all_reservations()
        
        self.assertEqual(len(rlist), 3)

        rsv = rlist[0]
        self.assertEqual(rsv.get_id(), "id1")
        self.assertEqual(rsv.get_reserver_name(), "reserver1")
        self.assertEqual(rsv.get_reserved_room_id(), "room1")
        self.assertEqual(rsv.get_start_time(), datetime.datetime(2015, 6, 10, 12))
        self.assertEqual(rsv.get_end_time(), datetime.datetime(2015, 6, 10, 16))        

        rsv = rlist[1]
        self.assertEqual(rsv.get_id(), "id2")
        self.assertEqual(rsv.get_reserver_name(), "reserver2")
        self.assertEqual(rsv.get_reserved_room_id(), "room2")
        self.assertEqual(rsv.get_start_time(), datetime.datetime(2015, 6, 10, 12))
        self.assertEqual(rsv.get_end_time(), datetime.datetime(2015, 6, 10, 16))

        rsv = rlist[2]
        self.assertEqual(rsv.get_id(), "id3")
        self.assertEqual(rsv.get_reserver_name(), "reserver3")
        self.assertEqual(rsv.get_reserved_room_id(), "room3")
        self.assertEqual(rsv.get_start_time(), datetime.datetime(2015, 6, 11, 12))
        self.assertEqual(rsv.get_end_time(), datetime.datetime(2015, 6, 12, 16))
    def test_update_then_save(self):
        rda = ReservationDataAccess("test.ini")
        rda.load()
        r = Reservation()
        r.set_id("id1")
        r.set_start_time(2015, 1, 1, 1)
        r.set_end_time(2015, 1, 1, 3)
        r.set_reserved_room_id("room0")
        r.set_reserver_name("reserver0")
        self.assertTrue(rda.update_reservation(r))
        rda.save()

        with open("test.ini", "rt") as f:
            self.assertEqual(f.readline(), "[id1]\n")
            self.assertEqual(f.readline(), "start_time = 01/01/15 01:00:00\n")
            self.assertEqual(f.readline(), "end_time = 01/01/15 03:00:00\n")
            self.assertEqual(f.readline(), "reserver = reserver0\n")
            self.assertEqual(f.readline(), "room_id = room0\n")

            self.assertEqual(f.readline(), "\n")

            self.assertEqual(f.readline(), "[id2]\n")
            self.assertEqual(f.readline(), "start_time = 06/10/15 12:00:00\n")
            self.assertEqual(f.readline(), "end_time = 06/10/15 16:00:00\n")
            self.assertEqual(f.readline(), "reserver = reserver2\n")
            self.assertEqual(f.readline(), "room_id = room2\n")

            self.assertEqual(f.readline(), "\n")

            self.assertEqual(f.readline(), "[id3]\n")
            self.assertEqual(f.readline(), "start_time = 06/11/15 12:00:00\n")
            self.assertEqual(f.readline(), "end_time = 06/12/15 16:00:00\n")
            self.assertEqual(f.readline(), "reserver = reserver3\n")
            self.assertEqual(f.readline(), "room_id = room3\n")

            self.assertEqual(f.readline(), "\n")
            self.assertEqual(f.readline(), "")
    def test_add_then_list(self):
        rda = ReservationDataAccess("test.ini")
        rda.load()
        r = Reservation()
        r.set_id("id4")
        r.set_reserver_name("reserver4")
        r.set_reserved_room_id("room3")
        r.set_start_time(2015, 6, 12, 16)
        r.set_end_time(2015, 6, 12, 18)
        self.assertTrue(rda.add_reservation(r))

        allRsv = rda.list_all_reservations()
        self.assertEqual(len(allRsv), 4)
        
        lastRsv = allRsv[3]
        self.assertFalse(lastRsv is r)

        self.assertEqual(lastRsv.get_id(), "id4")
        self.assertEqual(lastRsv.get_reserver_name(), "reserver4")
        self.assertEqual(lastRsv.get_reserved_room_id(), "room3")
        self.assertEqual(lastRsv.get_start_time_as_str(), "06/12/15 16:00:00")
        self.assertEqual(lastRsv.get_end_time_as_str(), "06/12/15 18:00:00")
    def test_conflict(self):
        r1 = Reservation()
        r2 = Reservation()
        r1.set_reserver_name("1")
        r2.set_reserver_name("2")
        r1.set_reserved_room_id("1")
        r2.set_reserved_room_id("2")

        r1.set_start_time(2000, 3, 29, 12)
        r1.set_end_time(2000, 3, 29, 15)
        r2.set_start_time(2000, 3, 29, 12)
        r2.set_end_time(2000, 3, 29, 15)
        self.assertEqual(r1.conflict_with(r2), False)
        self.assertEqual(r2.conflict_with(r1), False)

        r2.set_reserved_room_id("1")
        self.assertEqual(r1.conflict_with(r2), True)
        self.assertEqual(r2.conflict_with(r1), True)

        r2.set_reserver_name("1")
        r2.set_reserved_room_id("2")
        self.assertEqual(r1.conflict_with(r2), True)
        self.assertEqual(r2.conflict_with(r1), True)

        r1.set_reserver_name("1")
        r2.set_reserver_name("2")
        r1.set_reserved_room_id("1")
        r2.set_reserved_room_id("1")

        r2.set_start_time(2000, 3, 29, 11)
        r2.set_end_time(2000, 3, 29, 12)
        self.assertEqual(r1.conflict_with(r2), False)
        self.assertEqual(r2.conflict_with(r1), False)
        r2.set_start_time(2000, 3, 29, 11)
        r2.set_end_time(2000, 3, 29, 13)
        self.assertEqual(r1.conflict_with(r2), True)
        self.assertEqual(r2.conflict_with(r1), True)
        r2.set_start_time(2000, 3, 29, 12)
        r2.set_end_time(2000, 3, 29, 15)
        self.assertEqual(r1.conflict_with(r2), True)
        self.assertEqual(r2.conflict_with(r1), True)

        r2.set_start_time(2000, 3, 29, 14)
        r2.set_end_time(2000, 3, 29, 15)
        self.assertEqual(r1.conflict_with(r2), True)
        self.assertEqual(r2.conflict_with(r1), True)
        r2.set_start_time(2000, 3, 29, 14)
        r2.set_end_time(2000, 3, 29, 16)
        self.assertEqual(r1.conflict_with(r2), True)
        self.assertEqual(r2.conflict_with(r1), True)
        r2.set_start_time(2000, 3, 29, 15)
        r2.set_end_time(2000, 3, 29, 16)
        self.assertEqual(r1.conflict_with(r2), False)
        self.assertEqual(r2.conflict_with(r1), False)
 def test_has_expired(self):
     r = Reservation()
     r.set_start_time(2099, 3, 29, 12)
     r.set_end_time(2099, 3, 29, 14)
     self.assertEqual(r.has_expired(), False)
     r.set_start_time(2000, 3, 29, 12)
     r.set_end_time(2000, 3, 29, 14)
     self.assertEqual(r.has_expired(), True)
     r.set_start_time(2000, 3, 29, 12)
     r.set_end_time(2099, 3, 29, 14)
     self.assertEqual(r.has_expired(), False)
 def test_time_is_valid(self):
     r = Reservation()
     r.set_start_time(2016, 2, 29, 12)
     r.set_end_time(2016, 2, 29, 12)
     self.assertEqual(r.is_reservation_time_valid(), False)
     r.set_start_time(2016, 3, 29, 12)
     self.assertEqual(r.is_reservation_time_valid(), False)
     r.set_end_time(2016, 3, 29, 14)
     self.assertEqual(r.is_reservation_time_valid(), True)
 def test_room_id(self):
     r = Reservation()
     r.set_reserved_room_id("1")
     self.assertEqual(r.get_reserved_room_id(), "1")
     r.set_reserved_room_id("2")
     self.assertEqual(r.get_reserved_room_id(), "2")
 def test_reserver_name(self):
     r = Reservation()
     r.set_reserver_name("Name1")
     self.assertEqual(r.get_reserver_name(), "Name1")
     r.set_reserver_name("Name2")
     self.assertEqual(r.get_reserver_name(), "Name2")