|
| 1 | +from datetime import datetime, timedelta |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +from ravendb.documents.session.loaders.include import TimeSeriesIncludeBuilder |
| 5 | +from ravendb.infrastructure.entities import User |
| 6 | +from ravendb.tests.test_base import TestBase |
| 7 | + |
| 8 | + |
| 9 | +document_id = "users/gracjan" |
| 10 | +company_id = "companies/1-A" |
| 11 | +order_id = "orders/1-A" |
| 12 | +base_line = datetime(2023, 8, 20, 21, 30) |
| 13 | +ts_name1 = "Heartrate" |
| 14 | +ts_name2 = "Speedrate" |
| 15 | +tag1 = "watches/fitbit" |
| 16 | +tag2 = "watches/apple" |
| 17 | +tag3 = "watches/sony" |
| 18 | + |
| 19 | + |
| 20 | +class Watch: |
| 21 | + def __init__(self, name: Optional[str] = None, accuracy: Optional[float] = None): |
| 22 | + self.name = name |
| 23 | + self.accuracy = accuracy |
| 24 | + |
| 25 | + |
| 26 | +class TestRavenDB14164(TestBase): |
| 27 | + def setUp(self): |
| 28 | + super(TestRavenDB14164, self).setUp() |
| 29 | + |
| 30 | + def test_can_get_time_series_with_include_tag_documents(self): |
| 31 | + tags = [tag1, tag2, tag3] |
| 32 | + with self.store.open_session() as session: |
| 33 | + session.store(User(), document_id) |
| 34 | + |
| 35 | + session.store(Watch("FitBit", 0.855), tags[0]) |
| 36 | + session.store(Watch("Apple", 0.9), tags[1]) |
| 37 | + session.store(Watch("Sony", 0.78), tags[2]) |
| 38 | + session.save_changes() |
| 39 | + |
| 40 | + with self.store.open_session() as session: |
| 41 | + tsf = session.time_series_for(document_id, ts_name1) |
| 42 | + for i in range(121): |
| 43 | + tsf.append_single(base_line + timedelta(minutes=i), i, tags[i % 3]) |
| 44 | + session.save_changes() |
| 45 | + |
| 46 | + with self.store.open_session() as session: |
| 47 | + get_results = session.time_series_for(document_id, ts_name1).get_include( |
| 48 | + base_line, base_line + timedelta(hours=2), lambda i: i.include_tags() |
| 49 | + ) |
| 50 | + |
| 51 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 52 | + |
| 53 | + self.assertEqual(121, len(get_results)) |
| 54 | + self.assertEqual(base_line, get_results[0].timestamp) |
| 55 | + self.assertEqual(base_line + timedelta(hours=2), get_results[-1].timestamp) |
| 56 | + |
| 57 | + # should not go to server |
| 58 | + tag_documents = session.load(tags, Watch) |
| 59 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 60 | + |
| 61 | + # assert tag documents |
| 62 | + |
| 63 | + self.assertEqual(3, len(tag_documents)) |
| 64 | + |
| 65 | + tag_doc = tag_documents.get("watches/fitbit") |
| 66 | + self.assertEqual("FitBit", tag_doc.name) |
| 67 | + self.assertEqual(0.855, tag_doc.accuracy) |
| 68 | + |
| 69 | + tag_doc = tag_documents.get("watches/apple") |
| 70 | + self.assertEqual("Apple", tag_doc.name) |
| 71 | + self.assertEqual(0.9, tag_doc.accuracy) |
| 72 | + |
| 73 | + tag_doc = tag_documents.get("watches/sony") |
| 74 | + self.assertEqual("Sony", tag_doc.name) |
| 75 | + self.assertEqual(0.78, tag_doc.accuracy) |
| 76 | + |
| 77 | + def test_can_get_time_series_with_include_tags_and_parent_document(self): |
| 78 | + tags = [tag1, tag2, tag3] |
| 79 | + with self.store.open_session() as session: |
| 80 | + session.store(User(name="poisson"), document_id) |
| 81 | + session.store(Watch("FitBit", 0.855), tags[0]) |
| 82 | + session.store(Watch("Apple", 0.9), tags[1]) |
| 83 | + session.store(Watch("Sony", 0.78), tags[2]) |
| 84 | + session.save_changes() |
| 85 | + |
| 86 | + with self.store.open_session() as session: |
| 87 | + tsf = session.time_series_for(document_id, ts_name1) |
| 88 | + for i in range(121): |
| 89 | + tsf.append_single(base_line + timedelta(minutes=i), i, tags[i % 3]) |
| 90 | + session.save_changes() |
| 91 | + |
| 92 | + with self.store.open_session() as session: |
| 93 | + get_results = session.time_series_for(document_id, ts_name1).get_include( |
| 94 | + base_line, base_line + timedelta(hours=2), lambda i: i.include_tags().include_document() |
| 95 | + ) |
| 96 | + |
| 97 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 98 | + |
| 99 | + self.assertEqual(121, len(get_results)) |
| 100 | + self.assertEqual(base_line, get_results[0].timestamp) |
| 101 | + self.assertEqual(base_line + timedelta(hours=2), get_results[-1].timestamp) |
| 102 | + |
| 103 | + # should not go to server |
| 104 | + user = session.load(document_id, User) |
| 105 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 106 | + self.assertEqual("poisson", user.name) |
| 107 | + |
| 108 | + # should not go to server |
| 109 | + tag_documents = session.load(tags, Watch) |
| 110 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 111 | + |
| 112 | + # assert tag documents |
| 113 | + |
| 114 | + self.assertEqual(3, len(tag_documents)) |
| 115 | + |
| 116 | + tag_doc = tag_documents.get("watches/fitbit") |
| 117 | + self.assertEqual("FitBit", tag_doc.name) |
| 118 | + self.assertEqual(0.855, tag_doc.accuracy) |
| 119 | + |
| 120 | + tag_doc = tag_documents.get("watches/apple") |
| 121 | + self.assertEqual("Apple", tag_doc.name) |
| 122 | + self.assertEqual(0.9, tag_doc.accuracy) |
| 123 | + |
| 124 | + tag_doc = tag_documents.get("watches/sony") |
| 125 | + self.assertEqual("Sony", tag_doc.name) |
| 126 | + self.assertEqual(0.78, tag_doc.accuracy) |
| 127 | + |
| 128 | + def test_includes_should_affect_time_series_get_command_etag(self): |
| 129 | + tags = [tag1, tag2, tag3] |
| 130 | + with self.store.open_session() as session: |
| 131 | + session.store(User(name="poisson"), document_id) |
| 132 | + session.store(Watch("FitBit", 0.855), tags[0]) |
| 133 | + session.store(Watch("Apple", 0.9), tags[1]) |
| 134 | + session.store(Watch("Sony", 0.78), tags[2]) |
| 135 | + session.save_changes() |
| 136 | + |
| 137 | + with self.store.open_session() as session: |
| 138 | + tsf = session.time_series_for(document_id, ts_name1) |
| 139 | + for i in range(121): |
| 140 | + tsf.append_single(base_line + timedelta(minutes=i), i, tags[i % 3]) |
| 141 | + session.save_changes() |
| 142 | + |
| 143 | + with self.store.open_session() as session: |
| 144 | + get_results = session.time_series_for(document_id, ts_name1).get_include( |
| 145 | + base_line, base_line + timedelta(hours=2), lambda i: i.include_tags() |
| 146 | + ) |
| 147 | + |
| 148 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 149 | + |
| 150 | + self.assertEqual(121, len(get_results)) |
| 151 | + self.assertEqual(base_line, get_results[0].timestamp) |
| 152 | + self.assertEqual(base_line + timedelta(hours=2), get_results[-1].timestamp) |
| 153 | + |
| 154 | + # should not go to server |
| 155 | + tag_documents = session.load(tags, Watch) |
| 156 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 157 | + |
| 158 | + # assert tag documents |
| 159 | + |
| 160 | + self.assertEqual(3, len(tag_documents)) |
| 161 | + |
| 162 | + tag_doc = tag_documents.get("watches/fitbit") |
| 163 | + self.assertEqual("FitBit", tag_doc.name) |
| 164 | + self.assertEqual(0.855, tag_doc.accuracy) |
| 165 | + |
| 166 | + tag_doc = tag_documents.get("watches/apple") |
| 167 | + self.assertEqual("Apple", tag_doc.name) |
| 168 | + self.assertEqual(0.9, tag_doc.accuracy) |
| 169 | + |
| 170 | + tag_doc = tag_documents.get("watches/sony") |
| 171 | + self.assertEqual("Sony", tag_doc.name) |
| 172 | + self.assertEqual(0.78, tag_doc.accuracy) |
| 173 | + |
| 174 | + with self.store.open_session() as session: |
| 175 | + # update tags[0] |
| 176 | + watch = session.load(tags[0], Watch) |
| 177 | + watch.accuracy += 0.05 |
| 178 | + session.save_changes() |
| 179 | + |
| 180 | + with self.store.open_session() as session: |
| 181 | + get_results = session.time_series_for(document_id, ts_name1).get_include( |
| 182 | + base_line, base_line + timedelta(hours=2), lambda i: i.include_tags() |
| 183 | + ) |
| 184 | + |
| 185 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 186 | + |
| 187 | + self.assertEqual(121, len(get_results)) |
| 188 | + self.assertEqual(base_line, get_results[0].timestamp) |
| 189 | + self.assertEqual(base_line + timedelta(hours=2), get_results[-1].timestamp) |
| 190 | + # should not go to server |
| 191 | + |
| 192 | + tag_documents = session.load(tags, Watch) |
| 193 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 194 | + |
| 195 | + # assert tag documents |
| 196 | + |
| 197 | + self.assertEqual(3, len(tag_documents)) |
| 198 | + |
| 199 | + tag_doc = tag_documents.get("watches/fitbit") |
| 200 | + self.assertEqual("FitBit", tag_doc.name) |
| 201 | + self.assertEqual(0.905, tag_doc.accuracy) |
| 202 | + |
| 203 | + new_tag = "watches/google" |
| 204 | + |
| 205 | + with self.store.open_session() as session: |
| 206 | + session.store(Watch("Google Watch", 0.75), new_tag) |
| 207 | + # update a time series entry to have the new tag |
| 208 | + |
| 209 | + session.time_series_for(document_id, ts_name1).append_single(base_line + timedelta(minutes=45), 90, new_tag) |
| 210 | + session.save_changes() |
| 211 | + |
| 212 | + with self.store.open_session() as session: |
| 213 | + get_results = session.time_series_for(document_id, ts_name1).get_include( |
| 214 | + base_line, base_line + timedelta(hours=2), lambda i: i.include_tags() |
| 215 | + ) |
| 216 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 217 | + |
| 218 | + self.assertEqual(121, len(get_results)) |
| 219 | + self.assertEqual(base_line, get_results[0].timestamp) |
| 220 | + self.assertEqual(base_line + timedelta(hours=2), get_results[-1].timestamp) |
| 221 | + |
| 222 | + # should not go to server |
| 223 | + tag_documents = session.load(tags, Watch) |
| 224 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 225 | + |
| 226 | + # assert that new tag is in cache |
| 227 | + watch = session.load(new_tag, Watch) |
| 228 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 229 | + |
| 230 | + self.assertEqual("Google Watch", watch.name) |
| 231 | + self.assertEqual(0.75, watch.accuracy) |
| 232 | + |
| 233 | + def test_can_get_time_series_with_include_cache_not_empty(self): |
| 234 | + tags = [tag1, tag2, tag3] |
| 235 | + with self.store.open_session() as session: |
| 236 | + session.store(User(name="poisson"), document_id) |
| 237 | + session.store(Watch("FitBit", 0.855), tags[0]) |
| 238 | + session.store(Watch("Apple", 0.9), tags[1]) |
| 239 | + session.store(Watch("Sony", 0.78), tags[2]) |
| 240 | + session.save_changes() |
| 241 | + |
| 242 | + with self.store.open_session() as session: |
| 243 | + tsf = session.time_series_for(document_id, ts_name1) |
| 244 | + for i in range(121): |
| 245 | + tag = tags[0 if i < 60 else 1 if i < 90 else 2] |
| 246 | + tsf.append_single(base_line + timedelta(minutes=i), i, tag) |
| 247 | + |
| 248 | + session.save_changes() |
| 249 | + |
| 250 | + with self.store.open_session() as session: |
| 251 | + # get [21:30 - 22:30] |
| 252 | + get_results = session.time_series_for(document_id, ts_name1).get(base_line, base_line + timedelta(hours=1)) |
| 253 | + |
| 254 | + self.assertEqual(61, len(get_results)) |
| 255 | + self.assertEqual(base_line, get_results[0].timestamp) |
| 256 | + self.assertEqual(base_line + timedelta(hours=1), get_results[-1].timestamp) |
| 257 | + |
| 258 | + # get [22:45 - 23:30] with includes |
| 259 | + get_results = session.time_series_for(document_id, ts_name1).get_include( |
| 260 | + base_line + timedelta(minutes=75), base_line + timedelta(hours=2), TimeSeriesIncludeBuilder.include_tags |
| 261 | + ) |
| 262 | + |
| 263 | + self.assertEqual(2, session.advanced.number_of_requests) |
| 264 | + |
| 265 | + self.assertEqual(46, len(get_results)) |
| 266 | + self.assertEqual(base_line + timedelta(minutes=75), get_results[0].timestamp) |
| 267 | + self.assertEqual(base_line + timedelta(hours=2), get_results[-1].timestamp) |
| 268 | + |
| 269 | + # should not go to server |
| 270 | + |
| 271 | + tags_documents = session.load(tags[1:3], Watch) |
| 272 | + self.assertEqual(2, session.advanced.number_of_requests) |
| 273 | + |
| 274 | + # assert tag documents |
| 275 | + self.assertEqual(2, len(tags_documents)) |
| 276 | + |
| 277 | + tag_doc = tags_documents.get("watches/apple") |
| 278 | + self.assertEqual("Apple", tag_doc.name) |
| 279 | + self.assertEqual(0.9, tag_doc.accuracy) |
| 280 | + |
| 281 | + tag_doc = tags_documents.get("watches/sony") |
| 282 | + self.assertEqual("Sony", tag_doc.name) |
| 283 | + self.assertEqual(0.78, tag_doc.accuracy) |
| 284 | + |
| 285 | + # watches/fitbit should not be in cache |
| 286 | + watch = session.load(tags[0], Watch) |
| 287 | + self.assertEqual(3, session.advanced.number_of_requests) |
| 288 | + self.assertEqual("FitBit", watch.name) |
| 289 | + self.assertEqual(0.855, watch.accuracy) |
| 290 | + |
| 291 | + def test_can_get_time_series_with_include_tags_when_not_all_entries_have_tags(self): |
| 292 | + tags = [tag1, tag2, tag3] |
| 293 | + with self.store.open_session() as session: |
| 294 | + session.store(User(name="poisson"), document_id) |
| 295 | + session.store(Watch("FitBit", 0.855), tags[0]) |
| 296 | + session.store(Watch("Apple", 0.9), tags[1]) |
| 297 | + session.store(Watch("Sony", 0.78), tags[2]) |
| 298 | + session.save_changes() |
| 299 | + |
| 300 | + with self.store.open_session() as session: |
| 301 | + tsf = session.time_series_for(document_id, ts_name1) |
| 302 | + for i in range(121): |
| 303 | + tsf.append_single(base_line + timedelta(minutes=i), i, tags[i % 3]) |
| 304 | + session.save_changes() |
| 305 | + |
| 306 | + with self.store.open_session() as session: |
| 307 | + get_results = session.time_series_for(document_id, ts_name1).get_include( |
| 308 | + base_line, base_line + timedelta(hours=2), lambda i: i.include_tags() |
| 309 | + ) |
| 310 | + |
| 311 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 312 | + |
| 313 | + self.assertEqual(121, len(get_results)) |
| 314 | + self.assertEqual(base_line, get_results[0].timestamp) |
| 315 | + self.assertEqual(base_line + timedelta(hours=2), get_results[-1].timestamp) |
| 316 | + |
| 317 | + # should not go to server |
| 318 | + tag_documents = session.load(tags, Watch) |
| 319 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 320 | + |
| 321 | + # assert tag documents |
| 322 | + |
| 323 | + self.assertEqual(3, len(tag_documents)) |
| 324 | + |
| 325 | + tag_doc = tag_documents.get("watches/fitbit") |
| 326 | + self.assertEqual("FitBit", tag_doc.name) |
| 327 | + self.assertEqual(0.855, tag_doc.accuracy) |
| 328 | + |
| 329 | + tag_doc = tag_documents.get("watches/apple") |
| 330 | + self.assertEqual("Apple", tag_doc.name) |
| 331 | + self.assertEqual(0.9, tag_doc.accuracy) |
| 332 | + |
| 333 | + tag_doc = tag_documents.get("watches/sony") |
| 334 | + self.assertEqual("Sony", tag_doc.name) |
| 335 | + self.assertEqual(0.78, tag_doc.accuracy) |
0 commit comments