def test_process_time_point_str_x(self): """DateTimeOperator.process_time_point_str(...) Basic parse and dump of a time point string. """ # 2009-02-13T23:31:30Z point_str = str(seconds2point(1234567890)) datetimeoper = idt_dtoper.DateTimeOperator() # Unix time self.assertEqual( '2019-01-11T10:40:15Z', datetimeoper.process_time_point_str( 'Fri 11 Jan 10:40:15 UTC 2019', print_format=datetimeoper.CURRENT_TIME_DUMP_FORMAT_Z)) # Basic self.assertEqual(point_str, datetimeoper.process_time_point_str(point_str)) # +ve offset point_str_1 = str(seconds2point(1234567890 + 3600)) self.assertEqual( point_str_1, datetimeoper.process_time_point_str(point_str, ['PT1H'])) # +ve offset, time point like duration point_str_1 = str(seconds2point(1234567890 + 3600)) self.assertEqual( point_str_1, datetimeoper.process_time_point_str(point_str, ['P0000-00-00T01'])) # -ve offset point_str_2 = str(seconds2point(1234567890 - 86400)) self.assertEqual( point_str_2, datetimeoper.process_time_point_str(point_str, ['-P1D'])) # offsets that cancel out self.assertEqual( point_str, datetimeoper.process_time_point_str(point_str, ['PT1H', '-PT60M'])) # Multiple offsets in 1 string point_str_3 = str(seconds2point(1234567890 - 86400 - 3600)) self.assertEqual( point_str_3, datetimeoper.process_time_point_str(point_str, ['-P1DT1H'])) # Multiple offsets self.assertEqual( point_str_3, datetimeoper.process_time_point_str(point_str, ['-P1D', '-PT1H'])) # Bad time point string self.assertRaises(ValueError, datetimeoper.process_time_point_str, 'teatime') # Bad offset string with self.assertRaises(OffsetValueError, ) as ctxmgr: datetimeoper.process_time_point_str(point_str, ['ages']) self.assertEqual('ages: bad offset value', str(ctxmgr.exception)) # Bad offset string, unsupported time point like duration with self.assertRaises(OffsetValueError, ) as ctxmgr: datetimeoper.process_time_point_str(point_str, ['P0000-W01-1']) self.assertEqual('P0000-W01-1: bad offset value', str(ctxmgr.exception))
def test_process_time_point_str_now_0(self, mock_now_func): """DateTimeOperator.process_time_point_str()""" # 2009-02-13T23:31:30Z mock_now = seconds2point(1234567890) mock_now_func.return_value = mock_now datetimeoper = idt_dtoper.DateTimeOperator() self.assertEqual(str(mock_now), datetimeoper.process_time_point_str()) self.assertEqual( str(mock_now), datetimeoper.process_time_point_str(datetimeoper.STR_NOW))
def test_process_time_point_str_ref_1(self): """DateTimeOperator.process_time_point_str('ref') With explicit reference time. """ # 2009-02-13T23:31:30Z ref_point_str = str(seconds2point(1234567890)) datetimeoper = idt_dtoper.DateTimeOperator(ref_point_str=ref_point_str) self.assertEqual( ref_point_str, datetimeoper.process_time_point_str(datetimeoper.STR_REF))
def test_process_time_point_str_ref_2(self): """DateTimeOperator.process_time_point_str('ref') With explicit reference time as ISODATETIMEREF environment variable. """ # 2009-02-13T23:31:30Z ref_point_str = str(seconds2point(1234567890)) # Set ISODATETIMEREF. # Or the test may not work. environ = os.environ.copy() environ[idt_dtoper.DateTimeOperator.ENV_REF] = (ref_point_str) with patch.dict(os.environ, environ): datetimeoper = idt_dtoper.DateTimeOperator() self.assertEqual( ref_point_str, datetimeoper.process_time_point_str(datetimeoper.STR_REF))
def test_process_time_point_str_ref_0(self, mock_now_func): """DateTimeOperator.process_time_point_str('ref') But without explicit reference time, so default to now. """ # 2009-02-13T23:31:30Z mock_now = seconds2point(1234567890) mock_now_func.return_value = mock_now datetimeoper = idt_dtoper.DateTimeOperator() # Ensure that the ISODATETIMEREF environment variable is not set # Or the test may not work. environ = os.environ.copy() if datetimeoper.ENV_REF in environ: del environ[datetimeoper.ENV_REF] with patch.dict(os.environ, environ, clear=True): self.assertEqual( str(mock_now), datetimeoper.process_time_point_str(datetimeoper.STR_REF))