import datetime

from nose.tools import ok_
from penn import StudySpaces


class TestStudySpaces():

    def setUp(self):
        from .credentials import LIBCAL_ID, LIBCAL_SECRET
        self.studyspaces = StudySpaces(LIBCAL_ID, LIBCAL_SECRET)

    def test_buildings(self):
        buildings = self.studyspaces.get_buildings()
        ok_(len(buildings) > 0)
        for location in buildings:
            ok_("lid" in location)
            ok_("name" in location)

    def test_rooms(self):
        buildings = self.studyspaces.get_buildings()
        rooms = self.studyspaces.get_rooms(buildings[0]["lid"])
        ok_("id" in rooms)
        ok_(len(rooms["categories"]) > 0)

    def test_booking(self):
        buildings = self.studyspaces.get_buildings()
        rooms = self.studyspaces.get_rooms(buildings[0]["lid"])
        room = rooms["categories"][0]["rooms"][0]
        item = room["id"]
        result = self.studyspaces.book_room(
            item=item,
            start=room["availability"][0]["from"],
            end=room["availability"][0]["to"],
            fname="First Name",
            lname="Last Name",
            email="test@pennlabs.org",
            nickname="Test Booking",
            custom={
                "q2533": "000-000-0000",
                "q2555": "2-3"
            },
            test=True
        )
        ok_("success" in result)
<<<<<<< 04c96f4e9a4364e247c180341105ff3c735ed175


class TestStudySpaces():

    def setUp(self):
        self.studyspaces = StudySpaces()

    def test_buildings(self):
        buildings = self.studyspaces.get_buildings()
        ok_(len(buildings) > 0)
        for building in buildings:
            ok_(building["lid"] > 0)
            ok_(building["name"])
            ok_(building["service"])

    def test_room_name_mapping(self):
        mapping = self.studyspaces.get_room_id_name_mapping(2683)
        ok_(len(mapping) > 0)

    def test_rooms(self):
        """ Make sure that at least 3 buildings have at least one room. """

        now = datetime.datetime.now()
        buildings = self.studyspaces.get_buildings()
        for building in buildings[:3]:
            rooms = self.studyspaces.get_rooms(building["lid"], now, now + datetime.timedelta(days=3))
            ok_(len(rooms) > 0, "The building {} does not have any rooms!".format(building))
            for room in rooms:
                ok_(room["room_id"] > 0)
                ok_(len(room["times"]) > 0)

    def test_booking(self):
        """ Test the checks before booking the room, but don't actually book a room. """

        buildings = self.studyspaces.get_buildings()
        # get the first building
        building_id = buildings[0]["lid"]

        now = datetime.datetime.now()
        rooms = self.studyspaces.get_rooms(building_id, now, now + datetime.timedelta(days=1))

        if not rooms:
            return

        # get the first room
        room_id = rooms[0]["room_id"]
        room_time = rooms[0]["times"][0]

        result = self.studyspaces.book_room(
            building_id,
            room_id,
            datetime.datetime.strptime(room_time["start"][:-6], "%Y-%m-%dT%H:%M:%S"),
            datetime.datetime.strptime(room_time["end"][:-6], "%Y-%m-%dT%H:%M:%S"),
            "John",
            "Doe",
            "test@example.com",
            "Test Meeting",
            "000-000-0000",
            "2-3",
            fake=True
        )
        ok_(result)
=======
>>>>>>> replace old studyspaces

@@ -1377,2115 +1377,4 @@
 lt)%0A
-%3C%3C%3C%3C%3C%3C%3C 04c96f4e9a4364e247c180341105ff3c735ed175%0A%0A%0Aclass TestStudySpaces():%0A%0A    def setUp(self):%0A        self.studyspaces = StudySpaces()%0A%0A    def test_buildings(self):%0A        buildings = self.studyspaces.get_buildings()%0A        ok_(len(buildings) %3E 0)%0A        for building in buildings:%0A            ok_(building%5B%22lid%22%5D %3E 0)%0A            ok_(building%5B%22name%22%5D)%0A            ok_(building%5B%22service%22%5D)%0A%0A    def test_room_name_mapping(self):%0A        mapping = self.studyspaces.get_room_id_name_mapping(2683)%0A        ok_(len(mapping) %3E 0)%0A%0A    def test_rooms(self):%0A        %22%22%22 Make sure that at least 3 buildings have at least one room. %22%22%22%0A%0A        now = datetime.datetime.now()%0A        buildings = self.studyspaces.get_buildings()%0A        for building in buildings%5B:3%5D:%0A            rooms = self.studyspaces.get_rooms(building%5B%22lid%22%5D, now, now + datetime.timedelta(days=3))%0A            ok_(len(rooms) %3E 0, %22The building %7B%7D does not have any rooms!%22.format(building))%0A            for room in rooms:%0A                ok_(room%5B%22room_id%22%5D %3E 0)%0A                ok_(len(room%5B%22times%22%5D) %3E 0)%0A%0A    def test_booking(self):%0A        %22%22%22 Test the checks before booking the room, but don't actually book a room. %22%22%22%0A%0A        buildings = self.studyspaces.get_buildings()%0A        # get the first building%0A        building_id = buildings%5B0%5D%5B%22lid%22%5D%0A%0A        now = datetime.datetime.now()%0A        rooms = self.studyspaces.get_rooms(building_id, now, now + datetime.timedelta(days=1))%0A%0A        if not rooms:%0A            return%0A%0A        # get the first room%0A        room_id = rooms%5B0%5D%5B%22room_id%22%5D%0A        room_time = rooms%5B0%5D%5B%22times%22%5D%5B0%5D%0A%0A        result = self.studyspaces.book_room(%0A            building_id,%0A            room_id,%0A            datetime.datetime.strptime(room_time%5B%22start%22%5D%5B:-6%5D, %22%25Y-%25m-%25dT%25H:%25M:%25S%22),%0A            datetime.datetime.strptime(room_time%5B%22end%22%5D%5B:-6%5D, %22%25Y-%25m-%25dT%25H:%25M:%25S%22),%0A            %22John%22,%0A            %22Doe%22,%0A            %22test@example.com%22,%0A            %22Test Meeting%22,%0A            %22000-000-0000%22,%0A            %222-3%22,%0A            fake=True%0A        )%0A        ok_(result)%0A=======%0A%3E%3E%3E%3E%3E%3E%3E replace old studyspaces%0A

remove old tests again