| 1 |
""" |
| 2 |
Tests for our datalink implementation. |
| 3 |
|
| 4 |
Note that there's also sodatest, which should contain tests for |
| 5 |
DaCHS-defined meta makers and data functions (at least where concerned |
| 6 |
with parameters contained in SODA). |
| 7 |
""" |
| 8 |
|
| 9 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 10 |
#c |
| 11 |
#c This program is free software, covered by the GNU GPL. See the |
| 12 |
#c COPYING file in the source distribution. |
| 13 |
|
| 14 |
|
| 15 |
import gc |
| 16 |
|
| 17 |
from gavo.helpers import testhelpers |
| 18 |
|
| 19 |
from gavo import api |
| 20 |
from gavo import svcs |
| 21 |
from gavo import votable |
| 22 |
from gavo.helpers import trialhelpers |
| 23 |
from gavo.protocols import datalink |
| 24 |
|
| 25 |
import tresc |
| 26 |
|
| 27 |
|
| 28 |
class MetaErrorSemanticsInferenceTest(testhelpers.VerboseTest): |
| 29 |
def _getDatalinkErrors(self, metaMakerDef): |
| 30 |
core = api.parseFromString(datalink.DatalinkCore, """<datalinkCore> |
| 31 |
<descriptorGenerator><code> |
| 32 |
return ProductDescriptor("contrived", "contrived", None, |
| 33 |
"application/bogus") |
| 34 |
</code></descriptorGenerator> |
| 35 |
%s |
| 36 |
</datalinkCore>"""%metaMakerDef) |
| 37 |
args = {"ID": "contrived"} #noflake: variable stolen by dl core |
| 38 |
dlCore = core.adaptForRenderer( |
| 39 |
svcs.getRenderer("dlmeta"), |
| 40 |
svcs.emptyQueryMeta) |
| 41 |
|
| 42 |
_, _, errors = dlCore.getMetaForDescriptor(dlCore.descriptors[0]) |
| 43 |
return errors |
| 44 |
|
| 45 |
def testSyntaxErrorInferred(self): |
| 46 |
errors = self._getDatalinkErrors(""" |
| 47 |
<metaMaker semantics="#grassy"><code> |
| 48 |
return return |
| 49 |
yield descriptor.makeLink("heu") |
| 50 |
</code></metaMaker>""") |
| 51 |
self.assertEqual(errors[0].semantics, "#grassy") |
| 52 |
|
| 53 |
def testDefaultError(self): |
| 54 |
errors = self._getDatalinkErrors(""" |
| 55 |
<metaMaker><code> |
| 56 |
return return |
| 57 |
yield descriptor.makeLink("heu") |
| 58 |
</code></metaMaker>""") |
| 59 |
self.assertEqual(errors[0].semantics, "http://dc.g-vo.org/datalink#other") |
| 60 |
|
| 61 |
|
| 62 |
class _DumbSODAService(testhelpers.TestResource): |
| 63 |
resources = [("prodtestTable", tresc.prodtestTable)] |
| 64 |
|
| 65 |
def make(self, dependents): |
| 66 |
svc = api.parseFromString(svcs.Service, """<service id="uh" |
| 67 |
allowed="dlget"> |
| 68 |
<datalinkCore> |
| 69 |
</datalinkCore></service>""") |
| 70 |
svc.parent = testhelpers.getTestRD() |
| 71 |
return svc |
| 72 |
|
| 73 |
|
| 74 |
class DLInterfaceTest(testhelpers.VerboseTest): |
| 75 |
resources = [("svc", _DumbSODAService()), |
| 76 |
("ssa", tresc.ssaTestTable)] |
| 77 |
|
| 78 |
def testIDNecessary(self): |
| 79 |
self.assertRaisesWithMsg(api.ValidationError, |
| 80 |
"Field ID: ID is mandatory with dlget", |
| 81 |
trialhelpers.runSvcWith, |
| 82 |
(self.svc, "dlget", {})) |
| 83 |
|
| 84 |
def testMissingAccref(self): |
| 85 |
tree = testhelpers.getXMLTree( |
| 86 |
trialhelpers.runSvcWith(self.svc, "dlmeta", |
| 87 |
{"ID": "junkjunk"})[1]) |
| 88 |
self.assertEqual(tree.xpath("//TR/TD[4]")[0].text, |
| 89 |
"NotFoundFault: accref 'junkjunk' could" |
| 90 |
" not be located in product table") |
| 91 |
|
| 92 |
def testWrongAuthority(self): |
| 93 |
tree = testhelpers.getXMLTree( |
| 94 |
trialhelpers.runSvcWith(self.svc, "dlmeta", |
| 95 |
{"ID": "ivo://junkjunk/knatter"})[1]) |
| 96 |
self.assertEqual(tree.xpath("//TR/TD[4]")[0].text, |
| 97 |
"NotFoundFault: The authority in the dataset identifier" |
| 98 |
" 'ivo://junkjunk/knatter' could not be located in the" |
| 99 |
" authorities managed here") |
| 100 |
|
| 101 |
def testNoNoArgProc(self): |
| 102 |
tree = testhelpers.getXMLTree( |
| 103 |
trialhelpers.runSvcWith(self.svc, "dlmeta", |
| 104 |
{"ID": "data/spec3.ssatest.vot"})[1], debug=False) |
| 105 |
# there's do inputKey-generating metaMaker here, so there shouldn't |
| 106 |
# be a #proc line or a meta resource |
| 107 |
self.assertEqual(len(tree.xpath("//TD[.='#proc']")), 0) |
| 108 |
self.assertEqual(len(tree.xpath("//RESOURCE")), 1) |
| 109 |
|
| 110 |
|
| 111 |
class _MetaMakerTestData(testhelpers.TestResource): |
| 112 |
# test data for datalink metadata generation |
| 113 |
resources = [ |
| 114 |
("prodtestTable", tresc.prodtestTable)] |
| 115 |
|
| 116 |
def make(self, dependents): |
| 117 |
svc = api.parseFromString(svcs.Service, """ |
| 118 |
<service id="foo" allowed="dlget,dlmeta,static"> |
| 119 |
<property key="staticData">data</property> |
| 120 |
<meta name="description">A sample processing service</meta> |
| 121 |
<datalinkCore> |
| 122 |
<descriptorGenerator procDef="//soda#fromStandardPubDID"> |
| 123 |
<setup> |
| 124 |
<code> |
| 125 |
def addExtras(descriptor): |
| 126 |
descriptor.bogosity = len(descriptor.pubDID) |
| 127 |
</code> |
| 128 |
</setup> |
| 129 |
</descriptorGenerator> |
| 130 |
<metaMaker> |
| 131 |
<code> |
| 132 |
ik = MS(InputKey, name="format", type="text", |
| 133 |
ucd="meta.format", |
| 134 |
description="Output format desired", |
| 135 |
values=MS(Values, |
| 136 |
options=[MS(Option, content_=descriptor.mime), |
| 137 |
MS(Option, content_="application/fits")])) |
| 138 |
ik.setProperty("defaultForForm", "application/fits") |
| 139 |
yield ik |
| 140 |
</code> |
| 141 |
</metaMaker> |
| 142 |
|
| 143 |
<metaMaker semantics="#sibling"> |
| 144 |
<code> |
| 145 |
yield descriptor.makeLink("http://foo/bar", |
| 146 |
contentType="test/junk", |
| 147 |
contentLength=500002, |
| 148 |
localSemantics="geneigt", |
| 149 |
contentQualifier="#other") |
| 150 |
yield descriptor.makeLink("http://foo/baz", |
| 151 |
contentType="test/gold", |
| 152 |
semantics="#calibration") |
| 153 |
</code> |
| 154 |
</metaMaker> |
| 155 |
<metaMaker semantics="#othermess"> |
| 156 |
<code> |
| 157 |
if descriptor.pubDID.endswith("b.imp"): |
| 158 |
yield DatalinkFault.NotFoundFault("ivo://not.asked.for", |
| 159 |
"Cannot locate other mess") |
| 160 |
</code> |
| 161 |
</metaMaker> |
| 162 |
<metaMaker semantics="http://www.g-vo.org/dl#unrelated"> |
| 163 |
<code> |
| 164 |
yield descriptor.makeLinkFromFile( |
| 165 |
"no.such.file", description="An unrelated nothing") |
| 166 |
yield descriptor.makeLinkFromFile( |
| 167 |
"expected.missing", description="A file that just does not" |
| 168 |
" exist and should not create diagnostics.", |
| 169 |
semantics="http://www.g-vo.org/dl#mustnothappen", |
| 170 |
suppressMissing=True) |
| 171 |
</code> |
| 172 |
</metaMaker> |
| 173 |
<metaMaker> |
| 174 |
<code> |
| 175 |
yield descriptor.makeLinkFromFile("data/map1.map", |
| 176 |
description="Some mapping", |
| 177 |
semantics="http://www.g-vo.org/dl#related", |
| 178 |
contentQualifier="#something") |
| 179 |
</code> |
| 180 |
</metaMaker> |
| 181 |
<metaMaker> |
| 182 |
<code> |
| 183 |
if descriptor.pubDID!='ivo://x-testing/~?data/a.imp': |
| 184 |
return |
| 185 |
yield ExternalProcLinkDef( |
| 186 |
descriptor.pubDID, [ |
| 187 |
MS(InputKey, name="QUERY", type="text", |
| 188 |
content_="SELECT * FROM some_table_name"), |
| 189 |
MS(InputKey, name="LANG", type="text", |
| 190 |
content_="ADQL")], |
| 191 |
"http://dc.g-vo.org/tap/sync", |
| 192 |
"GAVO TAP", |
| 193 |
"TAP service with an alternative representation of the data", |
| 194 |
standardId="ivo://ivoa.net/std/tap") |
| 195 |
</code> |
| 196 |
</metaMaker> |
| 197 |
<metaMaker semantics="#testing"> |
| 198 |
<code> |
| 199 |
yield descriptor.makeLink( |
| 200 |
f"http://bogosity.code/{descriptor.bogosity}") |
| 201 |
</code> |
| 202 |
</metaMaker> |
| 203 |
<dataFunction procDef="//soda#generateProduct"/> |
| 204 |
</datalinkCore> |
| 205 |
<publish render="dlmeta" sets="ivo_managed"/> |
| 206 |
</service>""") |
| 207 |
svc.parent = testhelpers.getTestRD() |
| 208 |
|
| 209 |
mime, data = trialhelpers.runSvcWith(svc, "dlmeta", { |
| 210 |
"id": [ |
| 211 |
api.getStandardPubDID("data/a.imp"), |
| 212 |
api.getStandardPubDID("data/b.imp"), |
| 213 |
]}) |
| 214 |
|
| 215 |
from gavo.registry import capabilities |
| 216 |
capEl = capabilities.getCapabilityElement(svc.publications[0]) |
| 217 |
|
| 218 |
return (mime, |
| 219 |
testhelpers.getXMLTree(data, debug=False), |
| 220 |
list(next(votable.parseBytes(data))), |
| 221 |
testhelpers.getXMLTree(capEl.render(), debug=False)) |
| 222 |
|
| 223 |
_metaMakerTestData = _MetaMakerTestData() |
| 224 |
|
| 225 |
|
| 226 |
class DatalinkMetaMakerTest(testhelpers.VerboseTest): |
| 227 |
resources = [("serviceResult", _metaMakerTestData), |
| 228 |
("prodtestTable", tresc.prodtestTable)] |
| 229 |
|
| 230 |
def testServiceMetaDeclared(self): |
| 231 |
infoEl = self.serviceResult[1].xpath( |
| 232 |
"RESOURCE[@type='results']/INFO[@name='reference_url']")[0] |
| 233 |
self.assertEqual( |
| 234 |
infoEl.get("value"), |
| 235 |
"http://localhost:8080/data/test/foo/info") |
| 236 |
|
| 237 |
def testStandardId(self): |
| 238 |
infoEl = self.serviceResult[1].xpath( |
| 239 |
"RESOURCE[@type='results']/INFO[@name='standardID']")[0] |
| 240 |
self.assertTrue(infoEl.text.startswith("Written by DaCHS")) |
| 241 |
self.assertEqual( |
| 242 |
infoEl.get("value"), |
| 243 |
"ivo://ivoa.net/std/datalink#links-1.1") |
| 244 |
|
| 245 |
def testIDSet(self): |
| 246 |
svc1 = self.serviceResult[1].xpath("//RESOURCE[@utype='adhoc:service']")[0] |
| 247 |
self.assertEqual( |
| 248 |
svc1.xpath("GROUP/PARAM[@name='ID']")[0].get("value"), |
| 249 |
"ivo://x-testing/~?data/a.imp") |
| 250 |
svc2 = self.serviceResult[1].xpath("//RESOURCE[@utype='adhoc:service']")[1] |
| 251 |
self.assertEqual( |
| 252 |
svc2.xpath("GROUP/PARAM[@name='ID']")[0].get("value"), |
| 253 |
"ivo://x-testing/~?data/b.imp") |
| 254 |
|
| 255 |
def testMimeOk(self): |
| 256 |
self.assertEqual(self.serviceResult[0], |
| 257 |
"application/x-votable+xml;content=datalink") |
| 258 |
|
| 259 |
def testUCDPresent(self): |
| 260 |
tree = self.serviceResult[1] |
| 261 |
self.assertEqual( |
| 262 |
tree.xpath("//PARAM[@name='format']")[0].get("ucd"), |
| 263 |
"meta.format") |
| 264 |
|
| 265 |
def testTypeTranslationWorks(self): |
| 266 |
tree = self.serviceResult[1] |
| 267 |
self.assertEqual( |
| 268 |
tree.xpath("//PARAM[@name='format']")[0].get("arraysize"), |
| 269 |
"*") |
| 270 |
|
| 271 |
def testPreset(self): |
| 272 |
link = self.serviceResult[1].xpath("//PARAM[@name='format']/LINK")[0] |
| 273 |
self.assertEqual(link.get("content-role"), "#pre-set") |
| 274 |
self.assertEqual(link.get("value"), "application/fits") |
| 275 |
|
| 276 |
def testOptionsRepresented(self): |
| 277 |
tree = self.serviceResult[1] |
| 278 |
self.assertEqual( |
| 279 |
tree.xpath("//PARAM[@name='format']/VALUES/OPTION")[0].get("value"), |
| 280 |
"text/plain") |
| 281 |
self.assertEqual( |
| 282 |
tree.xpath("//PARAM[@name='format']/VALUES/OPTION")[1].get("value"), |
| 283 |
"application/fits") |
| 284 |
|
| 285 |
def testAccessURLPresent(self): |
| 286 |
tree = self.serviceResult[1] |
| 287 |
self.assertEqual( |
| 288 |
tree.xpath("//PARAM[@name='accessURL']")[0].get("value"), |
| 289 |
"http://localhost:8080/data/test/foo/dlget") |
| 290 |
|
| 291 |
def testAddExtras(self): |
| 292 |
rows = self.serviceResult[1].xpath("//TR[TD[6]='#testing']") |
| 293 |
self.assertEqual(rows[0][1].text, "http://bogosity.code/28") |
| 294 |
self.assertEqual(rows[1][1].text, "http://bogosity.code/28") |
| 295 |
|
| 296 |
def testExternalProc(self): |
| 297 |
tree = testhelpers.pickSingle( |
| 298 |
self.serviceResult[1].xpath("RESOURCE[@utype='adhoc:service'][" |
| 299 |
"INFO[@name='title']/@value='GAVO TAP']")) |
| 300 |
self.assertEqual( |
| 301 |
testhelpers.pickSingle(tree.xpath("PARAM[@name='standardID']") |
| 302 |
).get("value"), |
| 303 |
"ivo://ivoa.net/std/tap") |
| 304 |
self.assertEqual( |
| 305 |
testhelpers.pickSingle(tree.xpath("PARAM[@ucd='meta.ref.url']") |
| 306 |
).get("value"), |
| 307 |
"http://dc.g-vo.org/tap/sync") |
| 308 |
self.assertEqual( |
| 309 |
testhelpers.pickSingle(tree.xpath("GROUP/PARAM[@name='QUERY']") |
| 310 |
).get("value"), |
| 311 |
"SELECT * FROM some_table_name") |
| 312 |
|
| 313 |
refLine = self.serviceResult[1].xpath("//TR[TD='TAP service with an alternative representation of the data']")[0] |
| 314 |
self.assertEqual( |
| 315 |
refLine[2].text, # svc_id |
| 316 |
tree.get("ID")) |
| 317 |
|
| 318 |
def testCapability(self): |
| 319 |
intfEl = self.serviceResult[3].xpath("//interface")[0] |
| 320 |
self.assertEqual( |
| 321 |
intfEl.attrib["{http://www.w3.org/2001/XMLSchema-instance}type"], |
| 322 |
"vs:ParamHTTP") |
| 323 |
self.assertEqual(intfEl.xpath("queryType")[0].text, "GET") |
| 324 |
self.assertEqual(intfEl.xpath("resultType")[0].text, |
| 325 |
'application/x-votable+xml;content=datalink') |
| 326 |
self.assertEqual(intfEl.xpath("accessURL")[0].text, |
| 327 |
'http://localhost:8080/data/test/foo/dlmeta') |
| 328 |
|
| 329 |
self.assertEqual(self.serviceResult[3].xpath("/capability")[0].attrib[ |
| 330 |
"standardID"], "ivo://ivoa.net/std/DataLink#links-1.1") |
| 331 |
|
| 332 |
def testServiceTitle(self): |
| 333 |
# the title info is used by the datalink XSLT |
| 334 |
self.assertEqual(self.serviceResult[1].xpath( |
| 335 |
"RESOURCE[@ID='proc_svc']/INFO[@name='title']")[0].get("value"), |
| 336 |
"Processing service with id foo") |
| 337 |
|
| 338 |
def testCapabilityParameters(self): |
| 339 |
intfEl = self.serviceResult[3].xpath("//interface")[0] |
| 340 |
for el in intfEl.xpath("param"): |
| 341 |
parName = el.xpath("name")[0].text |
| 342 |
if parName=="ID": |
| 343 |
self.assertEqual(el.attrib["std"], "true") |
| 344 |
self.assertEqual(el.xpath("ucd")[0].text, "meta.id;meta.main") |
| 345 |
|
| 346 |
elif parName=="responseformat": |
| 347 |
datatype = el.xpath("dataType")[0] |
| 348 |
self.assertEqual(datatype.text, "char") |
| 349 |
self.assertEqual(datatype.get("arraysize"), "*") |
| 350 |
|
| 351 |
elif parName=="maxrec": |
| 352 |
self.assertEqual(el.xpath("description")[0].text, |
| 353 |
"Maximum number of records returned." |
| 354 |
" Pass 0 to retrieve service parameters.") |
| 355 |
|
| 356 |
elif parName=="verb": |
| 357 |
pass # forget it; perhaps we'll use it some day. |
| 358 |
|
| 359 |
else: |
| 360 |
raise AssertionError("Unexpected Parameter %s"%parName) |
| 361 |
|
| 362 |
def testAsyncDeclared(self): |
| 363 |
svc = api.parseFromString(svcs.Service, """ |
| 364 |
<service id="foo" allowed="dlget,dlasync,dlmeta"> |
| 365 |
<datalinkCore> |
| 366 |
<metaMaker> |
| 367 |
<code> |
| 368 |
yield MS(InputKey, name="PAR", type="text") |
| 369 |
</code> |
| 370 |
</metaMaker> |
| 371 |
<dataFunction procDef="//soda#generateProduct"/> |
| 372 |
</datalinkCore> |
| 373 |
</service>""") |
| 374 |
svc.parent = testhelpers.getTestRD() |
| 375 |
|
| 376 |
mime, data = trialhelpers.runSvcWith(svc, "dlmeta", { |
| 377 |
"ID": [ |
| 378 |
api.getStandardPubDID("data/a.imp"), |
| 379 |
]}) |
| 380 |
|
| 381 |
tree = testhelpers.getXMLTree(data, debug=False) |
| 382 |
|
| 383 |
self.assertEqual(len(tree.xpath("//TR")), 4) |
| 384 |
self.assertEqual( |
| 385 |
set(['http://localhost:8080/data/test/foo/dlget', |
| 386 |
'http://localhost:8080/data/test/foo/dlasync']), |
| 387 |
set([p.get("value") for p in tree.xpath("//PARAM[@name='accessURL']")])) |
| 388 |
self.assertEqual( |
| 389 |
set(['ivo://ivoa.net/std/soda#async-1.0', |
| 390 |
'ivo://ivoa.net/std/soda#sync-1.0']), |
| 391 |
set([p.get("value") |
| 392 |
for p in tree.xpath("//PARAM[@name='standardID']")])) |
| 393 |
|
| 394 |
def testCoreForgetting(self): |
| 395 |
from gavo.svcs import renderers |
| 396 |
args = {"ID": api.getStandardPubDID("data/ex.fits")} |
| 397 |
svc = api.getRD("data/cores").getById("dl") |
| 398 |
renderer = renderers.getRenderer("dlmeta") |
| 399 |
gns = testhelpers.getMemDiffer() |
| 400 |
core = svc.core.adaptForRenderer(renderer, svcs.emptyQueryMeta) |
| 401 |
|
| 402 |
class _Sentinel(object): |
| 403 |
pass |
| 404 |
s = _Sentinel() |
| 405 |
core.ref = s |
| 406 |
|
| 407 |
coreId = id(core.__dict__) |
| 408 |
self.assertTrue(coreId in set(id(r) for r in gc.get_referrers(s))) |
| 409 |
it = svcs.CoreArgs.fromRawArgs(core.inputTable, args) |
| 410 |
core.runForMeta(svc, it, svcs.emptyQueryMeta) |
| 411 |
core.finalize() |
| 412 |
core.inputTable.breakCircles() |
| 413 |
del core |
| 414 |
del it |
| 415 |
gc.collect() |
| 416 |
|
| 417 |
# Hack to work around delayed removal of core from locals() |
| 418 |
# perhaps that's actually a python 3.7 bug? |
| 419 |
if "core" in locals(): |
| 420 |
locals().pop("core") |
| 421 |
|
| 422 |
ns = gns() |
| 423 |
self.assertEqual(len(ns), 0, "Uncollected garbage: %s"%ns) |
| 424 |
self.assertFalse(coreId in set(id(r) for r in gc.get_referrers(s)), |
| 425 |
"core still lives and references s") |
| 426 |
|
| 427 |
|
| 428 |
class _MetaMakerTestRows(testhelpers.TestResource): |
| 429 |
resources = [ |
| 430 |
("serviceResult", _metaMakerTestData)] |
| 431 |
|
| 432 |
def make(self, dependents): |
| 433 |
td = api.resolveCrossId("//datalink#dlresponse", None) |
| 434 |
rows = {} |
| 435 |
for tuple in dependents["serviceResult"][2]: |
| 436 |
row = td.makeRowFromTuple(tuple) |
| 437 |
rows.setdefault((row["ID"], row["semantics"]), []).append(row) |
| 438 |
return rows |
| 439 |
|
| 440 |
|
| 441 |
class DatalinkMetaRowsTest(testhelpers.VerboseTest): |
| 442 |
resources = [("rows", _MetaMakerTestRows()), |
| 443 |
("serviceResult", _metaMakerTestData)] |
| 444 |
|
| 445 |
def testAllLinks(self): |
| 446 |
self.assertEqual(len(self.rows), 17) |
| 447 |
for key, r in list(self.rows.items()): |
| 448 |
if key[1]!="#proc": |
| 449 |
self.assertEqual(len(r), 1) |
| 450 |
|
| 451 |
def testAllWithId(self): |
| 452 |
self.assertEqual(set(r[0] for r in self.rows), |
| 453 |
set(['ivo://x-testing/~?data/b.imp', |
| 454 |
'ivo://x-testing/~?data/a.imp', |
| 455 |
'ivo://not.asked.for'])) |
| 456 |
|
| 457 |
def testAccessURLStatic(self): |
| 458 |
self.assertEqual(self.rows[ |
| 459 |
('ivo://x-testing/~?data/b.imp', '#sibling')][0]["access_url"], |
| 460 |
'http://foo/bar') |
| 461 |
|
| 462 |
def testAccessURLAccess(self): |
| 463 |
self.assertEqual(self.rows[ |
| 464 |
('ivo://x-testing/~?data/b.imp', '#proc')][0]["access_url"], |
| 465 |
# take the actual access URL from the service block. |
| 466 |
None) |
| 467 |
|
| 468 |
def testAccessURLSelf(self): |
| 469 |
self.assertEqual(self.rows[ |
| 470 |
('ivo://x-testing/~?data/b.imp', '#this')][0]["access_url"], |
| 471 |
"http://localhost:8080/getproduct/data/b.imp") |
| 472 |
self.assertEqual(self.rows[ |
| 473 |
('ivo://x-testing/~?data/a.imp', '#this')][0]["access_url"], |
| 474 |
"http://localhost:8080/getproduct/data/a.imp") |
| 475 |
|
| 476 |
def testProcDescribed(self): |
| 477 |
self.assertEqual(self.rows[ |
| 478 |
('ivo://x-testing/~?data/b.imp', '#proc')][0]["description"], |
| 479 |
"A sample processing service") |
| 480 |
|
| 481 |
def testMimes(self): |
| 482 |
self.assertEqual(self.rows[('ivo://x-testing/~?data/a.imp', |
| 483 |
'#calibration')][0]["content_type"], 'test/gold') |
| 484 |
|
| 485 |
def testSemantics(self): |
| 486 |
self.assertEqual(set(r[1] for r in self.rows), |
| 487 |
set(['#proc', '#this', '#sibling', '#calibration', '#preview', |
| 488 |
"#testing", |
| 489 |
"http://dc.g-vo.org/datalink#other", |
| 490 |
'http://www.g-vo.org/dl#related', |
| 491 |
'http://www.g-vo.org/dl#unrelated', |
| 492 |
])) |
| 493 |
|
| 494 |
def testLocalSemantics(self): |
| 495 |
withLS = self.rows[('ivo://x-testing/~?data/b.imp', |
| 496 |
'#sibling')] |
| 497 |
self.assertEqual(withLS[0]["local_semantics"], "geneigt") |
| 498 |
|
| 499 |
def testSizes(self): |
| 500 |
self.assertEqual(self.rows[('ivo://x-testing/~?data/a.imp', |
| 501 |
'#sibling')][0]["content_length"], 500002) |
| 502 |
self.assertEqual(self.rows[('ivo://x-testing/~?data/a.imp', |
| 503 |
'#calibration')][0]["content_length"], None) |
| 504 |
|
| 505 |
def testServiceLink(self): |
| 506 |
svcRow = self.rows[('ivo://x-testing/~?data/a.imp', |
| 507 |
'#proc')][0] |
| 508 |
resId = svcRow["service_def"] |
| 509 |
for res in self.serviceResult[1].xpath("//RESOURCE"): |
| 510 |
if res.attrib.get("ID")==resId: |
| 511 |
break |
| 512 |
else: |
| 513 |
self.fail("Processing service not in datalink links") |
| 514 |
self.assertEqual(res.attrib.get("type"), "meta") |
| 515 |
self.assertEqual(res.attrib.get("utype"), "adhoc:service") |
| 516 |
|
| 517 |
def testSelfMeta(self): |
| 518 |
selfRow = self.rows[('ivo://x-testing/~?data/b.imp', '#this')][0] |
| 519 |
self.assertEqual(selfRow["content_type"], "text/plain") |
| 520 |
self.assertEqual(selfRow["content_length"], 73) |
| 521 |
|
| 522 |
def testMetaError(self): |
| 523 |
errors = self.rows[('ivo://not.asked.for', datalink.DEFAULT_SEMANTICS)] |
| 524 |
self.assertEqual(errors[0]["error_message"], |
| 525 |
'NotFoundFault: Cannot locate other mess') |
| 526 |
|
| 527 |
def testPreviewMetaURL(self): |
| 528 |
previewRow = self.rows[('ivo://x-testing/~?data/b.imp', '#preview')][0] |
| 529 |
self.assertEqual(previewRow["access_url"], |
| 530 |
"http://example.com/borken.jpg") |
| 531 |
self.assertEqual(previewRow["content_type"], |
| 532 |
"image/jpeg") |
| 533 |
|
| 534 |
def testPreviewMetaAuto(self): |
| 535 |
previewRow = self.rows[('ivo://x-testing/~?data/a.imp', '#preview')][0] |
| 536 |
self.assertEqual(previewRow["access_url"], |
| 537 |
"http://localhost:8080/getproduct/data/a.imp?preview=True") |
| 538 |
self.assertEqual(previewRow["content_type"], |
| 539 |
"text/plain") |
| 540 |
|
| 541 |
def testFromNonExistingFile(self): |
| 542 |
errRow = self.rows[('ivo://x-testing/~?data/b.imp', |
| 543 |
'http://www.g-vo.org/dl#unrelated')][0] |
| 544 |
self.assertEqual(errRow["error_message"], |
| 545 |
"NotFoundFault: No file for linked item") |
| 546 |
self.assertEqual(errRow["description"], |
| 547 |
"An unrelated nothing") |
| 548 |
|
| 549 |
def testExpectedNonExisting(self): |
| 550 |
self.assertFalse(('ivo://x-testing/~?data/b.imp', |
| 551 |
'http://www.g-vo.org/dl#mustnothappen') in self.rows, |
| 552 |
"suppressMissing in makeLinkFromFile broken?") |
| 553 |
|
| 554 |
def testFromFile(self): |
| 555 |
row = self.rows[('ivo://x-testing/~?data/b.imp', |
| 556 |
'http://www.g-vo.org/dl#related')][0] |
| 557 |
self.assertEqual(row["error_message"], None) |
| 558 |
self.assertEqual(row["content_length"], 8) |
| 559 |
self.assertEqual(row["description"], "Some mapping") |
| 560 |
self.assertEqual(row["content_type"], "application/octet-stream") |
| 561 |
|
| 562 |
def testSemanticsOverridable(self): |
| 563 |
row = self.rows[('ivo://x-testing/~?data/b.imp', "#calibration")][0] |
| 564 |
self.assertEqual(row["content_type"], "test/gold") |
| 565 |
|
| 566 |
def contentQualifierDefault(self): |
| 567 |
row = self.rows[('ivo://x-testing/~?data/b.imp', "#calibration")][0] |
| 568 |
self.assertEqual(row["content_qualifier"], None) |
| 569 |
|
| 570 |
def contentQualifierFromLink(self): |
| 571 |
row = self.rows[('ivo://x-testing/~?data/a.imp', "#sibling")][0] |
| 572 |
self.assertEqual(row["content_qualifier"], "#other") |
| 573 |
|
| 574 |
def contentQualifierFromFile(self): |
| 575 |
row = self.rows[('ivo://x-testing/~?data/a.imp', |
| 576 |
"http://www.g-vo.org/dl#related")][0] |
| 577 |
self.assertEqual(row["content_qualifier"], "#something") |
| 578 |
|
| 579 |
|
| 580 |
class _TimestampService(testhelpers.TestResource): |
| 581 |
def make(self, ignored): |
| 582 |
svc = api.parseFromString(svcs.Service, """ |
| 583 |
<service id="foo" allowed="dlget,dlmeta"> |
| 584 |
<meta name="title">A Test Service</meta> |
| 585 |
<datalinkCore> |
| 586 |
<metaMaker> |
| 587 |
<code> |
| 588 |
yield MS(InputKey, name="t", type="timestamp[2]", |
| 589 |
ucd="time.epoch", |
| 590 |
xtype="interval", |
| 591 |
values=MS(Values, default=[ |
| 592 |
datetime.datetime(2020, 1, 1),datetime.datetime(2021, 1, 1)])) |
| 593 |
</code> |
| 594 |
</metaMaker> |
| 595 |
<descriptorGenerator> |
| 596 |
<setup> |
| 597 |
<code> |
| 598 |
def addExtras(descriptor): |
| 599 |
descriptor.bogosity = len(descriptor.pubDID) |
| 600 |
</code> |
| 601 |
</setup> |
| 602 |
<code> |
| 603 |
if pubDID=="bogus": |
| 604 |
return DatalinkFault.NotFoundFault(pubDID, "No bogus data here") |
| 605 |
|
| 606 |
res = ProductDescriptor(pubDID, "bogus", None, |
| 607 |
"application/bogus") |
| 608 |
res.contentQualifier = "#timeseries" |
| 609 |
|
| 610 |
return res |
| 611 |
</code> |
| 612 |
</descriptorGenerator> |
| 613 |
<metaMaker semantics="#testing"> |
| 614 |
<code> |
| 615 |
yield descriptor.makeLink( |
| 616 |
f"http://bogosity.code/{descriptor.bogosity}") |
| 617 |
</code> |
| 618 |
</metaMaker> |
| 619 |
<dataFunction> |
| 620 |
<code> |
| 621 |
assert isinstance(args["t"][0], datetime.datetime) |
| 622 |
assert isinstance(args["t"][1], datetime.datetime) |
| 623 |
descriptor.data = "%s: %s"%(descriptor.pubDID, repr(args["t"])) |
| 624 |
</code> |
| 625 |
</dataFunction> |
| 626 |
</datalinkCore> |
| 627 |
</service>""") |
| 628 |
svc.parent = testhelpers.getTestRD() |
| 629 |
return svc |
| 630 |
|
| 631 |
|
| 632 |
class TimestampTest(testhelpers.VerboseTest): |
| 633 |
resources = [("svc", _TimestampService())] |
| 634 |
|
| 635 |
def testTimestampsMeta(self): |
| 636 |
mime, data = trialhelpers.runSvcWith(self.svc, "dlmeta", { |
| 637 |
"ID": "beknackt"}) |
| 638 |
tree = testhelpers.getXMLTree(data, debug=False) |
| 639 |
# this sucks -- xtype=interval and xtype=timestamp is mutually |
| 640 |
# exclusive at this point. I invent something. |
| 641 |
pe = tree.xpath("//GROUP[@name='inputParams']/PARAM[@name='t']") |
| 642 |
param = testhelpers.pickSingle(pe) |
| 643 |
self.assertEqual(param.get("xtype"), "timestamp-interval") |
| 644 |
self.assertEqual(param.get("arraysize"), "19x2") |
| 645 |
self.assertEqual(param.get("datatype"), "char") |
| 646 |
self.assertEqual( |
| 647 |
param.get("value"), |
| 648 |
"2020-01-01T00:00:002021-01-01T00:00:00") |
| 649 |
|
| 650 |
def testTimestampsParsed(self): |
| 651 |
data = trialhelpers.runSvcWith(self.svc, "dlget", { |
| 652 |
"ID": "beknackt", "t": "2008-04-01T12:00:002008-05-01T12:00:00"} |
| 653 |
) |
| 654 |
self.assertEqual(data, |
| 655 |
# should be different when we properly do the xtype thing |
| 656 |
"beknackt: [datetime.datetime(2008, 4, 1, 12, 0), datetime.datetime(2008, 5, 1, 12, 0)]") |
| 657 |
|
| 658 |
def testFaultsFromDescGen(self): |
| 659 |
data = trialhelpers.runSvcWith(self.svc, "dlmeta", { |
| 660 |
"ID": "bogus"}) |
| 661 |
s, metadata = votable.loads(data[1]) |
| 662 |
res = list(metadata.iterDicts(s)) |
| 663 |
|
| 664 |
self.assertEqual( |
| 665 |
(testhelpers.pickSingle(res)["ID"], res[0]["error_message"], res[0]["semantics"]), |
| 666 |
("bogus", "NotFoundFault: No bogus data here", "#this")) |
| 667 |
|
| 668 |
def testTitleMetaInInfo(self): |
| 669 |
data = trialhelpers.runSvcWith(self.svc, "dlmeta", { |
| 670 |
"ID": "beknackt"})[1] |
| 671 |
tree = testhelpers.getXMLTree(data, debug=False) |
| 672 |
self.assertEqual(tree.xpath( |
| 673 |
"RESOURCE[@ID='proc_svc']/INFO[@name='title']")[0].get("value"), |
| 674 |
"A Test Service") |
| 675 |
|
| 676 |
def testThisMeta(self): |
| 677 |
data = trialhelpers.runSvcWith(self.svc, "dlmeta", { |
| 678 |
"ID": "tictac"})[1] |
| 679 |
tree = testhelpers.getXMLTree(data, debug=False) |
| 680 |
# semantics in TD 8, contentQualifier 10 |
| 681 |
self.assertEqual(tree.xpath( |
| 682 |
"//TR[TD[6]='#this']/TD[10]")[0].text, |
| 683 |
"#timeseries") |
| 684 |
|
| 685 |
|
| 686 |
class _TwoDatalinkServices(testhelpers.TestResource): |
| 687 |
def make(self, ignored): |
| 688 |
rd = api.parseFromString(api.RD, |
| 689 |
"""<resource schema="test"> |
| 690 |
<procDef type="descriptorGenerator" id="gen"> |
| 691 |
<code> |
| 692 |
res = ProductDescriptor(pubDID, None, None, None) |
| 693 |
res.suppressAutoLinks = True |
| 694 |
return res |
| 695 |
</code> |
| 696 |
</procDef> |
| 697 |
<service id="dl" allowed="dlmeta,dlget"> |
| 698 |
<datalinkCore> |
| 699 |
<descriptorGenerator procDef="gen"/> |
| 700 |
<metaMaker> |
| 701 |
<code> |
| 702 |
yield MS(InputKey, name="arg1", type="text", |
| 703 |
description="First service's argument 1") |
| 704 |
</code> |
| 705 |
</metaMaker> |
| 706 |
<metaMaker> |
| 707 |
<code> |
| 708 |
yield ProcLinkDef(descriptor.pubDID, rd.getById("dl2")) |
| 709 |
</code> |
| 710 |
</metaMaker> |
| 711 |
<metaMaker> |
| 712 |
<code> |
| 713 |
yield ProcLinkDef(descriptor.pubDID, rd.getById("dl3")) |
| 714 |
</code> |
| 715 |
</metaMaker> |
| 716 |
</datalinkCore> |
| 717 |
</service> |
| 718 |
|
| 719 |
<service id="dl2" allowed="dlget"> |
| 720 |
<meta name="description">An external datalink service</meta> |
| 721 |
<meta name="standardID"/> |
| 722 |
<datalinkCore> |
| 723 |
<descriptorGenerator procDef="gen"/> |
| 724 |
<metaMaker> |
| 725 |
<code> |
| 726 |
yield MS(InputKey, name="knurr", type="text", |
| 727 |
description="Second service's argument") |
| 728 |
</code> |
| 729 |
</metaMaker> |
| 730 |
</datalinkCore> |
| 731 |
</service> |
| 732 |
|
| 733 |
<service id="dl3" allowed="dlget"> |
| 734 |
<meta name="description">Another external datalink service</meta> |
| 735 |
<datalinkCore> |
| 736 |
<descriptorGenerator procDef="gen"/> |
| 737 |
<metaMaker> |
| 738 |
<code> |
| 739 |
yield MS(InputKey, name="knurr", type="text", |
| 740 |
description="One of the third service's arguments") |
| 741 |
yield MS(InputKey, name="ruck", type="integer", |
| 742 |
description="an integer-valued argument") |
| 743 |
</code> |
| 744 |
</metaMaker> |
| 745 |
</datalinkCore> |
| 746 |
</service> |
| 747 |
</resource>""") |
| 748 |
|
| 749 |
metaTree = testhelpers.getXMLTree( |
| 750 |
trialhelpers.runSvcWith(rd.getById("dl"), "dlmeta", |
| 751 |
{"ID": "junkjunk"})[1], debug=False) |
| 752 |
return metaTree, rd |
| 753 |
|
| 754 |
|
| 755 |
class SecondServiceTest(testhelpers.VerboseTest): |
| 756 |
resources = [("treeAndRD", _TwoDatalinkServices())] |
| 757 |
|
| 758 |
def testProcLinks(self): |
| 759 |
svc1, svc2, svc3 = self.treeAndRD[0].xpath("//TR[TD[6]='#proc']") |
| 760 |
|
| 761 |
self.assertEqual(svc1.xpath("TD[6]")[0].text, "#proc") |
| 762 |
self.assertEqual(svc1.xpath("TD[1]")[0].text, "junkjunk") |
| 763 |
self.assertEqual(svc1.xpath("TD[3]")[0].text, "ext_svc") |
| 764 |
self.assertEqual(svc1.xpath("TD[5]")[0].text, |
| 765 |
"An external datalink service") |
| 766 |
|
| 767 |
self.assertEqual(svc2.xpath("TD[6]")[0].text, "#proc") |
| 768 |
self.assertEqual(svc2.xpath("TD[3]")[0].text, "ext_svc-02") |
| 769 |
self.assertEqual(svc2.xpath("TD[5]")[0].text, |
| 770 |
"Another external datalink service") |
| 771 |
|
| 772 |
self.assertEqual(svc3.xpath("TD[6]")[0].text, "#proc") |
| 773 |
self.assertEqual(svc3.xpath("TD[3]")[0].text, "proc_svc") |
| 774 |
self.assertEqual(svc3.xpath("TD[5]")[0].text, |
| 775 |
"An interactive service on this dataset.") |
| 776 |
|
| 777 |
def testBuiltinDef(self): |
| 778 |
svcrsc = self.treeAndRD[0].xpath("RESOURCE[@ID='proc_svc']")[0] |
| 779 |
self.assertEqual(svcrsc.xpath("DESCRIPTION")[0].text, |
| 780 |
"An interactive service on this dataset.") |
| 781 |
self.assertEqual(svcrsc.get("utype"), "adhoc:service") |
| 782 |
self.assertEqual(svcrsc.get("name"), "dl") |
| 783 |
self.assertEqual(svcrsc.xpath("PARAM[@name='standardID']")[0] |
| 784 |
.get("value"), "ivo://ivoa.net/std/soda#sync-1.0") |
| 785 |
self.assertEqual(svcrsc.xpath("PARAM[@name='accessURL']")[0] |
| 786 |
.get("value"), "http://localhost:8080/temporary/dl/dlget") |
| 787 |
self.assertEqual(svcrsc.xpath("GROUP/PARAM[@name='arg1']/DESCRIPTION" |
| 788 |
)[0].text, "First service's argument 1") |
| 789 |
|
| 790 |
def testExt1Rsc(self): |
| 791 |
svcrsc = self.treeAndRD[0].xpath("RESOURCE[@ID='ext_svc']")[0] |
| 792 |
self.assertEqual(svcrsc.get("name"), "dl2") |
| 793 |
self.assertEqual(svcrsc.xpath("GROUP/PARAM[@name='ID']")[0] |
| 794 |
.get("value"), "junkjunk") |
| 795 |
self.assertEqual(svcrsc.xpath("GROUP/PARAM[@name='knurr']")[0] |
| 796 |
.get("datatype"), "char") |
| 797 |
self.assertEqual(len(svcrsc.xpath("PARAM[@name='standardID']")), 0) |
| 798 |
self.assertEqual(svcrsc.xpath("PARAM[@name='accessURL']")[0] |
| 799 |
.get("value"), "http://localhost:8080/temporary/dl2/dlget") |
| 800 |
|
| 801 |
def testExt2Rsc(self): |
| 802 |
svcrsc = self.treeAndRD[0].xpath("RESOURCE[@ID='ext_svc-02']")[0] |
| 803 |
self.assertEqual(svcrsc.get("name"), "dl3") |
| 804 |
self.assertEqual(svcrsc.xpath("PARAM[@name='standardID']")[0] |
| 805 |
.get("value"), "ivo://ivoa.net/std/soda#sync-1.0") |
| 806 |
self.assertEqual(svcrsc.xpath("PARAM[@name='accessURL']")[0] |
| 807 |
.get("value"), "http://localhost:8080/temporary/dl3/dlget") |
| 808 |
self.assertEqual(svcrsc.xpath("GROUP/PARAM[@name='knurr']")[0] |
| 809 |
.get("datatype"), "char") |
| 810 |
self.assertEqual(svcrsc.xpath("GROUP/PARAM[@name='ruck']")[0] |
| 811 |
.get("datatype"), "int") |
| 812 |
|
| 813 |
def testWrongProcLinkType(self): |
| 814 |
self.assertRaisesWithMsg(api.StructureError, |
| 815 |
"ProcLinkDef must be constructed with a dlget-renderable service.", |
| 816 |
datalink.ProcLinkDef, |
| 817 |
('ivo://x-gone', api.resolveCrossId("//adql#query"))) |
| 818 |
|
| 819 |
|
| 820 |
class FromtableTest(testhelpers.VerboseTest): |
| 821 |
resources = [("sst", tresc.ssaTestTable)] |
| 822 |
|
| 823 |
def testFromrow(self): |
| 824 |
svc = api.parseFromString(svcs.Service, """<service id="uh" |
| 825 |
allowed="dlmeta"> |
| 826 |
<datalinkCore> |
| 827 |
<descriptorGenerator procDef="//datalink#fromtable"> |
| 828 |
<bind key="tableName">"test.hcdtest"</bind> |
| 829 |
<bind key="idColumn">"accref"</bind> |
| 830 |
<setup> |
| 831 |
<code> |
| 832 |
def addExtras(desc): |
| 833 |
desc.extraAttribute = "nowaythiscouldnotbefromhere" |
| 834 |
</code> |
| 835 |
</setup> |
| 836 |
</descriptorGenerator> |
| 837 |
<metaMaker semantics="#frommeta"> |
| 838 |
<code> |
| 839 |
yield descriptor.makeLink( |
| 840 |
"http://foo/bar/"+descriptor.metadata["mime"]) |
| 841 |
</code> |
| 842 |
</metaMaker> |
| 843 |
<metaMaker semantics="#fromextr"> |
| 844 |
<code> |
| 845 |
yield descriptor.makeLink( |
| 846 |
"http://foo/bar/"+descriptor.extraAttribute) |
| 847 |
</code> |
| 848 |
</metaMaker> |
| 849 |
|
| 850 |
</datalinkCore></service>""") |
| 851 |
svc.parent = testhelpers.getTestRD() |
| 852 |
mime, vot = trialhelpers.runSvcWith( |
| 853 |
svc, "dlmeta", {"ID": "data/spec1.ssatest"}) |
| 854 |
tree = testhelpers.getXMLTree(vot, debug=False) |
| 855 |
links = [node.text for node in tree.xpath("//TR/TD[2]")] |
| 856 |
self.assertEqual(links, [ |
| 857 |
'http://foo/bar/image/jpeg', |
| 858 |
'http://foo/bar/nowaythiscouldnotbefromhere']) |
| 859 |
|
| 860 |
|
| 861 |
if __name__=="__main__": |
| 862 |
testhelpers.main(DatalinkMetaRowsTest) |