def setUp(self) -> None: self.value_history1 = ValueHistory( {ValueHistory.DATE_MS: self.DATE_MS1, ValueHistory.VALUE: self.VALUE1}) self.value_history_list = \ [ValueHistory.build(self.VALUE1, self.DATE_MS1), ValueHistory.build(self.VALUE2, self.DATE_MS2), ValueHistory.build(self.VALUE3, self.DATE_MS3)]
def test_12_ge(self): value_history = ValueHistory( {ValueHistory.DATE_MS: self.DATE_MS2, ValueHistory.VALUE: self.VALUE2}) self.assertTrue(self.value_history1 <= value_history) value_history = ValueHistory( {ValueHistory.DATE_MS: self.DATE_MS1, ValueHistory.VALUE: self.VALUE1}) self.assertTrue(value_history >= self.value_history1)
def test_15_to_list_and_to_doc_list(self): doc_list = ValueHistory.to_doc_list(self.value_history_list) self.assertEqual(len(self.value_history_list), len(doc_list)) self.assertEqual( self.value_history_list[1].date_ms, doc_list[1][DocumentBase.DATE_MS]) value_history_list = ValueHistory.to_list(doc_list) self.assertEqual(len(value_history_list), len(doc_list)) self.assertEqual( value_history_list[0].date_ms, doc_list[0][DocumentBase.DATE_MS])
def test_20_get_value_history_ge_date_ms(self): value_history = \ ValueHistory.get_value_history_ge_date_ms( self.DATE_MS2, self.value_history_list) self.assertEqual(self.DATE_MS2, value_history.date_ms) self.assertTrue(value_history.value == self.VALUE2) value_history = \ ValueHistory.get_value_history_ge_date_ms( self.DATE_MS2 + 1, self.value_history_list) self.assertEqual(self.DATE_MS3, value_history.date_ms) self.assertTrue(value_history.value == self.VALUE3)
def test_16_get_value_le_date_ms(self): value = ValueHistory.get_value_le_date_ms( self.DATE_MS1 + 1, self.value_history_list) self.assertTrue(value == self.VALUE1) value = ValueHistory.get_value_le_date_ms( self.DATE_MS2, self.value_history_list) self.assertTrue(value == self.VALUE2) value = ValueHistory.get_value_le_date_ms( self.DATE_MS3 + 1, self.value_history_list) self.assertTrue(value == self.VALUE3)
def get_history_ymd_list( self, date_ms: int, sort: SortDateMsEnum = SortDateMsEnum.ASCENDING) \ -> Optional[list[ValueHistory]]: first_ymd_date_ms = TimeUtil.date_ms_to_first_ymd_date_ms(date_ms) last_ymd_date_ms = TimeUtil.date_ms_to_last_ymd_date_ms(date_ms) gte_first_ymd_filter = \ { ValueHistory.DATE_MS: { MongoDbModifierEnum.GTE.value: first_ymd_date_ms } } lte_last_ymd_filter = \ { ValueHistory.DATE_MS: { MongoDbModifierEnum.LTE.value: last_ymd_date_ms } } filter = {'$and': [gte_first_ymd_filter, lte_last_ymd_filter]} return \ ValueHistory.to_list( self.collection.find(filter, sort=sort.value))
def get_history( self, date_ms: int, is_find_prev: bool = False) -> ValueHistory: """ Get history value in date_time ms. :param date_ms: Date in ms. :param is_find_prev: Find previous value in case of True, else find value only in the specific date_ms. :return: """ if is_find_prev: filter = \ { ValueHistory.DATE_MS: { MongoDbModifierEnum.LTE.value: date_ms } } else: filter = {ValueHistory.DATE_MS: date_ms} doc = self.collection.find_one( filter=filter, sort=SortDateMsEnum.DESCENDING.value) return ValueHistory(doc) if doc else None
def __find_value_history_list( self, filter: dict = None, start_index: int = 0, batch_amount: int = 0, sort: SortDateMsEnum = SortDateMsEnum.DESCENDING): return ValueHistory.to_list( self._find(filter, start_index, batch_amount, sort.value))
def update(self, value: DecimalNumber, date_ms: int): if not self.is_exist(date_ms): raise ApplicationException( f'History in [{date_ms}] not exist' f' in collection {self.collection_name}') filter = \ {ValueHistory.DATE_MS: {MongoDbModifierEnum.EQ.value: date_ms}} value_history = ValueHistory.build(value, date_ms) update = {'$set': value_history.document} self.collection.update_one( filter=filter, update=update)
def get_prev_ymd_date_history( self, date_ms: int) -> Optional[ValueHistory]: prev_history_value = self.get_prev_date_history(date_ms) if prev_history_value is None: return None prev_ymd_date_ms = \ TimeUtil.date_ms_to_first_ymd_date_ms(prev_history_value.date_ms) return \ ValueHistory( self.find_between_dates( prev_ymd_date_ms, prev_history_value.date_ms)[0])
def add(self, value: DecimalNumber, date_ms: int): """ Add history document that not exist. :param value: Value. :param date_ms: Date in ms. :return: """ if self.is_exist(date_ms): raise ApplicationException( f'History in date ms [{date_ms}] already exist' f' in collection {self.collection_name}') value_history = ValueHistory.build(value, date_ms) self.collection.insert_one(value_history.document)
def test_14_get_history_until_date(self): until_date_list = ValueHistory.get_history_until_date( self.value_history_list, self.DATE_MS2) self.assertEqual(1, len(until_date_list)) self.assertEqual(self.DATE_MS1, until_date_list[0].date_ms)
def test_13_ge_negative(self): value_history_neg = ValueHistory( {ValueHistory.DATE_MS: self.DATE_MS3, ValueHistory.VALUE: self.VALUE3}) self.assertFalse(self.value_history1 >= value_history_neg)
def test_18_index_negative(self): index = ValueHistory.index(1234, self.value_history_list) self.assertEqual(-1, index)
def test_08_eq(self): value_history = ValueHistory.build(self.VALUE1, self.DATE_MS1) self.assertTrue(self.value_history1 == value_history)
def test_04_lt(self): value_history = ValueHistory( {ValueHistory.DATE_MS: self.DATE_MS2, ValueHistory.VALUE: self.VALUE2}) self.assertTrue(self.value_history1 < value_history)
def test_17_index(self): index = ValueHistory.index(self.DATE_MS3, self.value_history_list) self.assertEqual(2, index)
def test_09_eq_negative(self): value_history_neg = ValueHistory( {ValueHistory.DATE_MS: self.DATE_MS3, ValueHistory.VALUE: self.VALUE3}) self.assertFalse(value_history_neg == self.value_history1)
def test_19_get_value_le_date_ms_negative(self): value = \ ValueHistory.get_value_le_date_ms( 1, self.value_history_list) self.assertIsNone(value)
def test_10_gt(self): value_history = ValueHistory( {ValueHistory.DATE_MS: self.DATE_MS2, ValueHistory.VALUE: self.VALUE2}) self.assertTrue(value_history > self.value_history1)
def test_21_get_value_history_ge_date_ms_negative(self): value_history = \ ValueHistory.get_value_history_ge_date_ms( 999999, self.value_history_list) self.assertIsNone(value_history)
def test_11_gt_negative(self): value_history_neg = ValueHistory( {ValueHistory.DATE_MS: self.DATE_MS1, ValueHistory.VALUE: self.VALUE1}) self.assertFalse(value_history_neg > self.value_history1)