class GetMomentsCase(unittest.TestCase): def setUp(self): self.day_series = DaySeries() def test_get(self): moments = [ Moment.from_dict(x) for x in STOCKS_DAILY['AAPL']['records'] ] moments_shuffled = moments.copy() random.shuffle(moments_shuffled) self.assertNotEqual(moments, moments_shuffled) self.day_series.add_moments(moments_shuffled) self.assertEqual(moments, self.day_series.get_moments())
class AddMomentsTestCase(unittest.TestCase): def setUp(self): self.day_series = DaySeries() def test_add_moments(self): moments = [ Moment.from_dict(x) for x in STOCKS_DAILY['AAPL']['records'] ] moments_shuffled = moments.copy() random.shuffle(moments_shuffled) self.assertNotEqual(moments, moments_shuffled) self.day_series.add_moments(moments_shuffled) expected_timestamps = [m.epoch_s for m in moments] self.assertEqual(expected_timestamps, self.day_series.timestamps) self.assertEqual( moments, [self.day_series.moments[x] for x in self.day_series.timestamps])
def add_moment(self, moment: Moment): """ Add a single moment. Re-establish chronological date order. """ stock = moment.get_stock() if stock not in self.series: self.series[stock] = DaySeries() self.series[stock].add_moment(moment)
def add_moments(self, moments: List[Moment]): """ Add a list of moments. Re-establish chronological date order. """ pre_sorted = dict() for moment in moments: stock = moment.get_stock() if stock not in pre_sorted: pre_sorted[stock] = list() pre_sorted[stock].append(moment) for stock, stock_moments in pre_sorted.items(): if stock not in self.series: self.series[stock] = DaySeries() self.series[stock].add_moments(stock_moments)
def setUp(self): self.day_series = DaySeries()
class AddMomentTestCase(unittest.TestCase): I_RECORD_1 = 5 I_RECORD_EARLIER = 4 I_RECORD_LATER = 6 def setUp(self): self.day_series = DaySeries() def test_add_moment(self): moment_1 = Moment.from_dict( STOCKS_DAILY['AAPL']['records'][self.I_RECORD_1]) self.day_series.add_moment(moment_1) self.assertEqual(moment_1.epoch_s, self.day_series.timestamps[0]) self.assertEqual( moment_1, self.day_series.moments[self.day_series.timestamps[0]]) def test_add_earlier_moment(self): moment_1 = Moment.from_dict( STOCKS_DAILY['AAPL']['records'][self.I_RECORD_1]) self.day_series.add_moment(moment_1) moment_earlier = Moment.from_dict( STOCKS_DAILY['AAPL']['records'][self.I_RECORD_EARLIER]) self.day_series.add_moment(moment_earlier) expected_timestamps = [ IntuitiveDateConverter.to_epoch_s(x) for x in [ STOCKS_DAILY['AAPL']['records'][self.I_RECORD_EARLIER] ['epoch_ms'], STOCKS_DAILY['AAPL']['records'][ self.I_RECORD_1]['epoch_ms'] ] ] expected_moments = [moment_earlier, moment_1] self.assertEqual(expected_timestamps, self.day_series.timestamps) self.assertEqual( expected_moments, [self.day_series.moments[x] for x in self.day_series.timestamps]) def test_add_later_moment(self): moment_later = Moment.from_dict( STOCKS_DAILY['AAPL']['records'][self.I_RECORD_LATER]) self.day_series.add_moment(moment_later) moment_1 = Moment.from_dict( STOCKS_DAILY['AAPL']['records'][self.I_RECORD_1]) self.day_series.add_moment(moment_1) moment_earlier = Moment.from_dict( STOCKS_DAILY['AAPL']['records'][self.I_RECORD_EARLIER]) self.day_series.add_moment(moment_earlier) expected_timestamps = [ IntuitiveDateConverter.to_epoch_s(x) for x in [ STOCKS_DAILY['AAPL']['records'][self.I_RECORD_EARLIER] ['epoch_ms'], STOCKS_DAILY['AAPL']['records'][self.I_RECORD_1] ['epoch_ms'], STOCKS_DAILY['AAPL']['records'][ self.I_RECORD_LATER]['epoch_ms'] ] ] expected_moments = [moment_earlier, moment_1, moment_later] self.assertEqual(expected_timestamps, self.day_series.timestamps) self.assertEqual( expected_moments, [self.day_series.moments[x] for x in self.day_series.timestamps])