Ejemplo n.º 1
0
    def test_set_item_like_dict(self):
        dd = DateData()
        assert dd.date_obj is None

        date = datetime(year=5432, month=3, day=1)
        dd['date_obj'] = date
        assert dd.date_obj == date
Ejemplo n.º 2
0
class TestDateLocaleParser(BaseTestCase):
    def setUp(self):
        super().setUp()

    @parameterized.expand([
        param(date_data=DateData(
            date_obj=datetime(1999, 10, 1, 0, 0))),  # missing period
        param(date_data=DateData(period='day')),  # missing date_obj
        param(date_data={
            'date': datetime(2007, 1, 22, 0, 0),
            'period': 'day'
        }),  # wrong format (dict)
        param(date_data=DateData(date_obj=datetime(2020, 10, 29, 0, 0),
                                 period='hour')),  # wrong period
        param(date_data=[datetime(2007, 1, 22, 0, 0), 'day'
                         ]),  # wrong format (list)
        param(date_data=DateData(date_obj=None,
                                 period='day')),  # date_obj is None
        param(date_data=DateData(date_obj='29/05/1994',
                                 period='day')),  # wrong date_obj format
    ])
    def test_is_valid_date_data(self, date_data):
        self.given_parser(language=['en'],
                          date_string='10 jan 2000',
                          date_formats=None,
                          settings=settings)
        self.when_date_object_is_validated(date_data)
        self.then_date_object_is_invalid()

    def given_parser(self, language, date_string, date_formats, settings):
        self.parser = date._DateLocaleParser(language, date_string,
                                             date_formats, settings)

    def when_date_object_is_validated(self, date_data):
        self.is_valid_date_obj = self.parser._is_valid_date_data(date_data)

    def then_date_object_is_invalid(self):
        self.assertFalse(self.is_valid_date_obj)
Ejemplo n.º 3
0
    def get_date_data(self, date_string, settings=None):
        from dateparser.date import DateData

        date, period = self.parse(date_string, settings)
        return DateData(date_obj=date, period=period)
Ejemplo n.º 4
0
 def test_get_item_like_dict(self):
     date = datetime(year=5432, month=3, day=1)
     dd = DateData(date_obj=date, period='day', locale='de')
     assert dd['date_obj'] == date
     assert dd['period'] == 'day'
     assert dd['locale'] == 'de'
Ejemplo n.º 5
0
 def test_repr(self, date, period, locale, expected):
     dd = DateData(date_obj=date, period=period, locale=locale)
     assert dd.__repr__() == expected
Ejemplo n.º 6
0
 def test_set_item_like_dict_keyerror(self):
     dd = DateData()
     with pytest.raises(KeyError) as e:
         dd['date'] = datetime(year=5432, month=3, day=1)
         assert e == 'date'
Ejemplo n.º 7
0
 def test_get_item_like_dict_keyerror(self):
     dd = DateData(date_obj=None, period='day', locale='de')
     with pytest.raises(KeyError) as e:
         date_obj = dd['date']
         assert e == 'date'
         assert not date_obj
Ejemplo n.º 8
0
 def get_date(self):
     try:
         date_obj, period = self.parser.parse(self.source, settings)
         return DateData(date_obj=date_obj, period=period)
     except ValueError:
         pass