def test_year(self):
     tree = datetimeparse.PastCalendarPeriodSubtree("x")
     fields = {}
     self.assertEqual(tree.check_match(("2012", ), fields=fields), None)
     self.assertEqual(
         fields,
         {"<x>": [(datetime.date(2012, 1, 1), datetime.date(2013, 1, 1))]})
    def test_n_periods_ago(self):

        tree = datetimeparse.PastCalendarPeriodSubtree("x")
        # 6th June 2012 is a Wednesday
        with fake_now(2012, 6, 6):
            fields = {}
            self.assertEqual(
                tree.check_match(("2", "days", "ago"), fields=fields), None)
            self.assertEqual(fields, {
                "<x>": [(datetime.date(2012, 6, 4), datetime.date(2012, 6, 5))]
            })
            fields = {}
            self.assertEqual(
                tree.check_match(("3", "weeks", "ago"), fields=fields), None)
            self.assertEqual(fields, {
                "<x>": [(datetime.date(2012, 5, 14), datetime.date(
                    2012, 5, 21))]
            })
            fields = {}
            self.assertEqual(
                tree.check_match(("4", "months", "ago"), fields=fields), None)
            self.assertEqual(fields, {
                "<x>": [(datetime.date(2012, 2, 1), datetime.date(2012, 3, 1))]
            })
            fields = {}
            self.assertEqual(
                tree.check_match(("5", "years", "ago"), fields=fields), None)
            self.assertEqual(fields, {
                "<x>": [(datetime.date(2007, 1, 1), datetime.date(2008, 1, 1))]
            })
 def test_week_containing(self):
     tree = datetimeparse.PastCalendarPeriodSubtree("x")
     fields = {}
     self.assertEqual(
         tree.check_match(("week", "of", "6-6-2012"), fields=fields), None)
     self.assertEqual(
         fields,
         {"<x>": [(datetime.date(2012, 6, 4), datetime.date(2012, 6, 11))]})
 def test_before(self):
     tree = datetimeparse.PastCalendarPeriodSubtree("x")
     fields = {}
     self.assertEqual(
         tree.check_match(("before", "1-2-2010"), fields=fields), None)
     self.assertEqual(
         fields,
         {"<x>": [(datetime.date(1970, 1, 1), datetime.date(2010, 2, 1))]})
     # 6th June 2012 is a Wednesday
     with fake_now(2012, 6, 6):
         fields = {}
         self.assertEqual(
             tree.check_match(("before", "today"), fields=fields), None)
         self.assertEqual(fields, {
             "<x>": [(datetime.date(1970, 1, 1), datetime.date(2012, 6, 6))]
         })
 def test_month(self):
     tree = datetimeparse.PastCalendarPeriodSubtree("x")
     fields = {}
     self.assertEqual(tree.check_match(("June", "2012"), fields=fields),
                      None)
     self.assertEqual(
         fields,
         {"<x>": [(datetime.date(2012, 6, 1), datetime.date(2012, 7, 1))]})
     with fake_now(2012, 6, 6):
         fields = {}
         self.assertEqual(
             tree.check_match(("August", "last", "year"), fields=fields),
             None)
         self.assertEqual(fields, {
             "<x>": [(datetime.date(2011, 8, 1), datetime.date(2011, 9, 1))]
         })
    def test_date(self):

        tree = datetimeparse.PastCalendarPeriodSubtree("x")
        fields = {}
        self.assertEqual(tree.check_match(("15-10-2012", ), fields=fields),
                         None)
        self.assertEqual(fields, {
            "<x>": [(datetime.date(2012, 10, 15), datetime.date(2012, 10, 16))]
        })
        with fake_now(2012, 6, 6):
            fields = {}
            self.assertEqual(tree.check_match(("yesterday", ), fields=fields),
                             None)
            self.assertEqual(fields, {
                "<x>": [(datetime.date(2012, 6, 5), datetime.date(2012, 6, 6))]
            })
 def test_between(self):
     tree = datetimeparse.PastCalendarPeriodSubtree("x")
     fields = {}
     self.assertEqual(
         tree.check_match(("from", "1-2-2010", "to", "3-4-2011"),
                          fields=fields), None)
     self.assertEqual(
         fields,
         {"<x>": [(datetime.date(2010, 2, 1), datetime.date(2011, 4, 3))]})
     # 6th June 2012 is a Wednesday
     with fake_now(2012, 6, 6):
         fields = {}
         self.assertEqual(
             tree.check_match(("between", "last", "Friday", "and", "today"),
                              fields=fields), None)
         self.assertEqual(fields, {
             "<x>": [(datetime.date(2012, 6, 1), datetime.date(2012, 6, 6))]
         })
    def test_last_period(self):

        tree = datetimeparse.PastCalendarPeriodSubtree("x")
        # 6th June 2012 is a Wednesday
        with fake_now(2012, 6, 6):
            fields = {}
            self.assertEqual(tree.check_match(("last", "week"), fields=fields),
                             None)
            self.assertEqual(fields, {
                "<x>": [(datetime.date(2012, 5, 28), datetime.date(2012, 6, 4))
                        ]
            })
            fields = {}
            self.assertEqual(
                tree.check_match(("last", "month"), fields=fields), None)
            self.assertEqual(fields, {
                "<x>": [(datetime.date(2012, 5, 1), datetime.date(2012, 6, 1))]
            })
            fields = {}
            self.assertEqual(tree.check_match(("last", "year"), fields=fields),
                             None)
            self.assertEqual(fields, {
                "<x>": [(datetime.date(2011, 1, 1), datetime.date(2012, 1, 1))]
            })