11import json
22import os
3+ from copy import deepcopy
4+ from time import sleep
35from unittest import TestCase
46
7+ from cumulus_lambda_functions .lib .uds_db .db_constants import DBConstants
8+ from mdps_ds_lib .lib .aws .es_abstract import ESAbstract
9+ from mdps_ds_lib .lib .aws .es_factory import ESFactory
10+
511from cumulus_lambda_functions .lib .uds_db .granules_db_index import GranulesDbIndex
612
713
@@ -50,4 +56,255 @@ def test_02(self):
5056 query_result = granules_index .dsl_search (self .tenant , self .tenant_venue , search_dsl )
5157 print (json .dumps (query_result , indent = 4 ))
5258 # self.assertEqual(custom_metadata, expected)
53- return
59+ return
60+
61+ def test_complete (self ):
62+ """
63+ Steps:
64+ 1. Create index 1
65+ 2. Add doc 1
66+ 3. Add doc 2
67+ 4. Create index 2
68+ 5. Add doc 3
69+ 6. Update doc 1
70+ 7. make sure index 1: doc 1 is disappeared
71+ 8. Update doc 3
72+ 9. make sure index 2 : doc 3 is updated
73+ 10. Create index 3
74+ 11. Update doc 4
75+ 12. It should throw error.
76+ 13. Update doc 2. Make sure index 1 : doc 2 is removed
77+ 14. Update doc 3. Make sure index 2 : doc 3 is removed
78+
79+
80+ :return:
81+ """
82+ os .environ ['ES_URL' ] = 'https://vpc-uds-sbx-cumulus-es-qk73x5h47jwmela5nbwjte4yzq.us-west-2.es.amazonaws.com'
83+ os .environ ['ES_PORT' ] = '9200'
84+ granules_db_index = GranulesDbIndex ()
85+ es : ESAbstract = ESFactory ().get_instance ('AWS' ,
86+ index = DBConstants .collections_index ,
87+ base_url = os .getenv ('ES_URL' ),
88+ port = int (os .getenv ('ES_PORT' , '443' ))
89+ )
90+
91+ self .tenant = 'UDS_LOCAL_UNIT_TEST' # 'uds_local_test' # 'uds_sandbox'
92+ self .tenant_venue = 'UNIT' # 'DEV1' # 'dev'
93+ self .collection_name = 'UDS_UNIT_TEST_1'
94+ self .collection_version = '001'
95+ collection_id = f'URN:NASA:UNITY:{ self .tenant } :{ self .tenant_venue } :{ self .collection_name } ___{ self .collection_version } '
96+ self .custom_metadata_body1 = {
97+ 'tag' : {'type' : 'keyword' },
98+ 'c_data1' : {'type' : 'long' },
99+ }
100+ self .custom_metadata_body2 = {
101+ 'tag' : {'type' : 'keyword' },
102+ 'c_data2' : {'type' : 'long' },
103+ }
104+ self .custom_metadata_body3 = {
105+ 'tag' : {'type' : 'keyword' },
106+ 'c_data3' : {'type' : 'long' },
107+ }
108+
109+ granule_id1 = f'{ collection_id } :test_file01'
110+ granule_id2 = f'{ collection_id } :test_file02'
111+ granule_id3 = f'{ collection_id } :test_file03'
112+ granule_id4 = f'{ collection_id } :test_file04'
113+
114+ mock_feature1 = {
115+ "type" : "Feature" ,
116+ "stac_version" : "1.0.0" ,
117+ "id" : "URN:NASA:UNITY:UDS_LOCAL_TEST:DEV:KKK-09___001:test_file09" ,
118+ "properties" : {
119+ "datetime" : "2024-11-26T23:37:15.288000Z" ,
120+ "start_datetime" : "2016-01-31T18:00:00.009000Z" ,
121+ "end_datetime" : "2016-01-31T19:59:59.991000Z" ,
122+ "created" : "1970-01-01T00:00:00Z" ,
123+ "updated" : "2024-11-26T23:38:01.692000Z" ,
124+ "status" : "completed" ,
125+ "provider" : "unity" ,
126+ },
127+ "geometry" : {
128+ "type" : "Point" ,
129+ "coordinates" : [
130+ 0.0 ,
131+ 0.0
132+ ]
133+ },
134+ "links" : [
135+ {
136+ "rel" : "collection" ,
137+ "href" : "."
138+ }
139+ ],
140+ "assets" : {
141+ "test_file09.nc" : {
142+ "href" : "s3://uds-sbx-cumulus-staging/URN:NASA:UNITY:UDS_LOCAL_TEST:DEV:KKK-09___001/URN:NASA:UNITY:UDS_LOCAL_TEST:DEV:KKK-09___001:test_file09/test_file09.nc" ,
143+ "title" : "test_file09.nc" ,
144+ "description" : "size=0;checksumType=md5;checksum=00000000000000000000000000000000;" ,
145+ "file:size" : 0 ,
146+ "file:checksum" : "00000000000000000000000000000000" ,
147+ "roles" : [
148+ "data"
149+ ]
150+ },
151+ "test_file09.nc.cas" : {
152+ "href" : "s3://uds-sbx-cumulus-staging/URN:NASA:UNITY:UDS_LOCAL_TEST:DEV:KKK-09___001/URN:NASA:UNITY:UDS_LOCAL_TEST:DEV:KKK-09___001:test_file09/test_file09.nc.cas" ,
153+ "title" : "test_file09.nc.cas" ,
154+ "description" : "size=0;checksumType=md5;checksum=00000000000000000000000000000000;" ,
155+ "file:size" : 0 ,
156+ "file:checksum" : "00000000000000000000000000000000" ,
157+ "roles" : [
158+ "metadata"
159+ ]
160+ },
161+ "test_file09.nc.stac.json" : {
162+ "href" : "s3://uds-sbx-cumulus-staging/URN:NASA:UNITY:UDS_LOCAL_TEST:DEV:KKK-09___001/URN:NASA:UNITY:UDS_LOCAL_TEST:DEV:KKK-09___001:test_file09/test_file09.nc.stac.json" ,
163+ "title" : "test_file09.nc.stac.json" ,
164+ "description" : "size=0;checksumType=md5;checksum=00000000000000000000000000000000;" ,
165+ "file:size" : 0 ,
166+ "file:checksum" : "00000000000000000000000000000000" ,
167+ "roles" : [
168+ "metadata"
169+ ]
170+ },
171+ "test_file09.cmr.xml" : {
172+ "href" : "s3://uds-sbx-cumulus-staging/URN:NASA:UNITY:UDS_LOCAL_TEST:DEV:KKK-09___001/URN:NASA:UNITY:UDS_LOCAL_TEST:DEV:KKK-09___001:test_file09/test_file09.cmr.xml" ,
173+ "title" : "test_file09.cmr.xml" ,
174+ "description" : "size=1716;checksumType=md5;checksum=f842ba4e23e76ae81014a01c820b01f7;" ,
175+ "file:size" : 1716 ,
176+ "file:checksum" : "f842ba4e23e76ae81014a01c820b01f7" ,
177+ "roles" : [
178+ "metadata"
179+ ]
180+ }
181+ },
182+ "bbox" : {
183+ "type" : "envelope" ,
184+ "coordinates" : [
185+ [
186+ - 180.0 ,
187+ 90.0
188+ ],
189+ [
190+ 180.0 ,
191+ - 90.0
192+ ]
193+ ]
194+ },
195+ "stac_extensions" : [
196+ "https://stac-extensions.github.io/file/v2.1.0/schema.json"
197+ ],
198+ "collection" : "URN:NASA:UNITY:UDS_LOCAL_TEST:DEV:KKK-09___001" ,
199+ "event_time" : 1732664287722
200+ }
201+ mock_feature2 = deepcopy (mock_feature1 )
202+ mock_feature3 = deepcopy (mock_feature1 )
203+ mock_feature4 = deepcopy (mock_feature1 )
204+ mock_feature1 ['id' ] = granule_id1
205+ mock_feature2 ['id' ] = granule_id2
206+ mock_feature3 ['id' ] = granule_id3
207+ mock_feature4 ['id' ] = granule_id4
208+
209+ new_index_name1 = f'{ DBConstants .granules_index_prefix } _{ self .tenant } _{ self .tenant_venue } __v01' .lower ().strip ()
210+ new_index_name2 = f'{ DBConstants .granules_index_prefix } _{ self .tenant } _{ self .tenant_venue } __v02' .lower ().strip ()
211+ new_index_name3 = f'{ DBConstants .granules_index_prefix } _{ self .tenant } _{ self .tenant_venue } __v03' .lower ().strip ()
212+
213+ if es .has_index (new_index_name1 ):
214+ es .delete_index (new_index_name1 )
215+ es .delete_index (f'{ DBConstants .granules_index_prefix } _{ self .tenant } _{ self .tenant_venue } _perc__v01' .lower ().strip ())
216+ if es .has_index (new_index_name2 ):
217+ es .delete_index (new_index_name2 )
218+ es .delete_index (f'{ DBConstants .granules_index_prefix } _{ self .tenant } _{ self .tenant_venue } _perc__v02' .lower ().strip ())
219+ if es .has_index (new_index_name3 ):
220+ es .delete_index (new_index_name3 )
221+ es .delete_index (f'{ DBConstants .granules_index_prefix } _{ self .tenant } _{ self .tenant_venue } _perc__v03' .lower ().strip ())
222+
223+
224+ #### index v1 ####
225+ granules_db_index .create_new_index (self .tenant , self .tenant_venue , self .custom_metadata_body1 )
226+ sleep (2 )
227+ self .assertTrue (es .has_index (new_index_name1 ), f'missing { new_index_name1 } ' )
228+
229+ granules_db_index .add_entry (self .tenant , self .tenant_venue , mock_feature1 , granule_id1 )
230+ sleep (2 )
231+ check_result = es .query_by_id (granule_id1 , new_index_name1 )
232+ self .assertTrue (check_result is not None , f'granule_id1 - new_index_name1 { check_result } ' )
233+
234+ granules_db_index .add_entry (self .tenant , self .tenant_venue , mock_feature2 , granule_id2 )
235+ sleep (2 )
236+ check_result = es .query_by_id (granule_id2 , new_index_name1 )
237+ self .assertTrue (check_result is not None , f'granule_id2 - new_index_name1 { check_result } ' )
238+
239+ #### index v2 ####
240+ granules_db_index .create_new_index (self .tenant , self .tenant_venue , self .custom_metadata_body2 )
241+ sleep (2 )
242+ self .assertTrue (es .has_index (new_index_name2 ), f'missing { new_index_name2 } ' )
243+
244+ granules_db_index .add_entry (self .tenant , self .tenant_venue , mock_feature3 , granule_id3 )
245+ sleep (2 )
246+ check_result = es .query_by_id (granule_id3 , new_index_name2 )
247+ self .assertTrue (check_result is not None , f'granule_id3 - new_index_name2 { check_result } ' )
248+
249+ check_result = es .query_by_id (granule_id3 , new_index_name1 )
250+ self .assertTrue (check_result is None , f'granule_id3 - new_index_name1 is not None{ check_result } ' )
251+
252+ granules_db_index .update_entry (self .tenant , self .tenant_venue , {'archive_status' : 'cnm_s_failed' }, granule_id1 )
253+ sleep (2 )
254+ check_result = es .query_by_id (granule_id1 , new_index_name2 )
255+ self .assertTrue (check_result is not None , f'granule_id1 - new_index_name2 { check_result } ' )
256+
257+ check_result = es .query_by_id (granule_id1 , new_index_name1 )
258+ self .assertTrue (check_result is None , f'granule_id1 - new_index_name1 is not None{ check_result } ' )
259+
260+ granules_db_index .update_entry (self .tenant , self .tenant_venue , {'archive_status' : 'cnm_s_successful' }, granule_id3 )
261+ sleep (2 )
262+ check_result = es .query_by_id (granule_id3 , new_index_name2 )
263+ self .assertTrue (check_result is not None , f'granule_id3 - new_index_name2 { check_result } ' )
264+
265+ check_result = es .query_by_id (granule_id3 , new_index_name1 )
266+ self .assertTrue (check_result is None , f'granule_id3 - new_index_name1 is not None{ check_result } ' )
267+
268+ #### index v3 ####
269+ granules_db_index .create_new_index (self .tenant , self .tenant_venue , self .custom_metadata_body3 )
270+ sleep (2 )
271+ self .assertTrue (es .has_index (new_index_name3 ), f'missing { new_index_name3 } ' )
272+
273+ with self .assertRaises (ValueError ) as context :
274+ granules_db_index .update_entry (self .tenant , self .tenant_venue , {'archive_status' : 'cnm_s_failed' }, granule_id4 )
275+ sleep (2 )
276+ self .assertTrue (str (context .exception ).startswith ('unable to update' ))
277+ # TODO check error
278+ granules_db_index .update_entry (self .tenant , self .tenant_venue , {'archive_status' : 'cnm_r_failed' }, granule_id2 )
279+ sleep (2 )
280+ check_result = es .query_by_id (granule_id2 , new_index_name3 )
281+ self .assertTrue (check_result is not None , f'granule_id2 - new_index_name3 { check_result } ' )
282+
283+ check_result = es .query_by_id (granule_id2 , new_index_name1 )
284+ self .assertTrue (check_result is None , f'granule_id2 - new_index_name1 is not None{ check_result } ' )
285+
286+ check_result = es .query_by_id (granule_id2 , new_index_name2 )
287+ self .assertTrue (check_result is None , f'granule_id2 - new_index_name2 is not None{ check_result } ' )
288+
289+ granules_db_index .update_entry (self .tenant , self .tenant_venue , {'archive_status' : 'cnm_s_failed' }, granule_id3 )
290+ sleep (2 )
291+ check_result = es .query_by_id (granule_id3 , new_index_name3 )
292+ self .assertTrue (check_result is not None , f'granule_id3 - new_index_name3 { check_result } ' )
293+
294+ check_result = es .query_by_id (granule_id3 , new_index_name1 )
295+ self .assertTrue (check_result is None , f'granule_id3 - new_index_name1 is not None{ check_result } ' )
296+
297+ check_result = es .query_by_id (granule_id3 , new_index_name2 )
298+ self .assertTrue (check_result is None , f'granule_id3 - new_index_name2 is not None{ check_result } ' )
299+
300+ if es .has_index (new_index_name1 ):
301+ es .delete_index (new_index_name1 )
302+ es .delete_index (f'{ DBConstants .granules_index_prefix } _{ self .tenant } _{ self .tenant_venue } _perc__v01' .lower ().strip ())
303+ if es .has_index (new_index_name2 ):
304+ es .delete_index (new_index_name2 )
305+ es .delete_index (f'{ DBConstants .granules_index_prefix } _{ self .tenant } _{ self .tenant_venue } _perc__v02' .lower ().strip ())
306+ if es .has_index (new_index_name3 ):
307+ es .delete_index (new_index_name3 )
308+ es .delete_index (f'{ DBConstants .granules_index_prefix } _{ self .tenant } _{ self .tenant_venue } _perc__v03' .lower ().strip ())
309+
310+ return
0 commit comments