def test_non_fixed_rollups(self): """Work the calendar rollup logic / utc / etc.""" timeseries = TimeSeries(SEPT_2014_DATA) # just silence the warnings, not do anything with them. with warnings.catch_warnings(record=True): daily_avg = timeseries.daily_rollup(dict(value=dict(value=Functions.avg()))) ts_1 = SEPT_2014_DATA.get('points')[0][0] self.assertEqual( Index.get_daily_index_string(dt_from_ms(ts_1), utc=False), daily_avg.at(0).index().to_string() ) monthly_avg = timeseries.monthly_rollup(dict(value=dict(value=Functions.avg()))) self.assertEqual( Index.get_monthly_index_string(dt_from_ms(ts_1), utc=False), monthly_avg.at(0).index().to_string() ) yearly_avg = timeseries.yearly_rollup(dict(value=dict(value=Functions.avg()))) self.assertEqual( Index.get_yearly_index_string(dt_from_ms(ts_1), utc=False), yearly_avg.at(0).index().to_string() )
def test_non_fixed_rollups(self): """Work the calendar rollup logic / utc / etc.""" timeseries = TimeSeries(SEPT_2014_DATA) # just silence the warnings, not do anything with them. with warnings.catch_warnings(record=True): daily_avg = timeseries.daily_rollup( dict(value=dict(value=Functions.avg()))) ts_1 = SEPT_2014_DATA.get('points')[0][0] self.assertEqual( Index.get_daily_index_string(dt_from_ms(ts_1), utc=False), daily_avg.at(0).index().to_string()) monthly_avg = timeseries.monthly_rollup( dict(value=dict(value=Functions.avg()))) self.assertEqual( Index.get_monthly_index_string(dt_from_ms(ts_1), utc=False), monthly_avg.at(0).index().to_string()) yearly_avg = timeseries.yearly_rollup( dict(value=dict(value=Functions.avg()))) self.assertEqual( Index.get_yearly_index_string(dt_from_ms(ts_1), utc=False), yearly_avg.at(0).index().to_string())
def test_other_accessors(self): """check other accessor methods() - primarily for coverage.""" # date string formatting self.assertEqual(self.canned_event.timestamp_as_utc_string(), dt_from_ms(self.msec).strftime(HUMAN_FORMAT)) self.assertEqual(self.canned_event.timestamp_as_local_string(), localtime_from_ms(self.msec).strftime(HUMAN_FORMAT)) # underlying datetime objects self.assertEqual(self.canned_event.begin(), dt_from_ms(self.msec)) self.assertEqual(self.canned_event.begin(), self.canned_event.end())
def setUp(self): super(TestConverter, self).setUp() self._event = Event(dt_from_ms(1426316400000), 3) self._tre = TimeRangeEvent(TimeRange([1426316400000, 1426320000000]), 3) self._idxe = IndexedEvent("1h-396199", 3)
def test_get_index_string(self): """ test get_index_string - datetime -> index Used to be: const d = Date.UTC(2015, 2, 14, 7, 32, 22); const generator = new Generator("5m"); it("should have the correct index", done => { const b = generator.bucket(d); const expected = "5m-4754394"; expect(b.index().asString()).to.equal(expected); done(); }); REMEMBER: JS Date.UTC month (arg2) is ZERO INDEXED get_index_string() calls window_position_from_date() which in turn calls window_duration() """ dtime = aware_dt_from_args( dict(year=2015, month=3, day=14, hour=7, minute=32, second=22)) self.assertEqual(dtime, dt_from_ms(1426318342000)) idx_str = Index.get_index_string('5m', dtime) self.assertEqual(idx_str, '5m-4754394')
def test_ms_from_dt(self): """Run reference ms into datetime and extract the ms again.""" dtime = dt_from_ms(self.ms_reference) new_ms = ms_from_dt(dtime) # after the round trip, value should be the same. self.assertEqual(new_ms, self.ms_reference) # test sanity check stopping naive datetime objects with self.assertRaises(UtilityException): ms_from_dt(self.naive)
def test_round_trip(self): """Test ms -> dt -> ms and dt -> ms -> dt""" # ms -> dt -> ms to_dt = dt_from_ms(self.ms_reference) from_dt = ms_from_dt(to_dt) self.assertEqual(from_dt, self.ms_reference) # dt -> ms -> dt to test rounding in aware_utcnow() now = aware_utcnow() to_ms = ms_from_dt(now) back_to_dt = dt_from_ms(to_ms) self.assertEqual(now, back_to_dt) # dt from unixtime -> ms -> dt utc = datetime.datetime.utcfromtimestamp(1459442035).replace(tzinfo=pytz.UTC) utcms = ms_from_dt(utc) back_to_utc = dt_from_ms(utcms) self.assertEqual(utc, back_to_utc)
def test_round_trip(self): """Test ms -> dt -> ms and dt -> ms -> dt""" # ms -> dt -> ms to_dt = dt_from_ms(self.ms_reference) from_dt = ms_from_dt(to_dt) self.assertEqual(from_dt, self.ms_reference) # dt -> ms -> dt to test rounding in aware_utcnow() now = aware_utcnow() to_ms = ms_from_dt(now) back_to_dt = dt_from_ms(to_ms) self.assertEqual(now, back_to_dt) # dt from unixtime -> ms -> dt utc = datetime.datetime.utcfromtimestamp(1459442035).replace( tzinfo=pytz.UTC) utcms = ms_from_dt(utc) back_to_utc = dt_from_ms(utcms) self.assertEqual(utc, back_to_utc)
def test_other_accessors(self): """check other accessor methods() - primarily for coverage.""" # date string formatting self.assertEqual( self.canned_event.timestamp_as_utc_string(), dt_from_ms(self.msec).strftime(HUMAN_FORMAT)) self.assertEqual( self.canned_event.timestamp_as_local_string(), localtime_from_ms(self.msec).strftime(HUMAN_FORMAT)) # underlying datetime objects self.assertEqual( self.canned_event.begin(), dt_from_ms(self.msec)) self.assertEqual( self.canned_event.begin(), self.canned_event.end())
def test_at_key_and_dedup(self): """test Collection.at_key() and dedup()""" # events coll = Collection(EVENT_LIST_DUP) key_time = dt_from_ms(1429673460000) find = coll.at_key(key_time) self.assertEqual(len(find), 2) self.assertEqual(find[0].get('in'), 3) self.assertEqual(find[1].get('in'), 4) ddcoll = coll.dedup() self.assertEqual(ddcoll.size(), 3) self.assertEqual(ddcoll.at(1).get('in'), 4) # the second dup event # indexed events coll = Collection(IDX_EVENT_DUP) find = coll.at_key('1d-12355') self.assertEqual(len(find), 2) self.assertEqual(find[0].get('value'), 43) self.assertEqual(find[1].get('value'), 44) ddcoll = coll.dedup() self.assertEqual(ddcoll.size(), 3) self.assertEqual(ddcoll.at(1).get('value'), 44) # the second dup event # time range events test_end_ts = aware_utcnow() test_begin_ts = test_end_ts - datetime.timedelta(hours=12) test_end_ms = ms_from_dt(test_end_ts) test_begin_ms = ms_from_dt(test_begin_ts) dup_tre = [ TimeRangeEvent((test_begin_ms, test_end_ms), 11), TimeRangeEvent((test_begin_ms + 60000, test_end_ms + 60000), 12), TimeRangeEvent((test_begin_ms + 60000, test_end_ms + 60000), 13), TimeRangeEvent((test_begin_ms + 120000, test_end_ms + 120000), 14), ] coll = Collection(dup_tre) search = TimeRange(test_begin_ms + 60000, test_end_ms + 60000) find = coll.at_key(search) self.assertEqual(len(find), 2) self.assertEqual(find[0].get('value'), 12) self.assertEqual(find[1].get('value'), 13) ddcoll = coll.dedup() self.assertEqual(ddcoll.size(), 3) self.assertEqual(ddcoll.at(1).get('value'), 13) # the second dup event
def test_slices_and_permutations(self): """methods that slice/etc the underlying series.""" # bisect search = dt_from_ms(1400425949000 + 30) bsect_idx = self._canned_wire_series.bisect(search) # bisect with bad arg bad_search = datetime.datetime.now() with self.assertRaises(CollectionException): self._canned_wire_series.bisect(bad_search) bsection = self._canned_wire_series.at(bsect_idx) self.assertEqual(bsection.data().get('status'), 'fail') # clean self.assertEqual(self._canned_event_series.clean('in').size(), 3) self.assertEqual(self._canned_event_series.clean('bogus_value').size(), 0) # slice sliced = self._canned_event_series.slice(1, 3) self.assertEqual(sliced.size(), 2) self.assertTrue(Event.same(sliced.at(0), EVENT_LIST[1]))
def test_slices_and_permutations(self): """methods that slice/etc the underlying series.""" # bisect search = dt_from_ms(1400425949000 + 30) bsect_idx = self._canned_wire_series.bisect(search) # bisect with bad arg bad_search = datetime.datetime.now() with self.assertRaises(CollectionException): self._canned_wire_series.bisect(bad_search) bsection = self._canned_wire_series.at(bsect_idx) self.assertEqual(bsection.data().get('status'), 'fail') # clean self.assertEqual(self._canned_event_series.clean('in').size(), 3) self.assertEqual( self._canned_event_series.clean('bogus_value').size(), 0) # slice sliced = self._canned_event_series.slice(1, 3) self.assertEqual(sliced.size(), 2) self.assertTrue(Event.same(sliced.at(0), EVENT_LIST[1]))
def test_dt_from_ms(self): """Test function to make datetime from epoch ms/verify aware.""" dtime = dt_from_ms(self.ms_reference) self.assertTrue(dt_is_aware(dtime))