| 1 |
""" |
| 2 |
Tests for function definitions and applications. |
| 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 os |
| 12 |
import re |
| 13 |
|
| 14 |
from gavo.helpers import testhelpers |
| 15 |
|
| 16 |
from gavo import base |
| 17 |
from gavo import rscdef |
| 18 |
from gavo import utils |
| 19 |
from gavo.base import macros |
| 20 |
from gavo.rscdef import procdef |
| 21 |
|
| 22 |
|
| 23 |
class TestApp(procdef.ProcApp): |
| 24 |
name_ = "testApp" |
| 25 |
requiredType = "t_t" |
| 26 |
formalArgs = "source, dest" |
| 27 |
|
| 28 |
|
| 29 |
class Foo(base.Structure, macros.MacroPackage): |
| 30 |
name_ = "foo" |
| 31 |
_apps = base.StructListAttribute("apps", childFactory=TestApp) |
| 32 |
_defs = base.StructListAttribute("defs", childFactory=procdef.ProcDef) |
| 33 |
|
| 34 |
def __init__(self, parent, **kwargs): |
| 35 |
base.Structure.__init__(self, parent, **kwargs) |
| 36 |
self.source, self.dest = {}, {} |
| 37 |
|
| 38 |
def onElementComplete(self): |
| 39 |
self._cApps = [a.compile() for a in self.apps] |
| 40 |
super().onElementComplete() |
| 41 |
|
| 42 |
def runApps(self): |
| 43 |
for a in self._cApps: |
| 44 |
a(self.source, self.dest) |
| 45 |
|
| 46 |
def macro_foobar(self): |
| 47 |
return "Foobar" |
| 48 |
|
| 49 |
def macro_mesmerize(self, arg): |
| 50 |
return "".join(reversed(list(arg))) |
| 51 |
|
| 52 |
|
| 53 |
class NoDefTest(testhelpers.VerboseTest): |
| 54 |
"""tests for ProcApps without procDefs. |
| 55 |
""" |
| 56 |
def testVerySimple(self): |
| 57 |
f = base.parseFromString(Foo, "<foo><testApp name='x'/></foo>") |
| 58 |
f.runApps() |
| 59 |
self.assertEqual(f.dest, {}) |
| 60 |
|
| 61 |
def testDoesSomething(self): |
| 62 |
f = base.parseFromString(Foo, "<foo><testApp name='x'><code>" |
| 63 |
"\t\tdest['fobba'] = source</code></testApp></foo>") |
| 64 |
f.runApps() |
| 65 |
self.assertEqual(f.dest, {"fobba": {}}) |
| 66 |
|
| 67 |
def testMultiline(self): |
| 68 |
f = base.parseFromString(Foo, "<foo><testApp name='x'><code>\n" |
| 69 |
"\t\tfor i in range(source['count']):\n" |
| 70 |
"\t\t\tdest[i] = 42-i</code></testApp></foo>") |
| 71 |
f.source["count"] = 2 |
| 72 |
f.runApps() |
| 73 |
self.assertEqual(f.dest, {0: 42, 1: 41}) |
| 74 |
|
| 75 |
def testWithParSetup(self): |
| 76 |
f = base.parseFromString(Foo, "<foo><testApp name='x'><code>\n" |
| 77 |
"\t\tfor i in range(count):\n" |
| 78 |
"\t\t\tdest[i] = 42-i</code>\n" |
| 79 |
"<setup><par key='count'>2</par></setup>" |
| 80 |
"</testApp></foo>") |
| 81 |
f.runApps() |
| 82 |
self.assertEqual(f.dest, {0: 42, 1: 41}) |
| 83 |
|
| 84 |
def testWithParAndBinding(self): |
| 85 |
f = base.parseFromString(Foo, "<foo><testApp name='x'><code>\n" |
| 86 |
"\t\tfor i in range(count):\n" |
| 87 |
"\t\t\tdest[i] = 42-i</code>\n" |
| 88 |
"<setup><par key='count'/></setup><bind key='count'>2</bind>" |
| 89 |
"</testApp></foo>") |
| 90 |
f.runApps() |
| 91 |
self.assertEqual(f.dest, {0: 42, 1: 41}) |
| 92 |
|
| 93 |
def testUnboundFails(self): |
| 94 |
self.assertRaisesWithMsg(base.StructureError, |
| 95 |
"At IO:'<foo><testApp name='x'><code> for i in range(count): ...', (4, 33):" |
| 96 |
" Parameter count is not defaulted in x and thus must be bound.", |
| 97 |
base.parseFromString, (Foo, "<foo><testApp name='x'><code>\n" |
| 98 |
"\t\tfor i in range(count):\n" |
| 99 |
"\t\t\tdest[i] = 42-i</code>\n" |
| 100 |
"<setup><par key='count'/></setup>" |
| 101 |
"</testApp></foo>")) |
| 102 |
|
| 103 |
def testBadKeyFails(self): |
| 104 |
self.assertRaisesWithMsg(base.StructureError, |
| 105 |
"At IO:'<foo><testApp name='x'><setup><par key=''/></setup></...', (1, 43):" |
| 106 |
" '' is not a valid value for name", |
| 107 |
base.parseFromString, (Foo, "<foo><testApp name='x'>" |
| 108 |
"<setup><par key=''/></setup>" |
| 109 |
"</testApp></foo>")) |
| 110 |
self.assertRaisesWithMsg(base.StructureError, |
| 111 |
"At IO:'<foo><testApp name='x'><setup><par key='a key'/></set...', (1, 48):" |
| 112 |
" 'a key' is not a valid value for name", |
| 113 |
base.parseFromString, (Foo, "<foo><testApp name='x'>" |
| 114 |
"<setup><par key='a key'/></setup>" |
| 115 |
"</testApp></foo>")) |
| 116 |
|
| 117 |
def testWithMacros(self): |
| 118 |
f = base.parseFromString(Foo, "<foo><testApp name='x'><code>\n" |
| 119 |
r"dest[\foobar] = weird+'\\\\n'</code>" |
| 120 |
r"<setup><par key='weird'>'\mesmerize{something}'</par>" |
| 121 |
"<par key='Foobar'>'res'</par></setup>" |
| 122 |
"</testApp></foo>") |
| 123 |
f.runApps() |
| 124 |
self.assertEqual(f.dest, {'res': r'gnihtemos\n'}) |
| 125 |
|
| 126 |
def testParentPresent(self): |
| 127 |
f = base.parseFromString(Foo, "<foo><testApp name='x'><code>" |
| 128 |
"\t\tdest['isRight'] = 'runApps' in dir(parent)</code></testApp></foo>") |
| 129 |
f.runApps() |
| 130 |
self.assertEqual(f.dest, {"isRight": True}) |
| 131 |
|
| 132 |
|
| 133 |
class WithDefTest(testhelpers.VerboseTest): |
| 134 |
def testSimpleDef(self): |
| 135 |
f = base.parseFromString(Foo, "<foo><procDef type='t_t' id='b'>" |
| 136 |
"<code>dest['par']='const'</code></procDef>" |
| 137 |
"<testApp name='x' procDef='b'/>" |
| 138 |
"</foo>") |
| 139 |
f.runApps() |
| 140 |
self.assertEqual(f.dest, {"par": 'const'}) |
| 141 |
|
| 142 |
def testPDDefaulting(self): |
| 143 |
f = base.parseFromString(Foo, "<foo><procDef type='t_t' id='b'>" |
| 144 |
"<setup><par key='par'>'const'</par></setup>" |
| 145 |
"<code>dest['par']=par</code></procDef>" |
| 146 |
"<testApp name='x' procDef='b'/>" |
| 147 |
"</foo>") |
| 148 |
f.runApps() |
| 149 |
self.assertEqual(f.dest, {"par": 'const'}) |
| 150 |
|
| 151 |
def testPDRebinding(self): |
| 152 |
f = base.parseFromString(Foo, "<foo><procDef type='t_t' id='b'>" |
| 153 |
"<setup><par key='par'>'const'</par></setup>" |
| 154 |
"<code>dest['par']=par</code></procDef>" |
| 155 |
"<testApp name='x' procDef='b'><bind key='par'>'noconst'</bind>" |
| 156 |
"</testApp></foo>") |
| 157 |
f.runApps() |
| 158 |
self.assertEqual(f.dest, {"par": 'noconst'}) |
| 159 |
|
| 160 |
def testFilling(self): |
| 161 |
f = base.parseFromString(Foo, "<foo><procDef type='t_t' id='b'>" |
| 162 |
"<setup><par key='par'/></setup>" |
| 163 |
"<code>dest['par']=par</code></procDef>" |
| 164 |
"<testApp name='x' procDef='b'><bind key='par'>'noconst'</bind>" |
| 165 |
"</testApp></foo>") |
| 166 |
f.runApps() |
| 167 |
self.assertEqual(f.dest, {"par": 'noconst'}) |
| 168 |
|
| 169 |
def testNoFillRaises(self): |
| 170 |
self.assertRaisesWithMsg(base.StructureError, |
| 171 |
"At IO:'<foo><procDef type='t_t' id='b'><setup><par key='par'...', (1, 131):" |
| 172 |
" Parameter par is not defaulted in x and thus must be bound.", |
| 173 |
base.parseFromString, (Foo, "<foo><procDef type='t_t' id='b'>" |
| 174 |
"<setup><par key='par'/></setup>" |
| 175 |
"<code>dest['par']=par</code></procDef>" |
| 176 |
"<testApp name='x' procDef='b'>" |
| 177 |
"</testApp></foo>")) |
| 178 |
|
| 179 |
def testFillRandomRaises(self): |
| 180 |
self.assertRaisesWithMsg(base.StructureError, |
| 181 |
"At IO:'<foo><procDef type='t_t' id='b'><setup><par key='par'...', (1, 190):" |
| 182 |
" May not bind non-existing parameter(s) random.", |
| 183 |
base.parseFromString, (Foo, "<foo><procDef type='t_t' id='b'>" |
| 184 |
"<setup><par key='par'/></setup>" |
| 185 |
"<code>dest['par']=par</code></procDef>" |
| 186 |
"<testApp name='x' procDef='b'><bind key='random'>'noconst'</bind>" |
| 187 |
"<bind key='par'>4</bind>" |
| 188 |
"</testApp></foo>")) |
| 189 |
|
| 190 |
|
| 191 |
class OriginalTest(testhelpers.VerboseTest): |
| 192 |
"""tests for ProcApp's setup inheritance. |
| 193 |
""" |
| 194 |
def testSCSCondDesc(self): |
| 195 |
from gavo import svcs |
| 196 |
base.caches.getRD("__system__/scs") |
| 197 |
core = base.parseFromString(svcs.DBCore, |
| 198 |
'<dbCore queriedTable="data/test#adql"><condDesc original=' |
| 199 |
'"//scs#humanInput"/></dbCore>') |
| 200 |
self.assertTrue("genQuery", core.condDescs[0].phraseMaker.getFuncCode()) |
| 201 |
self.assertTrue("genQuery", core.condDescs[0].phraseMaker.getSetupCode()) |
| 202 |
|
| 203 |
|
| 204 |
class SetupTest(testhelpers.VerboseTest): |
| 205 |
# Tests going after combining setup parameters and code |
| 206 |
def testMultiSetupPars(self): |
| 207 |
f = base.parseFromString(Foo, """<foo><procDef type='t_t' id='b'> |
| 208 |
<setup><par name="unk1">"def1"</par></setup> |
| 209 |
<setup><par name="unk2">"def2"</par></setup> |
| 210 |
</procDef></foo>""") |
| 211 |
pars = f.defs[0].getSetupPars() |
| 212 |
pars.sort(key=lambda p: p.key) |
| 213 |
self.assertEqual(pars[0].key, "unk1") |
| 214 |
self.assertEqual(pars[1].content_, '"def2"') |
| 215 |
|
| 216 |
def testOverridingJoin(self): |
| 217 |
f = base.parseFromString(Foo, """<foo><procDef type='t_t' id='b'> |
| 218 |
<setup><par name="unk1">"def1"</par></setup> |
| 219 |
<setup><par name="unk2">"def2"</par></setup></procDef> |
| 220 |
<procDef type="t_t" id="u" original="b"> |
| 221 |
<setup><par name="unk1">"overridden"</par></setup></procDef></foo>""") |
| 222 |
pars = f.defs[1].getSetupPars() |
| 223 |
pars.sort(key=lambda p: p.key) |
| 224 |
self.assertEqual(len(pars), 2) |
| 225 |
self.assertEqual(pars[0].key, "unk1") |
| 226 |
self.assertEqual(pars[0].content_, '"overridden"') |
| 227 |
self.assertEqual(pars[1].content_, '"def2"') |
| 228 |
|
| 229 |
def testApplyJoin(self): |
| 230 |
f = base.parseFromString(Foo, """<foo><procDef type='t_t' id='b'> |
| 231 |
<setup><par name="unk1">"def1"</par></setup> |
| 232 |
<setup><par name="unk2">"def2"</par></setup></procDef> |
| 233 |
<testApp procDef="b"> |
| 234 |
<setup><par name="unk1">"overridden"</par></setup></testApp></foo>""") |
| 235 |
pars = f.apps[0].getSetupPars() |
| 236 |
pars.sort(key=lambda p: p.key) |
| 237 |
self.assertEqual(len(pars), 2) |
| 238 |
self.assertEqual(pars[0].key, "unk1") |
| 239 |
self.assertEqual(pars[0].content_, '"overridden"') |
| 240 |
self.assertEqual(pars[1].content_, '"def2"') |
| 241 |
|
| 242 |
def testInheritedSetup(self): |
| 243 |
f = base.parseFromString(Foo, """<foo><procDef type='t_t' id='b'> |
| 244 |
<setup><par name="unk1">"def1"</par> |
| 245 |
<code> |
| 246 |
def f1(i): return unk1*i</code></setup> |
| 247 |
<setup><par name="unk2">4</par> |
| 248 |
<code>def f2(s): return s*unk2</code></setup> |
| 249 |
<setup><par name="c"/><par name="n"/></setup> |
| 250 |
<code> |
| 251 |
return f2(c)+f1(n) |
| 252 |
</code></procDef> |
| 253 |
<testApp procDef="b"><bind key="c">"a"</bind> |
| 254 |
<bind name="n">2</bind></testApp></foo>""") |
| 255 |
func = f.apps[0].compile() |
| 256 |
self.assertEqual(func(None, None), "aaaadef1def1") |
| 257 |
|
| 258 |
def testMultiLateSetup(self): |
| 259 |
f = base.parseFromString(Foo, """<foo><procDef type='t_t' id='b'> |
| 260 |
<setup><par late="True" name="unk1">"def1"</par> |
| 261 |
<code>def f1(a, i): return a*i</code></setup> |
| 262 |
</procDef> |
| 263 |
<testApp procDef="b"> |
| 264 |
<setup><par late="True" name="unk1">"over"</par></setup> |
| 265 |
<code>return f1(unk1, 2)</code></testApp></foo>""") |
| 266 |
func = f.apps[0].compile() |
| 267 |
self.assertEqual(func(None, None), "overover") |
| 268 |
|
| 269 |
def testModules(self): |
| 270 |
f = base.parseFromString(Foo, """<foo><procDef type='t_t' id='b'> |
| 271 |
<setup imports="random,gavo.utils,gavo.utils.pyfits"/> |
| 272 |
</procDef> |
| 273 |
<testApp procDef="b"> |
| 274 |
<setup imports="fnmatch,gavo.rscdef,gavo.rscdef.procdef"> |
| 275 |
<code> |
| 276 |
def l(mod): |
| 277 |
return len(dir(mod)) |
| 278 |
</code> |
| 279 |
</setup> |
| 280 |
<code>return l(random)+l(utils)+l(pyfits)+l(fnmatch |
| 281 |
)+l(rscdef)+l(procdef)</code></testApp></foo>""") |
| 282 |
func = f.apps[0].compile() |
| 283 |
res = func(None, None) |
| 284 |
self.assertTrue(res>100) |
| 285 |
|
| 286 |
|
| 287 |
class TypeSafetyTest(testhelpers.VerboseTest): |
| 288 |
def testRejects(self): |
| 289 |
self.assertRaisesWithMsg(base.StructureError, |
| 290 |
'At IO:\'<foo><testApp procDef="//soda#trivialFormatter"/></foo>\', (1, 49):' |
| 291 |
" The procDef" |
| 292 |
" trivialFormatter has type dataFormatter, but here t_t procDefs" |
| 293 |
" are required.", |
| 294 |
base.parseFromString, |
| 295 |
(Foo, '<foo><testApp procDef="//soda#trivialFormatter"/></foo>')) |
| 296 |
|
| 297 |
|
| 298 |
class SourceKeepingTest(testhelpers.VerboseTest): |
| 299 |
def testSourceAccess(self): |
| 300 |
f = base.parseFromString(Foo, "<foo><testApp><code>" |
| 301 |
"res = 1+1\n" |
| 302 |
"this = broken\n" |
| 303 |
# "import ipdb;ipdb.set_trace()\n" |
| 304 |
"dest['res'] = res" |
| 305 |
"</code></testApp></foo>") |
| 306 |
try: |
| 307 |
f.runApps() |
| 308 |
except Exception: |
| 309 |
self.assertTrue("this = broken" in utils.getTracebackAsString()) |
| 310 |
|
| 311 |
def testSetupSourceAccess(self): |
| 312 |
f = base.parseFromString(Foo, """<foo><testApp><setup><code> |
| 313 |
def brokenFunction(): |
| 314 |
a = 1+1 |
| 315 |
return 1/(a-2) # this comment is in the traceback |
| 316 |
</code></setup> |
| 317 |
<code> |
| 318 |
brokenFunction() |
| 319 |
</code> |
| 320 |
</testApp></foo>""") |
| 321 |
try: |
| 322 |
f.runApps() |
| 323 |
except Exception: |
| 324 |
self.assertTrue("return 1/(a-2) # this comment is in the traceback" |
| 325 |
in utils.getTracebackAsString()) |
| 326 |
|
| 327 |
|
| 328 |
class DepreciationTest(testhelpers.VerboseTest): |
| 329 |
def testMessage(self): |
| 330 |
with testhelpers.messageCollector() as messages: |
| 331 |
base.parseFromString(Foo, "<foo><procDef type='t_t' id='b'>\n" |
| 332 |
"<deprecated>This is a test depreciation</deprecated>\n" |
| 333 |
"<code>dest['par']='const'</code></procDef>\n" |
| 334 |
"<testApp name='x' procDef='b'/>" |
| 335 |
"</foo>") |
| 336 |
self.assertEqual(messages.events, |
| 337 |
[('Warning', |
| 338 |
("IO:'<foo><procDef type='t_t' id='b'> <deprecated>This is ...', line 4," |
| 339 |
" procApp x: This is a test depreciation",), {})]) |
| 340 |
|
| 341 |
def testWithActiveTag(self): |
| 342 |
from gavo import api |
| 343 |
with testhelpers.messageCollector() as messages: |
| 344 |
with testhelpers.testFile( |
| 345 |
os.path.join(base.getConfig("inputsDir"), "procborken.rd"), |
| 346 |
"""<resource schema="test"> |
| 347 |
<procDef id="dep"> |
| 348 |
<deprecated>gone</deprecated> |
| 349 |
</procDef> |
| 350 |
<STREAM id="break"> |
| 351 |
<rowmaker><apply name="my" procDef="dep"/></rowmaker> |
| 352 |
</STREAM> |
| 353 |
<data> |
| 354 |
<make> |
| 355 |
<table/> |
| 356 |
<FEED source="break"/> |
| 357 |
</make> |
| 358 |
</data> |
| 359 |
</resource>"""): |
| 360 |
api.getRD("procborken") |
| 361 |
type, (msg,), _ = messages.events[0] |
| 362 |
self.assertEqual( |
| 363 |
re.sub(".*/", "", msg), |
| 364 |
"procborken.rd, line 11, procApp my: gone") |
| 365 |
|
| 366 |
|
| 367 |
class ParameterAliasingTest(testhelpers.VerboseTest): |
| 368 |
_procDef = """ |
| 369 |
<foo> |
| 370 |
<procDef type='t_t' id='b'> |
| 371 |
<setup><par name="orig" alias="alias"/></setup> |
| 372 |
<code>dest['par']=orig</code> |
| 373 |
</procDef> |
| 374 |
%s |
| 375 |
</foo>""" |
| 376 |
|
| 377 |
def testBasic(self): |
| 378 |
f = base.parseFromString(Foo, self._procDef%""" |
| 379 |
<testApp name='x' procDef='b'> |
| 380 |
<bind name="alias">"xx"</bind> |
| 381 |
</testApp>""") |
| 382 |
f.runApps() |
| 383 |
self.assertEqual(f.dest["par"], "xx") |
| 384 |
|
| 385 |
def testAliasOverriding(self): |
| 386 |
self.assertRaisesWithMsg( |
| 387 |
base.StructureError, |
| 388 |
'At IO:\' <foo> <procDef type=\'t_t\' id=\'b\'> <setup><par name="...\', (11, 3):' |
| 389 |
' Both canonical name and alias bound: orig, alias', |
| 390 |
base.parseFromString, |
| 391 |
(Foo, self._procDef%""" |
| 392 |
<testApp name='x' procDef='b'> |
| 393 |
<bind name="alias">"xx"</bind> |
| 394 |
<bind name="orig">"yy"</bind> |
| 395 |
</testApp>""")) |
| 396 |
|
| 397 |
|
| 398 |
class SCSTest(testhelpers.VerboseTest): |
| 399 |
def testSplitPosColumn(self): |
| 400 |
td = base.parseFromString(rscdef.TableDef, """ |
| 401 |
<table id="foo"> |
| 402 |
<FEED source="//scs#splitPosIndex" long="ra" lat="dec" columns="ra,dec"/> |
| 403 |
<column name="ra"/><column name="dec"/> |
| 404 |
</table>""") |
| 405 |
td.parent = testhelpers.getTestRD() |
| 406 |
self.assertEqual(td.columns[0].isIndexed(), {'straight'}) |
| 407 |
self.assertEqual(td.columns[1].isIndexed(), {'straight'}) |
| 408 |
self.assertEqual(td.indices[0].cluster, True) |
| 409 |
# We probably should expand the name in DBIndex, but for now we don't; |
| 410 |
# however, the macro will be expanded when the index is actually created. |
| 411 |
self.assertEqual(td.indices[0].name, r"splitpos_\tablename") |
| 412 |
self.assertEqual(re.sub(r"\s+", " ", |
| 413 |
";".join(td.indices[0].iterCode())), |
| 414 |
"CREATE INDEX foo_splitpos_foo ON test.foo ( q3c_ang2ipix(ra,dec) );CLUSTER foo_splitpos_foo ON test.foo") |
| 415 |
|
| 416 |
def testSplitPosSpoint(self): |
| 417 |
td = base.parseFromString(rscdef.TableDef, """ |
| 418 |
<table id="foo"> |
| 419 |
<FEED source="//scs#splitPosIndex" long="long(p)" lat="lat(p)" |
| 420 |
cluster="False"/> |
| 421 |
<column name="p" type="spoint"/> |
| 422 |
</table>""") |
| 423 |
td.parent = testhelpers.getTestRD() |
| 424 |
self.assertEqual(td.columns[0].isIndexed(), False) |
| 425 |
self.assertEqual(re.sub(r"\s+", " ", |
| 426 |
";".join(td.indices[0].iterCode())), |
| 427 |
"CREATE INDEX foo_splitpos_foo ON test.foo ( q3c_ang2ipix(long(p),lat(p)) )") |
| 428 |
|
| 429 |
def testSpointIndex(self): |
| 430 |
td = base.parseFromString(rscdef.TableDef, """ |
| 431 |
<table id="foo" mixin="//scs#pgs-pos-index"> |
| 432 |
<column name="ra" ucd="pos.eq.ra;meta.main"/> |
| 433 |
<column name="dec" ucd="pos.eq.dec;meta.main"/> |
| 434 |
</table>""") |
| 435 |
td.parent = testhelpers.getTestRD() |
| 436 |
self.assertEqual(td.columns[0].isIndexed(), {'spoint'}) |
| 437 |
self.assertEqual(re.sub(r"\s+", " ", |
| 438 |
";".join(td.indices[0].iterCode())), |
| 439 |
"CREATE INDEX foo_spoint_foo ON test.foo USING GIST ( spoint(radians(ra),radians(dec)) );CLUSTER foo_spoint_foo ON test.foo") |
| 440 |
|
| 441 |
|
| 442 |
if __name__=="__main__": |
| 443 |
testhelpers.main(SetupTest) |