diff --git a/ravendb/tests/jvm_migrated_tests/client_tests/time_series_tests/test_time_series_ranges_cache.py b/ravendb/tests/jvm_migrated_tests/client_tests/time_series_tests/test_time_series_ranges_cache.py index 9514f50b..becae6dc 100644 --- a/ravendb/tests/jvm_migrated_tests/client_tests/time_series_tests/test_time_series_ranges_cache.py +++ b/ravendb/tests/jvm_migrated_tests/client_tests/time_series_tests/test_time_series_ranges_cache.py @@ -456,3 +456,106 @@ def test_should_merge_time_series_ranges_in_cache_2(self): self.assertEqual(base_line + timedelta(minutes=0), ranges[0].from_date) self.assertEqual(base_line + timedelta(minutes=45), ranges[0].to_date) + + def test_should_merge_time_series_range_in_cache_3(self): + base_line = datetime(2023, 8, 20, 21, 30) + doc_id = "users/ayende" + ts_name = "Heartrate" + tag = "watches/fitbit" + + with self.store.open_session() as session: + session.store(User(name="Oren"), doc_id) + session.save_changes() + + with self.store.open_session() as session: + tsf = session.time_series_for(doc_id, ts_name) + for i in range(360): + tsf.append_single(base_line + timedelta(seconds=i * 10), 60, tag) + + tsf = session.time_series_for(doc_id, ts_name) + + tsf.append_single(base_line + timedelta(hours=1), 70, tag) + tsf.append_single(base_line + timedelta(minutes=90), 75, tag) + + session.save_changes() + + with self.store.open_session() as session: + vals = session.time_series_for(doc_id, ts_name).get( + base_line + timedelta(minutes=1), base_line + timedelta(minutes=2) + ) + + self.assertEqual(7, len(vals)) + self.assertEqual(base_line + timedelta(minutes=1), vals[0].timestamp) + self.assertEqual(base_line + timedelta(minutes=2), vals[6].timestamp) + + vals = session.time_series_for(doc_id, ts_name).get( + base_line + timedelta(minutes=5), base_line + timedelta(minutes=6) + ) + + self.assertEqual(2, session.advanced.number_of_requests) + + self.assertEqual(7, len(vals)) + self.assertEqual(base_line + timedelta(minutes=5), vals[0].timestamp) + self.assertEqual(base_line + timedelta(minutes=6), vals[6].timestamp) + + cache = session.time_series_by_doc_id.get(doc_id) + ranges = cache.get(ts_name) + self.assertIsNotNone(ranges) + self.assertEqual(2, len(ranges)) + + self.assertEqual(base_line + timedelta(minutes=1), ranges[0].from_date) + self.assertEqual(base_line + timedelta(minutes=2), ranges[0].to_date) + self.assertEqual(base_line + timedelta(minutes=5), ranges[1].from_date) + self.assertEqual(base_line + timedelta(minutes=6), ranges[1].to_date) + + # should go to server to get [2, 3] and merge with [1, 2] + + vals = session.time_series_for(doc_id, ts_name).get( + base_line + timedelta(minutes=2), base_line + timedelta(minutes=3) + ) + self.assertEqual(3, session.advanced.number_of_requests) + + self.assertEqual(7, len(vals)) + self.assertEqual(base_line + timedelta(minutes=2), vals[0].timestamp) + self.assertEqual(base_line + timedelta(minutes=3), vals[6].timestamp) + + self.assertEqual(2, len(ranges)) + + self.assertEqual(base_line + timedelta(minutes=1), ranges[0].from_date) + self.assertEqual(base_line + timedelta(minutes=3), ranges[0].to_date) + self.assertEqual(base_line + timedelta(minutes=5), ranges[1].from_date) + self.assertEqual(base_line + timedelta(minutes=6), ranges[1].to_date) + + # should go to server to get [4, 5] and merge with [5, 6] + vals = session.time_series_for(doc_id, ts_name).get( + base_line + timedelta(minutes=4), base_line + timedelta(minutes=5) + ) + self.assertEqual(4, session.advanced.number_of_requests) + + self.assertEqual(7, len(vals)) + self.assertEqual(base_line + timedelta(minutes=4), vals[0].timestamp) + self.assertEqual(base_line + timedelta(minutes=5), vals[6].timestamp) + + self.assertEqual(2, len(ranges)) + + self.assertEqual(base_line + timedelta(minutes=1), ranges[0].from_date) + self.assertEqual(base_line + timedelta(minutes=3), ranges[0].to_date) + self.assertEqual(base_line + timedelta(minutes=4), ranges[1].from_date) + self.assertEqual(base_line + timedelta(minutes=6), ranges[1].to_date) + + # should go to server to get [3, 4] and merge all ranges into [1, 6] + + vals = session.time_series_for(doc_id, ts_name).get( + base_line + timedelta(minutes=3), base_line + timedelta(minutes=4) + ) + + self.assertEqual(5, session.advanced.number_of_requests) + + self.assertEqual(7, len(vals)) + self.assertEqual(base_line + timedelta(minutes=3), vals[0].timestamp) + self.assertEqual(base_line + timedelta(minutes=4), vals[6].timestamp) + + self.assertEqual(1, len(ranges)) + + self.assertEqual(base_line + timedelta(minutes=1), ranges[0].from_date) + self.assertEqual(base_line + timedelta(minutes=6), ranges[0].to_date)