| 1 |
""" |
| 2 |
Tests for active tags within RDs (and friends). |
| 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 gavo.helpers import testhelpers |
| 12 |
|
| 13 |
from gavo import base |
| 14 |
from gavo import rscdef |
| 15 |
from gavo import rscdesc |
| 16 |
from gavo import utils |
| 17 |
|
| 18 |
|
| 19 |
class BasicTest(testhelpers.VerboseTest): |
| 20 |
def testStreamContent(self): |
| 21 |
ctx = base.ParseContext() |
| 22 |
_ = base.parseFromString(rscdef.TableDef, """<table id="bar"> |
| 23 |
<STREAM id="foo"><table id="u" onDisk="True"><column name="x"/> |
| 24 |
</table></STREAM></table>""", context=ctx) |
| 25 |
parsedEvents = ctx.idmap["foo"].events_ |
| 26 |
self.assertEqual(len(parsedEvents), 7) |
| 27 |
self.assertEqual(parsedEvents[0][1], "table") |
| 28 |
self.assertEqual(parsedEvents[4][:3], |
| 29 |
("value", "name", "x")) |
| 30 |
self.assertEqual(parsedEvents[-1][:2], ("end", "table")) |
| 31 |
self.assertEqual(str(parsedEvents[3][-1]), |
| 32 |
'IO:\'<table id="bar"> <STREAM id="foo"><table id="u" onDis...\', (2, 48)') |
| 33 |
|
| 34 |
def testBasicReplay(self): |
| 35 |
res = base.parseFromString(rscdef.DataDescriptor, """<data id="bar"> |
| 36 |
<STREAM id="foo"><table id="u" onDisk="True"><column name="x"/> |
| 37 |
</table></STREAM><FEED source="foo"/></data>""") |
| 38 |
self.assertEqual(res.tables[0].id, "u") |
| 39 |
self.assertEqual(res.tables[0].columns[0].name, "x") |
| 40 |
|
| 41 |
def testPlainError(self): |
| 42 |
self.assertRaisesWithMsg(base.StructureError, |
| 43 |
'At IO:\'<data id="bar"> <STREAM id="foo"><table id="u" onDisk...\', (3, 40) (replaying, real error position IO:\'<data id="bar"> <STREAM id="foo"><table id="u" onDisk...\', (2, 48)):' |
| 44 |
" table elements have no honk attributes or children.", |
| 45 |
base.parseFromString, (rscdef.DataDescriptor, """<data id="bar"> |
| 46 |
<STREAM id="foo"><table id="u" onDisk="True"><honk name="x"/> |
| 47 |
</table></STREAM><FEED source="foo"/></data>""")) |
| 48 |
|
| 49 |
def testDocTag(self): |
| 50 |
ctx = base.ParseContext() |
| 51 |
_ = base.parseFromString(rscdef.DataDescriptor, """<data id="bar"> |
| 52 |
<STREAM id="foo"><doc>A null table.</doc> |
| 53 |
<table id="u" onDisk="True"/></STREAM><FEED source="foo"/></data>""", |
| 54 |
context=ctx) |
| 55 |
self.assertEqual(ctx.idmap["foo"].doc, "A null table.") |
| 56 |
|
| 57 |
def testDocTagAtEnd(self): |
| 58 |
ctx = base.ParseContext() |
| 59 |
_ = base.parseFromString(rscdef.DataDescriptor, """<data id="bar"> |
| 60 |
<STREAM id="foo"> |
| 61 |
<table id="u" onDisk="True"/><doc>A null table.</doc></STREAM></data>""", |
| 62 |
context=ctx) |
| 63 |
self.assertEqual(ctx.idmap["foo"].doc, "A null table.") |
| 64 |
|
| 65 |
|
| 66 |
class ReplayMacroTest(testhelpers.VerboseTest): |
| 67 |
def testBasic(self): |
| 68 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 69 |
"""<data><STREAM id="foo"><table id="\\tabname" onDisk="True"> |
| 70 |
<column name="x"/></table></STREAM> |
| 71 |
<FEED source="foo" tabname="abc"/></data>""") |
| 72 |
self.assertEqual(res.tables[0].id, "abc") |
| 73 |
|
| 74 |
def testStandardMacrosAvailable(self): |
| 75 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 76 |
r"""<data><STREAM id="foo"><table id="test"> |
| 77 |
<column name="x\upper{\col}"/></table></STREAM> |
| 78 |
<FEED source="foo" col="abc"/></data>""") |
| 79 |
self.assertEqual(res.tables[0].columns[0].name, "xABC") |
| 80 |
|
| 81 |
def testHandover(self): |
| 82 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 83 |
"""<data><STREAM id="foo"><table id="\\tabname" onDisk="True"> |
| 84 |
<index columns="\\\\test"/> |
| 85 |
<column name="x"/></table></STREAM> |
| 86 |
<FEED source="foo" tabname="abc"/></data>""") |
| 87 |
self.assertEqual(res.tables[0].indexedColumns.popitem(), |
| 88 |
("test macro expansion", {'straight'})) |
| 89 |
|
| 90 |
def testMissingSource(self): |
| 91 |
self.assertRaisesWithMsg(base.StructureError, |
| 92 |
'At IO:\'<data><STREAM id="foo"><table id="\\tabname" onDisk="T...\', (3, 24):' |
| 93 |
" Need exactly one of source and events on FEED elements", |
| 94 |
base.parseFromString, (rscdef.DataDescriptor, |
| 95 |
"""<data><STREAM id="foo"><table id="\\tabname" onDisk="True"> |
| 96 |
<column name="x"/></table></STREAM> |
| 97 |
<FEED tabname="abc"/></data>""")) |
| 98 |
|
| 99 |
def testMissingAttribute(self): |
| 100 |
try: |
| 101 |
_ = base.parseFromString(rscdef.DataDescriptor, |
| 102 |
"""<data><STREAM id="foo"><table id="\\tabname" onDisk="True"> |
| 103 |
<column name="x"/></table></STREAM> |
| 104 |
<FEED source="foo" /></data>""") |
| 105 |
except base.MacroError as ex: |
| 106 |
self.assertEqual(ex.hint, "This probably means that you should" |
| 107 |
" have set a tabname attribute in the FEED tag. For details" |
| 108 |
" see the documentation of the STREAM with id foo.") |
| 109 |
return |
| 110 |
self.fail("MacroError not raised") |
| 111 |
|
| 112 |
def testShortMacroName(self): |
| 113 |
self.assertRaisesWithMsg( |
| 114 |
base.StructureError, |
| 115 |
'At IO:\'<data><STREAM id="foo"><table id="\\t" onDisk="True"> ...\', (3, 4): DaCHS does not support one-character macro names. Hence, you cannot use "t" as a FEED attribute.', |
| 116 |
base.parseFromString, |
| 117 |
(rscdef.DataDescriptor, |
| 118 |
"""<data><STREAM id="foo"><table id="\\t" onDisk="True"> |
| 119 |
<column name="x"/></table></STREAM> |
| 120 |
<FEED source="foo" t="gockel"/></data>""")) |
| 121 |
|
| 122 |
def testDEFAULTSPlain(self): |
| 123 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 124 |
"""<data><STREAM id="foo"><DEFAULTS tabname="fro"/> |
| 125 |
<table id="\\tabname" onDisk="True"> |
| 126 |
<column name="x"/></table></STREAM> |
| 127 |
<FEED source="foo" /></data>""") |
| 128 |
self.assertEqual(res.tables[0].id, "fro") |
| 129 |
|
| 130 |
def testDEFAULTSElement(self): |
| 131 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 132 |
"""<data><STREAM id="foo"><DEFAULTS><tabname>fro</tabname></DEFAULTS> |
| 133 |
<table id="\\tabname" onDisk="True"> |
| 134 |
<column name="x"/></table></STREAM> |
| 135 |
<FEED source="foo" /></data>""") |
| 136 |
self.assertEqual(res.tables[0].id, "fro") |
| 137 |
|
| 138 |
def testDEFAULTSOverridden(self): |
| 139 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 140 |
"""<data><STREAM id="foo"><DEFAULTS tabname="fro"/> |
| 141 |
<table id="\\tabname" onDisk="True"> |
| 142 |
<column name="x"/></table></STREAM> |
| 143 |
<FEED source="foo" tabname="quux"/></data>""") |
| 144 |
self.assertEqual(res.tables[0].id, "quux") |
| 145 |
|
| 146 |
def testWithUnicodeValue(self): |
| 147 |
input = """<?xml version="1.0" encoding="iso-8859-1"?> |
| 148 |
<table id="gack"><STREAM id="foo"> |
| 149 |
<column name="\\na" description="\\de\\+m"/></STREAM> |
| 150 |
<FEED source="foo" na="bla" de="foo \xb5"/></table>""" |
| 151 |
res = base.parseFromString(rscdef.TableDef, input) |
| 152 |
self.assertEqual(res.columns[0].description, "foo \u00b5m") |
| 153 |
|
| 154 |
def testWithUnicodeStream(self): |
| 155 |
input = """<?xml version="1.0" encoding="iso-8859-1"?> |
| 156 |
<table id="gack"><STREAM id="foo"> |
| 157 |
<column name="\\na" description="\xb5m"/></STREAM> |
| 158 |
<FEED source="foo" na="bla"/></table>""" |
| 159 |
res = base.parseFromString(rscdef.TableDef, input) |
| 160 |
self.assertEqual(res.columns[0].description, "\u00b5m") |
| 161 |
|
| 162 |
def testExpandedArguments(self): |
| 163 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 164 |
"""<data><STREAM id="foo"><table id="\\upper{\\tabname}" |
| 165 |
onDisk="True"/></STREAM> |
| 166 |
<FEED source="foo" tabname="abc"/></data>""") |
| 167 |
self.assertEqual(res.tables[0].id, "ABC") |
| 168 |
|
| 169 |
def testSourceExpanded(self): |
| 170 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 171 |
r"""<data> |
| 172 |
<STREAM id="s-one"><column name="fromone"/></STREAM> |
| 173 |
<STREAM id="s-two"><column name="fromtwo"/></STREAM> |
| 174 |
<NXSTREAM id="switch"><FEED source="s-\use"/></NXSTREAM> |
| 175 |
<table id="x"> |
| 176 |
<FEED source="switch" use="one"/></table> |
| 177 |
<table id="y"> |
| 178 |
<FEED source="switch" use="two"/></table></data>""") |
| 179 |
self.assertEqual(res.tables[0].id, "x") |
| 180 |
self.assertEqual( |
| 181 |
testhelpers.pickSingle(res.tables[0].columns).name, |
| 182 |
"fromone") |
| 183 |
self.assertEqual( |
| 184 |
testhelpers.pickSingle(res.tables[1].columns).name, |
| 185 |
"fromtwo") |
| 186 |
|
| 187 |
|
| 188 |
class NestedTest(testhelpers.VerboseTest): |
| 189 |
def testDoubleNest(self): |
| 190 |
res = base.parseFromString(rscdesc.RD, |
| 191 |
r"""<resource schema="test"><STREAM id="cols"> |
| 192 |
<column name="from2"/> |
| 193 |
<index columns="\\curtable"/></STREAM> |
| 194 |
<STREAM id="foo"> |
| 195 |
<table id="\tabname" onDisk="True"> |
| 196 |
<FEED source="cols"/> |
| 197 |
<column name="from1"/></table></STREAM> |
| 198 |
<FEED source="foo" tabname="abc"/></resource>""") |
| 199 |
td = res.tables[0] |
| 200 |
self.assertEqual(td.id, "abc") |
| 201 |
self.assertEqual(", ".join(c.name for c in td), "from2, from1") |
| 202 |
self.assertEqual(res.tables[0].indexedColumns.popitem(), |
| 203 |
("test.abc", {'straight'})) |
| 204 |
|
| 205 |
def testInnermostExpansion(self): |
| 206 |
res = base.parseFromString(rscdesc.RD, |
| 207 |
r"""<resource schema="test"><STREAM id="cols"> |
| 208 |
<column name="\innercol"/> |
| 209 |
<index columns="\\curtable"/></STREAM> |
| 210 |
<STREAM id="foo"> |
| 211 |
<table id="\tabname" onDisk="True"> |
| 212 |
<FEED source="cols" innercol="whoppa"/> |
| 213 |
<column name="from1"/></table></STREAM> |
| 214 |
<FEED source="foo" tabname="abc"/></resource>""") |
| 215 |
td = res.tables[0] |
| 216 |
self.assertEqual(td.id, "abc") |
| 217 |
self.assertEqual(", ".join(c.name for c in td), "whoppa, from1") |
| 218 |
self.assertEqual(res.tables[0].indexedColumns.popitem(), |
| 219 |
("test.abc", {'straight'})) |
| 220 |
|
| 221 |
def testLoopReplay(self): |
| 222 |
res = base.parseFromString(rscdesc.RD, |
| 223 |
r"""<resource schema="test"><STREAM id="cols"> |
| 224 |
<LOOP listItems="x y"> |
| 225 |
<events><column name="\\mode\\+_\item"/></events></LOOP></STREAM> |
| 226 |
<table id="foo"> |
| 227 |
<FEED source="cols" mode="a" reexpand="True"/> |
| 228 |
<FEED source="cols" mode="b" reexpand="True"/> |
| 229 |
</table></resource>""") |
| 230 |
self.assertEqual([c.name for c in res.tables[0]], |
| 231 |
['a_x', 'a_y', 'b_x', 'b_y']) |
| 232 |
|
| 233 |
def testLoopExpansion(self): |
| 234 |
res = base.parseFromString(rscdesc.RD, |
| 235 |
r"""<resource schema="test"><NXSTREAM id="cols"> |
| 236 |
<LOOP listItems="\stuff"> |
| 237 |
<events><column name="\\item"/></events></LOOP></NXSTREAM> |
| 238 |
<table id="foo"><FEED source="cols" stuff="x y"/></table> |
| 239 |
</resource>""") |
| 240 |
self.assertEqual([c.name for c in res.tables[0]], |
| 241 |
['x', 'y']) |
| 242 |
|
| 243 |
def testFeedInLoop(self): |
| 244 |
res = base.parseFromString(rscdesc.RD, |
| 245 |
r"""<resource schema="test"> |
| 246 |
<STREAM id="cols"> |
| 247 |
<column name="\foo"/> |
| 248 |
<column name="err_\foo"/> |
| 249 |
</STREAM> |
| 250 |
<STREAM id="coords"> |
| 251 |
<LOOP listItems="x y"> |
| 252 |
<events passivate="True"> |
| 253 |
<FEED source="cols" foo="\\mode\\+\item" reexpand="True"/> |
| 254 |
</events> |
| 255 |
</LOOP> |
| 256 |
</STREAM> |
| 257 |
<STREAM id="modes"> |
| 258 |
<FEED source="coords" mode="a" reexpand="True"/> |
| 259 |
<FEED source="coords" mode="b" reexpand="True"/> |
| 260 |
</STREAM> |
| 261 |
<table id="abc"> |
| 262 |
<FEED source="modes"/> |
| 263 |
</table> |
| 264 |
</resource>""") |
| 265 |
self.assertEqual([c.name for c in res.tables[0]], |
| 266 |
['ax', 'err_ax', 'ay', 'err_ay', 'bx', 'err_bx', 'by', 'err_by']) |
| 267 |
|
| 268 |
|
| 269 |
class PruneTest(testhelpers.VerboseTest): |
| 270 |
def testWithName(self): |
| 271 |
td = base.parseFromString(rscdef.TableDef, """ |
| 272 |
<table> |
| 273 |
<STREAM id="foo"> |
| 274 |
<column name="abta"/> |
| 275 |
<column name="gub"/> |
| 276 |
</STREAM> |
| 277 |
<FEED source="foo"> |
| 278 |
<PRUNE name="abta"/> |
| 279 |
</FEED> |
| 280 |
</table>""") |
| 281 |
self.assertEqual(" ".join(c.name for c in td), "gub") |
| 282 |
|
| 283 |
def testWithUCD(self): |
| 284 |
td = base.parseFromString(rscdef.TableDef, """ |
| 285 |
<table id="foo"> |
| 286 |
<FEED> |
| 287 |
<events> |
| 288 |
<column name="abta" ucd="clin.dead"/> |
| 289 |
<column name="gub"/> |
| 290 |
</events> |
| 291 |
<PRUNE ucd="clin.dead"/> |
| 292 |
</FEED> |
| 293 |
</table>""") |
| 294 |
self.assertEqual(" ".join(c.name for c in td), "gub") |
| 295 |
|
| 296 |
def testIsConjunction(self): |
| 297 |
td = base.parseFromString(rscdef.TableDef, """ |
| 298 |
<table id="foo"> |
| 299 |
<FEED> |
| 300 |
<events> |
| 301 |
<column name="abta" ucd="clin.dead"/> |
| 302 |
<column name="gub"/> |
| 303 |
</events> |
| 304 |
<PRUNE ucd="clin.dead" name="gub"/> |
| 305 |
</FEED> |
| 306 |
</table>""") |
| 307 |
self.assertEqual(" ".join(c.name for c in td), "abta gub") |
| 308 |
|
| 309 |
def testMultiprune(self): |
| 310 |
td = base.parseFromString(rscdef.TableDef, """ |
| 311 |
<table id="foo"> |
| 312 |
<FEED> |
| 313 |
<events> |
| 314 |
<column name="abta" ucd="clin.dead"/> |
| 315 |
<column name="abto"/> |
| 316 |
</events> |
| 317 |
<PRUNE name="ab.*"/> |
| 318 |
</FEED> |
| 319 |
<column name="neuro"/> |
| 320 |
</table>""") |
| 321 |
self.assertEqual(" ".join(c.name for c in td), "neuro") |
| 322 |
|
| 323 |
def testDeepPrune(self): |
| 324 |
dd = base.parseFromString(rscdef.DataDescriptor, """ |
| 325 |
<data> |
| 326 |
<table id="foo"> |
| 327 |
<FEED> |
| 328 |
<events> |
| 329 |
<column name="abta" ucd="clin.dead"> |
| 330 |
<values><option>a</option><option>b</option></values> |
| 331 |
</column> |
| 332 |
<column name="abto"/> |
| 333 |
<column name="neuro"/> |
| 334 |
</events> |
| 335 |
<PRUNE name="ab.*"/> |
| 336 |
</FEED> |
| 337 |
</table> |
| 338 |
</data>""") |
| 339 |
self.assertEqual(" ".join(c.name for c in dd.tables[0]), "neuro") |
| 340 |
|
| 341 |
def testRecursivePune(self): |
| 342 |
dd = base.parseFromString(rscdef.DataDescriptor, """ |
| 343 |
<data> |
| 344 |
<STREAM id="srcev"> |
| 345 |
<column name="a"/> |
| 346 |
<column name="b"/> |
| 347 |
</STREAM> |
| 348 |
<STREAM id="pruned"> |
| 349 |
<FEED source="srcev"> |
| 350 |
<PRUNE name="a"/> |
| 351 |
</FEED> |
| 352 |
</STREAM> |
| 353 |
<table id="foo"> |
| 354 |
<FEED source="pruned"/> |
| 355 |
</table> |
| 356 |
</data>""") |
| 357 |
self.assertEqual(" ".join(c.name for c in dd.tables[0]), "b") |
| 358 |
|
| 359 |
def testRecursivePuneWithId(self): |
| 360 |
dd = base.parseFromString(rscdef.DataDescriptor, """ |
| 361 |
<data> |
| 362 |
<STREAM id="srcev"> |
| 363 |
<column name="a"/> |
| 364 |
<column name="b" id="kill"/> |
| 365 |
</STREAM> |
| 366 |
<STREAM id="pruned"> |
| 367 |
<FEED source="srcev"> |
| 368 |
<PRUNE id="kill"/> |
| 369 |
</FEED> |
| 370 |
</STREAM> |
| 371 |
<table id="foo"> |
| 372 |
<FEED source="pruned"/> |
| 373 |
</table> |
| 374 |
</data>""") |
| 375 |
self.assertEqual(" ".join(c.name for c in dd.tables[0]), "a") |
| 376 |
|
| 377 |
def testRecursivePuneLate(self): |
| 378 |
dd = base.parseFromString(rscdef.DataDescriptor, """ |
| 379 |
<data> |
| 380 |
<STREAM id="srcev"> |
| 381 |
<column name="a"/> |
| 382 |
<column name="b" id="kill"/> |
| 383 |
</STREAM> |
| 384 |
<STREAM id="pruned"> |
| 385 |
<FEED source="srcev"/> |
| 386 |
<column name="c"/> |
| 387 |
</STREAM> |
| 388 |
<table id="foo"> |
| 389 |
<FEED source="pruned"> |
| 390 |
<PRUNE id="kill"/> |
| 391 |
</FEED> |
| 392 |
</table> |
| 393 |
</data>""") |
| 394 |
self.assertEqual(" ".join(c.name for c in dd.tables[0]), "a c") |
| 395 |
|
| 396 |
|
| 397 |
class EditTest(testhelpers.VerboseTest): |
| 398 |
def testProd(self): |
| 399 |
res = base.parseFromString(rscdef.TableDef, |
| 400 |
"""<table><FEED source="//products#tablecols"> |
| 401 |
<EDIT ref="column[accref]" utype="ssa:Access.Reference"> |
| 402 |
<values default="notfound.fits"/></EDIT></FEED> |
| 403 |
</table>""") |
| 404 |
self.assertEqual(res.columns[0].utype, "ssa:Access.Reference") |
| 405 |
self.assertEqual(res.columns[0].values.default, "notfound.fits") |
| 406 |
|
| 407 |
def testInBetween(self): |
| 408 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 409 |
"""<data><STREAM id="foo"><table id="bla" onDisk="True"> |
| 410 |
<column name="x"/><column name="y"/></table></STREAM> |
| 411 |
<FEED source="foo"><EDIT ref="column[x]" type="text"/></FEED></data>""") |
| 412 |
td = res.tables[0] |
| 413 |
self.assertEqual(", ".join(c.type for c in td), "text, real") |
| 414 |
|
| 415 |
def testDoubleEdit(self): |
| 416 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 417 |
"""<data> |
| 418 |
<STREAM id="inc"><column name="grok"/></STREAM> |
| 419 |
<STREAM id="foo"><table id="bla" onDisk="True"> |
| 420 |
<column name="x"/><column name="y"/> |
| 421 |
<FEED source="inc"> |
| 422 |
<EDIT ref="column[grok]" type="spoint"/></FEED></table></STREAM> |
| 423 |
<FEED source="foo"><EDIT ref="column[x]" type="text"/></FEED></data>""") |
| 424 |
td = res.tables[0] |
| 425 |
self.assertEqual(", ".join(c.type for c in td), "text, real, spoint") |
| 426 |
|
| 427 |
def testRecursiveEdit(self): |
| 428 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 429 |
"""<data> |
| 430 |
<STREAM id="stage0"><column name="grok"/><column name="nok"/></STREAM> |
| 431 |
<STREAM id="stage1"><FEED source="stage0"> |
| 432 |
<EDIT ref="column[grok]" type="text"/></FEED></STREAM> |
| 433 |
<STREAM id="stage2"><FEED source="stage1"/></STREAM> |
| 434 |
<table><FEED source="stage2"/></table></data>""") |
| 435 |
td = res.tables[0] |
| 436 |
self.assertEqual(", ".join(c.type for c in td), "text, real") |
| 437 |
|
| 438 |
def testRecursiveDoubleEdit(self): |
| 439 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 440 |
"""<data> |
| 441 |
<STREAM id="stage0"><column name="grok"/><column name="nok"/></STREAM> |
| 442 |
<STREAM id="stage1"><FEED source="stage0"> |
| 443 |
<EDIT ref="column[nok]" type="text"/></FEED></STREAM> |
| 444 |
<STREAM id="stage2"><FEED source="stage1"> |
| 445 |
<EDIT ref="column[nok]" type="char"/></FEED></STREAM> |
| 446 |
<STREAM id="stage3"><FEED source="stage2"/></STREAM> |
| 447 |
<table><FEED source="stage3"/></table></data>""") |
| 448 |
td = res.tables[0] |
| 449 |
self.assertEqual(", ".join(c.type for c in td), "real, char") |
| 450 |
|
| 451 |
def testRemoteEdit(self): |
| 452 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 453 |
"""<data> |
| 454 |
<STREAM id="stage0"><column name="grok"/><column name="nok"/></STREAM> |
| 455 |
<STREAM id="stage1"><FEED source="stage0"/></STREAM> |
| 456 |
<STREAM id="stage2"><FEED source="stage1"> |
| 457 |
<EDIT ref="column[grok]" type="text"/></FEED></STREAM> |
| 458 |
<table><FEED source="stage2"/></table></data>""") |
| 459 |
td = res.tables[0] |
| 460 |
self.assertEqual(", ".join(c.type for c in td), "text, real") |
| 461 |
|
| 462 |
|
| 463 |
class LoopTest(testhelpers.VerboseTest): |
| 464 |
def testBasic(self): |
| 465 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 466 |
"""<?xml version="1.0" encoding="iso-8859-1"?><data><STREAM id="foo"> |
| 467 |
<column name="c_\\name" description="\\count, \\desc" |
| 468 |
verbLevel="\\count"/> |
| 469 |
</STREAM> |
| 470 |
<table id="gook"> |
| 471 |
<LOOP source="foo"><csvItems> |
| 472 |
name,desc,count |
| 473 |
anInt,m\xf6rkel,2 |
| 474 |
aString,sth,3 |
| 475 |
</csvItems> |
| 476 |
</LOOP></table></data>""") |
| 477 |
cols = list(res.tables[0]) |
| 478 |
self.assertEqual(len(cols), 2) |
| 479 |
self.assertEqual(cols[0].name, "c_anInt") |
| 480 |
self.assertEqual(cols[0].description, "2, m\u00f6rkel") |
| 481 |
self.assertEqual(cols[1].verbLevel, 3) |
| 482 |
|
| 483 |
def testEmbedded(self): |
| 484 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 485 |
r"""<data> |
| 486 |
<table id="gook"> |
| 487 |
<LOOP><csvItems> |
| 488 |
band,desc |
| 489 |
B,Johnson B |
| 490 |
C,Kernighan C |
| 491 |
d,"cumbersome, outdated band d" |
| 492 |
</csvItems> |
| 493 |
<events> |
| 494 |
<column name="mag\band" tablehead="m_\band" |
| 495 |
description="Magnitude in \desc"/> |
| 496 |
<column name="e_mag\band" tablehead="Err. m_\band" |
| 497 |
description="Error in \desc magnitude."/> |
| 498 |
</events> |
| 499 |
</LOOP></table></data>""") |
| 500 |
cols = list(res.tables[0]) |
| 501 |
self.assertEqual(len(cols), 6) |
| 502 |
self.assertEqual(cols[-1].description, |
| 503 |
"Error in cumbersome, outdated band d magnitude.") |
| 504 |
|
| 505 |
def testNoTwoRowSources(self): |
| 506 |
self.assertRaisesWithMsg(base.StructureError, |
| 507 |
'At IO:\'<data> <table id="gook"> <LOOP listItems="a b"><csvIt...\', (5, 4):' |
| 508 |
" Must give exactly one data source in LOOP", |
| 509 |
base.parseFromString, (rscdef.DataDescriptor, |
| 510 |
r"""<data> |
| 511 |
<table id="gook"> |
| 512 |
<LOOP listItems="a b"><csvItems>band,desc</csvItems> |
| 513 |
<events><column name="mag\band"/></events> |
| 514 |
</LOOP></table></data>""")) |
| 515 |
|
| 516 |
def testListItems(self): |
| 517 |
res = base.parseFromString(rscdef.DataDescriptor, |
| 518 |
r"""<data> |
| 519 |
<table id="gook"> |
| 520 |
<LOOP listItems="a b c"> |
| 521 |
<events> |
| 522 |
<column name="orig_\item"/> |
| 523 |
</events> |
| 524 |
</LOOP></table></data>""") |
| 525 |
cols = list(res.tables[0]) |
| 526 |
self.assertEqual(len(cols), 3) |
| 527 |
self.assertEqual(cols[-1].name, "orig_c") |
| 528 |
|
| 529 |
def testCodeItems(self): |
| 530 |
ctx = base.ParseContext() |
| 531 |
base.parseFromString(rscdef.DataDescriptor, |
| 532 |
r"""<data> |
| 533 |
<table id="nok"><column name="a"/><column name="b"/></table> |
| 534 |
<table id="cop"><LOOP><codeItems> |
| 535 |
for item in context.getById("nok"): |
| 536 |
yield {"copName": item.name+"_copy"}</codeItems> |
| 537 |
<events> |
| 538 |
<column name="\copName"/> |
| 539 |
</events></LOOP></table></data>""", context=ctx) |
| 540 |
cols = list(ctx.getById("cop")) |
| 541 |
self.assertEqual(",".join(c.name for c in cols), "a_copy,b_copy") |
| 542 |
|
| 543 |
def testAdditionalMacros(self): |
| 544 |
table = base.parseFromString(rscdesc.RD, |
| 545 |
r"""<resource schema="data"> |
| 546 |
<table id="o"> |
| 547 |
<column name="a"/><column name="b"/> |
| 548 |
</table> |
| 549 |
<table id="c"> |
| 550 |
<LOOP listItems="a b" tabname="o"> |
| 551 |
<events> |
| 552 |
<column original="\tabname.\item"/> |
| 553 |
</events> |
| 554 |
</LOOP> |
| 555 |
</table></resource>""").tables[1] |
| 556 |
self.assertEqual(table.id, "c") |
| 557 |
self.assertEqual(table.columns[0].name, "a") |
| 558 |
self.assertEqual(table.columns[1].name, "b") |
| 559 |
|
| 560 |
def testPassivate(self): |
| 561 |
table = base.parseFromString(rscdesc.RD, |
| 562 |
r"""<resource schema="data"> |
| 563 |
<STREAM id="col1"> |
| 564 |
<column name="foo"/> |
| 565 |
</STREAM> |
| 566 |
<STREAM id="col2"> |
| 567 |
<column name="bar"/> |
| 568 |
</STREAM> |
| 569 |
<STREAM id="cols"> |
| 570 |
<LOOP listItems="1 2"> |
| 571 |
<events passivate="True"> |
| 572 |
<FEED source="col\item"/> |
| 573 |
</events> |
| 574 |
</LOOP> |
| 575 |
</STREAM> |
| 576 |
|
| 577 |
<table id="o"> |
| 578 |
<FEED source="cols"/> |
| 579 |
</table> |
| 580 |
</resource>""").tables[0] |
| 581 |
self.assertEqual(table.columns[0].name, "foo") |
| 582 |
self.assertEqual(table.columns[1].name, "bar") |
| 583 |
|
| 584 |
def testGeneratedSTREAMs(self): |
| 585 |
rd = base.parseFromString(rscdesc.RD, |
| 586 |
r"""<resource schema="data"> |
| 587 |
<LOOP listItems="1 2"> |
| 588 |
<events passivate="True"> |
| 589 |
<STREAM id="\item\+stream"> |
| 590 |
<table id="tab\item"><column name="colin\item"/></table> |
| 591 |
</STREAM> |
| 592 |
</events> |
| 593 |
</LOOP> |
| 594 |
<FEED source="1stream"/> |
| 595 |
<FEED source="2stream"/> |
| 596 |
</resource>""") |
| 597 |
self.assertEqual(rd.getById("tab1").columns[0].name, "colin1") |
| 598 |
self.assertEqual(rd.getById("tab2").columns[0].name, "colin2") |
| 599 |
|
| 600 |
def testNestedExpansion(self): |
| 601 |
rd = base.parseFromString(rscdesc.RD, |
| 602 |
r"""<resource schema="data"> |
| 603 |
<LOOP listItems="1 2" reexpand="True"> |
| 604 |
<events> |
| 605 |
<LOOP listItems="a b"> |
| 606 |
<events> |
| 607 |
<meta name="gob\item\\+\\item">present</meta> |
| 608 |
</events> |
| 609 |
</LOOP> |
| 610 |
</events> |
| 611 |
</LOOP> |
| 612 |
</resource>""") |
| 613 |
self.assertEqual(set(rd.meta_.keys()), |
| 614 |
set(['gobb1', 'goba1', 'gobb2', 'goba2'])) |
| 615 |
|
| 616 |
def testStreamReexpansion(self): |
| 617 |
rd = base.parseFromString(rscdesc.RD, |
| 618 |
r"""<resource schema="data"> |
| 619 |
<STREAM id="plusminus"> |
| 620 |
<column name="\prefix\+\basecol\+_error_m"/> |
| 621 |
<column name="\prefix\+\basecol\+_error_p"/> |
| 622 |
</STREAM> |
| 623 |
<table id="t"> |
| 624 |
<LOOP listItems="l1_ l2_"> |
| 625 |
<events passivate="True"> |
| 626 |
<FEED source="plusminus" basecol="dist" prefix="\item" |
| 627 |
reexpand="True"/> |
| 628 |
</events> |
| 629 |
</LOOP> |
| 630 |
</table> |
| 631 |
</resource>""") |
| 632 |
self.assertEqual( |
| 633 |
[c.name for c in rd.getById("t").columns], [ |
| 634 |
"l1_dist_error_m", "l1_dist_error_p", |
| 635 |
"l2_dist_error_m", "l2_dist_error_p"]) |
| 636 |
|
| 637 |
def testBrokenCodeItemMeta(self): |
| 638 |
self.assertRaisesWithMsg( |
| 639 |
base.BadCode, |
| 640 |
utils.EqualingRE("At IO:'<resource schema=\"data\"> <LOOP><codeItems>return = 3<...', \\(2, 31\\): Bad source code in function \\(invalid syntax \\(<LOOP at <internally built>>[0-9-]*, line 2\\)\\)"), |
| 641 |
base.parseFromString, |
| 642 |
(rscdesc.RD, |
| 643 |
r"""<resource schema="data"> |
| 644 |
<LOOP><codeItems>return = 3</codeItems> |
| 645 |
<events/></LOOP></resource>""")) |
| 646 |
|
| 647 |
|
| 648 |
class RDBasedTest(testhelpers.VerboseTest): |
| 649 |
def setUp(self): |
| 650 |
self.rd = testhelpers.getTestRD("activetest") |
| 651 |
|
| 652 |
def testCSVMacrosExpandedInTable(self): |
| 653 |
cols = list(self.rd.getById("mags")) |
| 654 |
self.assertEqual(len(cols), 6) |
| 655 |
self.assertEqual(cols[0].name, "jmag") |
| 656 |
self.assertEqual(cols[-1].description, "Error in magnitude in the K band") |
| 657 |
|
| 658 |
|
| 659 |
class MixinTest(testhelpers.VerboseTest): |
| 660 |
baseRDLit = r"""<resource schema="test"> |
| 661 |
<mixinDef id="bla"> |
| 662 |
<mixinPar key="xy" alias="ov_default">xy</mixinPar> |
| 663 |
<mixinPar key="nd" alias="nd_new"/> |
| 664 |
<events> |
| 665 |
<param name="\xy" type="text">\nd</param> |
| 666 |
</events> |
| 667 |
</mixinDef> |
| 668 |
%s |
| 669 |
</resource>""" |
| 670 |
|
| 671 |
def testWorkingMacro(self): |
| 672 |
res = base.parseFromString(rscdesc.RD, |
| 673 |
self.baseRDLit%'<table><mixin nd="uu">bla</mixin></table>') |
| 674 |
self.assertEqual(res.tables[0].params[0].name, "xy") |
| 675 |
self.assertEqual(res.tables[0].params[0].value, "uu") |
| 676 |
|
| 677 |
def testWorkingMacroElement(self): |
| 678 |
res = base.parseFromString(rscdesc.RD, |
| 679 |
self.baseRDLit%'<table><mixin><nd>"uu"</nd>bla</mixin></table>') |
| 680 |
self.assertEqual(res.tables[0].params[0].name, "xy") |
| 681 |
self.assertEqual(res.tables[0].params[0].value, '"uu"') |
| 682 |
|
| 683 |
def testOverridingMacro(self): |
| 684 |
res = base.parseFromString(rscdesc.RD, |
| 685 |
self.baseRDLit%'<table><mixin xy="zq" nd="uu">bla</mixin></table>') |
| 686 |
self.assertEqual(res.tables[0].params[0].name, "zq") |
| 687 |
self.assertEqual(res.tables[0].params[0].value, "uu") |
| 688 |
|
| 689 |
def testNotFilledMacro(self): |
| 690 |
self.assertRaisesWithMsg(base.StructureError, |
| 691 |
'At IO:\'<resource schema="test"> <mixinDef id="bla"> <mixinPa...\', (9, 27):' |
| 692 |
" Mixin parameter nd mandatory", |
| 693 |
base.parseFromString, |
| 694 |
(rscdesc.RD, |
| 695 |
self.baseRDLit%'<table><mixin xy="zq">bla</mixin></table>')) |
| 696 |
|
| 697 |
def testBadFillingRaises(self): |
| 698 |
self.assertRaisesWithMsg(base.StructureError, |
| 699 |
'At IO:\'<resource schema="test"> <mixinDef id="bla"> <mixinPa...\', (9, 20):' |
| 700 |
" nd elements cannot have a children in mixins.", |
| 701 |
base.parseFromString, |
| 702 |
(rscdesc.RD, |
| 703 |
self.baseRDLit%'<table><mixin><nd><a>uu</a></nd>bla</mixin></table>')) |
| 704 |
|
| 705 |
def testUnknownMacroRaises(self): |
| 706 |
self.assertRaisesWithMsg(base.StructureError, |
| 707 |
'At IO:\'<resource schema="test"> <mixinDef id="bla"> <mixinPa...\', (9, 35):' |
| 708 |
' The attribute(s) a is/are not allowed on this mixin', |
| 709 |
base.parseFromString, |
| 710 |
(rscdesc.RD, |
| 711 |
self.baseRDLit%'<table><mixin nd="u"><a>uu</a>bla</mixin></table>')) |
| 712 |
|
| 713 |
def testBadMacroNamesRejected(self): |
| 714 |
self.assertRaises(base.StructureError, |
| 715 |
base.parseFromString, rscdesc.RD, |
| 716 |
r"""<resource schema="test"><mixinDef id="bla"> |
| 717 |
<mixinPar key="a">__NULL__</mixinPar></mixinDef></resource>""") |
| 718 |
|
| 719 |
def testNULLDefault(self): |
| 720 |
res = base.parseFromString(rscdesc.RD, |
| 721 |
r"""<resource schema="test"><mixinDef id="bla"> |
| 722 |
<mixinPar key="aa">__NULL__</mixinPar><events> |
| 723 |
<param name="u">\aa</param></events></mixinDef> |
| 724 |
<table mixin="bla"/></resource>""") |
| 725 |
self.assertEqual(res.tables[0].params[0].value, None) |
| 726 |
|
| 727 |
def testEmptyDefault(self): |
| 728 |
res = base.parseFromString(rscdesc.RD, |
| 729 |
r"""<resource schema="test"><mixinDef id="bla"> |
| 730 |
<mixinPar key="aa">__EMPTY__</mixinPar><events> |
| 731 |
<param name="u" unit=""/></events></mixinDef> |
| 732 |
<table mixin="bla"/></resource>""") |
| 733 |
self.assertEqual(res.tables[0].params[0].unit, "") |
| 734 |
|
| 735 |
def testNestedFeeds(self): |
| 736 |
res = base.parseFromString(rscdesc.RD, |
| 737 |
r"""<resource schema="test"> |
| 738 |
<STREAM id="field"> |
| 739 |
<param name="u">\uVal</param> |
| 740 |
</STREAM> |
| 741 |
<mixinDef id="bla"> |
| 742 |
<mixinPar key="uVal"/> |
| 743 |
<events> |
| 744 |
<LFEED source="field"/> |
| 745 |
</events> |
| 746 |
</mixinDef> |
| 747 |
<table><mixin uVal="5">bla</mixin></table> |
| 748 |
</resource>""") |
| 749 |
self.assertEqual(res.tables[0].params[0].value, 5.) |
| 750 |
|
| 751 |
def testReplayedRD(self): |
| 752 |
res = base.parseFromString(rscdesc.RD, |
| 753 |
r"""<resource schema="data"> |
| 754 |
<STREAM id="_part"> |
| 755 |
<events> |
| 756 |
<column name="urks"/> |
| 757 |
</events> |
| 758 |
</STREAM> |
| 759 |
<mixinDef id="bla"> |
| 760 |
<LFEED source="_part"/> |
| 761 |
</mixinDef> |
| 762 |
<table mixin="bla"/></resource>""") |
| 763 |
self.assertEqual(res.tables[0].columns[0].name, "urks") |
| 764 |
|
| 765 |
def testMixinParsInActiveTags(self): |
| 766 |
res = base.parseFromString(rscdesc.RD, |
| 767 |
r"""<resource schema="data"> |
| 768 |
<table id="src"> |
| 769 |
<column name="a"/><column name="b"/> |
| 770 |
</table> |
| 771 |
<mixinDef id="bla"> |
| 772 |
<mixinPar name="srctable"/> |
| 773 |
<events> |
| 774 |
<LOOP listItems="a b"> |
| 775 |
<events> |
| 776 |
<column original="\\srctable.\item"/> |
| 777 |
</events> |
| 778 |
</LOOP> |
| 779 |
</events> |
| 780 |
</mixinDef> |
| 781 |
<table id="dest"><mixin srctable="src">bla</mixin></table> |
| 782 |
</resource>""") |
| 783 |
self.assertEqual(res.tables[1].id, "dest") |
| 784 |
self.assertEqual(res.tables[1].columns[0].name, "a") |
| 785 |
self.assertEqual(res.tables[1].columns[1].name, "b") |
| 786 |
|
| 787 |
def testMaterialOverrideable(self): |
| 788 |
res = base.parseFromString(rscdesc.RD, |
| 789 |
"""<resource schema="data"> |
| 790 |
<table id="bla" mixin="//siap#pgs" onDisk="True" temporary="True"> |
| 791 |
<column name="dateObs" description="Don't believe this."/> |
| 792 |
</table></resource>""") |
| 793 |
self.assertEqual( |
| 794 |
res.getById("bla").getColumnByName("dateObs").description, |
| 795 |
"Don't believe this.") |
| 796 |
|
| 797 |
def testLateEvents(self): |
| 798 |
res = base.parseFromString(rscdesc.RD, |
| 799 |
"""<resource schema="data"> |
| 800 |
<table id="bla" mixin="//scs#q3cindex" onDisk="True" temporary="True"> |
| 801 |
<column name="r" ucd="pos.eq.ra;meta.main"/> |
| 802 |
<column name="d" ucd="pos.eq.dec;meta.main"/> |
| 803 |
</table></resource>""") |
| 804 |
self.assertTrue("q3c_ang2ipix(r, d)" in |
| 805 |
"".join(res.getById("bla").indices[0].iterCode())) |
| 806 |
|
| 807 |
def testParameterOverwriting(self): |
| 808 |
rd = base.parseFromString(rscdesc.RD, r"""<resource schema="test"> |
| 809 |
<mixinDef id="bla"> |
| 810 |
<mixinPar key="xy">xy</mixinPar> |
| 811 |
<mixinPar key="xy">zz</mixinPar> |
| 812 |
<events> |
| 813 |
<param name="val" type="text">\xy</param> |
| 814 |
</events> |
| 815 |
</mixinDef> |
| 816 |
<table id="foo" mixin="bla"/> |
| 817 |
</resource>""") |
| 818 |
self.assertEqual(len(rd.getById("bla").pars), 1) |
| 819 |
self.assertEqual(rd.getById("foo").getParamByName("val").value, "zz") |
| 820 |
|
| 821 |
def testBasicAliasing(self): |
| 822 |
res = base.parseFromString(rscdesc.RD, |
| 823 |
self.baseRDLit%'<table><mixin nd_new="hey">bla</mixin></table>') |
| 824 |
self.assertEqual(res.tables[0].params[0].name, "xy") |
| 825 |
self.assertEqual(res.tables[0].params[0].value, "hey") |
| 826 |
|
| 827 |
def testAliasingNoDefault(self): |
| 828 |
res = base.parseFromString(rscdesc.RD, |
| 829 |
self.baseRDLit%'<table><mixin ov_default="w"' |
| 830 |
' nd="hey">bla</mixin></table>') |
| 831 |
self.assertEqual(res.tables[0].params[0].name, "w") |
| 832 |
self.assertEqual(res.tables[0].params[0].value, "hey") |
| 833 |
|
| 834 |
def testAliasingBothGiven(self): |
| 835 |
self.assertRaisesWithMsg(base.StructureError, |
| 836 |
'At IO:\'<resource schema="test"> <mixinDef id="bla"> <mixinPa...\', (9, 50):' |
| 837 |
' Both canonical name and alias bound: xy, ov_default', |
| 838 |
base.parseFromString, |
| 839 |
(rscdesc.RD, |
| 840 |
self.baseRDLit%'<table><mixin xy="g" ov_default="w"' |
| 841 |
' nd="hey">bla</mixin></table>')) |
| 842 |
|
| 843 |
|
| 844 |
class ReferenceAttributeTest(testhelpers.VerboseTest): |
| 845 |
def testRefattChangeHonored(self): |
| 846 |
rd = base.parseFromString(rscdesc.RD, """ |
| 847 |
<resource schema="test"> |
| 848 |
<STREAM id="woo"><var key="src">@inp+33</var></STREAM> |
| 849 |
<STREAM id="loo"><var key="gno">4</var></STREAM> |
| 850 |
<table id="t1"><column name="foo" type="integer" required="True"/> |
| 851 |
</table> |
| 852 |
<data id="make_t1"><dictlistGrammar/> |
| 853 |
<make table="t1"> |
| 854 |
<rowmaker> |
| 855 |
<FEED source="woo"/> |
| 856 |
<FEED source="loo"/> |
| 857 |
<map key="foo">@src+1-@gno</map></rowmaker></make></data> |
| 858 |
</resource> |
| 859 |
""") |
| 860 |
from gavo import rsc |
| 861 |
d = rsc.makeData(rd.getById("make_t1"), forceSource=[{'inp': 1}]) |
| 862 |
self.assertEqual(d.getPrimaryTable().rows, [{'foo': 31}]) |
| 863 |
|
| 864 |
|
| 865 |
if __name__=="__main__": |
| 866 |
testhelpers.main(MixinTest) |