| 1 |
""" |
| 2 |
Unit-ish tests for our SIAP2 infrastructure. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
import functools |
| 12 |
|
| 13 |
from gavo.helpers import testhelpers |
| 14 |
|
| 15 |
from gavo import api |
| 16 |
from gavo import rscdef |
| 17 |
from gavo.grammars import fitsprodgrammar |
| 18 |
from gavo.helpers import trialhelpers |
| 19 |
from gavo.protocols import siap |
| 20 |
|
| 21 |
import tresc |
| 22 |
|
| 23 |
|
| 24 |
@functools.cache |
| 25 |
def _getTestTable(): |
| 26 |
td = api.parseFromString(api.TableDef, |
| 27 |
"""<table id="sia2"> |
| 28 |
<FEED source="//obscore#obscore-columns"/> |
| 29 |
</table>""") |
| 30 |
td.parent = testhelpers.getTestRD() |
| 31 |
td.setMetaParent(testhelpers.getTestRD()) |
| 32 |
return td |
| 33 |
|
| 34 |
|
| 35 |
def _getRawdictFor(fitsPath): |
| 36 |
"""returns fitsPath as parsed by a FITSProdGrammar with minimal |
| 37 |
products#define keys added in artificially. |
| 38 |
""" |
| 39 |
row = next(iter(_getRawdictFor.grammar.parse(fitsPath))) |
| 40 |
row.update({ |
| 41 |
"prodtblAccref": fitsPath, |
| 42 |
"prodtblPath": fitsPath, |
| 43 |
"prodtblPreview": None, |
| 44 |
"prodtblFsize": 65536, |
| 45 |
"prodtblTable": "test.sia2", |
| 46 |
"prodtblMime": "application/fits",}) |
| 47 |
return row |
| 48 |
_getRawdictFor.grammar = api.makeStruct(fitsprodgrammar.FITSProdGrammar) |
| 49 |
|
| 50 |
|
| 51 |
def _getMappedMetaRow(applyStuff, fitsPath="test_data/ex.fits"): |
| 52 |
rmd = api.parseFromString(rscdef.RowmakerDef, |
| 53 |
f"""<rowmaker><apply procDef="//siap2#setMeta" name="romp"> |
| 54 |
<bind name="obs_collection">"Testers"</bind> |
| 55 |
{applyStuff} |
| 56 |
</apply> |
| 57 |
<apply procDef="//siap2#getBandFromFilter"/></rowmaker>""") |
| 58 |
mapper = rmd.compileForTableDef(_getTestTable()) |
| 59 |
return mapper(_getRawdictFor(fitsPath), _getTestTable()) |
| 60 |
|
| 61 |
|
| 62 |
class SetMetaTest(testhelpers.VerboseTest): |
| 63 |
def testCollectionRequired(self): |
| 64 |
self.assertRaisesWithMsg(api.StructureError, |
| 65 |
"At IO:'<rowmaker><apply procDef=\"//siap2#setMeta\" name=\"romp...', (1, 56): Parameter obs_collection is not defaulted in romp and thus must be bound.", |
| 66 |
api.parseFromString, |
| 67 |
(rscdef.RowmakerDef, |
| 68 |
"""<rowmaker><apply procDef="//siap2#setMeta" name="romp"/></rowmaker""")) |
| 69 |
|
| 70 |
def testSetMetaDefaults(self): |
| 71 |
row = _getMappedMetaRow("") |
| 72 |
self.assertEqual(row["source_table"], "test.sia2") |
| 73 |
self.assertEqual(row["dataproduct_type"], "image") |
| 74 |
self.assertEqual(row["t_xel"], 1) |
| 75 |
self.assertEqual(row["obs_publisher_did"], |
| 76 |
"ivo://x-testing/~?test_data/ex.fits") |
| 77 |
self.assertEqual(row["o_ucd"], "phot.count") |
| 78 |
self.assertEqual(row["target_class"], None) |
| 79 |
self.assertEqual(row["instrument_name"], None) |
| 80 |
self.assertEqual(row["facility_name"], |
| 81 |
"Markus' Proving Grounds") |
| 82 |
self.assertEqual(row["access_estsize"], 64) |
| 83 |
self.assertEqual(row["access_url"], "test_data/ex.fits") |
| 84 |
|
| 85 |
def testObsDateComputation(self): |
| 86 |
row = _getMappedMetaRow( |
| 87 |
'<bind name="dateObs">23000</bind>' |
| 88 |
'<bind name="t_exptime">43200</bind>') |
| 89 |
self.assertEqual(row["t_min"], 22999.75) |
| 90 |
self.assertEqual(row["t_max"], 23000.25) |
| 91 |
|
| 92 |
def testObsDateNoOverride(self): |
| 93 |
row = _getMappedMetaRow( |
| 94 |
'<bind name="dateObs">23000</bind>' |
| 95 |
'<bind name="t_exptime">43200</bind>' |
| 96 |
'<bind name="t_min">22900</bind>') |
| 97 |
self.assertEqual(row["t_min"], 22900) |
| 98 |
self.assertEqual(row["t_max"], 23000.25) |
| 99 |
|
| 100 |
def testBandMapping(self): |
| 101 |
row = _getMappedMetaRow( |
| 102 |
'<bind name="bandpassId">"Johnson B"</bind>') |
| 103 |
self.assertEqual(row["em_min"], 3.7e-7) |
| 104 |
self.assertEqual(row["em_max"], 5.5e-7) |
| 105 |
|
| 106 |
def testBadBandMapping(self): |
| 107 |
row = _getMappedMetaRow( |
| 108 |
'<bind name="bandpassId">"Jacobson B"</bind>') |
| 109 |
self.assertEqual(row["em_min"], None) |
| 110 |
self.assertEqual(row["em_max"], None) |
| 111 |
|
| 112 |
|
| 113 |
class ComputePGSTest(testhelpers.VerboseTest): |
| 114 |
def testCoverageComputation(self): |
| 115 |
rmd = api.parseFromString(rscdef.RowmakerDef, |
| 116 |
"""<rowmaker><apply procDef="//siap2#computePGS" name="romp"> |
| 117 |
</apply></rowmaker>""") |
| 118 |
mapper = rmd.compileForTableDef(_getTestTable()) |
| 119 |
row = mapper(_getRawdictFor("test_data/ex.fits"), _getTestTable()) |
| 120 |
self.assertAlmostEqual(row["s_ra"], 168.2454770094) |
| 121 |
self.assertAlmostEqual(row["s_resolution"], 1.0164694847) |
| 122 |
self.assertAlmostEqual(row["s_pixel_scale"], 1.0164694847) |
| 123 |
self.assertAlmostEqual(row["s_xel1"], 12) |
| 124 |
self.assertAlmostEqual(row["s_fov"], 0.00695060466) |
| 125 |
self.assertAlmostEqual(row["s_region"].asCooPairs()[0][0], 168.2470850301) |
| 126 |
|
| 127 |
def testNoWCSDefault(self): |
| 128 |
rmd = api.parseFromString(rscdef.RowmakerDef, |
| 129 |
"""<rowmaker><apply procDef="//siap2#computePGS" name="romp"> |
| 130 |
</apply></rowmaker>""") |
| 131 |
mapper = rmd.compileForTableDef(_getTestTable()) |
| 132 |
inRow = _getRawdictFor("test_data/ex.fits") |
| 133 |
del inRow["CD1_1"] |
| 134 |
self.assertRaisesWithMsg(api.ValidationError, |
| 135 |
"Field romp: While executing romp in <rowmaker without id>: No WCS information", |
| 136 |
mapper, |
| 137 |
(inRow, _getTestTable())) |
| 138 |
|
| 139 |
def testNoWCSIgnored(self): |
| 140 |
rmd = api.parseFromString(rscdef.RowmakerDef, |
| 141 |
"""<rowmaker><apply procDef="//siap2#computePGS" name="romp"> |
| 142 |
<bind key="missingIsError">False</bind> |
| 143 |
</apply></rowmaker>""") |
| 144 |
mapper = rmd.compileForTableDef(_getTestTable()) |
| 145 |
inRow = _getRawdictFor("test_data/ex.fits") |
| 146 |
del inRow["CD1_1"] |
| 147 |
row = mapper(inRow, _getTestTable()) |
| 148 |
self.assertEqual(row["s_ra"], None) |
| 149 |
self.assertEqual(row["s_xel1"], 12) |
| 150 |
|
| 151 |
|
| 152 |
class _SIAP2ImportedTable(tresc.RDDataResource): |
| 153 |
rdName = "data/siap2test" |
| 154 |
dataId = "import" |
| 155 |
|
| 156 |
|
| 157 |
class SIAP2SingleTableTest(testhelpers.VerboseTest): |
| 158 |
resources = [("data", _SIAP2ImportedTable())] |
| 159 |
|
| 160 |
def _doQuery(self, params): |
| 161 |
return trialhelpers.runSvcWith( |
| 162 |
self.data.tableDef.rd.getById("svc"), |
| 163 |
"siap2.xml", |
| 164 |
params).getPrimaryTable() |
| 165 |
|
| 166 |
def testServiceNoParams(self): |
| 167 |
tab = self._doQuery({}) |
| 168 |
row = testhelpers.pickSingle(tab.rows) |
| 169 |
nones = {k for k,v in row.items() if v is None} |
| 170 |
|
| 171 |
self.assertEqual(nones, {'target_class', 'em_ucd', 't_resolution', |
| 172 |
'pol_states', 'obs_creator_did', 'target_name', 'em_max', |
| 173 |
'instrument_name', 'pol_xel', 'em_min', 'em_res_power'}) |
| 174 |
self.assertEqual(row["calib_level"], 2) |
| 175 |
self.assertEqual(row["extracolumn"], 5) |
| 176 |
self.assertEqual(row["access_format"], "application/fits") |
| 177 |
# this is un-value-mapped at this point; the full URI is built during |
| 178 |
# serialisation |
| 179 |
self.assertEqual(row["access_url"], |
| 180 |
"data/ex.fits") |
| 181 |
self.assertEqual(row['obs_publisher_did'], |
| 182 |
'ivo://x-testing/~?data/ex.fits') |
| 183 |
# the next assertion we should kick when obs_id is sanitised. |
| 184 |
self.assertEqual(row['obs_id'], 'ivo://x-testing/~?data/ex.fits') |
| 185 |
|
| 186 |
self.assertEqual(row["s_xel1"], 12) |
| 187 |
self.assertAlmostEqual(row["s_ra"], 168.2454770094) |
| 188 |
self.assertAlmostEqual(row["s_pixel_scale"], 1.016469484705153) |
| 189 |
|
| 190 |
self.assertEqual(row["bandpass_id"], "Ks") |
| 191 |
|
| 192 |
def testParamOverNullColumn(self): |
| 193 |
tab = self._doQuery({"TARGET": [" "]}) |
| 194 |
self.assertEqual(len(tab.rows), 0) |
| 195 |
|
| 196 |
|
| 197 |
class SIAP2GeometryStringTest(testhelpers.VerboseTest): |
| 198 |
def testEmpty(self): |
| 199 |
self.assertRaisesWithMsg(api.ValidationError, |
| 200 |
"Field POS: Invalid SIAPv2 geometry: '' (expected a SIAPv2 shape name)", |
| 201 |
siap.parseSIAP2Geometry, |
| 202 |
("",)) |
| 203 |
|
| 204 |
def testBadShape(self): |
| 205 |
self.assertRaisesWithMsg(api.ValidationError, |
| 206 |
"Field POS: Invalid SIAPv2 geometry: 'Trash 13 14 1 4'" |
| 207 |
" (expected a SIAPv2 shape name)", |
| 208 |
siap.parseSIAP2Geometry, |
| 209 |
("Trash 13 14 1 4",)) |
| 210 |
|
| 211 |
def testBadCoo(self): |
| 212 |
self.assertRaisesWithMsg(api.ValidationError, |
| 213 |
"Field POS: Invalid SIAPv2 coordinates: 'depp 12 13'" |
| 214 |
" (bad floating point literal)", |
| 215 |
siap.parseSIAP2Geometry, |
| 216 |
("CIRCLE depp 12 13",)) |
| 217 |
|
| 218 |
def testGoodCircle(self): |
| 219 |
res = siap.parseSIAP2Geometry("CIRCLE 143 82 13") |
| 220 |
self.assertEqual(res.asSTCS("Unknown"), |
| 221 |
"Circle Unknown 143. 82. 13.") |
| 222 |
|
| 223 |
def testBadCircle(self): |
| 224 |
self.assertRaisesWithMsg(api.ValidationError, |
| 225 |
"Field POS: Invalid SIAPv2 CIRCLE: 'CIRCLE 12 13'" |
| 226 |
" (need exactly three numbers)", |
| 227 |
siap.parseSIAP2Geometry, |
| 228 |
("CIRCLE 12 13",)) |
| 229 |
|
| 230 |
def testGoodRange(self): # as if there were such a thing |
| 231 |
res = siap.parseSIAP2Geometry("RANGE 345 355 -13 13") |
| 232 |
self.assertEqual(res.asSTCS("Unknown"), |
| 233 |
"PositionInterval Unknown 345. -13. 355. 13.") |
| 234 |
|
| 235 |
def testBadRange1(self): |
| 236 |
self.assertRaisesWithMsg(api.ValidationError, |
| 237 |
"Field POS: Invalid SIAPv2 RANGE: 'RANGE 345 355 -13'" |
| 238 |
" (need exactly four numbers)", |
| 239 |
siap.parseSIAP2Geometry, |
| 240 |
("RANGE 345 355 -13",)) |
| 241 |
|
| 242 |
def testBadRange2(self): |
| 243 |
self.assertRaisesWithMsg(api.ValidationError, |
| 244 |
"Field POS: Invalid SIAPv2 RANGE: 'RANGE 345 355 13 -13'" |
| 245 |
" (lower limits must be smaller than upper limits)", |
| 246 |
siap.parseSIAP2Geometry, |
| 247 |
("RANGE 345 355 13 -13",)) |
| 248 |
|
| 249 |
def testGoodPolygon(self): # as if there were such a thing |
| 250 |
res = siap.parseSIAP2Geometry("POLYGON 12 13 34 -34 35 12") |
| 251 |
self.assertEqual(res.asSTCS("Unknown"), |
| 252 |
"Polygon Unknown 12. 13. 34. -34. 35. 12.") |
| 253 |
|
| 254 |
def testBadPolygon(self): |
| 255 |
self.assertRaisesWithMsg(api.ValidationError, |
| 256 |
"Field POS: Invalid SIAPv2 POLYGON: '12 13 34 -34 35 1...' (need" |
| 257 |
" more than three coordinate *pairs*)", |
| 258 |
siap.parseSIAP2Geometry, |
| 259 |
("POLYGON 12 13 34 -34 35 12 22.3290032",)) |
| 260 |
|
| 261 |
|
| 262 |
class SIAP2ServiceTest(testhelpers.VerboseTest): |
| 263 |
resources = [("data", tresc.siapTestTable), |
| 264 |
# we want spectra in the table so we can make sure they don't come back |
| 265 |
# in a query below. |
| 266 |
("spectra", tresc.ssaTestTable), |
| 267 |
("obscore", tresc.obscoreTable)] |
| 268 |
|
| 269 |
def _doQuery(self, params): |
| 270 |
return trialhelpers.runSvcWith( |
| 271 |
api.resolveCrossId("//siap2#sitewide"), |
| 272 |
"siap2.xml", |
| 273 |
params |
| 274 |
).getPrimaryTable() |
| 275 |
|
| 276 |
def testBasicCooQuery(self): |
| 277 |
res = self._doQuery({"POS": ["CIRCLE 4 44 0.5"]}) |
| 278 |
row = testhelpers.pickSingle(res.rows) |
| 279 |
self.assertEqual(row["access_estsize"], 81) |
| 280 |
self.assertEqual(row["access_url"], |
| 281 |
"image/(1, 45)/(4.1, 1.1)") |
| 282 |
for info in res.getMeta("info"): |
| 283 |
if info.infoName=="queryPars": |
| 284 |
self.assertEqual(info.getContent(), |
| 285 |
"{'pos0': <pgsphere Circle Unknown 4. 44. 0.5>}") |
| 286 |
self.assertEqual(info.infoValue, |
| 287 |
"(s_region &&%(pos0)s) AND (dataproduct_type in ('image', 'cube'))") |
| 288 |
break |
| 289 |
|
| 290 |
def testDualCooQuery(self): |
| 291 |
res = self._doQuery({"POS": ["CIRCLE 4 44 0.5", "RANGE 250 260 89 90"]}) |
| 292 |
for info in res.getMeta("info"): |
| 293 |
if info.infoName=="queryPars": |
| 294 |
self.assertEqual(info.infoValue, |
| 295 |
'((s_region &&%(pos0)s) OR (s_region &&%(pos1)s))' |
| 296 |
" AND (dataproduct_type in ('image', 'cube'))") |
| 297 |
self.assertEqual(info.getContent(), |
| 298 |
"{'pos0': <pgsphere Circle Unknown 4. 44. 0.5>, 'pos1':" |
| 299 |
" <pgsphere\nPositionInterval Unknown 250. 89. 260. 90.>}") |
| 300 |
break |
| 301 |
self.assertEqual(len(res.rows), 2) |
| 302 |
|
| 303 |
def testBANDQuery(self): |
| 304 |
res = self._doQuery({"BAND": ["-Inf 0.5", "40 60", "165 +Inf"]}) |
| 305 |
self.assertEqual(len(res.rows), 5) |
| 306 |
for info in res.getMeta("info"): |
| 307 |
if info.infoName=="queryPars": |
| 308 |
self.assertEqual(info.infoValue, |
| 309 |
'((%(BAND1)s >= em_min AND em_max >= %(BAND0)s)' |
| 310 |
' OR (%(BAND3)s >= em_min AND em_max >= %(BAND2)s)' |
| 311 |
' OR (%(BAND5)s >= em_min AND em_max >= %(BAND4)s))' |
| 312 |
" AND (dataproduct_type in ('image', 'cube'))") |
| 313 |
|
| 314 |
def testBadBANDQuery(self): |
| 315 |
self.assertRaisesWithMsg(api.ValidationError, |
| 316 |
"Field BAND: 'Random junk' is not a valid literal for BAND", |
| 317 |
self._doQuery, |
| 318 |
({"BAND": ["Random junk"]},)) |
| 319 |
|
| 320 |
def testCombinedQuery(self): |
| 321 |
self.maxDiff = None |
| 322 |
res = self._doQuery({"BAND": ["-Inf 0.5", "40 60"], |
| 323 |
"POS": ["CIRCLE 4 44 0.5", "RANGE 250 260 89 90"]}) |
| 324 |
for info in res.getMeta("info"): |
| 325 |
if info.infoName=="queryPars": |
| 326 |
self.assertEqual(info.infoValue, |
| 327 |
'((s_region &&%(pos0)s) OR (s_region &&%(pos1)s)) AND' |
| 328 |
' ((%(BAND1)s >= em_min AND em_max >= %(BAND0)s)' |
| 329 |
' OR (%(BAND3)s >= em_min AND em_max >= %(BAND2)s))' |
| 330 |
" AND (dataproduct_type in ('image', 'cube'))") |
| 331 |
self.assertEqual(len(res.rows), 1) |
| 332 |
|
| 333 |
def testDPConstraint(self): |
| 334 |
res = self._doQuery({"INSTRUMENT": ["DaCHS test suite"]}) |
| 335 |
# this would return the ssaptest spectra if not for the empty constraint |
| 336 |
self.assertEqual(len(res.rows), 0) |
| 337 |
# just make sure ssaptest actually is in ivoa.obscore |
| 338 |
spectra = list(self.spectra.connection.query( |
| 339 |
"SELECT * FROM ivoa.obscore" |
| 340 |
" WHERE instrument_name='DaCHS test suite'")) |
| 341 |
if len(spectra)!=6: |
| 342 |
import pdb;pdb.Pdb(nosigint=True).set_trace() |
| 343 |
self.assertEqual(len(spectra), 6) |
| 344 |
|
| 345 |
def testDPOverride(self): |
| 346 |
res = self._doQuery({"INSTRUMENT": ["cube"]}) |
| 347 |
# TODO: Add some cubes at some point |
| 348 |
self.assertEqual(len(res.rows), 0) |
| 349 |
|
| 350 |
def testFOV(self): |
| 351 |
res = self._doQuery({"FOV": ["1 2.5", "11 +Inf"]}) |
| 352 |
self.assertEqual(len(res.rows), 6) |
| 353 |
|
| 354 |
def testTIME(self): |
| 355 |
res = self._doQuery({"TIME": ["-Inf 55200", "55300.5 55302.3"]}) |
| 356 |
self.assertEqual(len(res.rows), 2) |
| 357 |
|
| 358 |
def testPOLPositive(self): |
| 359 |
res = self._doQuery({"POL": ["RR", "LL"]}) |
| 360 |
self.assertEqual(len(res.rows), 9) |
| 361 |
|
| 362 |
def testPOLNegative(self): |
| 363 |
res = self._doQuery({"POL": ["I", "Q", "U", "LR", "X"]}) |
| 364 |
self.assertEqual(len(res.rows), 0) |
| 365 |
|
| 366 |
|
| 367 |
if __name__=="__main__": |
| 368 |
testhelpers.main(ComputePGSTest) |