()
| 266 | |
| 267 | |
| 268 | async def main(): |
| 269 | await setup() |
| 270 | |
| 271 | # save and load objects! |
| 272 | |
| 273 | tokyo = WeatherLocation("Asia", "Tokyo") |
| 274 | newyork = WeatherLocation("North America", "New York") |
| 275 | toronto = WeatherLocation("North America", "Toronto") |
| 276 | london = WeatherLocation("Europe", "London") |
| 277 | dublin = WeatherLocation("Europe", "Dublin") |
| 278 | brasilia = WeatherLocation("South America", "Brasila") |
| 279 | quito = WeatherLocation("South America", "Quito") |
| 280 | |
| 281 | tokyo.reports.append(Report(80.0)) |
| 282 | newyork.reports.append(Report(75)) |
| 283 | quito.reports.append(Report(85)) |
| 284 | |
| 285 | async with Session() as sess: |
| 286 | sess.add_all( |
| 287 | [tokyo, newyork, toronto, london, dublin, brasilia, quito] |
| 288 | ) |
| 289 | |
| 290 | await sess.commit() |
| 291 | |
| 292 | t = await sess.get( |
| 293 | WeatherLocation, |
| 294 | tokyo.id, |
| 295 | options=[immediateload(WeatherLocation.reports)], |
| 296 | ) |
| 297 | assert t.city == tokyo.city |
| 298 | assert t.reports[0].temperature == 80.0 |
| 299 | |
| 300 | # select across shards |
| 301 | asia_and_europe = ( |
| 302 | await sess.execute( |
| 303 | select(WeatherLocation).filter( |
| 304 | WeatherLocation.continent.in_(["Europe", "Asia"]) |
| 305 | ) |
| 306 | ) |
| 307 | ).scalars() |
| 308 | |
| 309 | assert {c.city for c in asia_and_europe} == { |
| 310 | "Tokyo", |
| 311 | "London", |
| 312 | "Dublin", |
| 313 | } |
| 314 | |
| 315 | # optionally set a shard id for the query and all related loaders |
| 316 | north_american_cities_w_t = ( |
| 317 | await sess.execute( |
| 318 | select(WeatherLocation) |
| 319 | .filter(WeatherLocation.city.startswith("T")) |
| 320 | .options(set_shard_id("north_america")) |
| 321 | ) |
| 322 | ).scalars() |
| 323 | |
| 324 | # Tokyo not included since not in the north_america shard |
| 325 | assert {c.city for c in north_american_cities_w_t} == { |
no test coverage detected