def test_that_filter_dates_raises_missing_column_error(self):

        with self.assertRaises(errors.MissingColumnError):

            f_mem_a = filter.filter_dates(
                mem_a,
                start_col='no_such_column',
                end_col='end_date')

        with self.assertRaises(errors.MissingColumnError):

            f_mem_a = filter.filter_dates(
                mem_a,
                start_col='start_date',
                end_col='no_such_column')
    def test_filter_dates_includes_rows_enclosing_dates(self):

        f_mem_a = filter.filter_dates(
            mem_a,
            start_col='start_date',
            end_col='end_date',
            from_date='2005-06-30',
            to_date='2005-06-30')

        self.assertEqual(f_mem_a.shape[0], 2)
        self.assertEqual(f_mem_a.shape[1], mem_a.shape[1])

        self.assertEqual(f_mem_a.iloc[0]['person_id'], 'p1')
        self.assertEqual(f_mem_a.iloc[0]['membership_id'], 'a2')
        self.assertEqual(f_mem_a.iloc[0]['start_date'],
            datetime.date(2005, 1, 1))
        self.assertEqual(f_mem_a.iloc[0]['end_date'],
            datetime.date(2005, 12, 31))

        self.assertEqual(f_mem_a.iloc[1]['person_id'], 'p2')
        self.assertEqual(f_mem_a.iloc[1]['membership_id'], 'a5')
        self.assertEqual(f_mem_a.iloc[1]['start_date'],
            datetime.date(2005, 1, 1))
        self.assertEqual(f_mem_a.iloc[-1]['end_date'],
            datetime.date(2005, 12, 31))
    def test_filter_dates_includes_rows_with_partial_instersection(self):

        f_mem_a = filter.filter_dates(
            mem_a,
            start_col='start_date',
            end_col='end_date',
            from_date='2005-06-30',
            to_date='2010-06-30')

        self.assertEqual(f_mem_a.shape[0], mem_a.shape[0] - 2)
        self.assertEqual(f_mem_a.shape[1], mem_a.shape[1])

        self.assertEqual(f_mem_a.iloc[0]['person_id'], 'p1')
        self.assertEqual(f_mem_a.iloc[0]['membership_id'], 'a2')
        self.assertEqual(f_mem_a.iloc[0]['start_date'],
            datetime.date(2005, 1, 1))
        self.assertEqual(f_mem_a.iloc[0]['end_date'],
            datetime.date(2005, 12, 31))

        self.assertEqual(f_mem_a.iloc[-1]['person_id'], 'p2')
        self.assertEqual(f_mem_a.iloc[-1]['membership_id'], 'a7')
        self.assertEqual(f_mem_a.iloc[-1]['start_date'],
            datetime.date(2010, 1, 1))
        self.assertEqual(f_mem_a.iloc[-1]['end_date'],
            datetime.date(2010, 12, 31))
    def test_filter_dates_excludes_rows_outside_both_dates(self):

        f_mem_a = filter.filter_dates(
            mem_a,
            start_col='start_date',
            end_col='end_date',
            from_date='2004-12-31',
            to_date='2011-01-01')

        self.assertEqual(f_mem_a.shape[0], mem_a.shape[0] - 2)
        self.assertEqual(f_mem_a.shape[1], mem_a.shape[1])

        self.assertEqual(f_mem_a.iloc[0]['person_id'], 'p1')
        self.assertEqual(f_mem_a.iloc[0]['membership_id'], 'a2')
        self.assertEqual(f_mem_a.iloc[0]['start_date'],
            datetime.date(2005, 1, 1))
        self.assertEqual(f_mem_a.iloc[0]['end_date'],
            datetime.date(2005, 12, 31))

        self.assertEqual(f_mem_a.iloc[-1]['person_id'], 'p2')
        self.assertEqual(f_mem_a.iloc[-1]['membership_id'], 'a7')
        self.assertEqual(f_mem_a.iloc[-1]['start_date'],
            datetime.date(2010, 1, 1))
        self.assertEqual(f_mem_a.iloc[-1]['end_date'],
            datetime.date(2010, 12, 31))
    def test_filter_dates_does_not_filter_without_dates(self):

        f_mem_a = filter.filter_dates(
            mem_a,
            start_col='start_date',
            end_col='end_date')

        self.assertEqual(f_mem_a.shape, mem_a.shape)
        self.assertTrue((f_mem_a == mem_a).all().all())
    def test_that_filter_dates_raises_date_format_error(self):

        with self.assertRaises(errors.DateFormatError):

            f_mem_a = filter.filter_dates(
                mem_a,
                start_col='start_date',
                end_col='end_date',
                from_date='2010-01-XX',
                to_date='2010-12-31')

        with self.assertRaises(errors.DateFormatError):

            f_mem_a = filter.filter_dates(
                mem_a,
                start_col='start_date',
                end_col='end_date',
                from_date='2010-01-01',
                to_date='2010-12-XX')
    def test_that_filter_dates_raises_value_error(self):

        with self.assertRaises(ValueError):

            f_mem_a = filter.filter_dates(
                mem_a,
                start_col='start_date',
                end_col='end_date',
                from_date='2010-01-01',
                to_date='2009-12-31')
    def test_filter_dates_excludes_rows_after_to_date(self):

        f_mem_a = filter.filter_dates(
            mem_a,
            start_col='start_date',
            end_col='end_date',
            to_date='2011-01-01')

        self.assertEqual(f_mem_a.shape[0], mem_a.shape[0] - 1)
        self.assertEqual(f_mem_a.shape[1], mem_a.shape[1])

        self.assertEqual(f_mem_a.iloc[-1]['person_id'], 'p2')
        self.assertEqual(f_mem_a.iloc[-1]['membership_id'], 'a7')
        self.assertEqual(f_mem_a.iloc[-1]['start_date'],
            datetime.date(2010, 1, 1))
        self.assertEqual(f_mem_a.iloc[-1]['end_date'],
            datetime.date(2010, 12, 31))
    def test_filter_dates_excludes_rows_before_from_date(self):

        f_mem_a = filter.filter_dates(
            mem_a,
            start_col='start_date',
            end_col='end_date',
            from_date='2004-12-31')

        self.assertEqual(f_mem_a.shape[0], mem_a.shape[0] - 1)
        self.assertEqual(f_mem_a.shape[1], mem_a.shape[1])

        self.assertEqual(f_mem_a.iloc[0]['person_id'], 'p1')
        self.assertEqual(f_mem_a.iloc[0]['membership_id'], 'a2')
        self.assertEqual(f_mem_a.iloc[0]['start_date'],
            datetime.date(2005, 1, 1))
        self.assertEqual(f_mem_a.iloc[0]['end_date'],
            datetime.date(2005, 12, 31))