44from datetime import datetime
55from pathlib import Path
66from textwrap import dedent
7+ from typing import cast
78
89import hypothesis .extra .lark as stlark
910import polars as pl
1617 BreakthroughObservation ,
1718 GeneralObservation ,
1819 RFTObservation ,
20+ SeismicObservation ,
1921 SummaryObservation ,
2022 make_observations ,
2123)
@@ -862,10 +864,13 @@ def test_that_seismic_observation_instantiates(file_context_token):
862864 ]
863865
864866
867+ @pytest .mark .usefixtures ("use_tmpdir" )
865868def test_that_non_existent_seismic_observation_file_raises_error (file_context_token ):
869+ directory = "dir"
870+ Path (directory ).mkdir ()
866871 with pytest .raises (ObservationConfigError ) as err :
867872 make_observations (
868- "dir" ,
873+ directory ,
869874 [
870875 ObservationDict (
871876 {
@@ -879,7 +884,7 @@ def test_that_non_existent_seismic_observation_file_raises_error(file_context_to
879884 shape_registry = ShapeRegistry (),
880885 )
881886
882- assert "/dir/ seismic_observations.csv) does not exist or is not accessible. " in str (
887+ assert "seismic_observations.csv' does not exist or is not accessible" in str (
883888 err .value
884889 )
885890
@@ -1180,17 +1185,27 @@ def test_that_seismic_observation_coordinate_distance_below_tolerance_raises(
11801185 )
11811186
11821187
1183- @pytest .mark .usefixtures ("use_tmpdir" )
1184- def test_that_seismic_observation_reads_boundary_file (file_context_token ):
1185- Path ("obs.csv" ).write_text (
1186- dedent (
1187- """
1188- X_UTME,Y_UTMN,OBS,OBS_ERROR,REGION
1189- 1.0,1.0,1.0,0.005,1.0
1190- """
1191- ),
1188+ def default_seismic_file_content () -> str :
1189+ return dedent (
1190+ """
1191+ X_UTME,Y_UTMN,OBS,OBS_ERROR,REGION
1192+ 1.0,1.0,1.0,0.005,1.0
1193+ """
1194+ )
1195+
1196+
1197+ def write_default_seismic_file_content (
1198+ filename = "horizon--amplitude_full_min_depth--20250101_20240101.csv" ,
1199+ ):
1200+ Path (filename ).write_text (
1201+ default_seismic_file_content (),
11921202 encoding = "utf8" ,
11931203 )
1204+
1205+
1206+ @pytest .mark .usefixtures ("use_tmpdir" )
1207+ def test_that_seismic_observation_reads_boundary_file (file_context_token ):
1208+ write_default_seismic_file_content ("obs.csv" )
11941209 Path ("boundary.pol" ).write_text (
11951210 dedent (
11961211 """
@@ -1249,15 +1264,9 @@ def test_that_non_existent_boundary_seismic_observation_file_raises_error(
12491264):
12501265 os .makedirs ("directory/right/path" , exist_ok = True )
12511266 os .makedirs ("directory/wrong/path" , exist_ok = True )
1252- Path ("directory/obs.csv" ).write_text (
1253- dedent (
1254- """
1255- X_UTME,Y_UTMN,OBS,OBS_ERROR,REGION
1256- 1.0,1.0,1.0,0.005,1.0
1257- """
1258- ),
1259- encoding = "utf8" ,
1260- )
1267+
1268+ write_default_seismic_file_content ("directory/obs.csv" )
1269+
12611270 Path ("directory/right/path/bound.pol" ).write_text (
12621271 "Unexpected file location" ,
12631272 encoding = "utf8" ,
@@ -1283,3 +1292,97 @@ def test_that_non_existent_boundary_seismic_observation_file_raises_error(
12831292 "/directory/wrong/path/bound.pol) does not exist or is not accessible."
12841293 in str (err .value )
12851294 )
1295+
1296+
1297+ @pytest .mark .usefixtures ("use_tmpdir" )
1298+ def test_that_seismic_observation_filenames_can_be_glob_pattern (file_context_token ):
1299+ filename0 = "surface--amplitude_far_mean_depth--20190701_20180101.csv"
1300+ filename1 = "surface--amplitude_full_min_depth--20190901_20180101.csv"
1301+ filename2 = "surface--amplitude_full_min_depth--20180701_20180101.csv"
1302+ filename3 = ".surface--amplitude_full_mean_depth--20190701_20180101.csv.yml"
1303+
1304+ directory = "dir1/dir2/.."
1305+ os .makedirs (directory , exist_ok = True )
1306+
1307+ for filename in [filename0 , filename1 , filename2 , filename3 ]:
1308+ write_default_seismic_file_content (f"{ directory } /{ filename } " )
1309+
1310+ def make_observations_with_pattern (
1311+ pattern : str , directory : str = directory
1312+ ) -> list [SeismicObservation ]:
1313+ shape_registry = ShapeRegistry ()
1314+ obs = make_observations (
1315+ "" ,
1316+ [
1317+ ObservationDict (
1318+ {
1319+ "type" : ObservationType .SEISMIC ,
1320+ "OBS_FILE" : f"{ directory } /{ pattern } " ,
1321+ },
1322+ context = file_context_token (obs_type = "SEISMIC_OBSERVATION" ),
1323+ )
1324+ ],
1325+ shape_registry = shape_registry ,
1326+ )
1327+ return [cast (SeismicObservation , o ) for o in obs ]
1328+
1329+ p1 = "surface--amplitude_*_*_depth--20190[1-9]01_20180101.csv"
1330+ obs = make_observations_with_pattern (p1 )
1331+ assert len (obs ) == 2
1332+ assert sorted ([o .filepath for o in obs ]) == sorted (
1333+ [
1334+ Path (f"{ directory } /{ filename0 } " ),
1335+ Path (f"{ directory } /{ filename1 } " ),
1336+ ]
1337+ )
1338+
1339+ p2 = "surface*"
1340+ obs = make_observations_with_pattern (p2 )
1341+ assert len (obs ) == 3
1342+ assert sorted ([o .filepath for o in obs ]) == sorted (
1343+ [
1344+ Path (f"{ directory } /{ filename0 } " ),
1345+ Path (f"{ directory } /{ filename1 } " ),
1346+ Path (f"{ directory } /{ filename2 } " ),
1347+ ]
1348+ )
1349+
1350+ p3 = p1 [:- 4 ]
1351+ with pytest .raises (ObservationConfigError ) as err :
1352+ make_observations_with_pattern (p3 )
1353+ assert f"No files matching pattern '{ p3 } ' found in '{ directory } '" in str (err .value )
1354+
1355+ with pytest .raises (ObservationConfigError ) as err :
1356+ make_observations_with_pattern (p1 , directory = "ufo" )
1357+ assert "ufo' does not exist or is not accessible" in str (err .value )
1358+
1359+
1360+ @pytest .mark .usefixtures ("use_tmpdir" )
1361+ def test_that_filepath_can_have_literal_metacharacters (file_context_token ):
1362+ def make_observations_with_pattern (pattern : str ) -> list [SeismicObservation ]:
1363+ shape_registry = ShapeRegistry ()
1364+ obs = make_observations (
1365+ "" ,
1366+ [
1367+ ObservationDict (
1368+ {
1369+ "type" : ObservationType .SEISMIC ,
1370+ "OBS_FILE" : pattern ,
1371+ },
1372+ context = file_context_token (obs_type = "SEISMIC_OBSERVATION" ),
1373+ )
1374+ ],
1375+ shape_registry = shape_registry ,
1376+ )
1377+ return [cast (SeismicObservation , o ) for o in obs ]
1378+
1379+ path_with_literal_wildcard = r"test*.csv"
1380+ path_fitting_to_wildcard = "test123.csv"
1381+ write_default_seismic_file_content (path_with_literal_wildcard )
1382+ write_default_seismic_file_content (path_fitting_to_wildcard )
1383+
1384+ wildcard_pattern = "test**"
1385+ literal_pattern = "test[*]*"
1386+
1387+ assert len (make_observations_with_pattern (pattern = wildcard_pattern )) == 2
1388+ assert len (make_observations_with_pattern (pattern = literal_pattern )) == 1
0 commit comments