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 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
|
"""
Tests for ADQL user defined functions and Region expressions.
"""
#c Copyright 2008-2024, the GAVO project <gavo@ari.uni-heidelberg.de>
#c
#c This program is free software, covered by the GNU GPL. See the
#c COPYING file in the source distribution.
from urllib import parse
import unittest
from gavo.helpers import testhelpers
from gavo import adql
from gavo import base
from gavo import rscdesc #noflake: for registration
from gavo import utils
from gavo.protocols import adqlglue
from gavo.protocols import simbadinterface #noflake: for ufunc registration
from gavo.adql import nodes
from gavo.adql import ufunctions
import adqltest
import tresc
# we need to open a database connection here to have the DB initialisation
# update the ufuncs list
_conn = base.getDBConnection("feed")
_conn.close()
del _conn
class BasicTest(unittest.TestCase):
def testRaising(self):
self.assertRaises(adql.UfuncError, adql.parseToTree,
"SELECT x FROM y WHERE gavo_foo(8)=7")
def testFlattening(self):
self.assertEqual(
adql.parseToTree("SELECT x FROM y WHERE 1=gavo_match('x.*', frob)"
).flatten(),
"SELECT x FROM y WHERE 1 = (CASE WHEN frob ~ 'x.*' THEN 1 ELSE 0 END)")
class _UfuncDefinition(testhelpers.TestResource):
def make(self, nodeps):
@adql.userFunction("gavo_testingXXX",
"(x INTEGER) -> INTEGER",
"""
This function returns its argument decreased by one.
This is the end.
""",
polymorphism=[("(x REAL, y INTEGER) -> REAL",
"This is an alternative function that does about the same thing")])
def _f1(args):
if len(args)!=1:
raise adql.UfuncError("gavo_testingXXX takes only a single argument")
return "(%s+1)"%nodes.flatten(args[0])
@adql.userFunction("gavo_testingYYY",
"(x DOUBLE PRECISION) -> DOUBLE PRECISION",
"This function will not work (since it's not defined in the DB)")
def _f2(args):
return None
def clean(self, ignored):
del ufunctions.UFUNC_REGISTRY["GAVO_TESTINGXXX"]
del ufunctions.UFUNC_REGISTRY["GAVO_TESTINGYYY"]
_ufuncDefinition = _UfuncDefinition()
class UfuncDefTest(testhelpers.VerboseTest):
resources = [("ufunc_defined", _ufuncDefinition),
("adqlTestTable", adqltest.adqlTestTable),
("querier", adqltest.adqlQuerier)]
def testUfuncMeta(self):
f = ufunctions.UFUNC_REGISTRY["GAVO_TESTINGXXX"]
self.assertEqual(f.adqlUDF_name, "gavo_testingXXX")
self.assertEqual(f.adqlUDF_signature,
"gavo_testingXXX(x INTEGER) -> INTEGER")
self.assertEqual(f.adqlUDF_doc, "This function returns its argument"
" decreased by one.\n\nThis is the end.")
def testFlattening(self):
self.assertEqual(
adql.parseToTree("SELECT GAVO_TESTINGXXX(frob) FROM x"
).flatten(),
"SELECT (frob+1) FROM x")
def testFlatteningTransparent(self):
self.assertEqual(
adql.parseToTree("SELECT GAVO_TESTINGYYY(CIRCLE('', a, b, c), u) FROM x"
).flatten(),
'SELECT GAVO_TESTINGYYY(CIRCLE(,a,b,c), u) FROM x')
def testQueryInSelectList(self):
self.assertEqual(list(self.querier.queryADQL(
"SELECT GAVO_TESTINGXXX(rV) FROM test.adql where mag<0").rows[0].values()),
[29.])
def testQueryInWhereClause(self):
self.assertEqual(list(self.querier.queryADQL(
"SELECT rV FROM test.adql where GAVO_TESTINGXXX(rV)>0").rows[0].values()),
[0.])
def testPolymorphism(self):
self.assertEqual(
ufunctions.UFUNC_REGISTRY["GAVO_TESTINGXXX"
].adqlUDF_additionalSignatures[0]["signature"],
"gavo_testingXXX(x REAL, y INTEGER) -> REAL")
self.assertEqual(
ufunctions.UFUNC_REGISTRY["GAVO_TESTINGYYY"
].adqlUDF_additionalSignatures, [])
class _UfuncTestbed(tresc.RDDataResource):
"""A table that contains test material for ufuncs.
If you write queries against this, make sure you survive schema
*and data* extensions. They are sure to come as new UDFs need exercise.
Use the kind column as necessary.
"""
rdName = "data/ufuncex.rd"
dataId = "import"
_ufuncTestbed = _UfuncTestbed()
class BuiltinUfuncTest(testhelpers.VerboseTest):
resources = [
("ssaTestTable", tresc.ssaTestTable),
("ufuncTestTable", _ufuncTestbed),
("querier", adqltest.adqlQuerier)]
def testHaswordQuery(self):
self.assertEqual(self.querier.queryADQL(
"select distinct ssa_targname from test.hcdtest where"
" 1=ivo_hasword(ssa_targname, 'rat hole')").rows,
[{'ssa_targname': 'rat hole in the yard'}])
def testHaswordQueryInsensitive(self):
self.assertEqual(self.querier.queryADQL(
"select distinct ssa_targname from test.hcdtest where"
" 1=ivo_hasword(ssa_targname, 'Booger')").rows,
[{'ssa_targname': 'booger star'}])
def testHaswordQueryBorders(self):
self.assertEqual(self.querier.queryADQL(
"select distinct ssa_targname from test.hcdtest where"
" 1=ivo_hasword(ssa_targname, 'ooger')").rows,
[])
def testHashlistSimple(self):
self.assertEqual(self.querier.queryADQL(
"select distinct ivo_hashlist_has('bork#nork#gaob norm', 'nork') as h"
" FROM test.hcdtest").rows,
[{'h': 1}])
def testHashlistBorders(self):
self.assertEqual(self.querier.queryADQL(
"select distinct ivo_hashlist_has('bork#nork#gaob norm', 'ork') as h"
" FROM test.hcdtest").rows,
[{'h': 0}])
def testHashlistNocase(self):
self.assertEqual(self.querier.queryADQL(
"select distinct ivo_hashlist_has('bork#nork#gaob norm', 'nOrk') as h"
" FROM test.hcdtest").rows,
[{'h': 1}])
def testHashlistNocaseList(self):
self.assertEqual(self.querier.queryADQL(
"select distinct ivo_hashlist_has('bork#nOrk#gaob norm', 'nork') as h"
" FROM test.hcdtest").rows,
[{'h': 1}])
def testNocasematch(self):
self.assertEqual(len(self.querier.queryADQL(
"select ssa_targname FROM test.hcdtest"
" WHERE 1=ivo_nocasematch(ssa_targname, 'BOOGER%')").rows),
2)
def testToMJD(self):
self.assertEqual(self.querier.queryADQL(
"select gavo_to_mjd(dt) as mjd from test.ufuncex where testgroup='jd'"
).rows, [{'mjd': 45917.5}])
def testToJD(self):
self.assertEqual(self.querier.queryADQL(
"select ivo_to_jd(dt) as jd from test.ufuncex where testgroup='jd'"
).rows, [{'jd': 2445918.0}])
class RegionTest(unittest.TestCase):
"""tests for sane parsing of our default region expressions.
"""
resources = [("fs", tresc.fakedSimbad)]
def testRaising(self):
"""tests for plausible exceptions.
"""
self.assertRaises(adql.RegionError, adql.parseToTree,
"SELECT x FROM y WHERE 1=CONTAINS(REGION('78y'), REGION('zzy9'))")
self.assertRaises(adql.RegionError, adql.parseToTree,
"SELECT x FROM y WHERE 1=CONTAINS(REGION(dbColumn || otherColumn),"
" CIRCLE('ICRS', 10, 10 ,2))")
def getMorphed(query):
return nodes.flatten(adql.morphPG(adql.parseToTree(query))[1])
class RRFunctionsTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
resources = [("ufuncTestTable", _ufuncTestbed)]
def _runTest(self, sample):
query, expected = sample
self.assertEqualIgnoringAliases(getMorphed(query), expected)
samples = [
("select testgroup from test.ufuncex where 1=ivo_hasword(testgroup, 'abc')",
"SELECT testgroup FROM test.ufuncex WHERE ("
"to_tsvector('english', testgroup)"
" @@ plainto_tsquery('english', 'abc'))"),
("select ivo_hasword(testgroup, 'abc') from test.ufuncex",
"SELECT IVO_HASWORD(testgroup, 'abc') ASWHATEVER FROM test.ufuncex"),
("select testgroup from test.ufuncex where"
" 1=ivo_hashlist_has('a#b#c', testgroup)",
"SELECT testgroup FROM test.ufuncex WHERE lower(testgroup) ="
" ANY(string_to_array(lower('a#b#c'), '#'))"),
("select testgroup from test.ufuncex where"
" 1=ivo_nocasematch(testgroup, 'honk%')",
"SELECT testgroup FROM test.ufuncex WHERE (LOWER(testgroup) like 'honk%')"),
]
class IntervalsMorphTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
resources = [("ufuncTestTable", _ufuncTestbed)]
def _runTest(self, sample):
query, expected = sample
self.assertEqualIgnoringAliases(getMorphed(query), expected)
samples = [
("select testgroup from test.ufuncex where 1=ivo_interval_has(a, iv)",
"SELECT testgroup FROM test.ufuncex WHERE ((a) <@ (iv))"),
("select testgroup from test.ufuncex where 0=ivo_interval_has(a, iv)",
"SELECT testgroup FROM test.ufuncex WHERE NOT ( ((a) <@ (iv)) )"),
]
class AggFunctionsTest(testhelpers.VerboseTest):
resources = [
("ssaTestTable", tresc.ssaTestTable),
("querier", adqltest.adqlQuerier)]
def testStringAggMorph(self):
self.assertEqualIgnoringAliases(
getMorphed("select ivoid, ivo_string_agg(res_subject, ',')"
" from rr.res_subject group by ivoid"),
"SELECT ivoid, string_agg(res_subject, ',') ASWHATEVER"
" FROM rr.res_subject GROUP BY ivoid")
def testStringAddExec(self):
res = self.querier.queryADQL(
"SELECT ivo_string_agg(accref || mime, '#') as goo, ssa_pubdid"
" from test.hcdtest group by ssa_pubdid order by ssa_pubdid").rows
self.assertEqual(res[0]["ssa_pubDID"], 'ivo://test.inv/test1')
self.assertEqual(set(res[0]["goo"].split("#")),
set('data/spec1.ssatestimage/jpeg#data/spec1.ssatest.'
'votapplication/x-votable+xml'.split("#")))
@unittest.skipUnless(testhelpers.hasUDF("IVO_HEALPIX_INDEX"), "pgsphere too old")
class HealpixFunctionsTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest):
resources = [("ufuncTestTable", _ufuncTestbed)]
def _runTest(self, sample):
query, expected = sample
self.assertEqualIgnoringAliases(getMorphed(query), expected)
samples = [
("select ivo_healpix_index(10, point('', ra, dec)) from test.ufuncex",
"SELECT healpix_nest(10, spoint(RADIANS(ra), RADIANS(dec))) ASWHATEVER FROM test.ufuncex"),
("select ivo_healpix_index(10, p) from test.ufuncex",
"SELECT healpix_nest(10, p) ASWHATEVER FROM test.ufuncex"),
("select ivo_healpix_center(10, 40002) from test.ufuncex",
"SELECT center_of_healpix_nest(10, 40002) ASWHATEVER FROM test.ufuncex"),
]
@unittest.skipUnless(testhelpers.hasUDF("IVO_HEALPIX_INDEX"), "pgsphere too old")
class HealpixExecTest(testhelpers.VerboseTest):
resources = [
("querier", adqltest.adqlQuerier),
("test_ufunc", _ufuncTestbed)]
def testIndexAndPoint(self):
res = self.querier.queryADQL(
"SELECT ivo_healpix_index(10, p) as hpi"
" from test.ufuncex WHERE dt<'1985-01-01'")
self.assertEqual(res.rows[0]["hpi"], 2937727)
def testCenterFinding(self):
res = self.querier.queryADQL(
"SELECT ivo_healpix_center(10, ivo_healpix_index(10, ra, dec)) as pt"
" from test.ufuncex WHERE dt<'1985-01-01'")
self.assertAlmostEqual(res.rows[0]["pt"].y, -12.25362992*utils.DEG)
self.assertAlmostEqual(res.rows[0]["pt"].x, 23.51074219*utils.DEG)
self.assertEqual(res.tableDef.columns[0].type, "spoint")
def testHealpixBadarg(self):
self.assertRaisesWithMsg(
adql.UfuncError,
"ivo_healpix_center only takes (index, order) arguments",
getMorphed,
("select ivo_healpix_center() from test.ufuncex",))
class SimbadpointTest(testhelpers.VerboseTest):
resources = [
("test_ufunc", _ufuncTestbed),
("fs", tresc.fakedSimbad)]
def testBadArgcount(self):
self.assertRaisesWithMsg(adql.UfuncError,
"gavo_simbadpoint takes exactly one string literal as argument",
getMorphed,
("SELECT * from test.ufuncex WHERE 1=CONTAINS("
"gavo_simbadpoint('aldebaran', 23), CIRCLE('ICRS', ra, dec, 1))",))
def testBadArgtype(self):
self.assertRaisesWithMsg(adql.UfuncError,
"gavo_simbadpoint takes exactly one string literal as argument",
getMorphed,
("SELECT * from test.ufuncex WHERE 1=CONTAINS("
"gavo_simbadpoint(testgroup), CIRCLE('ICRS', ra, dec, 1))",))
def testBadName(self):
self.assertRaisesWithMsg(adql.UfuncError,
"No simbad position for 'Henker'",
getMorphed,
("SELECT * from test.ufuncex WHERE 1=CONTAINS("
"gavo_simbadpoint('Henker'), CIRCLE('ICRS', ra, dec, 1))",))
def testResolution(self):
self.assertEqualIgnoringAliases(
getMorphed("SELECT * from test.ufuncex WHERE 1=CONTAINS("
"ivo_simbadpoint('M1'), CIRCLE('ICRS', ra, dec, 0.333))"),
"SELECT * FROM test.ufuncex WHERE ((spoint(RADIANS(83.65625),"
" RADIANS(22.0145))) <@ (scircle(spoint(RADIANS(ra), RADIANS(dec)),"
" RADIANS(0.333))))")
def testDistance(self):
self.assertEqual(getMorphed("SELECT * from test.ufuncex WHERE 2>"
"distance(point(ra, dec), gavo_simbadpoint('M1'))"),
"SELECT * FROM test.ufuncex WHERE (spoint(RADIANS(ra), RADIANS(dec))) <@ scircle(spoint(RADIANS(83.65625), RADIANS(22.0145)), RADIANS(2))")
@unittest.skipUnless(testhelpers.hasUDF("IVO_EPOCH_PROP"), "pgsphere too old")
class LegacyMotionTest(testhelpers.VerboseTest):
# Let's eventually remove gavo_apply_pm once ivo_epoch_prop is
# safely installed.
resources = [
("test_ufunc", _ufuncTestbed),
("querier", adqltest.adqlQuerier),]
def testQueryDecTrans(self):
res = self.querier.queryADQL(
"SELECT ivo_apply_pm(ra, dec, 0., -0.1, 50)"
" as newpos from test.ufuncex where testgroup='jd'")
self.assertEqual(res.rows[0]["newpos"].asSTCS('ICRS'),
"Position ICRS 23.5 -17.2373652888")
def testQueryGavoPrefix(self):
res = self.querier.queryADQL(
"SELECT gavo_apply_pm(ra, dec, 0., -0.1, 50)"
" as newpos from test.ufuncex where testgroup='jd'")
self.assertEqual(res.rows[0]["newpos"].asSTCS('ICRS'),
"Position ICRS 23.5 -17.2373652888")
def testQueryRATrans(self):
# Starting pos are (10, 20) and (50, 80)
res = self.querier.queryADQL(
"SELECT ivo_apply_pm(ra, dec, 0.1, 0, 50) as newpos"
" from test.ufuncex where testgroup='intervals'"
" ORDER BY dec")
transPos = [r["newpos"].asDALI() for r in res]
self.assertAlmostEqual(transPos[0][0], 15.30567124204)
self.assertAlmostEqual(transPos[0][1], 19.92106445273)
self.assertTrue(transPos[0][0]-10<transPos[1][0]-50)
def testQueryConstraint(self):
res = self.querier.queryADQL(
"SELECT testgroup from test.ufuncex"
" where 1=Contains("
"ivo_apply_pm(ra, dec, 0.01, -0.01, 23),"
"CIRCLE('ICRS', 23.735, -12.48, 0.01))")
self.assertEqual(testhelpers.pickSingle(res.rows)["testgroup"], "jd")
def testTransformBadarg(self):
self.assertRaisesWithMsg(
adql.UfuncError,
"gavo_apply_pm requires exactly ra, dec, pmra, pmdec, epdist.",
getMorphed,
("select ivo_apply_pm(1, 2) from test.ufuncex",))
@unittest.skipUnless(testhelpers.hasUDF("IVO_EPOCH_PROP"), "pgsphere too old")
class EpochPropTest(testhelpers.VerboseTest):
resources = [
("test_ufunc", _ufuncTestbed),
("querier", adqltest.adqlQuerier),]
def testPropDecTrans(self):
res = self.querier.queryADQL(
"SELECT ivo_epoch_prop(ra, dec, NULL, 0., 400, NULL, 2000, 2022)"
" as newpv from test.ufuncex where testgroup='jd'")
colDef = res.tableDef.getColumnByName("newpv")
self.assertEqual(
colDef.type,
"double precision[6]")
self.assertEqual(
colDef.ucd,
'') # TODO: We really want a UCD for this kind of monster
val = res.rows[0]["newpv"]
self.assertAlmostEqual(val[0], 23.5)
self.assertAlmostEqual(val[1], -12.247555555555)
self.assertEqual(val[2], None)
self.assertAlmostEqual(val[3], 0)
self.assertAlmostEqual(val[4], 399.99999927192)
self.assertEqual(val[5], None)
def testProp6Par(self):
# Starting pos are (10, 20) and (50, 80)
res = self.querier.queryADQL(
"SELECT ivo_epoch_prop(ra, dec, 60, 120, -40, -140, 2022, 1980) as newpos"
" from test.ufuncex where testgroup='intervals'"
" ORDER BY dec")
pv1, pv2 = res.rows[0]["newpos"], res.rows[1]["newpos"]
self.assertAlmostEqual(pv1[0], 9.998510684068139)
self.assertAlmostEqual(pv2[1], 80.0004664014105)
self.assertAlmostEqual(pv1[2], 59.9783590172609)
self.assertAlmostEqual(pv1[3], 119.91380703443762)
self.assertAlmostEqual(pv2[3], 119.91898781569608)
self.assertAlmostEqual(pv2[4], -39.95453831783573)
self.assertAlmostEqual(pv1[5], -140.00025726418698)
def testPropBadarg(self):
self.assertRaisesWithMsg(
adql.UfuncError,
"ivo_epoch_prop gets ra, dec [deg], parallax [mas], pmra, pmdec"
" [mas/yr], radial_velocity [km/s], ref_epoch, out_epoch [yr] arguments",
getMorphed,
("select ivo_epoch_prop(1, 2) from test.ufuncex",))
def testPropPos(self):
# Starting pos are (10, 20) and (50, 80)
res = self.querier.queryADQL(
"SELECT ivo_epoch_prop_pos(ra, dec, 60, 120, -40, -140, 2022, 1980)"
" as newpos"
" from test.ufuncex where testgroup='intervals'"
" ORDER BY dec")
colDef = res.tableDef.getColumnByName("newpos")
self.assertEqual(colDef.type, "spoint")
self.assertEqual(colDef.ucd, "pos")
pv1, pv2 = res.rows[0]["newpos"], res.rows[1]["newpos"]
self.assertAlmostEqual(pv1.asDALI()[0], 9.998510684068163)
self.assertAlmostEqual(pv2.asDALI()[1], 80.00046640141034)
def testPropPos6Arg(self):
# Starting pos are (10, 20) and (50, 80)
res = self.querier.queryADQL(
"SELECT ivo_epoch_prop_pos(ra, dec, 120, -40, 2000+22, 990*2)"
" as newpos"
" from test.ufuncex where testgroup='intervals'"
" ORDER BY dec")
pv1, pv2 = res.rows[0]["newpos"], res.rows[1]["newpos"]
# this is different from above because par/rv are assumed 0 here.
self.assertAlmostEqual(pv1.asDALI()[0], 9.99851014670222)
self.assertAlmostEqual(pv2.asDALI()[1], 80.0004665696594)
def testPropPosWhere(self):
res = self.querier.queryADQL(
"SELECT ra, dec from test.ufuncex where testgroup='intervals'"
" AND distance(ivo_epoch_prop_pos(12, 0, NULL, -120000, 0,"
" NULL, 2000, 2060), point(ra, dec-20))<0.001")
self.assertEqual(testhelpers.pickSingle(res.rows)["ra"], 10.)
def testPropPosMsg(self):
self.assertRaisesWithMsg(base.ValidationError,
"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.",
self.querier.queryADQL,
("SELECT ivo_epoch_prop_pos(ra, dec, 120, -40)"
" as newpos"
" from test.ufuncex where testgroup='intervals'"
" ORDER BY dec",))
def testPropNoPM(self):
res = self.querier.queryADQL(
"SELECT TOP 1"
" ivo_epoch_prop(10, 12, NULL, NULL, NULL, NULL, 2000, 2020) as prop"
" from tap_schema.tables")
self.assertEqual(res.rows[0]["prop"],
[10.0, 12.000000000000002, None, None, None, None])
def testPropNoPMDec(self):
res = self.querier.queryADQL(
"SELECT TOP 1"
" ivo_epoch_prop(10, 12, 3, NULL, NULL, NULL, 2000, 2020) as prop"
" from tap_schema.tables")
self.assertEqual(res.rows[0]["prop"],
[10.0, 12.000000000000002, 3, None, None, None])
def testPropNoPMRA(self):
res = self.querier.queryADQL(
"SELECT TOP 1"
" ivo_epoch_prop(10, 12, NULL, 3, NULL, NULL, 2000, 2020) as prop"
" from tap_schema.tables")
self.assertAlmostEqualVector(res.rows[0]["prop"],
[10.0, 12., None, None, None, None])
def testPropNoRV(self):
res = self.querier.queryADQL(
"SELECT TOP 1"
" ivo_epoch_prop(10, 12, 4, 2, 5, NULL, 2000, 2020) as prop"
" from tap_schema.tables")
self.assertAlmostEqualVector(res.rows[0]["prop"],
[10.000011359341114, 12.000027777777, 3.999999999999,
2.000000206100, 4.99999991755, None])
def testPropNoParallax(self):
res = self.querier.queryADQL(
"SELECT TOP 1"
" ivo_epoch_prop(10, 12, NULL, 2, 5, 4000, 2000, 2020) as prop"
" from tap_schema.tables")
self.assertAlmostEqualVector(res.rows[0]["prop"],
[10.000011359341022, 12.000027777777323, None,
2.0000001733735733, 4.999999835741298, None])
class IntervalQueryTest(testhelpers.VerboseTest,
metaclass=testhelpers.SamplesBasedAutoTest):
resources = [
("test_ufunc", _ufuncTestbed),
("querier", adqltest.adqlQuerier),]
def _runTest(self, sample):
constraint, expected = sample
res = self.querier.queryADQL(
"select count(*) as ct from test.ufuncex"
" where testgroup='intervals'"
" and "+constraint)
self.assertEqual(res.rows[0]["ct"], expected)
samples = [
("1=ivo_interval_overlaps(ra, dec, 0, 5)", 0),
("ivo_interval_overlaps(ra, dec, 5, 10)=1", 1),
("1=ivo_interval_overlaps(ra, dec, 5, 25)", 1),
("1=ivo_interval_overlaps(ra, dec, 20, 25)", 1),
("1=ivo_interval_overlaps(ra, dec, 25, 35)", 0),
#5
("1=ivo_interval_overlaps(ra, dec, 5, 55)", 2),
("1=ivo_interval_overlaps(ra, dec, 5, 155)", 2),
("1!=ivo_interval_overlaps(ra, dec, 5, 155)", 0),
("ivo_interval_overlaps(ra, dec, 5, 155)=0", 0),
("1=ivo_interval_overlaps(COORD1(p), COORD2(p), 5, 25)", 1),
#10
("1=ivo_interval_overlaps(COORD2(p), COORD1(p), 5, 25)", 0),
("1=ivo_interval_overlaps(ra, dec, 15, 15)", 1),
("1=ivo_interval_overlaps(dec, dec, 15, 25)", 1),
("0=ivo_interval_overlaps(ra, dec, 150, 250)", 2),
("ivo_interval_overlaps(ra, dec, -100, -30)=0", 2),
]
class HistogramTest(testhelpers.VerboseTest):
resources = [
("test_ufunc", _ufuncTestbed),
("querier", adqltest.adqlQuerier),]
def testBasic(self):
res = self.querier.queryADQL(
"SELECT ivo_histogram(ra, 20., 60., 2) as ras,"
" ivo_histogram(dec, -20., 81., 10) as decs from test.ufuncex"
" WHERE testgroup IN ('jd', 'intervals')")
row = testhelpers.pickSingle(res.rows)
self.assertEqual(row["ras"], [1, 1, 1, 0])
self.assertEqual(row["decs"],
[0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0])
def testAlias(self):
res = self.querier.queryADQL(
"SELECT gavo_histogram(ra, 20., 60., 2) as ras"
" from test.ufuncex"
" WHERE testgroup IN ('jd', 'intervals')")
self.assertEqual(res.rows[0]["ras"], [1, 1, 1, 0])
def testBadarg(self):
self.assertRaisesWithMsg(
adql.UfuncError,
"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).",
getMorphed,
("select ivo_histogram(1, 2) from test.ufuncex",))
class TransformTest(testhelpers.VerboseTest):
resources = [
("test_ufunc", _ufuncTestbed),
("querier", adqltest.adqlQuerier),]
def testMorphing(self):
self.assertEqual(
getMorphed("SELECT gavo_transform('ICRS', 'GALACTIC', p) as ps"
" from test.ufuncex"),
"SELECT (p)+strans(1.346356097441,-1.097319001837,0.574770524729) AS ps FROM test.ufuncex")
def testICRSGalactic(self):
res = self.querier.queryADQL(
"SELECT ivo_geom_transform('ICRS', 'GALACTIC', p) as ps, p"
" from test.ufuncex where testgroup='jd'")
ps = testhelpers.pickSingle(res.rows)["ps"]
# XXX TODO: figure out where the precision goes -- this should have
# about 13 s.f. rather than just 8.
self.assertAlmostEqual(ps.x/utils.DEG, 95.79525025241531, places=5)
self.assertAlmostEqual(ps.y/utils.DEG, 48.55779439628186, places=5)
self.assertEqual(res.tableDef.getColumnByName("ps").ucd, "pos.galactic")
def testGalacticFK4(self):
res = self.querier.queryADQL(
"SELECT gavo_transform('GALACTIC', 'FK4', p) as ps, p"
" from test.ufuncex where testgroup='jd'")
self.assertEqual(
res.tableDef.getColumnByName("ps").type,
"spoint")
ps = testhelpers.pickSingle(res.rows)["ps"]
self.assertAlmostEqual(ps.x/utils.DEG, 162.4867829887925)
self.assertAlmostEqual(ps.y/utils.DEG, 15.59041298060233)
self.assertEqual(res.tableDef.getColumnByName("ps").ucd, "pos.eq")
def testTransformImmediate(self):
res = self.querier.queryADQL(
"SELECT TOP 1 gavo_transform('GALACTIC', 'ICRS',"
" CIRCLE(10, 10, 3)) AS c"
" from test.ufuncex")
self.assertEqual(
res.tableDef.getColumnByName("c").type,
"scircle")
c = res.rows[0]["c"]
self.assertAlmostEqual(c.center.x/utils.DEG, 262.89288250964)
self.assertAlmostEqual(c.radius/utils.DEG, 3)
self.assertEqual(res.tableDef.getColumnByName("c").ucd, "pos.eq")
def testInvalidRefsys(self):
self.assertRaisesWithMsg(base.ValidationError,
"Field query: Cannot compute transformation between J2015 and FK4: Unknown reference frame: J2015",
self.querier.queryADQL,
("SELECT gavo_transform('J2015', 'FK4', p) as ps, p"
" from test.ufuncex where testgroup='jd'",))
def testTransformBadarg(self):
self.assertRaisesWithMsg(
adql.UfuncError,
"gavo_transform takes exactly three arguments",
getMorphed,
("select gavo_transform(1, 2) from test.ufuncex",))
class URLFuncsTest(testhelpers.VerboseTest):
resources = [('conn', tresc.dbConnection),
("ufuncTestTable", _ufuncTestbed)]
def testEscaping(self):
# Well, fair enough: this is not an ADQL UDF at this moment.
# But it may become one and is defined in //adql, so I'll test it
# here.
input = '10% more than 2+5 & fewer = #sql '
res = list(self.conn.queryToDicts(
"SELECT gavo_urlescape(%(input)s) as r", {"input": input}))
self.assertEqual(res[0]["r"],
"10%25%20more%20than%202%2B5%20%26%20fewer%20%3D%20%23sql%20")
self.assertEqual(parse.unquote(res[0]["r"]), input)
def testAuthority(self):
res = list(self.conn.query(
"select gavo_getauthority('ivo://org.gavo.dc/foo:bar#quoox') as x"
" FROM test.ufuncex where testgroup='jd'"))
self.assertEqual(res[0][0], "org.gavo.dc")
def testAuthorityBadarg(self):
self.assertRaisesWithMsg(
adql.UfuncError,
"gavo_getauthority only takes an ivoid argument.",
getMorphed,
("select gavo_getauthority(1, 2) from test.ufuncex",))
class NormalRandomTest(testhelpers.VerboseTest):
resources = [
("test_ufunc", _ufuncTestbed),
("querier", adqltest.adqlQuerier),]
def testMorph(self):
self.assertEqual(
getMorphed("SELECT ra+gavo_normal_random(sin(radians(dec)), dec/10)"
" as g from test.ufuncex"),
'SELECT ra + (((random()+random()+random()+random()+random()+random()+random()+random()+random()+random()-5)*(dec / 10))+(SIN(RADIANS(dec)))) AS g FROM test.ufuncex')
def testMessage(self):
self.assertRaisesWithMsg(adql.UfuncError,
"gavo_normal_random takes mu, sigma arguments.",
getMorphed,
("SELECT ra+ivo_normal_random(dec/10) as g from test.ufuncex",))
def testValues(self):
res = self.querier.queryADQL(
"SELECT gavo_normal_random(10, 1) as r FROM test.ufuncex")
vals = [r["r"] for r in res.rows]
self.assertTrue(min(vals)>-10, "20-sigma outlier in normal_random?")
self.assertTrue(max(vals)<30, "20-sigma outlier in normal_random?")
var = sum([(v-10)**2 for v in vals])
self.assertTrue(var>1e-2, "No variance in normal_random?")
@unittest.skipUnless(testhelpers.hasUDF("GAVO_MOCUNION"), "pgsphere too old")
class MocTest(testhelpers.VerboseTest):
resources = [
("test_ufunc", _ufuncTestbed),
("querier", adqltest.adqlQuerier),]
def testUnion(self):
res = self.querier.queryADQL(
"SELECT gavo_mocunion(a.m, b.m) as res"
" FROM test.ufuncex AS a JOIN test.ufuncex AS b USING (testgroup)"
" WHERE testgroup='moc' and a.m!=b.m")
mocs = set(r['res'].asASCII() for r in res)
self.assertEqual(mocs, set(["6/33 38-40 7/49-51"]))
self.assertEqual(res.tableDef.columns[0].type, "smoc")
def testIntersect(self):
res = self.querier.queryADQL(
"SELECT gavo_mocintersect(a.m, b.m) as res"
" FROM test.ufuncex AS a JOIN test.ufuncex AS b USING (testgroup)"
" WHERE testgroup='moc' and a.m!=b.m")
mocs = set(r['res'].asASCII() for r in res)
self.assertEqual(mocs, set(["7/50"]))
self.assertEqual(res.tableDef.columns[0].type, "smoc")
class VocMatchTest(testhelpers.VerboseTest):
def testDatalinkExpansion(self):
self.assertEqualIgnoringAliases(
getMorphed("SELECT * FROM whatever"
" WHERE 1=gavo_vocmatch('datalink/core', 'calibration', semantics)"),
"SELECT * FROM whatever WHERE semantics IN ('calibration',"
" 'bias', 'dark', 'flat')")
def testInvertedDatalinkExpansion(self):
self.assertEqual(
getMorphed("SELECT * FROM whatever"
" WHERE gavo_vocmatch('datalink/core', 'calibration', semantics)=0"),
"SELECT * FROM whatever WHERE NOT ( semantics IN ('calibration',"
" 'bias', 'dark', 'flat') )")
def testUnknownVocabulary(self):
self.assertRaisesWithMsg(
base.ReportableError,
"No such vocabulary: free invention",
getMorphed,
("SELECT * FROM whatever"
" WHERE 1=gavo_vocmatch('free invention', 'calibration', semantics)",))
def testUnknownTerm(self):
self.assertRaisesWithMsg(
adql.UfuncError,
"'müll' is not a term in the vocabulary"
" http://www.ivoa.net/rdf/datalink/core",
getMorphed,
("SELECT * FROM whatever"
" WHERE 1=gavo_vocmatch('datalink/core', 'müll', semantics)",))
class SpectralConversionTest(testhelpers.VerboseTest,
metaclass=testhelpers.SamplesBasedAutoTest):
def _runTest(self, sample):
fromVal, fromUnit, toVal, toUnit = sample
genExpr = base.getSpecExpr(fromUnit, toUnit).format(fromVal)
foundVal = eval(genExpr)
self.assertAlmostEqual(toVal/foundVal, 1, msg=f"{toVal}!={foundVal}")
samples = [
# E -> x
("2-1", "eV", 1.602176634e-19, "J"),
("20+3", "keV", 0.05390617278104659, "nm"),
("0.75e-10+0.25e-10", "pJ", 150.9190204696357, "GHz"),
# λ -> x
("30+0.2", "nm", 3.02e-8, "m"),
("0.5+0.5", "AU", 2.0039888121375062, "mHz"),
# 5
("2000+2000", "Angstrom", 4.966114560429395e-22, "kJ"),
# ν -> x
("2e10+4.3e11", "mHz", 450, "MHz"),
("40+10", "MHz", 5995849160.0, "nm"),
("30+20", "THz", 0.206783383117008, "eV"),
# fun
("0.6", "um", 2.0664032899401197, "eV"),
# 10
("3", "apc", 3238.513767810653, "MHz"),
]
class FieldInfoGetter(testhelpers.TestResource):
def make(self, ignored):
return adqlglue.DaCHSFieldInfoGetter()
class SpecConvUfuncTest(testhelpers.VerboseTest):
resources = [
("ufuncTestTable", _ufuncTestbed),
("querier", adqltest.adqlQuerier),
("fig", FieldInfoGetter())]
def testNoFirstArgUnit(self):
self.assertRaisesWithMsg(adql.UfuncError,
"specconv: Cannot infer unit of first argument."
" Perhaps use the three-argument specconv?",
adql.parseAnnotating,
("SELECT gavo_specconv(2, 'kHz') FROM test.ufuncex", self.fig))
def testIncompatibleUnit(self):
self.assertRaisesWithMsg(adql.UfuncError,
"deg is not a spectral unit understood here",
adql.parseAnnotating,
("SELECT * FROM test.ufuncex where ivo_specconv(ra, 'eV')>2",
self.fig))
def testUnannotated(self):
self.assertRaisesWithMsg(adql.UfuncError,
"specconv: Cannot infer unit of first argument. Perhaps use the"
" three-argument specconv?",
adql.parseAnnotating,
("SELECT * FROM test.ufuncex where gavo_specconv(3+4, 'eV')>2",
self.fig))
def testBorkenUnit(self):
self.assertRaisesWithMsg(adql.UfuncError,
"Hurgel is not a spectral unit understood here",
adql.parseAnnotating,
("SELECT gavo_specconv(freq, 'cm') as wl"
" FROM test.ufuncex WHERE gavo_specconv(1.42, 'Hurgel', 'GHz')<1",
self.fig))
def testBasic(self):
res = self.querier.queryADQL(
"SELECT gavo_specconv(freq, 'eV') as energy"
" FROM test.ufuncex WHERE gavo_specconv(freq, 'cm')"
" BETWEEN 1.4 AND 1.5 AND testgroup='specconv'")
self.assertAlmostEqual(res.rows[0]["energy"], 8.684902090914346e-05)
self.assertEqual(res.tableDef.getColumnByName("energy").unit, "eV")
self.assertEqual(res.tableDef.getColumnByName("energy").type,
"double precision")
def testThreePar(self):
res = self.querier.queryADQL(
"SELECT gavo_specconv(freq, 'cm') as wl"
" FROM test.ufuncex WHERE ABS(gavo_specconv(1.42, 'cm', 'GHz')-freq)<1"
" AND testgroup='specconv'")
self.assertAlmostEqual(res.rows[0]["wl"], 1.4275831333333334)
self.assertEqual(res.tableDef.getColumnByName("wl").unit, "cm")
def testLiteral(self):
res = self.querier.queryADQL(
"SELECT TOP 1 gavo_specconv(2, 'keV', 'pm') as en"
" FROM tap_schema.tables")
self.assertAlmostEqual(res.rows[0]["en"], 619.920997273)
self.assertEqual(res.tableDef.getColumnByName("en").unit, "pm")
self.assertEqual(res.tableDef.getColumnByName("en").type,
"double precision")
if __name__=="__main__":
testhelpers.main(UfuncDefTest)
|