def assert_extents(self, extents, start, end): message = "%s of extents not equal: \nactual: %s\nexpect: %s" actual_start, actual_end = extents expect = local_datetime(*start) self.assertEquals(actual_start, expect, message % ("start", actual_start.__repr__(), expect.__repr__())) expect = local_datetime(*end) self.assertEquals(actual_end, expect, message % ("end", actual_end.__repr__(), expect.__repr__()))
def test_valid(self): tests = ( (quarter(2014, 1), (2014, 1, 1), (2014, 3, 31)), (quarter(2001, 2), (2001, 4, 1), (2001, 6, 30)), (quarter(2015, 3), (2015, 7, 1), (2015, 9, 30)), (quarter(2016, 4), (2016, 10, 1), (2016, 12, 31)), ) for actual, start, end in tests: start = local_datetime(start) end = local_datetime(end) self.assertEqual(actual[0], start) self.assertEqual(actual[1], end)
def test_to_json(self): keys = ['timestamp', 'foo'] values = ( (local_datetime(2015, 1, 1, 12, 0, 0), 1), (local_datetime(2015, 1, 2, 12, 0, 0), 2), (local_datetime(2015, 1, 3, 12, 0, 0), 3), (local_datetime(2015, 1, 4, 12, 0, 0), 4) ) container = TimeseriesContainer(keys, values) data = container.to_json_dict(extra='foo') self.assertEquals(data['keys'], keys) self.assertEquals(data['extra'], 'foo')
def test_timeseriescontainer(self): keys = ['timestamp', 'foo'] values = ( (local_datetime(2015, 1, 1, 12, 0, 0), 1), (local_datetime(2015, 1, 2, 12, 0, 0), 2), (local_datetime(2015, 1, 3, 12, 0, 0), 3), (local_datetime(2015, 1, 4, 12, 0, 0), 4) ) container = TimeseriesContainer(keys, values) response = json_response(container) # is valid json response_json = json.loads(response.content) self.assertEquals(response_json['keys'], keys)
def test_period_defined(self): extents = Extents(local_datetime(2014, 6, 18, 11, 11, 11), period="day") self.assert_extents(extents, (2014, 6, 18, 0, 0, 0), (2014, 6, 18, 23, 59, 59)) # period can be changed extents.set_period("month") self.assert_extents(extents, (2014, 6, 1), (2014, 6, 30, 23, 59, 59))
def test_extents_to_string(self): t = local_datetime(2014, 6, 18, 11, 11, 11) t_formatted = t.__repr__() extents = Extents(t) self.assertEquals(str(extents), "Extents for undefined period around %s" % t_formatted) extents.set_period("week") self.assertEquals(str(extents), "Extents for `week` around %s" % t_formatted)
def test_period_next(self): extents = Extents(local_datetime(2014, 6, 18, 11, 11, 11), period="week") self.assertEquals(extents.next(), local_datetime(2014, 6, 26)) extents = Extents(local_datetime(2014, 6, 25, 11, 11), period="week") self.assertEquals(extents.next(), local_datetime(2014, 7, 3)) extents = Extents(local_datetime(2015, 4, 14, 11, 11), period="week") self.assertEquals(extents.next(), local_datetime(2015, 4, 23))
def test_without_DST(self): t = datetime(2015, 1, 18, 12, 0, 0, tzinfo=utc) self.assertEquals(t, local_datetime(2015, 1, 18, 13, 0, 0))
def test_amsterdam_vs_utc(self): t = local_datetime(2015, 6, 18, 14, 0, 0) self.assertEquals(t, datetime(2015, 6, 18, 12, 0, 0, tzinfo=utc))
def test_slices_illigal_interval(self): extents = Extents(local_datetime(2014, 6, 18, 11, 11, 11), "week") self.assertRaises(ValueError, lambda: extents.slices("year"))
def test_utc_vs_amsterdam(self): t = datetime(2015, 6, 18, 12, 0, 0, tzinfo=utc) self.assertEquals(t, local_datetime(2015, 6, 18, 14, 0, 0))
def test_period_previous(self): t = datetime(2014, 6, 18, 11, 11, 11, tzinfo=utc) extents = Extents(t, period="week") self.assertEquals(extents.previous(), local_datetime(2014, 6, 12))
def test_for_period(self): extents = Extents(local_datetime(2014, 6, 18, 11, 11, 11)) start, end = extents.for_period("week") self.assertEquals(start, local_datetime(2014, 6, 16, 0, 0, 0)) self.assertEquals(end, local_datetime(2014, 6, 22, 23, 59, 59))
def test_period_undefinde(self): extents = Extents(local_datetime(2014, 6, 18)) self.assertRaises(ValueError, lambda: list(extents))
def test_slices_in_week(self): extents = Extents(local_datetime(2014, 6, 18, 11, 11, 11), "week") self.assertEquals(extents.slices("day"), 7) self.assertEquals(extents.slices("hour"), 7 * 24) self.assertEquals(extents.slices("15minute"), 7 * 24 * 4)
def test_period_set_invalid(self): extents = Extents(local_datetime(2014, 6, 18)) self.assertRaises(ValueError, lambda: extents.set_period("lightyear"))
def test_extents_year(self): extents = Extents(local_datetime(2014, 2, 18, 11, 11, 11), "year") self.assert_extents(extents, (2014, 1, 1, 0, 0, 0), (2014, 12, 31, 23, 59, 59))
def test_extents_month(self): extents = Extents(local_datetime(2014, 6, 18, 11, 11, 11), "month") self.assert_extents(extents, (2014, 6, 1, 0, 0, 0), (2014, 6, 30, 23, 59, 59))
def test_extents_week(self): # monday june 16 ... sunday june 22 extents = Extents(local_datetime(2014, 6, 18, 11, 11, 11), "week") self.assert_extents(extents, (2014, 6, 16, 0, 0, 0), (2014, 6, 22, 23, 59, 59))
def test_extents_day(self): t = local_datetime(2014, 6, 18, 11, 11, 11) extents = Extents(t, "day") self.assertEqual(t, extents.current()) self.assert_extents(extents, (2014, 6, 18, 0, 0, 0), (2014, 6, 18, 23, 59, 59))