| 1 |
""" |
| 2 |
Tests for ADQL user defined functions and Region expressions. |
| 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 |
from urllib import parse |
| 12 |
import unittest |
| 13 |
|
| 14 |
from gavo.helpers import testhelpers |
| 15 |
|
| 16 |
from gavo import adql |
| 17 |
from gavo import base |
| 18 |
from gavo import rscdesc #noflake: for registration |
| 19 |
from gavo import utils |
| 20 |
from gavo.protocols import adqlglue |
| 21 |
from gavo.protocols import simbadinterface #noflake: for ufunc registration |
| 22 |
from gavo.adql import nodes |
| 23 |
from gavo.adql import ufunctions |
| 24 |
|
| 25 |
import adqltest |
| 26 |
import tresc |
| 27 |
|
| 28 |
|
| 29 |
# we need to open a database connection here to have the DB initialisation |
| 30 |
# update the ufuncs list |
| 31 |
_conn = base.getDBConnection("feed") |
| 32 |
_conn.close() |
| 33 |
del _conn |
| 34 |
|
| 35 |
|
| 36 |
class BasicTest(unittest.TestCase): |
| 37 |
def testRaising(self): |
| 38 |
self.assertRaises(adql.UfuncError, adql.parseToTree, |
| 39 |
"SELECT x FROM y WHERE gavo_foo(8)=7") |
| 40 |
|
| 41 |
def testFlattening(self): |
| 42 |
self.assertEqual( |
| 43 |
adql.parseToTree("SELECT x FROM y WHERE 1=gavo_match('x.*', frob)" |
| 44 |
).flatten(), |
| 45 |
"SELECT x FROM y WHERE 1 = (CASE WHEN frob ~ 'x.*' THEN 1 ELSE 0 END)") |
| 46 |
|
| 47 |
|
| 48 |
class _UfuncDefinition(testhelpers.TestResource): |
| 49 |
def make(self, nodeps): |
| 50 |
@adql.userFunction("gavo_testingXXX", |
| 51 |
"(x INTEGER) -> INTEGER", |
| 52 |
""" |
| 53 |
This function returns its argument decreased by one. |
| 54 |
|
| 55 |
This is the end. |
| 56 |
""", |
| 57 |
polymorphism=[("(x REAL, y INTEGER) -> REAL", |
| 58 |
"This is an alternative function that does about the same thing")]) |
| 59 |
def _f1(args): |
| 60 |
if len(args)!=1: |
| 61 |
raise adql.UfuncError("gavo_testingXXX takes only a single argument") |
| 62 |
return "(%s+1)"%nodes.flatten(args[0]) |
| 63 |
|
| 64 |
@adql.userFunction("gavo_testingYYY", |
| 65 |
"(x DOUBLE PRECISION) -> DOUBLE PRECISION", |
| 66 |
"This function will not work (since it's not defined in the DB)") |
| 67 |
def _f2(args): |
| 68 |
return None |
| 69 |
|
| 70 |
def clean(self, ignored): |
| 71 |
del ufunctions.UFUNC_REGISTRY["GAVO_TESTINGXXX"] |
| 72 |
del ufunctions.UFUNC_REGISTRY["GAVO_TESTINGYYY"] |
| 73 |
|
| 74 |
|
| 75 |
_ufuncDefinition = _UfuncDefinition() |
| 76 |
|
| 77 |
class UfuncDefTest(testhelpers.VerboseTest): |
| 78 |
resources = [("ufunc_defined", _ufuncDefinition), |
| 79 |
("adqlTestTable", adqltest.adqlTestTable), |
| 80 |
("querier", adqltest.adqlQuerier)] |
| 81 |
|
| 82 |
def testUfuncMeta(self): |
| 83 |
f = ufunctions.UFUNC_REGISTRY["GAVO_TESTINGXXX"] |
| 84 |
self.assertEqual(f.adqlUDF_name, "gavo_testingXXX") |
| 85 |
self.assertEqual(f.adqlUDF_signature, |
| 86 |
"gavo_testingXXX(x INTEGER) -> INTEGER") |
| 87 |
self.assertEqual(f.adqlUDF_doc, "This function returns its argument" |
| 88 |
" decreased by one.\n\nThis is the end.") |
| 89 |
|
| 90 |
def testFlattening(self): |
| 91 |
self.assertEqual( |
| 92 |
adql.parseToTree("SELECT GAVO_TESTINGXXX(frob) FROM x" |
| 93 |
).flatten(), |
| 94 |
"SELECT (frob+1) FROM x") |
| 95 |
|
| 96 |
def testFlatteningTransparent(self): |
| 97 |
self.assertEqual( |
| 98 |
adql.parseToTree("SELECT GAVO_TESTINGYYY(CIRCLE('', a, b, c), u) FROM x" |
| 99 |
).flatten(), |
| 100 |
'SELECT GAVO_TESTINGYYY(CIRCLE(,a,b,c), u) FROM x') |
| 101 |
|
| 102 |
def testQueryInSelectList(self): |
| 103 |
self.assertEqual(list(self.querier.queryADQL( |
| 104 |
"SELECT GAVO_TESTINGXXX(rV) FROM test.adql where mag<0").rows[0].values()), |
| 105 |
[29.]) |
| 106 |
|
| 107 |
def testQueryInWhereClause(self): |
| 108 |
self.assertEqual(list(self.querier.queryADQL( |
| 109 |
"SELECT rV FROM test.adql where GAVO_TESTINGXXX(rV)>0").rows[0].values()), |
| 110 |
[0.]) |
| 111 |
|
| 112 |
def testPolymorphism(self): |
| 113 |
self.assertEqual( |
| 114 |
ufunctions.UFUNC_REGISTRY["GAVO_TESTINGXXX" |
| 115 |
].adqlUDF_additionalSignatures[0]["signature"], |
| 116 |
"gavo_testingXXX(x REAL, y INTEGER) -> REAL") |
| 117 |
self.assertEqual( |
| 118 |
ufunctions.UFUNC_REGISTRY["GAVO_TESTINGYYY" |
| 119 |
].adqlUDF_additionalSignatures, []) |
| 120 |
|
| 121 |
|
| 122 |
class _UfuncTestbed(tresc.RDDataResource): |
| 123 |
"""A table that contains test material for ufuncs. |
| 124 |
|
| 125 |
If you write queries against this, make sure you survive schema |
| 126 |
*and data* extensions. They are sure to come as new UDFs need exercise. |
| 127 |
Use the kind column as necessary. |
| 128 |
""" |
| 129 |
rdName = "data/ufuncex.rd" |
| 130 |
dataId = "import" |
| 131 |
|
| 132 |
_ufuncTestbed = _UfuncTestbed() |
| 133 |
|
| 134 |
|
| 135 |
class BuiltinUfuncTest(testhelpers.VerboseTest): |
| 136 |
resources = [ |
| 137 |
("ssaTestTable", tresc.ssaTestTable), |
| 138 |
("ufuncTestTable", _ufuncTestbed), |
| 139 |
("querier", adqltest.adqlQuerier)] |
| 140 |
|
| 141 |
def testHaswordQuery(self): |
| 142 |
self.assertEqual(self.querier.queryADQL( |
| 143 |
"select distinct ssa_targname from test.hcdtest where" |
| 144 |
" 1=ivo_hasword(ssa_targname, 'rat hole')").rows, |
| 145 |
[{'ssa_targname': 'rat hole in the yard'}]) |
| 146 |
|
| 147 |
def testHaswordQueryInsensitive(self): |
| 148 |
self.assertEqual(self.querier.queryADQL( |
| 149 |
"select distinct ssa_targname from test.hcdtest where" |
| 150 |
" 1=ivo_hasword(ssa_targname, 'Booger')").rows, |
| 151 |
[{'ssa_targname': 'booger star'}]) |
| 152 |
|
| 153 |
def testHaswordQueryBorders(self): |
| 154 |
self.assertEqual(self.querier.queryADQL( |
| 155 |
"select distinct ssa_targname from test.hcdtest where" |
| 156 |
" 1=ivo_hasword(ssa_targname, 'ooger')").rows, |
| 157 |
[]) |
| 158 |
|
| 159 |
def testHashlistSimple(self): |
| 160 |
self.assertEqual(self.querier.queryADQL( |
| 161 |
"select distinct ivo_hashlist_has('bork#nork#gaob norm', 'nork') as h" |
| 162 |
" FROM test.hcdtest").rows, |
| 163 |
[{'h': 1}]) |
| 164 |
|
| 165 |
def testHashlistBorders(self): |
| 166 |
self.assertEqual(self.querier.queryADQL( |
| 167 |
"select distinct ivo_hashlist_has('bork#nork#gaob norm', 'ork') as h" |
| 168 |
" FROM test.hcdtest").rows, |
| 169 |
[{'h': 0}]) |
| 170 |
|
| 171 |
def testHashlistNocase(self): |
| 172 |
self.assertEqual(self.querier.queryADQL( |
| 173 |
"select distinct ivo_hashlist_has('bork#nork#gaob norm', 'nOrk') as h" |
| 174 |
" FROM test.hcdtest").rows, |
| 175 |
[{'h': 1}]) |
| 176 |
|
| 177 |
def testHashlistNocaseList(self): |
| 178 |
self.assertEqual(self.querier.queryADQL( |
| 179 |
"select distinct ivo_hashlist_has('bork#nOrk#gaob norm', 'nork') as h" |
| 180 |
" FROM test.hcdtest").rows, |
| 181 |
[{'h': 1}]) |
| 182 |
|
| 183 |
|
| 184 |
def testNocasematch(self): |
| 185 |
self.assertEqual(len(self.querier.queryADQL( |
| 186 |
"select ssa_targname FROM test.hcdtest" |
| 187 |
" WHERE 1=ivo_nocasematch(ssa_targname, 'BOOGER%')").rows), |
| 188 |
2) |
| 189 |
|
| 190 |
def testToMJD(self): |
| 191 |
self.assertEqual(self.querier.queryADQL( |
| 192 |
"select gavo_to_mjd(dt) as mjd from test.ufuncex where testgroup='jd'" |
| 193 |
).rows, [{'mjd': 45917.5}]) |
| 194 |
|
| 195 |
def testToJD(self): |
| 196 |
self.assertEqual(self.querier.queryADQL( |
| 197 |
"select ivo_to_jd(dt) as jd from test.ufuncex where testgroup='jd'" |
| 198 |
).rows, [{'jd': 2445918.0}]) |
| 199 |
|
| 200 |
|
| 201 |
class RegionTest(unittest.TestCase): |
| 202 |
"""tests for sane parsing of our default region expressions. |
| 203 |
""" |
| 204 |
resources = [("fs", tresc.fakedSimbad)] |
| 205 |
|
| 206 |
def testRaising(self): |
| 207 |
"""tests for plausible exceptions. |
| 208 |
""" |
| 209 |
self.assertRaises(adql.RegionError, adql.parseToTree, |
| 210 |
"SELECT x FROM y WHERE 1=CONTAINS(REGION('78y'), REGION('zzy9'))") |
| 211 |
self.assertRaises(adql.RegionError, adql.parseToTree, |
| 212 |
"SELECT x FROM y WHERE 1=CONTAINS(REGION(dbColumn || otherColumn)," |
| 213 |
" CIRCLE('ICRS', 10, 10 ,2))") |
| 214 |
|
| 215 |
|
| 216 |
def getMorphed(query): |
| 217 |
return nodes.flatten(adql.morphPG(adql.parseToTree(query))[1]) |
| 218 |
|
| 219 |
|
| 220 |
class RRFunctionsTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest): |
| 221 |
resources = [("ufuncTestTable", _ufuncTestbed)] |
| 222 |
def _runTest(self, sample): |
| 223 |
query, expected = sample |
| 224 |
self.assertEqualIgnoringAliases(getMorphed(query), expected) |
| 225 |
|
| 226 |
samples = [ |
| 227 |
("select testgroup from test.ufuncex where 1=ivo_hasword(testgroup, 'abc')", |
| 228 |
"SELECT testgroup FROM test.ufuncex WHERE (" |
| 229 |
"to_tsvector('english', testgroup)" |
| 230 |
" @@ plainto_tsquery('english', 'abc'))"), |
| 231 |
("select ivo_hasword(testgroup, 'abc') from test.ufuncex", |
| 232 |
"SELECT IVO_HASWORD(testgroup, 'abc') ASWHATEVER FROM test.ufuncex"), |
| 233 |
("select testgroup from test.ufuncex where" |
| 234 |
" 1=ivo_hashlist_has('a#b#c', testgroup)", |
| 235 |
"SELECT testgroup FROM test.ufuncex WHERE lower(testgroup) =" |
| 236 |
" ANY(string_to_array(lower('a#b#c'), '#'))"), |
| 237 |
("select testgroup from test.ufuncex where" |
| 238 |
" 1=ivo_nocasematch(testgroup, 'honk%')", |
| 239 |
"SELECT testgroup FROM test.ufuncex WHERE (LOWER(testgroup) like 'honk%')"), |
| 240 |
] |
| 241 |
|
| 242 |
|
| 243 |
class IntervalsMorphTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest): |
| 244 |
resources = [("ufuncTestTable", _ufuncTestbed)] |
| 245 |
def _runTest(self, sample): |
| 246 |
query, expected = sample |
| 247 |
self.assertEqualIgnoringAliases(getMorphed(query), expected) |
| 248 |
|
| 249 |
samples = [ |
| 250 |
("select testgroup from test.ufuncex where 1=ivo_interval_has(a, iv)", |
| 251 |
"SELECT testgroup FROM test.ufuncex WHERE ((a) <@ (iv))"), |
| 252 |
("select testgroup from test.ufuncex where 0=ivo_interval_has(a, iv)", |
| 253 |
"SELECT testgroup FROM test.ufuncex WHERE NOT ( ((a) <@ (iv)) )"), |
| 254 |
] |
| 255 |
|
| 256 |
|
| 257 |
class AggFunctionsTest(testhelpers.VerboseTest): |
| 258 |
resources = [ |
| 259 |
("ssaTestTable", tresc.ssaTestTable), |
| 260 |
("querier", adqltest.adqlQuerier)] |
| 261 |
|
| 262 |
def testStringAggMorph(self): |
| 263 |
self.assertEqualIgnoringAliases( |
| 264 |
getMorphed("select ivoid, ivo_string_agg(res_subject, ',')" |
| 265 |
" from rr.res_subject group by ivoid"), |
| 266 |
"SELECT ivoid, string_agg(res_subject, ',') ASWHATEVER" |
| 267 |
" FROM rr.res_subject GROUP BY ivoid") |
| 268 |
|
| 269 |
def testStringAddExec(self): |
| 270 |
res = self.querier.queryADQL( |
| 271 |
"SELECT ivo_string_agg(accref || mime, '#') as goo, ssa_pubdid" |
| 272 |
" from test.hcdtest group by ssa_pubdid order by ssa_pubdid").rows |
| 273 |
self.assertEqual(res[0]["ssa_pubDID"], 'ivo://test.inv/test1') |
| 274 |
self.assertEqual(set(res[0]["goo"].split("#")), |
| 275 |
set('data/spec1.ssatestimage/jpeg#data/spec1.ssatest.' |
| 276 |
'votapplication/x-votable+xml'.split("#"))) |
| 277 |
|
| 278 |
|
| 279 |
@unittest.skipUnless(testhelpers.hasUDF("IVO_HEALPIX_INDEX"), "pgsphere too old") |
| 280 |
class HealpixFunctionsTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest): |
| 281 |
resources = [("ufuncTestTable", _ufuncTestbed)] |
| 282 |
def _runTest(self, sample): |
| 283 |
query, expected = sample |
| 284 |
self.assertEqualIgnoringAliases(getMorphed(query), expected) |
| 285 |
|
| 286 |
samples = [ |
| 287 |
("select ivo_healpix_index(10, point('', ra, dec)) from test.ufuncex", |
| 288 |
"SELECT healpix_nest(10, spoint(RADIANS(ra), RADIANS(dec))) ASWHATEVER FROM test.ufuncex"), |
| 289 |
("select ivo_healpix_index(10, p) from test.ufuncex", |
| 290 |
"SELECT healpix_nest(10, p) ASWHATEVER FROM test.ufuncex"), |
| 291 |
("select ivo_healpix_center(10, 40002) from test.ufuncex", |
| 292 |
"SELECT center_of_healpix_nest(10, 40002) ASWHATEVER FROM test.ufuncex"), |
| 293 |
] |
| 294 |
|
| 295 |
|
| 296 |
@unittest.skipUnless(testhelpers.hasUDF("IVO_HEALPIX_INDEX"), "pgsphere too old") |
| 297 |
class HealpixExecTest(testhelpers.VerboseTest): |
| 298 |
resources = [ |
| 299 |
("querier", adqltest.adqlQuerier), |
| 300 |
("test_ufunc", _ufuncTestbed)] |
| 301 |
|
| 302 |
def testIndexAndPoint(self): |
| 303 |
res = self.querier.queryADQL( |
| 304 |
"SELECT ivo_healpix_index(10, p) as hpi" |
| 305 |
" from test.ufuncex WHERE dt<'1985-01-01'") |
| 306 |
self.assertEqual(res.rows[0]["hpi"], 2937727) |
| 307 |
|
| 308 |
def testCenterFinding(self): |
| 309 |
res = self.querier.queryADQL( |
| 310 |
"SELECT ivo_healpix_center(10, ivo_healpix_index(10, ra, dec)) as pt" |
| 311 |
" from test.ufuncex WHERE dt<'1985-01-01'") |
| 312 |
self.assertAlmostEqual(res.rows[0]["pt"].y, -12.25362992*utils.DEG) |
| 313 |
self.assertAlmostEqual(res.rows[0]["pt"].x, 23.51074219*utils.DEG) |
| 314 |
self.assertEqual(res.tableDef.columns[0].type, "spoint") |
| 315 |
|
| 316 |
def testHealpixBadarg(self): |
| 317 |
self.assertRaisesWithMsg( |
| 318 |
adql.UfuncError, |
| 319 |
"ivo_healpix_center only takes (index, order) arguments", |
| 320 |
getMorphed, |
| 321 |
("select ivo_healpix_center() from test.ufuncex",)) |
| 322 |
|
| 323 |
|
| 324 |
class SimbadpointTest(testhelpers.VerboseTest): |
| 325 |
resources = [ |
| 326 |
("test_ufunc", _ufuncTestbed), |
| 327 |
("fs", tresc.fakedSimbad)] |
| 328 |
|
| 329 |
def testBadArgcount(self): |
| 330 |
self.assertRaisesWithMsg(adql.UfuncError, |
| 331 |
"gavo_simbadpoint takes exactly one string literal as argument", |
| 332 |
getMorphed, |
| 333 |
("SELECT * from test.ufuncex WHERE 1=CONTAINS(" |
| 334 |
"gavo_simbadpoint('aldebaran', 23), CIRCLE('ICRS', ra, dec, 1))",)) |
| 335 |
|
| 336 |
def testBadArgtype(self): |
| 337 |
self.assertRaisesWithMsg(adql.UfuncError, |
| 338 |
"gavo_simbadpoint takes exactly one string literal as argument", |
| 339 |
getMorphed, |
| 340 |
("SELECT * from test.ufuncex WHERE 1=CONTAINS(" |
| 341 |
"gavo_simbadpoint(testgroup), CIRCLE('ICRS', ra, dec, 1))",)) |
| 342 |
|
| 343 |
def testBadName(self): |
| 344 |
self.assertRaisesWithMsg(adql.UfuncError, |
| 345 |
"No simbad position for 'Henker'", |
| 346 |
getMorphed, |
| 347 |
("SELECT * from test.ufuncex WHERE 1=CONTAINS(" |
| 348 |
"gavo_simbadpoint('Henker'), CIRCLE('ICRS', ra, dec, 1))",)) |
| 349 |
|
| 350 |
def testResolution(self): |
| 351 |
self.assertEqualIgnoringAliases( |
| 352 |
getMorphed("SELECT * from test.ufuncex WHERE 1=CONTAINS(" |
| 353 |
"ivo_simbadpoint('M1'), CIRCLE('ICRS', ra, dec, 0.333))"), |
| 354 |
"SELECT * FROM test.ufuncex WHERE ((spoint(RADIANS(83.65625)," |
| 355 |
" RADIANS(22.0145))) <@ (scircle(spoint(RADIANS(ra), RADIANS(dec))," |
| 356 |
" RADIANS(0.333))))") |
| 357 |
|
| 358 |
def testDistance(self): |
| 359 |
self.assertEqual(getMorphed("SELECT * from test.ufuncex WHERE 2>" |
| 360 |
"distance(point(ra, dec), gavo_simbadpoint('M1'))"), |
| 361 |
"SELECT * FROM test.ufuncex WHERE (spoint(RADIANS(ra), RADIANS(dec))) <@ scircle(spoint(RADIANS(83.65625), RADIANS(22.0145)), RADIANS(2))") |
| 362 |
|
| 363 |
|
| 364 |
@unittest.skipUnless(testhelpers.hasUDF("IVO_EPOCH_PROP"), "pgsphere too old") |
| 365 |
class LegacyMotionTest(testhelpers.VerboseTest): |
| 366 |
# Let's eventually remove gavo_apply_pm once ivo_epoch_prop is |
| 367 |
# safely installed. |
| 368 |
resources = [ |
| 369 |
("test_ufunc", _ufuncTestbed), |
| 370 |
("querier", adqltest.adqlQuerier),] |
| 371 |
|
| 372 |
def testQueryDecTrans(self): |
| 373 |
res = self.querier.queryADQL( |
| 374 |
"SELECT ivo_apply_pm(ra, dec, 0., -0.1, 50)" |
| 375 |
" as newpos from test.ufuncex where testgroup='jd'") |
| 376 |
self.assertEqual(res.rows[0]["newpos"].asSTCS('ICRS'), |
| 377 |
"Position ICRS 23.5 -17.2373652888") |
| 378 |
|
| 379 |
def testQueryGavoPrefix(self): |
| 380 |
res = self.querier.queryADQL( |
| 381 |
"SELECT gavo_apply_pm(ra, dec, 0., -0.1, 50)" |
| 382 |
" as newpos from test.ufuncex where testgroup='jd'") |
| 383 |
self.assertEqual(res.rows[0]["newpos"].asSTCS('ICRS'), |
| 384 |
"Position ICRS 23.5 -17.2373652888") |
| 385 |
|
| 386 |
def testQueryRATrans(self): |
| 387 |
# Starting pos are (10, 20) and (50, 80) |
| 388 |
res = self.querier.queryADQL( |
| 389 |
"SELECT ivo_apply_pm(ra, dec, 0.1, 0, 50) as newpos" |
| 390 |
" from test.ufuncex where testgroup='intervals'" |
| 391 |
" ORDER BY dec") |
| 392 |
transPos = [r["newpos"].asDALI() for r in res] |
| 393 |
self.assertAlmostEqual(transPos[0][0], 15.30567124204) |
| 394 |
self.assertAlmostEqual(transPos[0][1], 19.92106445273) |
| 395 |
self.assertTrue(transPos[0][0]-10<transPos[1][0]-50) |
| 396 |
|
| 397 |
def testQueryConstraint(self): |
| 398 |
res = self.querier.queryADQL( |
| 399 |
"SELECT testgroup from test.ufuncex" |
| 400 |
" where 1=Contains(" |
| 401 |
"ivo_apply_pm(ra, dec, 0.01, -0.01, 23)," |
| 402 |
"CIRCLE('ICRS', 23.735, -12.48, 0.01))") |
| 403 |
self.assertEqual(testhelpers.pickSingle(res.rows)["testgroup"], "jd") |
| 404 |
|
| 405 |
def testTransformBadarg(self): |
| 406 |
self.assertRaisesWithMsg( |
| 407 |
adql.UfuncError, |
| 408 |
"gavo_apply_pm requires exactly ra, dec, pmra, pmdec, epdist.", |
| 409 |
getMorphed, |
| 410 |
("select ivo_apply_pm(1, 2) from test.ufuncex",)) |
| 411 |
|
| 412 |
|
| 413 |
@unittest.skipUnless(testhelpers.hasUDF("IVO_EPOCH_PROP"), "pgsphere too old") |
| 414 |
class EpochPropTest(testhelpers.VerboseTest): |
| 415 |
resources = [ |
| 416 |
("test_ufunc", _ufuncTestbed), |
| 417 |
("querier", adqltest.adqlQuerier),] |
| 418 |
|
| 419 |
def testPropDecTrans(self): |
| 420 |
res = self.querier.queryADQL( |
| 421 |
"SELECT ivo_epoch_prop(ra, dec, NULL, 0., 400, NULL, 2000, 2022)" |
| 422 |
" as newpv from test.ufuncex where testgroup='jd'") |
| 423 |
|
| 424 |
colDef = res.tableDef.getColumnByName("newpv") |
| 425 |
self.assertEqual( |
| 426 |
colDef.type, |
| 427 |
"double precision[6]") |
| 428 |
self.assertEqual( |
| 429 |
colDef.ucd, |
| 430 |
'') # TODO: We really want a UCD for this kind of monster |
| 431 |
|
| 432 |
val = res.rows[0]["newpv"] |
| 433 |
self.assertAlmostEqual(val[0], 23.5) |
| 434 |
self.assertAlmostEqual(val[1], -12.247555555555) |
| 435 |
self.assertEqual(val[2], None) |
| 436 |
self.assertAlmostEqual(val[3], 0) |
| 437 |
self.assertAlmostEqual(val[4], 399.99999927192) |
| 438 |
self.assertEqual(val[5], None) |
| 439 |
|
| 440 |
def testProp6Par(self): |
| 441 |
# Starting pos are (10, 20) and (50, 80) |
| 442 |
res = self.querier.queryADQL( |
| 443 |
"SELECT ivo_epoch_prop(ra, dec, 60{mas/yr}, 120, -40, -140, 2022, 1980) as newpos" |
| 444 |
" from test.ufuncex where testgroup='intervals'" |
| 445 |
" ORDER BY dec") |
| 446 |
|
| 447 |
pv1, pv2 = res.rows[0]["newpos"], res.rows[1]["newpos"] |
| 448 |
self.assertAlmostEqual(pv1[0], 9.998510684068139) |
| 449 |
self.assertAlmostEqual(pv2[1], 80.0004664014105) |
| 450 |
self.assertAlmostEqual(pv1[2], 59.9783590172609) |
| 451 |
self.assertAlmostEqual(pv1[3], 119.91380703443762) |
| 452 |
self.assertAlmostEqual(pv2[3], 119.91898781569608) |
| 453 |
self.assertAlmostEqual(pv2[4], -39.95453831783573) |
| 454 |
self.assertAlmostEqual(pv1[5], -140.00025726418698) |
| 455 |
|
| 456 |
def testPropBadarg(self): |
| 457 |
self.assertRaisesWithMsg( |
| 458 |
adql.UfuncError, |
| 459 |
"ivo_epoch_prop gets ra, dec [deg], parallax [mas], pmra, pmdec" |
| 460 |
" [mas/yr], radial_velocity [km/s], ref_epoch, out_epoch [yr] arguments", |
| 461 |
getMorphed, |
| 462 |
("select ivo_epoch_prop(1, 2) from test.ufuncex",)) |
| 463 |
|
| 464 |
def testPropPos(self): |
| 465 |
# Starting pos are (10, 20) and (50, 80) |
| 466 |
res = self.querier.queryADQL( |
| 467 |
"SELECT ivo_epoch_prop_pos(ra, dec, 60, 120, -40, -140, 2022, 1980)" |
| 468 |
" as newpos" |
| 469 |
" from test.ufuncex where testgroup='intervals'" |
| 470 |
" ORDER BY dec") |
| 471 |
|
| 472 |
colDef = res.tableDef.getColumnByName("newpos") |
| 473 |
self.assertEqual(colDef.type, "spoint") |
| 474 |
self.assertEqual(colDef.ucd, "pos") |
| 475 |
|
| 476 |
pv1, pv2 = res.rows[0]["newpos"], res.rows[1]["newpos"] |
| 477 |
self.assertAlmostEqual(pv1.asDALI()[0], 9.998510684068163) |
| 478 |
self.assertAlmostEqual(pv2.asDALI()[1], 80.00046640141034) |
| 479 |
|
| 480 |
def testPropPos6Arg(self): |
| 481 |
# Starting pos are (10, 20) and (50, 80) |
| 482 |
res = self.querier.queryADQL( |
| 483 |
"SELECT ivo_epoch_prop_pos(ra@{deg}, dec, 120{mas/yr}, -40, 2000+22, 990*2)" |
| 484 |
" as newpos" |
| 485 |
" from test.ufuncex where testgroup='intervals'" |
| 486 |
" ORDER BY dec") |
| 487 |
|
| 488 |
pv1, pv2 = res.rows[0]["newpos"], res.rows[1]["newpos"] |
| 489 |
# this is different from above because par/rv are assumed 0 here. |
| 490 |
self.assertAlmostEqual(pv1.asDALI()[0], 9.99851014670222) |
| 491 |
self.assertAlmostEqual(pv2.asDALI()[1], 80.0004665696594) |
| 492 |
|
| 493 |
def testPropPosWhere(self): |
| 494 |
res = self.querier.queryADQL( |
| 495 |
"SELECT ra, dec from test.ufuncex where testgroup='intervals'" |
| 496 |
" AND distance(ivo_epoch_prop_pos(12, 0, NULL, -120000, 0," |
| 497 |
" NULL, 2000, 2060), point(ra, dec-20))<0.001") |
| 498 |
self.assertEqual(testhelpers.pickSingle(res.rows)["ra"], 10.) |
| 499 |
|
| 500 |
def testPropPosMsg(self): |
| 501 |
self.assertRaisesWithMsg(base.ValidationError, |
| 502 |
"Field query: ivo_epoch_prop_pos gets ra, dec [deg], parallax [mas], pmra, pmdec [mas/yr], radial_velocity [km/s], ref_epoch, out_epoch [yr] arguments, where you may leave out parallax and radial_velocity.", |
| 503 |
self.querier.queryADQL, |
| 504 |
("SELECT ivo_epoch_prop_pos(ra, dec, 120, -40)" |
| 505 |
" as newpos" |
| 506 |
" from test.ufuncex where testgroup='intervals'" |
| 507 |
" ORDER BY dec",)) |
| 508 |
|
| 509 |
def testPropNoPM(self): |
| 510 |
res = self.querier.queryADQL( |
| 511 |
"SELECT TOP 1" |
| 512 |
" ivo_epoch_prop(10, 12, NULL, NULL, NULL, NULL, 2000, 2020) as prop" |
| 513 |
" from tap_schema.tables") |
| 514 |
self.assertEqual(res.rows[0]["prop"], |
| 515 |
[10.0, 12.000000000000002, None, None, None, None]) |
| 516 |
|
| 517 |
def testPropNoPMDec(self): |
| 518 |
res = self.querier.queryADQL( |
| 519 |
"SELECT TOP 1" |
| 520 |
" ivo_epoch_prop(10, 12, 3, NULL, NULL, NULL, 2000, 2020) as prop" |
| 521 |
" from tap_schema.tables") |
| 522 |
self.assertEqual(res.rows[0]["prop"], |
| 523 |
[10.0, 12.000000000000002, 3, None, None, None]) |
| 524 |
|
| 525 |
def testPropNoPMRA(self): |
| 526 |
res = self.querier.queryADQL( |
| 527 |
"SELECT TOP 1" |
| 528 |
" ivo_epoch_prop(10, 12, NULL, 3, NULL, NULL, 2000, 2020) as prop" |
| 529 |
" from tap_schema.tables") |
| 530 |
self.assertAlmostEqualVector(res.rows[0]["prop"], |
| 531 |
[10.0, 12., None, None, None, None]) |
| 532 |
|
| 533 |
def testPropNoRV(self): |
| 534 |
res = self.querier.queryADQL( |
| 535 |
"SELECT TOP 1" |
| 536 |
" ivo_epoch_prop(10, 12, 4, 2, 5, NULL, 2000, 2020) as prop" |
| 537 |
" from tap_schema.tables") |
| 538 |
self.assertAlmostEqualVector(res.rows[0]["prop"], |
| 539 |
[10.000011359341114, 12.000027777777, 3.999999999999, |
| 540 |
2.000000206100, 4.99999991755, None]) |
| 541 |
|
| 542 |
def testPropNoParallax(self): |
| 543 |
res = self.querier.queryADQL( |
| 544 |
"SELECT TOP 1" |
| 545 |
" ivo_epoch_prop(10, 12, NULL, 2, 5, 4000, 2000, 2020) as prop" |
| 546 |
" from tap_schema.tables") |
| 547 |
self.assertAlmostEqualVector(res.rows[0]["prop"], |
| 548 |
[10.000011359341022, 12.000027777777323, None, |
| 549 |
2.0000001733735733, 4.999999835741298, 4000]) |
| 550 |
|
| 551 |
class IntervalQueryTest(testhelpers.VerboseTest, |
| 552 |
metaclass=testhelpers.SamplesBasedAutoTest): |
| 553 |
resources = [ |
| 554 |
("test_ufunc", _ufuncTestbed), |
| 555 |
("querier", adqltest.adqlQuerier),] |
| 556 |
|
| 557 |
def _runTest(self, sample): |
| 558 |
constraint, expected = sample |
| 559 |
res = self.querier.queryADQL( |
| 560 |
"select count(*) as ct from test.ufuncex" |
| 561 |
" where testgroup='intervals'" |
| 562 |
" and "+constraint) |
| 563 |
self.assertEqual(res.rows[0]["ct"], expected) |
| 564 |
|
| 565 |
samples = [ |
| 566 |
("1=ivo_interval_overlaps(ra, dec, 0, 5)", 0), |
| 567 |
("ivo_interval_overlaps(ra, dec, 5, 10)=1", 1), |
| 568 |
("1=ivo_interval_overlaps(ra, dec, 5, 25)", 1), |
| 569 |
("1=ivo_interval_overlaps(ra, dec, 20, 25)", 1), |
| 570 |
("1=ivo_interval_overlaps(ra, dec, 25, 35)", 0), |
| 571 |
#5 |
| 572 |
("1=ivo_interval_overlaps(ra, dec, 5, 55)", 2), |
| 573 |
("1=ivo_interval_overlaps(ra, dec, 5, 155)", 2), |
| 574 |
("1!=ivo_interval_overlaps(ra, dec, 5, 155)", 0), |
| 575 |
("ivo_interval_overlaps(ra, dec, 5, 155)=0", 0), |
| 576 |
("1=ivo_interval_overlaps(COORD1(p), COORD2(p), 5, 25)", 1), |
| 577 |
#10 |
| 578 |
("1=ivo_interval_overlaps(COORD2(p), COORD1(p), 5, 25)", 0), |
| 579 |
("1=ivo_interval_overlaps(ra, dec, 15, 15)", 1), |
| 580 |
("1=ivo_interval_overlaps(dec, dec, 15, 25)", 1), |
| 581 |
("0=ivo_interval_overlaps(ra, dec, 150, 250)", 2), |
| 582 |
("ivo_interval_overlaps(ra, dec, -100, -30)=0", 2), |
| 583 |
] |
| 584 |
|
| 585 |
|
| 586 |
class HistogramTest(testhelpers.VerboseTest): |
| 587 |
resources = [ |
| 588 |
("test_ufunc", _ufuncTestbed), |
| 589 |
("querier", adqltest.adqlQuerier),] |
| 590 |
|
| 591 |
def testBasic(self): |
| 592 |
res = self.querier.queryADQL( |
| 593 |
"SELECT ivo_histogram(ra, 20., 60., 2) as ras," |
| 594 |
" ivo_histogram(dec, -20., 81., 10) as decs from test.ufuncex" |
| 595 |
" WHERE testgroup IN ('jd', 'intervals')") |
| 596 |
row = testhelpers.pickSingle(res.rows) |
| 597 |
self.assertEqual(row["ras"], [1, 1, 1, 0]) |
| 598 |
self.assertEqual(row["decs"], |
| 599 |
[0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0]) |
| 600 |
|
| 601 |
def testAlias(self): |
| 602 |
res = self.querier.queryADQL( |
| 603 |
"SELECT gavo_histogram(ra, 20., 60., 2) as ras" |
| 604 |
" from test.ufuncex" |
| 605 |
" WHERE testgroup IN ('jd', 'intervals')") |
| 606 |
self.assertEqual(res.rows[0]["ras"], [1, 1, 1, 0]) |
| 607 |
|
| 608 |
def testBadarg(self): |
| 609 |
self.assertRaisesWithMsg( |
| 610 |
adql.UfuncError, |
| 611 |
"ivo_histogram takes exactly four arguments (the column to aggregate, a lower and upper limit of values to tabulate, and the number of bins desired).", |
| 612 |
getMorphed, |
| 613 |
("select ivo_histogram(1, 2) from test.ufuncex",)) |
| 614 |
|
| 615 |
|
| 616 |
class TransformTest(testhelpers.VerboseTest): |
| 617 |
resources = [ |
| 618 |
("test_ufunc", _ufuncTestbed), |
| 619 |
("querier", adqltest.adqlQuerier),] |
| 620 |
|
| 621 |
def testMorphing(self): |
| 622 |
self.assertEqual( |
| 623 |
getMorphed("SELECT gavo_transform('ICRS', 'GALACTIC', p) as ps" |
| 624 |
" from test.ufuncex"), |
| 625 |
"SELECT (p)+strans(1.346356097441,-1.097319001837,0.574770524729) AS ps FROM test.ufuncex") |
| 626 |
|
| 627 |
def testICRSGalactic(self): |
| 628 |
res = self.querier.queryADQL( |
| 629 |
"SELECT ivo_geom_transform('ICRS', 'GALACTIC', p) as ps, p" |
| 630 |
" from test.ufuncex where testgroup='jd'") |
| 631 |
ps = testhelpers.pickSingle(res.rows)["ps"] |
| 632 |
|
| 633 |
# XXX TODO: figure out where the precision goes -- this should have |
| 634 |
# about 13 s.f. rather than just 8. |
| 635 |
self.assertAlmostEqual(ps.x/utils.DEG, 95.79525025241531, places=5) |
| 636 |
self.assertAlmostEqual(ps.y/utils.DEG, 48.55779439628186, places=5) |
| 637 |
self.assertEqual(res.tableDef.getColumnByName("ps").ucd, "pos.galactic") |
| 638 |
|
| 639 |
def testGalacticFK4(self): |
| 640 |
res = self.querier.queryADQL( |
| 641 |
"SELECT gavo_transform('GALACTIC', 'FK4', p) as ps, p" |
| 642 |
" from test.ufuncex where testgroup='jd'") |
| 643 |
self.assertEqual( |
| 644 |
res.tableDef.getColumnByName("ps").type, |
| 645 |
"spoint") |
| 646 |
|
| 647 |
ps = testhelpers.pickSingle(res.rows)["ps"] |
| 648 |
self.assertAlmostEqual(ps.x/utils.DEG, 162.4867829887925) |
| 649 |
self.assertAlmostEqual(ps.y/utils.DEG, 15.59041298060233) |
| 650 |
self.assertEqual(res.tableDef.getColumnByName("ps").ucd, "pos.eq") |
| 651 |
|
| 652 |
def testTransformImmediate(self): |
| 653 |
res = self.querier.queryADQL( |
| 654 |
"SELECT TOP 1 gavo_transform('GALACTIC', 'ICRS'," |
| 655 |
" CIRCLE(10, 10, 3)) AS c" |
| 656 |
" from test.ufuncex") |
| 657 |
self.assertEqual( |
| 658 |
res.tableDef.getColumnByName("c").type, |
| 659 |
"scircle") |
| 660 |
|
| 661 |
c = res.rows[0]["c"] |
| 662 |
self.assertAlmostEqual(c.center.x/utils.DEG, 262.89288250964) |
| 663 |
self.assertAlmostEqual(c.radius/utils.DEG, 3) |
| 664 |
self.assertEqual(res.tableDef.getColumnByName("c").ucd, "pos.eq") |
| 665 |
|
| 666 |
def testInvalidRefsys(self): |
| 667 |
self.assertRaisesWithMsg(base.ValidationError, |
| 668 |
"Field query: Cannot compute transformation between J2015 and FK4: Unknown reference frame: J2015", |
| 669 |
self.querier.queryADQL, |
| 670 |
("SELECT gavo_transform('J2015', 'FK4', p) as ps, p" |
| 671 |
" from test.ufuncex where testgroup='jd'",)) |
| 672 |
|
| 673 |
def testTransformBadarg(self): |
| 674 |
self.assertRaisesWithMsg( |
| 675 |
adql.UfuncError, |
| 676 |
"gavo_transform takes exactly three arguments", |
| 677 |
getMorphed, |
| 678 |
("select gavo_transform(1, 2) from test.ufuncex",)) |
| 679 |
|
| 680 |
|
| 681 |
class URLFuncsTest(testhelpers.VerboseTest): |
| 682 |
resources = [('conn', tresc.dbConnection), |
| 683 |
("ufuncTestTable", _ufuncTestbed)] |
| 684 |
|
| 685 |
def testEscaping(self): |
| 686 |
# Well, fair enough: this is not an ADQL UDF at this moment. |
| 687 |
# But it may become one and is defined in //adql, so I'll test it |
| 688 |
# here. |
| 689 |
input = '10% more than 2+5 & fewer = #sql ' |
| 690 |
res = list(self.conn.queryToDicts( |
| 691 |
"SELECT gavo_urlescape(%(input)s) as r", {"input": input})) |
| 692 |
self.assertEqual(res[0]["r"], |
| 693 |
"10%25%20more%20than%202%2B5%20%26%20fewer%20%3D%20%23sql%20") |
| 694 |
self.assertEqual(parse.unquote(res[0]["r"]), input) |
| 695 |
|
| 696 |
def testAuthority(self): |
| 697 |
res = list(self.conn.query( |
| 698 |
"select gavo_getauthority('ivo://org.gavo.dc/foo:bar#quoox') as x" |
| 699 |
" FROM test.ufuncex where testgroup='jd'")) |
| 700 |
self.assertEqual(res[0][0], "org.gavo.dc") |
| 701 |
|
| 702 |
def testAuthorityBadarg(self): |
| 703 |
self.assertRaisesWithMsg( |
| 704 |
adql.UfuncError, |
| 705 |
"gavo_getauthority only takes an ivoid argument.", |
| 706 |
getMorphed, |
| 707 |
("select gavo_getauthority(1, 2) from test.ufuncex",)) |
| 708 |
|
| 709 |
|
| 710 |
class NormalRandomTest(testhelpers.VerboseTest): |
| 711 |
resources = [ |
| 712 |
("test_ufunc", _ufuncTestbed), |
| 713 |
("querier", adqltest.adqlQuerier),] |
| 714 |
|
| 715 |
def testMorph(self): |
| 716 |
self.assertEqual( |
| 717 |
getMorphed("SELECT ra+gavo_normal_random(sin(radians(dec)), dec/10)" |
| 718 |
" as g from test.ufuncex"), |
| 719 |
'SELECT ra + (((random()+random()+random()+random()+random()+random()+random()+random()+random()+random()-5)*(dec / 10))+(SIN(RADIANS(dec)))) AS g FROM test.ufuncex') |
| 720 |
|
| 721 |
def testMessage(self): |
| 722 |
self.assertRaisesWithMsg(adql.UfuncError, |
| 723 |
"gavo_normal_random takes mu, sigma arguments.", |
| 724 |
getMorphed, |
| 725 |
("SELECT ra+ivo_normal_random(dec/10) as g from test.ufuncex",)) |
| 726 |
|
| 727 |
def testValues(self): |
| 728 |
res = self.querier.queryADQL( |
| 729 |
"SELECT gavo_normal_random(10, 1) as r FROM test.ufuncex") |
| 730 |
vals = [r["r"] for r in res.rows] |
| 731 |
self.assertTrue(min(vals)>-10, "20-sigma outlier in normal_random?") |
| 732 |
self.assertTrue(max(vals)<30, "20-sigma outlier in normal_random?") |
| 733 |
var = sum([(v-10)**2 for v in vals]) |
| 734 |
self.assertTrue(var>1e-2, "No variance in normal_random?") |
| 735 |
|
| 736 |
|
| 737 |
@unittest.skipUnless(testhelpers.hasUDF("GAVO_MOCUNION"), "pgsphere too old") |
| 738 |
class MocTest(testhelpers.VerboseTest): |
| 739 |
resources = [ |
| 740 |
("test_ufunc", _ufuncTestbed), |
| 741 |
("querier", adqltest.adqlQuerier),] |
| 742 |
|
| 743 |
def testUnion(self): |
| 744 |
res = self.querier.queryADQL( |
| 745 |
"SELECT gavo_mocunion(a.m, b.m) as res" |
| 746 |
" FROM test.ufuncex AS a JOIN test.ufuncex AS b USING (testgroup)" |
| 747 |
" WHERE testgroup='moc' and a.m!=b.m") |
| 748 |
mocs = set(r['res'].asASCII() for r in res) |
| 749 |
self.assertEqual(mocs, set(["6/33 38-40 7/49-51"])) |
| 750 |
self.assertEqual(res.tableDef.columns[0].type, "smoc") |
| 751 |
|
| 752 |
def testIntersect(self): |
| 753 |
res = self.querier.queryADQL( |
| 754 |
"SELECT gavo_mocintersect(a.m, b.m) as res" |
| 755 |
" FROM test.ufuncex AS a JOIN test.ufuncex AS b USING (testgroup)" |
| 756 |
" WHERE testgroup='moc' and a.m!=b.m") |
| 757 |
mocs = set(r['res'].asASCII() for r in res) |
| 758 |
self.assertEqual(mocs, set(["7/50"])) |
| 759 |
self.assertEqual(res.tableDef.columns[0].type, "smoc") |
| 760 |
|
| 761 |
|
| 762 |
class VocMatchTest(testhelpers.VerboseTest): |
| 763 |
def testDatalinkExpansion(self): |
| 764 |
self.assertEqualIgnoringAliases( |
| 765 |
getMorphed("SELECT * FROM whatever" |
| 766 |
" WHERE 1=gavo_vocmatch('datalink/core', 'calibration', semantics)"), |
| 767 |
"SELECT * FROM whatever WHERE semantics IN ('calibration'," |
| 768 |
" 'bias', 'dark', 'flat')") |
| 769 |
|
| 770 |
def testInvertedDatalinkExpansion(self): |
| 771 |
self.assertEqual( |
| 772 |
getMorphed("SELECT * FROM whatever" |
| 773 |
" WHERE gavo_vocmatch('datalink/core', 'calibration', semantics)=0"), |
| 774 |
"SELECT * FROM whatever WHERE NOT ( semantics IN ('calibration'," |
| 775 |
" 'bias', 'dark', 'flat') )") |
| 776 |
|
| 777 |
def testUnknownVocabulary(self): |
| 778 |
self.assertRaisesWithMsg( |
| 779 |
base.ReportableError, |
| 780 |
"No such vocabulary: free invention", |
| 781 |
getMorphed, |
| 782 |
("SELECT * FROM whatever" |
| 783 |
" WHERE 1=gavo_vocmatch('free invention', 'calibration', semantics)",)) |
| 784 |
|
| 785 |
def testUnknownTerm(self): |
| 786 |
self.assertRaisesWithMsg( |
| 787 |
adql.UfuncError, |
| 788 |
"'müll' is not a term in the vocabulary" |
| 789 |
" http://www.ivoa.net/rdf/datalink/core", |
| 790 |
getMorphed, |
| 791 |
("SELECT * FROM whatever" |
| 792 |
" WHERE 1=gavo_vocmatch('datalink/core', 'müll', semantics)",)) |
| 793 |
|
| 794 |
|
| 795 |
class SpectralConversionTest(testhelpers.VerboseTest, |
| 796 |
metaclass=testhelpers.SamplesBasedAutoTest): |
| 797 |
def _runTest(self, sample): |
| 798 |
fromVal, fromUnit, toVal, toUnit = sample |
| 799 |
genExpr = base.getSpecExpr(fromUnit, toUnit).format(fromVal) |
| 800 |
foundVal = eval(genExpr) |
| 801 |
self.assertAlmostEqual(toVal/foundVal, 1, msg=f"{toVal}!={foundVal}") |
| 802 |
|
| 803 |
samples = [ |
| 804 |
# E -> x |
| 805 |
("2-1", "eV", 1.602176634e-19, "J"), |
| 806 |
("20+3", "keV", 0.05390617278104659, "nm"), |
| 807 |
("0.75e-10+0.25e-10", "pJ", 150.9190204696357, "GHz"), |
| 808 |
# λ -> x |
| 809 |
("30+0.2", "nm", 3.02e-8, "m"), |
| 810 |
("0.5+0.5", "AU", 2.0039888121375062, "mHz"), |
| 811 |
# 5 |
| 812 |
("2000+2000", "Angstrom", 4.966114560429395e-22, "kJ"), |
| 813 |
# ν -> x |
| 814 |
("2e10+4.3e11", "mHz", 450, "MHz"), |
| 815 |
("40+10", "MHz", 5995849160.0, "nm"), |
| 816 |
("30+20", "THz", 0.206783383117008, "eV"), |
| 817 |
# fun |
| 818 |
("0.6", "um", 2.0664032899401197, "eV"), |
| 819 |
# 10 |
| 820 |
("3", "apc", 3238.513767810653, "MHz"), |
| 821 |
] |
| 822 |
|
| 823 |
|
| 824 |
class FieldInfoGetter(testhelpers.TestResource): |
| 825 |
def make(self, ignored): |
| 826 |
return adqlglue.DaCHSFieldInfoGetter() |
| 827 |
|
| 828 |
|
| 829 |
class SpecConvUfuncTest(testhelpers.VerboseTest): |
| 830 |
resources = [ |
| 831 |
("ufuncTestTable", _ufuncTestbed), |
| 832 |
("querier", adqltest.adqlQuerier), |
| 833 |
("fig", FieldInfoGetter())] |
| 834 |
|
| 835 |
def testNoFirstArgUnit(self): |
| 836 |
self.assertRaisesWithMsg(adql.UfuncError, |
| 837 |
"specconv: Cannot infer unit of first argument." |
| 838 |
" Perhaps use the three-argument specconv?", |
| 839 |
adql.parseAnnotating, |
| 840 |
("SELECT gavo_specconv(2, 'kHz') FROM test.ufuncex", self.fig)) |
| 841 |
|
| 842 |
def testIncompatibleUnit(self): |
| 843 |
self.assertRaisesWithMsg(adql.UfuncError, |
| 844 |
"deg is not a spectral unit understood here", |
| 845 |
adql.parseAnnotating, |
| 846 |
("SELECT * FROM test.ufuncex where ivo_specconv(ra, 'eV')>2", |
| 847 |
self.fig)) |
| 848 |
|
| 849 |
def testUnannotated(self): |
| 850 |
self.assertRaisesWithMsg(adql.UfuncError, |
| 851 |
"specconv: Cannot infer unit of first argument. Perhaps use the" |
| 852 |
" three-argument specconv?", |
| 853 |
adql.parseAnnotating, |
| 854 |
("SELECT * FROM test.ufuncex where gavo_specconv(3+4, 'eV')>2", |
| 855 |
self.fig)) |
| 856 |
|
| 857 |
def testBorkenUnit(self): |
| 858 |
self.assertRaisesWithMsg(adql.UfuncError, |
| 859 |
"Hurgel is not a spectral unit understood here", |
| 860 |
adql.parseAnnotating, |
| 861 |
("SELECT gavo_specconv(freq, 'cm') as wl" |
| 862 |
" FROM test.ufuncex WHERE gavo_specconv(1.42, 'Hurgel', 'GHz')<1", |
| 863 |
self.fig)) |
| 864 |
|
| 865 |
def testBasic(self): |
| 866 |
res = self.querier.queryADQL( |
| 867 |
"SELECT gavo_specconv(freq, 'eV') as energy" |
| 868 |
" FROM test.ufuncex WHERE gavo_specconv(freq, 'cm')" |
| 869 |
" BETWEEN 1.4 AND 1.5 AND testgroup='specconv'") |
| 870 |
self.assertAlmostEqual(res.rows[0]["energy"], 8.684902090914346e-05) |
| 871 |
self.assertEqual(res.tableDef.getColumnByName("energy").unit, "eV") |
| 872 |
self.assertEqual(res.tableDef.getColumnByName("energy").type, |
| 873 |
"double precision") |
| 874 |
|
| 875 |
def testThreePar(self): |
| 876 |
res = self.querier.queryADQL( |
| 877 |
"SELECT gavo_specconv(freq, 'cm') as wl" |
| 878 |
" FROM test.ufuncex WHERE ABS(gavo_specconv(1.42, 'cm', 'GHz')-freq)<1" |
| 879 |
" AND testgroup='specconv'") |
| 880 |
self.assertAlmostEqual(res.rows[0]["wl"], 1.4275831333333334) |
| 881 |
self.assertEqual(res.tableDef.getColumnByName("wl").unit, "cm") |
| 882 |
|
| 883 |
def testLiteral(self): |
| 884 |
res = self.querier.queryADQL( |
| 885 |
"SELECT TOP 1 gavo_specconv(2, 'keV', 'pm') as en" |
| 886 |
" FROM tap_schema.tables") |
| 887 |
self.assertAlmostEqual(res.rows[0]["en"], 619.920997273) |
| 888 |
self.assertEqual(res.tableDef.getColumnByName("en").unit, "pm") |
| 889 |
self.assertEqual(res.tableDef.getColumnByName("en").type, |
| 890 |
"double precision") |
| 891 |
|
| 892 |
if __name__=="__main__": |
| 893 |
testhelpers.main(UfuncDefTest) |