/
test_memory_index.py
729 lines (667 loc) · 32.6 KB
/
test_memory_index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# This file is part of the Open Data Cube, see https://opendatacube.org for more information
#
# Copyright (c) 2015-2024 ODC Contributors
# SPDX-License-Identifier: Apache-2.0
import datetime
from uuid import uuid4
import pytest
from datacube.cfg import ODCEnvironment
from datacube.testutils import gen_dataset_test_dag
from datacube.utils import InvalidDocException, read_documents, SimpleDocNav
from datacube import Datacube
from datacube.model import Range
def test_init_memory(in_memory_config: ODCEnvironment):
from datacube.drivers.indexes import index_cache
idxs = index_cache()
assert "default" in idxs._drivers
assert "memory" in idxs._drivers
with Datacube(env=in_memory_config, validate_connection=True) as dc:
assert (dc.index.url) == "memory"
assert dc.index.environment.index_driver == "memory"
def test_mem_user_resource(mem_index_fresh: Datacube):
# Test default user
assert mem_index_fresh.index.users.list_users() == [("local_user", "localuser", "Default user")]
# Test create_user success
mem_index_fresh.index.users.create_user("test_user_1", "password123", "odc_user", "Test1")
mem_index_fresh.index.users.create_user("test_user_2", "password123", "agdc_user", "Test2")
users = mem_index_fresh.index.users.list_users()
assert ("local_user", "localuser", "Default user") in users
assert ("odc_user", "test_user_1", "Test1") in users
assert ("agdc_user", "test_user_2", "Test2") in users
# Test create_user errors
with pytest.raises(ValueError) as e:
mem_index_fresh.index.users.create_user("test_user_2", "password123", "agdc_user", "Test2")
assert "User test_user_2 already exists" in str(e.value)
with pytest.raises(ValueError) as e:
mem_index_fresh.index.users.create_user("test_user_3", "password123", "omg_user", "Test3")
assert "omg_user is not a known role" in str(e.value)
# Test grant_role success
mem_index_fresh.index.users.grant_role("agdc_admin", "test_user_1", "test_user_2")
# Roles can be granted multiple times.
mem_index_fresh.index.users.grant_role("agdc_admin", "test_user_1", "test_user_2")
# Test grant_role errors
with pytest.raises(ValueError) as e:
mem_index_fresh.index.users.grant_role("omg_admin", "test_user_1", "test_user_2")
assert "omg_admin is not a known role" in str(e.value)
with pytest.raises(ValueError) as e:
mem_index_fresh.index.users.grant_role("odc_admin", "test_user_1", "test_user_3")
assert "test_user_3 is not a known username" in str(e.value)
# Test delete_user errors
with pytest.raises(ValueError) as e:
mem_index_fresh.index.users.delete_user("test_user_1", "test_user_2", "test_user_3")
assert "test_user_3 is not a known username" in str(e.value)
# Confirm one error means no users deleted
users = mem_index_fresh.index.users.list_users()
assert ("odc_user", "test_user_1", "Test1") in users
# Confirm delete error success
mem_index_fresh.index.users.delete_user("test_user_1", "test_user_2")
assert mem_index_fresh.index.users.list_users() == [("local_user", "localuser", "Default user")]
def test_mem_metadatatype_resource(mem_index_fresh: Datacube):
assert len(mem_index_fresh.index.metadata_types.by_id) == len(mem_index_fresh.index.metadata_types.by_name)
assert len(list(mem_index_fresh.index.metadata_types.get_all())) == 3
mdt = mem_index_fresh.index.metadata_types.get(1)
assert mdt is not None and mdt.id == 1
eo3 = mem_index_fresh.index.metadata_types.get_by_name("eo3")
assert eo3 is not None and eo3.name == "eo3"
# Verify we cannot mess with the cache
eo3.definition["description"] = "foo"
eo3.definition["dataset"]["measurements"] = ["over_here", "measurements"]
eo3_fresh = mem_index_fresh.index.metadata_types.get_by_name("eo3")
assert eo3.description != eo3_fresh.description
assert eo3.definition["dataset"]["measurements"] != eo3_fresh.definition["dataset"]["measurements"]
# Updating measurements definition is not safe
with pytest.raises(ValueError) as e:
mem_index_fresh.index.metadata_types.update(eo3)
# Updating descriptions is safe.
eo3_fresh.definition["description"] = "New description"
mem_index_fresh.index.metadata_types.update(eo3_fresh)
eo3_fresher = mem_index_fresh.index.metadata_types.get_by_name("eo3")
assert eo3_fresher.description == eo3_fresh.description
# Check get_with_fields
platform_mdts = list(mem_index_fresh.index.metadata_types.get_with_fields(["platform"]))
assert len(platform_mdts) == 3
assert eo3 in platform_mdts
ptype_mdts = list(mem_index_fresh.index.metadata_types.get_with_fields(["platform", "product_type"]))
assert len(ptype_mdts) == 2
assert eo3 not in ptype_mdts
pfam_mdts = list(mem_index_fresh.index.metadata_types.get_with_fields(["platform", "product_family"]))
pfam_mdts == [eo3]
def test_mem_product_resource(mem_index_fresh: Datacube,
extended_eo3_metadata_type_doc,
extended_eo3_product_doc,
base_eo3_product_doc
):
eo3 = mem_index_fresh.index.metadata_types.get_by_name("eo3")
# Test Empty index works as expected:
assert list(mem_index_fresh.index.products.get_with_fields(("measurements", "extent"))) == []
assert list(mem_index_fresh.index.products.search_robust()) == []
assert list(mem_index_fresh.index.products.get_with_types([eo3])) == []
assert mem_index_fresh.index.products.get_by_name("product1") is None
# Add an e03 product doc
wo_prod = mem_index_fresh.index.products.add_document(base_eo3_product_doc)
assert wo_prod is not None
assert wo_prod.name == 'ga_ls_wo_3'
assert mem_index_fresh.index.products.get_by_name("ga_ls_wo_3").name == "ga_ls_wo_3"
# Attempt to add a product without a metadata type
with pytest.raises(InvalidDocException) as e:
ls8_prod = mem_index_fresh.index.products.add_document(extended_eo3_product_doc)
# Add extended eo3 metadatatype
eo3ext = mem_index_fresh.index.metadata_types.from_doc(extended_eo3_metadata_type_doc)
mem_index_fresh.index.metadata_types.add(eo3ext)
# Add an extended eo3 product doc
ls8_prod = mem_index_fresh.index.products.add_document(extended_eo3_product_doc)
assert ls8_prod.name == 'ga_ls8c_ard_3'
assert mem_index_fresh.index.products.get_by_name("ga_ls8c_ard_3").name == 'ga_ls8c_ard_3'
assert list(mem_index_fresh.index.products.get_with_types([eo3ext])) == [ls8_prod]
# Verify we cannot mess with the cache
ls8_prod.definition["description"] = "foo"
ls8_prod.definition["measurements"][0]["name"] = "blueish"
ls8_fresh = mem_index_fresh.index.products.get_by_name("ga_ls8c_ard_3")
assert ls8_prod.description != ls8_fresh.description
assert ls8_prod.definition["measurements"][0]["name"] != ls8_fresh.definition["measurements"][0]["name"]
# Updating measurements definition is not safe
with pytest.raises(ValueError) as e:
mem_index_fresh.index.products.update(ls8_prod)
# Updating descriptions is safe.
ls8_fresh.definition["description"] = "New description"
mem_index_fresh.index.products.update(ls8_fresh)
ls8_fresher = mem_index_fresh.index.products.get_by_name("ga_ls8c_ard_3")
assert ls8_fresher.description == ls8_fresh.description
# Test get_with_fields
assert len(list(mem_index_fresh.index.products.get_with_fields(("platform", "product_family")))) == 2
assert len(list(mem_index_fresh.index.products.get_with_fields(("platform", "eo_sun_elevation")))) == 1
# Test search_robust
search_results = list(mem_index_fresh.index.products.search_robust(region_code="my_backyard"))
assert len(search_results) == 2
for prod, unmatched in search_results:
assert "region_code" in unmatched
search_results = list(mem_index_fresh.index.products.search_robust(product_family="the_simpsons"))
assert len(search_results) == 0
search_results = list(mem_index_fresh.index.products.search_robust(platform="landsat-8"))
assert len(search_results) == 2
for prod, unmatched in search_results:
if prod.name == 'ga_ls8c_ard_3':
assert unmatched == {}
else:
assert unmatched["platform"] == 'landsat-8'
# Test search_by_metadata
lds = list(mem_index_fresh.index.products.search_by_metadata({"product_family": "ard"}))
assert len(lds) == 0
lds = list(mem_index_fresh.index.products.search_by_metadata({"odc:product_family": "ard"}))
assert len(lds) == 1
lds = list(mem_index_fresh.index.products.search_by_metadata({"platform": "landsat-8"}))
assert len(lds) == 0
lds = list(mem_index_fresh.index.products.search_by_metadata({"eo:platform": "landsat-8"}))
assert len(lds) == 1
# Test delete
mem_index_fresh.index.products.delete(ls8_fresh)
assert mem_index_fresh.index.products.get_by_name("ga_ls8c_ard_3") is None
# Hand crafted tests with recent real-world eo3 examples
def test_mem_dataset_add_eo3(mem_index_eo3: Datacube,
dataset_with_lineage_doc,
datasets_with_unembedded_lineage_doc):
dc = mem_index_eo3
assert list(dc.index.datasets.get_all_dataset_ids(True)) == []
assert list(dc.index.datasets.get_all_dataset_ids(False)) == []
from datacube.index.hl import Doc2Dataset
resolver = Doc2Dataset(dc.index)
with pytest.raises(ValueError) as e:
ds, err = resolver(dataset_with_lineage_doc[0], dataset_with_lineage_doc[1])
assert "Embedded lineage not supported for eo3 metadata types" in str(e.value)
(doc_ls8, loc_ls8), (doc_wo, loc_wo) = datasets_with_unembedded_lineage_doc
assert not dc.index.datasets.has(doc_ls8["id"])
assert not dc.index.datasets.has(doc_wo["id"])
assert list(dc.index.datasets.bulk_has((doc_ls8["id"], doc_wo["id"]))) == [False, False]
ds, err = resolver(doc_ls8, loc_ls8)
assert err is None
dc.index.datasets.add(ds)
assert dc.index.datasets.has(doc_ls8["id"])
ls8_ds = dc.index.datasets.get(
doc_ls8["id"],
include_sources=True
)
assert ls8_ds is not None
ds, err = resolver(datasets_with_unembedded_lineage_doc[1][0], datasets_with_unembedded_lineage_doc[1][1])
assert err is None
dc.index.datasets.add(ds)
assert list(dc.index.datasets.bulk_has((doc_ls8["id"], doc_wo["id"]))) == [True, True]
def test_mem_ds_lineage(mem_eo3_data: tuple):
dc, ls8_id, wo_id = mem_eo3_data
wo_ds = dc.index.datasets.get(wo_id, include_sources=True)
ls8_ds = wo_ds.sources["ard"]
assert ls8_ds.id == ls8_id
wo_ds = dc.index.datasets.get(wo_ds.id, include_sources=False)
assert not wo_ds.sources
assert dc.index.datasets.bulk_get((wo_ds.id, ls8_id))
derived = list(dc.index.datasets.get_derived(ls8_id))
assert len(derived) == 1
assert derived[0].id == wo_id
assert "cloud_cover" in dc.index.products.get_field_names(ls8_ds.product)
def test_mem_ds_search_dups(mem_eo3_data: tuple):
dc, ls8_id, wo_id = mem_eo3_data
ls8_ds = dc.index.datasets.get(ls8_id)
dup_results = dc.index.datasets.search_product_duplicates(ls8_ds.product, "cloud_cover", "dataset_maturity")
assert len(dup_results) == 0
def test_mem_ds_locations(mem_eo3_data: tuple):
dc, ls8_id, wo_id = mem_eo3_data
before_test = datetime.datetime.now()
ls8ds = dc.index.datasets.get(ls8_id)
dc.index.datasets.add_location(ls8_id, "file:///test_loc_1") # Test of deprecated method
assert ls8ds.uri == dc.index.datasets.get_location(ls8_id)
assert "file:///test_loc_1" in dc.index.datasets.get_locations(ls8_id) # Test of deprecated method
assert list(dc.index.datasets.get_archived_locations(ls8_id)) == [] # Test of deprecated method
dc.index.datasets.archive_location(ls8_id, "file:///test_loc_1") # Test of deprecated method
assert "file:///test_loc_1" not in dc.index.datasets.get_locations(ls8_id) # Test of deprecated method
assert "file:///test_loc_1" in dc.index.datasets.get_archived_locations(ls8_id) # Test of deprecated method
found = False
for loc, dt in dc.index.datasets.get_archived_location_times(ls8_id): # Test of deprecated method
if loc == "file:///test_loc_1":
found = True
assert dt >= before_test
break
assert found
assert dc.index.datasets.restore_location(ls8_id, "file:///test_loc_1") # Test of deprecated method
assert "file:///test_loc_1" in dc.index.datasets.get_locations(ls8_id) # Test of deprecated method
assert list(dc.index.datasets.get_archived_locations(ls8_id)) == [] # Test of deprecated method
assert list(dc.index.datasets.get_datasets_for_location("file:///test_loc_1", "exact"))[0].id == ls8_id
assert dc.index.datasets.remove_location(ls8_id, "file:///test_loc_1") # Test of deprecated method
assert "file:///test_loc_1" not in dc.index.datasets.get_locations(ls8_id) # Test of deprecated method
assert "file:///test_loc_1" not in dc.index.datasets.get_archived_locations(ls8_id) # Test of deprecated method
assert dc.index.datasets.add_location(ls8_id, "file:///test_loc_2") # Test of deprecated method
assert "file:///test_loc_2" in dc.index.datasets.get_locations(ls8_id) # Test of deprecated method
ds = dc.index.datasets.get(ls8_id)
assert ds.uri in ds.uris # Test of deprecated property
assert dc.index.datasets.archive_location(ls8_id, "file:///test_loc_2") # Test of deprecated method
assert dc.index.datasets.remove_location(ls8_id, "file:///test_loc_2") # Test of deprecated method
assert "file:///test_loc_2" not in list(dc.index.datasets.get_locations(ls8_id)) # Test of deprecated method
assert "file:///test_loc_2" not in list(
dc.index.datasets.get_archived_locations(ls8_id) # Test of deprecated method
)
assert not dc.index.datasets.archive_location(ls8_id, "file:////not_a_valid_loc") # Test of deprecated method
assert not dc.index.datasets.remove_location(ls8_id, "file:////not_a_valid_loc") # Test of deprecated method
assert dc.index.datasets.remove_location(ls8_id, ls8ds.uri)
ls8ds = dc.index.datasets.get(ls8_id)
assert ls8ds.uri is None
assert dc.index.datasets.get_location(ls8_id) is None
def test_mem_ds_updates(mem_eo3_data: tuple):
dc, ls8_id, wo_id = mem_eo3_data
# Test updates
raw = dc.index.datasets.get(ls8_id)
# Update location only
raw._uris.append("file:///update_test_1")
updated = dc.index.datasets.update(raw)
raw = dc.index.datasets.get(ls8_id)
assert raw._uris == updated.uris
# Test bad change
raw._uris.append("file:///update_test_2")
raw.metadata_doc["properties"]["silly_sausages"] = ["weisswurst", "frankfurter"]
with pytest.raises(ValueError):
updated = dc.index.datasets.update(raw)
assert "file:///update_test_2" in raw._uris
# Make bad change ok
from datacube.utils import changes
updated = dc.index.datasets.update(raw, updates_allowed={
("properties", "silly_sausages"): changes.allow_any
})
assert "silly_sausages" in updated.metadata_doc["properties"]
raw = dc.index.datasets.get(ls8_id)
assert "silly_sausages" in raw.metadata_doc["properties"]
assert "file:///update_test_1" in raw._uris
assert "file:///update_test_2" in raw._uris
def test_mem_ds_expand_periods(mem_index_fresh: Datacube):
periods = list(mem_index_fresh.index.datasets._expand_period(
"1 day",
datetime.datetime(2016, 5, 5),
datetime.datetime(2016, 5, 8),
))
assert periods == [
Range(
datetime.datetime(2016, 5, 5, tzinfo=datetime.timezone.utc),
datetime.datetime(2016, 5, 6, tzinfo=datetime.timezone.utc)
),
Range(
datetime.datetime(2016, 5, 6, tzinfo=datetime.timezone.utc),
datetime.datetime(2016, 5, 7, tzinfo=datetime.timezone.utc)
),
Range(
datetime.datetime(2016, 5, 7, tzinfo=datetime.timezone.utc),
datetime.datetime(2016, 5, 8, tzinfo=datetime.timezone.utc)
),
]
periods = list(mem_index_fresh.index.datasets._expand_period(
"2 days",
datetime.datetime(2016, 5, 5),
datetime.datetime(2016, 5, 8),
))
assert periods == [
Range(
datetime.datetime(2016, 5, 5, tzinfo=datetime.timezone.utc),
datetime.datetime(2016, 5, 7, tzinfo=datetime.timezone.utc)
),
Range(
datetime.datetime(2016, 5, 7, tzinfo=datetime.timezone.utc),
datetime.datetime(2016, 5, 9, tzinfo=datetime.timezone.utc)
),
]
periods = list(mem_index_fresh.index.datasets._expand_period(
"1 weeks",
datetime.datetime(2016, 5, 1),
datetime.datetime(2016, 5, 25),
))
assert periods == [
Range(
datetime.datetime(2016, 5, 1, tzinfo=datetime.timezone.utc),
datetime.datetime(2016, 5, 8, tzinfo=datetime.timezone.utc)
),
Range(
datetime.datetime(2016, 5, 8, tzinfo=datetime.timezone.utc),
datetime.datetime(2016, 5, 15, tzinfo=datetime.timezone.utc)
),
Range(
datetime.datetime(2016, 5, 15, tzinfo=datetime.timezone.utc),
datetime.datetime(2016, 5, 22, tzinfo=datetime.timezone.utc)
),
Range(
datetime.datetime(2016, 5, 22, tzinfo=datetime.timezone.utc),
datetime.datetime(2016, 5, 29, tzinfo=datetime.timezone.utc)
),
]
periods = list(mem_index_fresh.index.datasets._expand_period(
"2 months",
datetime.datetime(2016, 3, 5),
datetime.datetime(2016, 12, 25),
))
assert periods == [
Range(
datetime.datetime(2016, 3, 5, tzinfo=datetime.timezone.utc),
datetime.datetime(2016, 5, 5, tzinfo=datetime.timezone.utc)
),
Range(
datetime.datetime(2016, 5, 5, tzinfo=datetime.timezone.utc),
datetime.datetime(2016, 7, 5, tzinfo=datetime.timezone.utc)
),
Range(
datetime.datetime(2016, 7, 5, tzinfo=datetime.timezone.utc),
datetime.datetime(2016, 9, 5, tzinfo=datetime.timezone.utc)
),
Range(
datetime.datetime(2016, 9, 5, tzinfo=datetime.timezone.utc),
datetime.datetime(2016, 11, 5, tzinfo=datetime.timezone.utc)
),
Range(
datetime.datetime(2016, 11, 5, tzinfo=datetime.timezone.utc),
datetime.datetime(2017, 1, 5, tzinfo=datetime.timezone.utc)
),
]
periods = list(mem_index_fresh.index.datasets._expand_period(
"2 years",
datetime.datetime(2016, 3, 5),
datetime.datetime(2019, 12, 25),
))
assert periods == [
Range(
datetime.datetime(2016, 3, 5, tzinfo=datetime.timezone.utc),
datetime.datetime(2018, 3, 5, tzinfo=datetime.timezone.utc)
),
Range(
datetime.datetime(2018, 3, 5, tzinfo=datetime.timezone.utc),
datetime.datetime(2020, 3, 5, tzinfo=datetime.timezone.utc)
),
]
def test_spatiotemporal_extent(mem_eo3_data: tuple):
dc, ls8_id, wo_id = mem_eo3_data
ls8 = dc.index.datasets.get(ls8_id)
wo = dc.index.datasets.get(wo_id)
with pytest.raises(KeyError) as e:
dc.index.datasets.temporal_extent(ids=[uuid4()])
with pytest.raises(KeyError) as e:
dc.index.datasets.get_product_time_bounds("orthentik_producked") # Test of deprecated method
# Test get_product_time_bounds
for ds in (ls8, wo):
tmin, tmax = dc.index.datasets.get_product_time_bounds(ds.product.name) # Test of deprecated method
assert (tmin is None and tmax is None) or tmin < tmax
tmin2, tmax2 = dc.index.products.temporal_extent(product=ds.product)
assert tmin2 == tmin and tmax2 == tmax
ids = [doc["id"] for prod, doc, uris in dc.index.datasets.get_all_docs_for_product(product=ds.product)]
tmin2, tmax2 = dc.index.datasets.temporal_extent(ids=ids)
assert tmin2 == tmin and tmax2 == tmax
# Spatial extent
assert dc.index.products.spatial_extent(ls8.product) is None
assert dc.index.datasets.spatial_extent([ls8_id, wo_id]) is None
def test_mem_ds_archive_purge(mem_eo3_data: tuple):
dc, ls8_id, wo_id = mem_eo3_data
# Test archiving, restoring and purging datasets
# Both datasets are not archived
all_ids = list(dc.index.datasets.get_all_dataset_ids(False))
assert ls8_id in all_ids
assert wo_id in all_ids
assert list(dc.index.datasets.get_all_dataset_ids(True)) == []
# Archive both datasets
dc.index.datasets.archive((wo_id, ls8_id))
# Both datasets ARE archived
all_ids = list(dc.index.datasets.get_all_dataset_ids(True))
assert ls8_id in all_ids
assert wo_id in all_ids
assert list(dc.index.datasets.get_all_dataset_ids(False)) == []
archived_ls_ds = dc.index.datasets.get(ls8_id)
assert archived_ls_ds.is_archived
# Purge ls8_ds and restore wo_ds
dc.index.datasets.purge((ls8_id,))
dc.index.datasets.restore((wo_id,))
active_ids = list(dc.index.datasets.get_all_dataset_ids(False))
archived_ids = list(dc.index.datasets.get_all_dataset_ids(True))
assert ls8_id not in active_ids
assert wo_id in active_ids
assert archived_ids == []
# And archive wo_ds again
dc.index.datasets.archive((wo_id,))
assert list(dc.index.datasets.get_all_dataset_ids(False)) == []
def test_mem_ds_search_and_count(mem_eo3_data: tuple):
dc, ls8_id, wo_id = mem_eo3_data
# No source_filter; no results
assert not list(dc.index.datasets.search(platform="deplatformed"))
lds = list(dc.index.datasets.search(platform='landsat-8'))
assert len(lds) == 2
assert dc.index.datasets.count(platform='landsat-8') == 2
lds = list(dc.index.datasets.search(platform='landsat-8', limit=1))
assert len(lds) == 1
lds = list(dc.index.datasets.search(source_filter={"product_family": 'ard'}))
assert len(lds) == 1
assert dc.index.datasets.count(source_filter={"product_family": 'ard'}) == 1
lds = list(dc.index.datasets.search(platform='landsat-8', source_filter={"product_family": 'ard'}))
assert len(lds) == 1
lds = list(dc.index.datasets.search(product_family='wo', source_filter={"product_family": 'ard'}))
assert len(lds) == 1
lds = list(dc.index.datasets.search(product_family='ard', source_filter={"product_family": 'ard'}))
assert len(lds) == 0
assert dc.index.datasets.count(product_family='ard', source_filter={"product_family": 'ard'}) == 0
lds = list(dc.index.datasets.search(product="ga_ls_wo_3", platform='landsat-8'))
assert len(lds) == 1
with pytest.raises(ValueError):
lds = list(dc.index.datasets.search(product="weird_whacky_product"))
with pytest.raises(ValueError):
lds = list(dc.index.datasets.search(product_family='addams'))
lds = list(dc.index.datasets.search(archived=None, platform='landsat-8'))
assert len(lds) == 2
lds = list(dc.index.datasets.search(archived=True, platform='landsat-8'))
assert len(lds) == 0
dc.index.datasets.archive([ls8_id, wo_id])
lds = list(dc.index.datasets.search(platform='landsat-8'))
assert len(lds) == 0
lds = list(dc.index.datasets.search(archived=None, platform='landsat-8'))
assert len(lds) == 2
lds = list(dc.index.datasets.search(archived=True, platform='landsat-8'))
assert len(lds) == 2
def test_mem_ds_search_and_count_by_product(mem_eo3_data: tuple):
dc, ls8_id, wo_id = mem_eo3_data
# No source_filter; no results
assert not list(dc.index.datasets.search_by_product(platform="deplatformed"))
lds = list(dc.index.datasets.search_by_product(platform='landsat-8'))
assert len(lds) == 2
for dss, product in lds:
for ds in dss:
assert ds.product.name == product.name
lds = list(dc.index.datasets.count_by_product(platform='landsat-8'))
assert len(lds) == 2
for prod, count in lds:
assert count == 1
def test_mem_ds_search_returning(mem_eo3_data: tuple):
dc, ls8_id, wo_id = mem_eo3_data
lds = list(dc.index.datasets.search_returning(
field_names=[
"platform", "id", "product_family"
],
platform='landsat-8'
))
assert len(lds) == 2
for res in lds:
assert res.platform == "landsat-8"
assert res.id in (str(ls8_id), str(wo_id))
lds = list(dc.index.datasets.search_returning(
platform='landsat-8'
))
assert len(lds) == 2
for res in lds:
assert res.platform == "landsat-8"
assert res.id in (str(ls8_id), str(wo_id))
lds = list(dc.index.datasets.search_returning(
field_names=["id", "platform"],
custom_offsets={
"cloud_cover": ["properties", "eo:cloud_cover"],
"instrument": ["properties", "eo:instrument"],
},
platform='landsat-8'
))
assert len(lds) == 2
for res in lds:
assert res.platform == "landsat-8"
assert res.id in (str(ls8_id), str(wo_id))
assert res.cloud_cover < 59.0 and res.cloud_cover > 58.9
assert res.instrument.endswith("OLI_TIRS")
lds = list(dc.index.datasets.search_returning(
custom_offsets={
"cloud_cover": ["properties", "eo:cloud_cover"],
"instrument": ["properties", "eo:instrument"],
},
platform='landsat-8'
))
for res in lds:
assert res.cloud_cover < 59.0 and res.cloud_cover > 58.9
assert res.instrument.endswith("OLI_TIRS")
with pytest.raises(AttributeError):
assert res.id in (str(ls8_id), str(wo_id))
def test_mem_ds_search_summary(mem_eo3_data: tuple):
dc, ls8_id, wo_id = mem_eo3_data
lds = list(dc.index.datasets.search_returning(platform='landsat-8'))
assert len(lds) == 2
for res in lds:
assert res.platform == "landsat-8"
assert res.id in (str(ls8_id), str(wo_id))
def test_mem_ds_search_returning_datasets_light(mem_eo3_data: tuple):
dc, ls8_id, wo_id = mem_eo3_data
lds = list(dc.index.datasets.search_returning_datasets_light(
field_names=['platform', 'id'],
platform='landsat-8'))
assert len(lds) == 2
for res in lds:
assert res.__class__.__name__ == 'DatasetLight'
assert res.platform == "landsat-8"
assert res.id in (str(ls8_id), str(wo_id))
lds = list(dc.index.datasets.search_returning_datasets_light(
archived=None,
field_names=['platform', 'id'],
platform='landsat-8'))
assert len(lds) == 2
lds = list(dc.index.datasets.search_returning_datasets_light(
archived=True,
field_names=['platform', 'id'],
platform='landsat-8'))
assert len(lds) == 0
def test_mem_ds_search_by_metadata(mem_eo3_data: tuple):
dc, ls8_id, wo_id = mem_eo3_data
lds = list(dc.index.datasets.search_by_metadata({"properties": {"product_family": "ard"}}))
assert len(lds) == 0
lds = list(dc.index.datasets.search_by_metadata({"properties": {"odc:product_family": "ard"}}))
assert len(lds) == 1
lds = list(dc.index.datasets.search_by_metadata({"properties": {"platform": "landsat-8"}}))
assert len(lds) == 0
lds = list(dc.index.datasets.search_by_metadata({"properties": {"eo:platform": "landsat-8"}}))
assert len(lds) == 2
lds = list(dc.index.datasets.search_by_metadata({"properties": {"eo:platform": "landsat-8"}}, archived=None))
assert len(lds) == 2
lds = list(dc.index.datasets.search_by_metadata({"properties": {"eo:platform": "landsat-8"}}, archived=True))
assert len(lds) == 0
dc.index.datasets.archive([ls8_id, wo_id])
lds = list(dc.index.datasets.search_by_metadata({"properties": {"eo:platform": "landsat-8"}}))
assert len(lds) == 0
lds = list(dc.index.datasets.search_by_metadata({"properties": {"eo:platform": "landsat-8"}}, archived=None))
assert len(lds) == 2
lds = list(dc.index.datasets.search_by_metadata({"properties": {"eo:platform": "landsat-8"}}, archived=True))
assert len(lds) == 2
def test_mem_ds_count_product_through_time(mem_eo3_data: tuple):
dc, ls8_id, wo_id = mem_eo3_data
lds = list(dc.index.datasets.count_by_product_through_time(
period="1 day",
time=[datetime.datetime(2016, 5, 10), datetime.datetime(2016, 5, 15)]
))
for prod, counts in lds:
for rng, count in counts:
if rng.begin == datetime.datetime(2016, 5, 12, tzinfo=datetime.timezone.utc):
assert count == 1
else:
assert count == 0
counts = list(dc.index.datasets.count_product_through_time(
period="1 month",
product="ga_ls_wo_3",
time=[datetime.datetime(2016, 4, 1), datetime.datetime(2016, 12, 1)]
))
for rng, count in counts:
if rng.begin == datetime.datetime(2016, 5, 1, tzinfo=datetime.timezone.utc):
assert count == 1
else:
assert count == 0
# Tests adapted from test_dataset_add
def test_memory_dataset_add(dataset_add_configs, mem_index_fresh: Datacube):
idx = mem_index_fresh.index
# Make sure index is empty
assert list(idx.products.get_all()) == []
for path, metadata_doc in read_documents(dataset_add_configs.metadata):
idx.metadata_types.add(idx.metadata_types.from_doc(metadata_doc))
for path, product_doc in read_documents(dataset_add_configs.products):
idx.products.add_document(product_doc)
ds_ids = set()
ds_bad_ids = set()
from datacube.index.hl import Doc2Dataset
resolver = Doc2Dataset(idx)
for path, ds_doc in read_documents(dataset_add_configs.datasets):
ds, err = resolver(ds_doc, 'file:///fake_uri')
assert err is None
ds_ids.add(ds.id)
idx.datasets.add(ds)
for path, ds_doc in read_documents(dataset_add_configs.datasets_bad1):
ds, err = resolver(ds_doc, 'file:///fake_bad_uri')
if err is not None:
ds_bad_ids.add(ds_doc["id"])
continue
ds_ids.add(ds.id)
idx.datasets.add(ds)
for path, ds_doc in read_documents(dataset_add_configs.datasets_eo3):
ds, err = resolver(ds_doc, 'file:///fake_eo3_uri')
assert err is None
ds_ids.add(ds.id)
idx.datasets.add(ds)
for id_ in ds_ids:
assert idx.datasets.has(id_)
for id_ in ds_bad_ids:
assert not idx.datasets.has(id_)
loc_matches = idx.datasets.get_datasets_for_location("file:///fake", mode="prefix")
loc_ids = [loc.id for loc in loc_matches]
for id_ in ds_ids:
assert id_ in loc_ids
ds_ = SimpleDocNav(gen_dataset_test_dag(1, force_tree=True))
assert ds_.id in ds_ids
ds_from_idx = idx.datasets.get(ds_.id, include_sources=True)
assert ds_from_idx.sources['ab'].id == ds_.sources['ab'].id
assert ds_from_idx.sources['ac'].sources["cd"].id == ds_.sources['ac'].sources['cd'].id
def test_mem_transactions(mem_index_fresh: Datacube):
trans = mem_index_fresh.index.transaction()
assert not trans.active
trans.begin()
assert trans.active
trans.commit()
assert not trans.active
trans.begin()
assert mem_index_fresh.index.thread_transaction() == trans
with pytest.raises(ValueError):
trans.begin()
trans.rollback()
assert not trans.active
assert mem_index_fresh.index.thread_transaction() is None
def test_default_clone_bulk_ops(mem_index_fresh: Datacube, index, extended_eo3_metadata_type,
ls8_eo3_product, wo_eo3_product, africa_s2_eo3_product,
ls8_eo3_dataset, ls8_eo3_dataset2,
ls8_eo3_dataset3, ls8_eo3_dataset4,
wo_eo3_dataset, africa_eo3_dataset):
mem_index_fresh.index.clone(index)
assert mem_index_fresh.index.datasets.has(africa_eo3_dataset.id)
assert mem_index_fresh.index.datasets.has(wo_eo3_dataset.id)
assert mem_index_fresh.index.datasets.has(ls8_eo3_dataset.id)
assert mem_index_fresh.index.datasets.has(ls8_eo3_dataset4.id)
def test_default_clone_bulk_ops_multiloc(
mem_index_fresh: Datacube, index, extended_eo3_metadata_type,
ls8_eo3_product, wo_eo3_product, africa_s2_eo3_product,
ls8_eo3_dataset, ls8_eo3_dataset2,
ls8_eo3_dataset3, ls8_eo3_dataset4,
wo_eo3_dataset, africa_eo3_dataset):
index.datasets.add_location(ls8_eo3_dataset.id, "file:///a/o/fish")
mem_index_fresh.index.clone(index)
assert mem_index_fresh.index.datasets.has(africa_eo3_dataset.id)
assert mem_index_fresh.index.datasets.has(wo_eo3_dataset.id)
assert mem_index_fresh.index.datasets.has(ls8_eo3_dataset.id)
assert mem_index_fresh.index.datasets.has(ls8_eo3_dataset4.id)
assert len(mem_index_fresh.index.datasets.get(ls8_eo3_dataset.id)._uris) == 2
def test_default_clone_bulk_ops_reverse(mem_eo3_data: tuple, index):
mem_idx, ls8id, woid = mem_eo3_data
index.clone(mem_idx.index)
assert index.datasets.has(ls8id)
assert index.datasets.has(woid)