def test_not_compare(self): d0 = PointInTime("2020-12-24 18") d1 = PointInTime("2020-12-23") with self.assertRaises(ValueError) as e: d1 - d0 self.assertIn("second operand must also be daily", e.exception.args[0]) d0 = PointInTime("2020-12-24") d1 = PointInTime("2020-12-23 18") with self.assertRaises(ValueError) as e: d1 - d0 self.assertIn("second operand must also be hourly", e.exception.args[0])
def test_from_int(self): # shall not instantiate from other data types with self.assertRaises(ValueError) as e: PointInTime(20211216) self.assertEqual(e.exception.args[0], "pass date, datetime or string") self.assertEqual(e.exception.args[1], int) self.assertEqual(e.exception.args[2], '20211216')
def test_wrong_format_dwdts_month(self): with self.assertRaises(ValueError) as e: PointInTime("20219916") self.assertEqual(e.exception.args[0], "invalid month") self.assertEqual( e.exception.args[1], "20219916") # occasional check whether 2nd argument is passed
def test_from_dwdts(self): d = PointInTime("20211216") self.assertFalse(d.hourly) self.assertEqual(d.dwdts(), "20211216") self.assertEqual(d.iso(), "2021-12-16") self.assertTrue(isinstance(d.datetime(), date)) self.assertEqual(d.datetime(), date(2021, 12, 16))
def overview(station: int, tabname: str = "readings", fields: List[str] = None, with_rows: bool = False) -> List[Timeframe]: assert isinstance(station, int) assert isinstance(tabname, str) if not fields: fields = get_data_fields(tabname=tabname) assert isinstance(fields, list) assert isinstance(with_rows, bool) sql = get_indicator_select(tabname=tabname, fields=fields) with johanna.Connection(f"select from {tabname}") as c: rows = c.cur.execute(sql, (station, )).fetchall() tfs = [] ts0 = PointInTime(rows[0][0]) srow0 = "".join(rows[0][1:]) # indicator string tf = Timeframe(ts0, None, srow0, None, None) tfs.append(tf) for i, row in enumerate(rows[1:]): ts = PointInTime(row[0]) srow = "".join(row[1:]) # indicator string if ts - ts0 > 1: # not next day # we passed an occurence of '---------' ('-' only) # -> insert n/a interval: [x, _, old] -> [x, ts0, old], [ts0+1, ts-1, n/a], [ts, _, new] tf.ts_to = ts0 tfs.append(Timeframe(ts0.next(), ts.prev(), "no data", None, None)) tf = Timeframe(ts, None, srow, None, None) tfs.append(tf) elif srow != srow0: tf.ts_to = ts0 tf = Timeframe(ts, None, srow, None, None) tfs.append(tf) ts0 = ts srow0 = srow tf.ts_to = ts for tf in tfs: tf.days = tf.ts_to - tf.ts_from + 1 if with_rows: for tf in tfs: tf.rows = get_two(station, tf.ts_to.dwdts(), tabname=tabname, fields=fields) return tfs
def test_wrong_format_dwdts_year(self): with self.assertRaises(ValueError) as e: PointInTime("21219916") self.assertEqual(e.exception.args[0], "invalid year")
def test_prev_hourly(self): neujahr = PointInTime("2022-01-01 00") silvester = neujahr.prev() self.assertEqual(silvester.iso(), "2021-12-31 23")
def test_funny(self): silvester = PointInTime("2021-12-31") neujahr = silvester.next() self.assertEqual(neujahr - silvester, 1)
def test_wrong_format_dwdts_year_character(self): with self.assertRaises(ValueError) as e: PointInTime("21X1991614") self.assertEqual(e.exception.args[0], "format must match YYYYMMDDHH")
def test_next_hourly(self): silvester = PointInTime("2021-12-31 23") neujahr = silvester.next() self.assertEqual(neujahr.iso(), "2022-01-01 00")
def test_wrong_format_dwdts_hour(self): with self.assertRaises(ValueError) as e: PointInTime("2021121688") self.assertEqual(e.exception.args[0], "invalid hour")
def test_wrong_length_iso_long(self): with self.assertRaises(ValueError) as e: PointInTime("2021-12-014") self.assertIn("YYYY-MM-DD", e.exception.args[0])
def test_wrong_format_iso_month(self): with self.assertRaises(ValueError) as e: PointInTime("2021-99-16") self.assertEqual(e.exception.args[0], "invalid month")
def test_wrong_format_iso_month_character(self): with self.assertRaises(ValueError) as e: PointInTime("2021-9X-16 14") self.assertEqual(e.exception.args[0], "format must match YYYY-MM-DD HH")
def test_hourly(self): d0 = PointInTime("2020-12-24 18") d1 = PointInTime("2020-12-24 17") self.assertTrue(d0.hourly) self.assertEqual(d1 - d0, 1) self.assertEqual(d0 - d1, 1)
def test_days(self): d0 = PointInTime("2020-12-24") d1 = PointInTime("2020-12-23") self.assertFalse(d0.hourly) self.assertEqual(d1 - d0, 1) self.assertEqual(d0 - d1, 1)
def test_wrong_format_iso_separator_dash(self): with self.assertRaises(ValueError) as e: PointInTime("2021-99-16-14") self.assertEqual(e.exception.args[0], "format must match YYYY-MM-DD HH")
def test_wrong_format_iso_year(self): # bad year, month, day with self.assertRaises(ValueError) as e: PointInTime("2121-99-16") self.assertEqual(e.exception.args[0], "invalid year")
def test_typecast_good_iso_dwdts(self): d = PointInTime("2021-12-16") self.assertEqual(d - "20211215", 1)
def test_wrong_format_iso_day(self): with self.assertRaises(ValueError) as e: PointInTime("2021-12-00") self.assertEqual(e.exception.args[0], "invalid day")
def test_typecast_good_iso_date(self): d = PointInTime("2021-12-16") self.assertEqual(d - date(2021, 12, 15), 1)
def test_wrong_length_dwdts_long(self): with self.assertRaises(ValueError) as e: PointInTime("202112014") self.assertIn("YYYYMMDD", e.exception.args[0])
def test_typecast_bad_hourly_iso(self): d = PointInTime("2021-12-16") with self.assertRaises(ValueError) as e: d - "2021-12-15 14" self.assertIn("second operand must also be daily", e.exception.args[0])
def test_bad_date_iso(self): # not a leap year with self.assertRaises(ValueError) as e: PointInTime("2021-02-29") self.assertEqual(e.exception.args[0], "day is out of range for month")
def test_typecast_bad_string_dwdts(self): d = PointInTime("2021-12-16") with self.assertRaises(AssertionError) as e: d - "HurzHurz" self.assertEqual(e.exception.args[0], "format must match YYYYMMDD")