class TestSearchDialogChooseDate(TestWithData):
    """
    tests the input check for the employee name: if the
    name identifies the employee, the query is returned and the search parameters
    are set. If more than one employee exist with that name, a list
    to choose from is presented.
    """
    def run(self, result=None):
        with test_database(test_db, (Employee, LogEntry)):
            super(TestSearchDialogChooseDate, self).run(result)

    def setUp(self):
        """
        the dialog is started and test data are created
        the date_list of logentries is established, in order to be compared
        with the dialogs result
        """
        self.create_test_data()
        self.dialog = SearchDialog()
        self.logentries_dates = [
            datetime.date(2015, 8, 29),
            datetime.date(2015, 9, 29),
            datetime.date(2016, 8, 29),
            datetime.date(2016, 8, 30),
            datetime.date(2016, 9, 29)
        ]

    def test_search_check_date_list(self):
        """
        the user has taken option 'l' list dates for the search by
        date: a list of dates is prepared for the user to choose from
        """
        func = self.dialog.check_date('l')
        self.assertListEqual(
            [logentry.logdate for logentry in self.dialog.choice_list.list],
            self.logentries_dates)
        self.assertEqual(self.dialog.choice_list.fmt_func, _logentry_to_date)
        self.assertEqual(self.dialog.choice_list.choice_func,
                         self.dialog.choice_date)
        self.assertTrue(self.dialog.choice)
        self.assertIsNone(func)
class TestSearchDialogInputChecks(unittest.TestCase):
    """
    tests the input checks of the search dialog
    """
    def setUp(self):
        """
        the dialog is started
        """
        self.dialog = SearchDialog()

    def test_search_check_term_no_input(self):
        """
        empty searchterm is not accepted: a message is send to the user
        """
        func = self.dialog.check_term('')
        self.assertIsNone(func)
        self.assertIsNotNone(self.dialog.msg)

    def test_search_check_term_valid_input(self):
        """
        a not empty searchterm is accepted: the searchparameters are prepared for
        actual the search
        """
        func = self.dialog.check_term('Some Term')
        self.assertEqual(func, self.dialog.query_by_term)
        self.assertEqual(self.dialog.search_parameters.searchterm, 'Some Term')
        self.assertIsNotNone(self.dialog.search_parameters.header)

    def test_search_check_time_spent_unvalid_input(self):
        """
        unvalid time input is not accepted: a message is send to the user
        """
        func = self.dialog.check_time_spent('x')
        self.assertIsNone(func)
        self.assertIsNotNone(self.dialog.msg)

    def test_search_check_time_spent_valid_exact_time(self):
        """
        a valid time input is number for the minutes:
        the searchparameters are prepared with time_to set to None
        """
        func = self.dialog.check_time_spent('30')
        self.assertEqual(func, self.dialog.query_by_time_spent)
        self.assertEqual(self.dialog.search_parameters.time_from, 30)
        self.assertIsNotNone(self.dialog.search_parameters.header)
        self.assertIsNone(self.dialog.search_parameters.time_to)

    def test_search_check_time_spent_valid_time_range(self):
        """
        the userinput is a time range, then time_from and time_to
        should be in the searchparameters
        """
        userinput = '30 - 40'
        func = self.dialog.check_time_spent('30 - 40')
        self.assertEqual(func, self.dialog.query_by_time_spent)
        self.assertEqual(self.dialog.search_parameters.time_from, 30)
        self.assertIsNotNone(self.dialog.search_parameters.header)
        self.assertEqual(self.dialog.search_parameters.time_to, 40)

    def test_search_check_date_valid_exact_time(self):
        """
        a valid time input is number for the minutes:
        the searchparameters are prepared with time_to set to None
        """
        func = self.dialog.check_date('29.8.2016')
        self.assertEqual(func, self.dialog.query_by_date)
        self.assertEqual(self.dialog.search_parameters.date_from,
                         datetime.date(2016, 8, 29))
        self.assertIsNotNone(self.dialog.search_parameters.header)
        self.assertIsNone(self.dialog.search_parameters.time_to)

    def test_search_check_date_valid_time_range(self):
        """
        the userinput is a time range, then time_from and time_to
        should be in the searchparameters
        """
        func = self.dialog.check_date('29.8.2016 - 30.8.2016')
        self.assertEqual(func, self.dialog.query_by_date)
        self.assertEqual(self.dialog.search_parameters.date_from,
                         datetime.date(2016, 8, 29))
        self.assertIsNotNone(self.dialog.search_parameters.header)
        self.assertEqual(self.dialog.search_parameters.date_to,
                         datetime.date(2016, 8, 30))

    def test_search_check_date_unvalid(self):
        """
        the userinput is a time range, then time_from and time_to
        should be in the searchparameters
        """
        func = self.dialog.check_date('x')
        self.assertIsNotNone(self.dialog.msg)