class TestAmity(unittest.TestCase):

    def setUp(self):
        self.amity = Amity()
        self.amity.prepopulate()
        self.parser = self.amity.create_parser()
        args = self.parser.parse_args(["input.txt"])
        self.amity.inputfile = args.inputfile
        self.amity.inputfile_reader()
        self.amity.allocate()

    def tearDown(self):
        del self.amity.rooms['office']
        del self.amity.rooms['living']
        self.amity.people = []

    def test_script_with_empty_args(self):
        with self.assertRaises(SystemExit):
            self.parser.parse_args([])

    def test_initial_rooms_in_amity(self):
        self.assertEqual(len(self.amity.rooms[
                         'living']), 10, msg="Amity should be preloaded with 10 living spaces")
        self.assertEqual(len(
            self.amity.rooms['office']), 10, msg="Amity should be preloaded with 10 offices")

    def test_number_of_persons_from_input(self):
        self.assertEquals(len(self.amity.people), 64)

    def test_members_of_room(self):
        self.assertLessEqual(
            len(self.amity.rooms['office'][0].get_members()), 6)
        self.assertLessEqual(
            len(self.amity.rooms['living'][0].get_members()), 4)

    def test_current_size_of_room(self):
        self.assertLessEqual(self.amity.rooms['office'][0].current_size(), 6)
        self.assertLessEqual(self.amity.rooms['living'][9].current_size(), 4)

    def test_unallocated(self):
        self.assertGreaterEqual(len(self.amity.unallocated['living']), 0)
        self.assertGreaterEqual(len(self.amity.unallocated['office']), 0)

    def test_total_number_of_romms_in_Amity(self):
        self.assertEquals(len(self.amity.get_rooms()), 20)

    @patch('sys.stdout', new_callable=StringIO)
    def test_print_result(self, mock_out):
        result_str = "Result:"
        self.amity.print_result()
        self.assertIn(result_str, mock_out.getvalue())

    @patch('sys.stdout', new_callable=StringIO)
    def test_print_unallocated(self, mock_out):
        result_str = "After Allocation, the following could not be allocated"
        self.amity.print_unallocated()
        self.assertIn(result_str, mock_out.getvalue())
            amity.print_unallocated()
        elif option == 2:
            user_input = raw_input("please enter room name: ")
            room_members = amity.return_room_members(user_input)
            room_members.pop(1)
            if room_members:
                for i in room_members:
                    print i,
                print
            else:
                print("There are no members in the room")


# get rooms from the text file
try:
    list_of_rooms = amity_system.get_rooms()
    for x in list_of_rooms:
        if x.upper() == "LIVING":
            living_rooms = list_of_rooms[x]
        elif x.upper() == "OFFICE":
            office_rooms = list_of_rooms[x]
    # check if their are rooms
    if living_rooms and office_rooms:
        switch = True
except IOError as e:
    print "I/O error({0}): {1}".format(e.errno, e.strerror)
except ValueError:
    print "Sorry there are not rooms in the file"
except:
    print "Unexpected error:", sys.exc_info()[0]