def test_nonstring_nondate(self): """ Tests the parse_date function for non-string/non-datetime inputs. """ self.assertEqual(dt.parse_date(100), None) self.assertEqual(dt.parse_date(100.0), None)
def get_date(self, doc): """Get the |datetime| associated with a document. Attempts to return the date defined by the Distillery's |Taste|. If none exists, attempts to return the date when the Distillery saved the data. Parameters ---------- doc : dict A document in the Distillery's |Collection|. Returns ------- |datetime| or |None| The date associated with the document, if one exists. Otherwise, returns |None|. Warning ------- If the Distillery is only used to retrieve documents saved by other means (e.g., Logstash), the document will not contain the '_saved_date' field that Cyphon adds to saved documents. In this case, `get_date()` will return |None| if no date is defined by the Distillery's |Taste|. """ taste_date = self._get_taste_date(doc) if taste_date: return taste_date else: date = doc.get(self._DATE_KEY) return parse_date(date)
def test_timestamp(self): """ Tests the parse_date function for a timestamp string. """ actual = dt.parse_date('1444316990') self.assertTrue(isinstance(actual, datetime.datetime)) self.assertEqual(str(actual), '2015-10-08 15:09:50+00:00')
def test_no_tz(self): """ Tests the parse_date function for a timezone naive date. """ date = 'Tue, 8 Sep 2015 16:08:59' actual = dt.parse_date(date) self.assertTrue(isinstance(actual, datetime.datetime)) self.assertEqual(str(actual), '2015-09-08 16:08:59+00:00')
def test_email_date(self): """ Tests the parse_date function for a date from an email. """ date = 'Tue, 8 Sep 2015 16:08:59 -0400' actual = dt.parse_date(date) self.assertTrue(isinstance(actual, datetime.datetime)) self.assertEqual(str(actual), '2015-09-08 16:08:59-04:00')
def test_twitter_date(self): """ Tests the parse_date function for date from a tweet. """ date_str = 'Wed Sep 21 23:55:07 +0000 2016' actual = dt.parse_date(date_str) self.assertEqual(actual.year, 2016) self.assertEqual(actual.month, 9) self.assertEqual(actual.day, 21)
def test_datetime(self): """ Tests the parse_date function with a datetime object. """ date = datetime.datetime.now() self.assertTrue(dt.parse_date(date), date)
def test_bad_string(self): """ Tests the parse_date function for a non-date string. """ self.assertEqual(dt.parse_date('foobar'), None)