| 1 |
""" |
| 2 |
Common test resources. |
| 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 contextlib |
| 12 |
import os |
| 13 |
|
| 14 |
from gavo.helpers import testhelpers |
| 15 |
|
| 16 |
from gavo import base |
| 17 |
from gavo import rsc |
| 18 |
from gavo import rscdef |
| 19 |
from gavo import utils |
| 20 |
from gavo.protocols import creds |
| 21 |
from gavo.protocols import tap |
| 22 |
from gavo.utils import pgsphere |
| 23 |
|
| 24 |
|
| 25 |
class DBConnection(testhelpers.TestResource): |
| 26 |
"""sets up a DB connection. |
| 27 |
""" |
| 28 |
setUpCost = 0.1 |
| 29 |
|
| 30 |
def make(self, ignored): |
| 31 |
conn = base.getDBConnection("admin") |
| 32 |
return conn |
| 33 |
|
| 34 |
def clean(self, conn): |
| 35 |
if not conn.closed: |
| 36 |
conn.commit() |
| 37 |
conn.close() |
| 38 |
|
| 39 |
def finishedWith(self, resource, result=None): |
| 40 |
testhelpers.TestResource.finishedWith(self, resource, result) |
| 41 |
try: |
| 42 |
resource.commit() |
| 43 |
except base.DBError: |
| 44 |
# the connection probably has been closed. Let someone else |
| 45 |
# worry about it. |
| 46 |
pass |
| 47 |
|
| 48 |
dbConnection = DBConnection() |
| 49 |
|
| 50 |
|
| 51 |
class TestWithDBConnection(testhelpers.VerboseTest): |
| 52 |
"""A base class for tests needing just a connection as test resources. |
| 53 |
|
| 54 |
You get auto rollback of the connection. |
| 55 |
""" |
| 56 |
resources = [("conn", dbConnection)] |
| 57 |
|
| 58 |
def tearDown(self): |
| 59 |
self.conn.rollback() |
| 60 |
testhelpers.VerboseTest.tearDown(self) |
| 61 |
|
| 62 |
|
| 63 |
def makeProdtestTable(conn): |
| 64 |
rd = testhelpers.getTestRD() |
| 65 |
dd = rd.getDataDescById("productimport") |
| 66 |
data = rsc.makeData(dd, |
| 67 |
parseOptions=rsc.parseValidating.change(keepGoing=True), |
| 68 |
connection=conn) |
| 69 |
conn.commit() |
| 70 |
return data |
| 71 |
|
| 72 |
class ProdtestTable(testhelpers.TestResource): |
| 73 |
"""the prodtest table defined in test.rd. |
| 74 |
|
| 75 |
The resource builds and tears down the table. It returns a connection |
| 76 |
that is commited in the end, so you should make sure you clean up |
| 77 |
after yourself. The prodtest table is dropped, of course. |
| 78 |
""" |
| 79 |
resources = [('conn', dbConnection)] |
| 80 |
setUpCost = 20 |
| 81 |
|
| 82 |
prodtbl = None |
| 83 |
|
| 84 |
def make(self, deps): |
| 85 |
self.conn = deps["conn"] |
| 86 |
self.data = makeProdtestTable(self.conn) |
| 87 |
return self.conn |
| 88 |
|
| 89 |
def clean(self, ignore): |
| 90 |
try: |
| 91 |
self.conn.commit() |
| 92 |
except: |
| 93 |
self.conn.rollback() |
| 94 |
self.data.tables["prodtest"].drop() |
| 95 |
self.conn.commit() |
| 96 |
|
| 97 |
_defaultProdtblEntry = { |
| 98 |
"accessPath": "/road/to/nowhere", |
| 99 |
"accref": "just.testing/nowhere", |
| 100 |
"sourceTable": "/////////////////////", |
| 101 |
"owner": None, |
| 102 |
"embargo": None, |
| 103 |
"mime": "application/x-bogus", |
| 104 |
"datalink": None, |
| 105 |
"preview": 'AUTO', |
| 106 |
"preview_mime": None,} |
| 107 |
|
| 108 |
@contextlib.contextmanager |
| 109 |
def prodtblRow(self, **kwargs): |
| 110 |
rec = self._defaultProdtblEntry.copy() |
| 111 |
rec.update(kwargs) |
| 112 |
if self.prodtbl is None: |
| 113 |
self.prodtbl = rsc.TableForDef( |
| 114 |
base.caches.getRD("//products").getById("products"), |
| 115 |
connection=self.conn) |
| 116 |
self.prodtbl.addRow(rec) |
| 117 |
self.conn.commit() |
| 118 |
try: |
| 119 |
yield |
| 120 |
finally: |
| 121 |
self.prodtbl.connection.execute( |
| 122 |
"delete from dc.products where sourceTable=%(s)s", |
| 123 |
{"s": rec["sourceTable"]}) |
| 124 |
self.conn.commit() |
| 125 |
|
| 126 |
prodtestTable = ProdtestTable() |
| 127 |
|
| 128 |
|
| 129 |
class TestUsers(testhelpers.TestResource): |
| 130 |
"""Test values in the user table. |
| 131 |
""" |
| 132 |
resources = [('conn', dbConnection)] |
| 133 |
setUpCost = 2 |
| 134 |
tearDownCost = 2 |
| 135 |
|
| 136 |
def make(self, deps): |
| 137 |
self.conn = deps["conn"] |
| 138 |
self.users = [ |
| 139 |
utils.NS(user="X_test", password="megapass", remarks=""), |
| 140 |
utils.NS(user="Y_test", password="megapass", remarks="second test user"), |
| 141 |
] |
| 142 |
try: |
| 143 |
for u in self.users: |
| 144 |
creds.addUser(self.conn, u.user, u.password, u.remarks) |
| 145 |
creds.addToGroup(self.conn, "X_test", "Y_test") |
| 146 |
self.conn.commit() |
| 147 |
except base.IntegrityError: # stale users? try again. |
| 148 |
self.conn.rollback() |
| 149 |
self.clean(None) |
| 150 |
for u in self.users: |
| 151 |
creds.addUser(self.conn, u.user, u.password, u.remarks) |
| 152 |
creds.addToGroup(self.conn, "X_test", "Y_test") |
| 153 |
self.conn.commit() |
| 154 |
return self.users |
| 155 |
|
| 156 |
def clean(self, ignore): |
| 157 |
for u in self.users: |
| 158 |
creds.delUser(self.conn, u.user) |
| 159 |
self.conn.commit() |
| 160 |
|
| 161 |
|
| 162 |
testUsers = TestUsers() |
| 163 |
|
| 164 |
|
| 165 |
class RDDataResource(testhelpers.TestResource): |
| 166 |
"""A resource consiting generated from a data item in an RD. |
| 167 |
|
| 168 |
Specify the name of an rd file in data in the rdName class attribute |
| 169 |
(default: test.rd), the id of the data object within in dataId. |
| 170 |
""" |
| 171 |
resources = [('conn', dbConnection)] |
| 172 |
|
| 173 |
rdName = "data/test" |
| 174 |
dataId = None |
| 175 |
doTAP = False |
| 176 |
forceSource = None |
| 177 |
|
| 178 |
def make(self, deps): |
| 179 |
self.conn = deps["conn"] |
| 180 |
dd = base.caches.getRD(self.rdName).getById(self.dataId) |
| 181 |
self.dataCreated = rsc.makeData(dd, connection=self.conn, |
| 182 |
forceSource=self.forceSource) |
| 183 |
if self.doTAP: |
| 184 |
tap.publishToTAP(dd.rd, self.conn) |
| 185 |
return self.dataCreated.getPrimaryTable() |
| 186 |
|
| 187 |
def clean(self, table): |
| 188 |
try: |
| 189 |
self.dataCreated.dropTables(rsc.parseNonValidating) |
| 190 |
if self.doTAP: |
| 191 |
tap.unpublishFromTAP(self.dataCreated.dd.rd, self.conn) |
| 192 |
self.conn.commit() |
| 193 |
except: |
| 194 |
self.conn.rollback() |
| 195 |
|
| 196 |
|
| 197 |
class CSTestTable(RDDataResource): |
| 198 |
"""A database table including positions, as defined by test#adql. |
| 199 |
""" |
| 200 |
dataId = "csTestTable" |
| 201 |
doTAP = True |
| 202 |
|
| 203 |
def make(self, deps): |
| 204 |
res = RDDataResource.make(self, deps) |
| 205 |
tap.publishToTAP(res.tableDef.rd, deps["conn"]) |
| 206 |
return res |
| 207 |
|
| 208 |
csTestTable = CSTestTable() |
| 209 |
|
| 210 |
|
| 211 |
class ADQLTestTable(RDDataResource): |
| 212 |
dataId = "import_adqltable" |
| 213 |
|
| 214 |
adqlTestTable = ADQLTestTable() |
| 215 |
|
| 216 |
|
| 217 |
class ADQLGeoTable(RDDataResource): |
| 218 |
forceSource = [ |
| 219 |
{"row_id": "moc-6-1", |
| 220 |
"a_moc": pgsphere.SMoc.fromASCII("6/450"), |
| 221 |
"a_point": pgsphere.SPoint.fromDegrees(23, 42)}, |
| 222 |
{"row_id": "moc-4-1", |
| 223 |
"a_moc": pgsphere.SMoc.fromASCII("4/28"), |
| 224 |
"a_point": pgsphere.SPoint.fromDegrees(25, 44)},] |
| 225 |
|
| 226 |
dataId = "import_adqlgeo" |
| 227 |
|
| 228 |
adqlGeoTable = ADQLGeoTable() |
| 229 |
|
| 230 |
|
| 231 |
class _TAPPublishedADQLTable(CSTestTable): |
| 232 |
doTAP = True |
| 233 |
|
| 234 |
tapPublishedADQLTable = _TAPPublishedADQLTable() |
| 235 |
|
| 236 |
|
| 237 |
class _SSATable(RDDataResource): |
| 238 |
rdName = "data/ssatest" |
| 239 |
dataId = "test_import" |
| 240 |
doTAP = True |
| 241 |
setUpCost = 15 |
| 242 |
|
| 243 |
ssaTestTable = _SSATable() |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
class _SIAPTestTable(testhelpers.TestResource): |
| 248 |
resources = [("conn", dbConnection)] |
| 249 |
setUpCost = 10 |
| 250 |
|
| 251 |
def __init__(self, ddid): |
| 252 |
self.ddid = ddid |
| 253 |
testhelpers.TestResource.__init__(self) |
| 254 |
|
| 255 |
def make(self, deps): |
| 256 |
self.conn = deps["conn"] |
| 257 |
|
| 258 |
rd = testhelpers.getTestRD() |
| 259 |
dd = rd.getById(self.ddid) |
| 260 |
return rsc.makeData(dd, connection=self.conn, forceSource=[ |
| 261 |
testhelpers.computeWCSKeys(pos, size) for pos, size in [ |
| 262 |
((0, 0), (10, 10)), |
| 263 |
((0, 90), (1, 1)), |
| 264 |
((45, -45), (1, 1)), |
| 265 |
((0, 45), (2.1, 1.1)), |
| 266 |
((1, 45), (4.1, 1.1)), |
| 267 |
((2, 45), (2.1, 1.1)), |
| 268 |
((160, 45), (2.1, 1.1)), |
| 269 |
((161, 45), (4.1, 1.1)), |
| 270 |
((162, 45), (2.1, 1.1))]]) |
| 271 |
|
| 272 |
def clean(self, data): |
| 273 |
data.dropTables(rsc.parseNonValidating) |
| 274 |
self.conn.commit() |
| 275 |
|
| 276 |
|
| 277 |
siapTestTable = _SIAPTestTable("pgs_siaptest") |
| 278 |
|
| 279 |
|
| 280 |
class _ObscoreTable(RDDataResource): |
| 281 |
rdName = "//obscore" |
| 282 |
dataId = "create" |
| 283 |
doTAP = True |
| 284 |
|
| 285 |
resources = RDDataResource.resources+[ |
| 286 |
("ssap", ssaTestTable), ("siap", siapTestTable)] |
| 287 |
|
| 288 |
def make(self, deps): |
| 289 |
self.conn = deps["conn"] |
| 290 |
dd = base.caches.getRD(self.rdName).getById("makeSources") |
| 291 |
rsc.makeData(dd, connection=self.conn) |
| 292 |
res = RDDataResource.make(self, deps) |
| 293 |
# we need to commit here since this is used in registry record |
| 294 |
# generation, which uses a different connection. |
| 295 |
self.conn.commit() |
| 296 |
return res |
| 297 |
|
| 298 |
obscoreTable = _ObscoreTable() |
| 299 |
|
| 300 |
|
| 301 |
class _WeirdTypesTable(RDDataResource): |
| 302 |
dataId = "import_weirdtypes" |
| 303 |
doTAP = True |
| 304 |
|
| 305 |
weirdTypesTable = _WeirdTypesTable() |
| 306 |
|
| 307 |
|
| 308 |
class RandomDataTable(testhelpers.TestResource): |
| 309 |
"""An in-memory table with a couple of rows. |
| 310 |
""" |
| 311 |
def make(self, deps): |
| 312 |
td = base.parseFromString(rscdef.TableDef, |
| 313 |
"""<table id="randomDataTable"><column name="n" type="integer" |
| 314 |
required="True"/> |
| 315 |
<column name="x"/></table>""") |
| 316 |
return rsc.TableForDef(td, rows=[ |
| 317 |
{"n": 23, "x": 29.25}, |
| 318 |
{"n": 42, "x": -1.75}]) |
| 319 |
|
| 320 |
randomDataTable = RandomDataTable() |
| 321 |
|
| 322 |
|
| 323 |
class FITSTable(RDDataResource): |
| 324 |
"""at least one FITS file in the products table. |
| 325 |
""" |
| 326 |
dataId = "import_fitsprod" |
| 327 |
|
| 328 |
fitsTable = FITSTable() |
| 329 |
|
| 330 |
|
| 331 |
class FileResource(testhelpers.TestResource): |
| 332 |
"""An "abstract" resource that represents a temporary file within |
| 333 |
the root directory with a given name and content. |
| 334 |
|
| 335 |
path and content are given in class attributes of the same names, |
| 336 |
where path is interpreted relative to rootDir. |
| 337 |
""" |
| 338 |
path, content = None, None |
| 339 |
|
| 340 |
def make(self, ignored): |
| 341 |
if self.path is None or self.content is None: |
| 342 |
raise Exception("FileResource without name or content") |
| 343 |
self.absPath = os.path.join(base.getConfig("rootDir"), self.path) |
| 344 |
dn = os.path.dirname(self.absPath) |
| 345 |
if not os.path.isdir(dn): |
| 346 |
os.makedirs(dn) |
| 347 |
|
| 348 |
with open(self.absPath, "w") as f: |
| 349 |
f.write(self.content) |
| 350 |
return self.absPath |
| 351 |
|
| 352 |
def clean(self, rsc): |
| 353 |
if os.path.exists(self.absPath): |
| 354 |
os.unlink(self.absPath) |
| 355 |
|
| 356 |
|
| 357 |
class _FakeSimbad(object): |
| 358 |
"""test instrumentation to avoid querying simbad in unit/regression |
| 359 |
tests. |
| 360 |
""" |
| 361 |
simbadData = {'Aldebaran': {'RA': 68.9375, |
| 362 |
'dec': 16.46875, |
| 363 |
'oname': 'Aldebaran', |
| 364 |
'otype': 'LP?'}, |
| 365 |
'M1': {'RA': 83.65625, 'dec': 22.0145, 'oname': 'M1', 'otype': 'SNR'}, |
| 366 |
'Wozzlfoo7xx': None} |
| 367 |
|
| 368 |
def __init__(self, *args, **kwargs): |
| 369 |
pass |
| 370 |
|
| 371 |
def query(self, ident): |
| 372 |
return self.simbadData.get(ident) |
| 373 |
|
| 374 |
def getPositionFor(self, identifier): |
| 375 |
data = self.query(identifier) |
| 376 |
if not data: |
| 377 |
raise KeyError(identifier) |
| 378 |
return float(data["RA"]), float(data["dec"]) |
| 379 |
|
| 380 |
|
| 381 |
class FakedSimbad(testhelpers.TestResource): |
| 382 |
"""installs a non-querying, fake simbad resolver with constant answers. |
| 383 |
|
| 384 |
This should be a resource whenever protocol.simbadinterface is used, |
| 385 |
except perhps when exercising that module itself. |
| 386 |
""" |
| 387 |
def make(self, deps): |
| 388 |
from gavo.protocols import simbadinterface #noflake: for registration |
| 389 |
self.realCache = base.caches.getSesame |
| 390 |
fs = _FakeSimbad() |
| 391 |
base.caches.getSesame = lambda *args: fs |
| 392 |
|
| 393 |
def cleanup(self, deps): |
| 394 |
base.caches.getSesame = self.realCache |
| 395 |
|
| 396 |
|
| 397 |
fakedSimbad = FakedSimbad() |
| 398 |
|
| 399 |
A_RES_REC = """ |
| 400 |
<resRec id="rec"> |
| 401 |
<meta> |
| 402 |
creationDate: 2010-10-10T10:10:10Z |
| 403 |
_dataUpdated: 2015-10-10T09:09:09 |
| 404 |
resType: document |
| 405 |
status: active |
| 406 |
title: a test document |
| 407 |
subject: testing |
| 408 |
referenceURL: http://bar/doc/fancy |
| 409 |
identifier: ivo://foo.bar |
| 410 |
uses: Sample useful resource |
| 411 |
uses.ivoId: ivo://useful/sample1 |
| 412 |
uses: Crapola junkyard bad example |
| 413 |
uses.ivoId: ivo://crapola/work |
| 414 |
uses: TOPCAT |
| 415 |
uses.altIdentifier: ascl:1101.010 |
| 416 |
_news: Cool date |
| 417 |
_news.date: 2010-10-10 |
| 418 |
_news.role: silly |
| 419 |
_news: Cooler date |
| 420 |
_news.date: 2011-11-11 |
| 421 |
tableset: //users#groups |
| 422 |
tableset: data/test#typesTable |
| 423 |
</meta> |
| 424 |
|
| 425 |
<publish render="edition" sets="ivo_managed,local"> |
| 426 |
<meta> |
| 427 |
accessURL: http://foo.us/fancy/doc |
| 428 |
sourceURL: svn://foo.us/docs/src/fancy |
| 429 |
</meta> |
| 430 |
</publish> |
| 431 |
|
| 432 |
<publish render="edition" sets="ivo_managed"> |
| 433 |
<meta> |
| 434 |
accessURL: http://foo.fr/ycnaf/cod.pdf |
| 435 |
sourceURL: git+ssh://foo.fr/scod/src/ycnaf |
| 436 |
languageCode: fr |
| 437 |
</meta> |
| 438 |
</publish> |
| 439 |
|
| 440 |
<publish render="edition" sets="ivo_managed"> |
| 441 |
<meta> |
| 442 |
accessURL: http://localhost:8080/fancy/doc |
| 443 |
accessURL.resultType: application/msword |
| 444 |
languageCode: de |
| 445 |
locTitle: Ja, geh zu. |
| 446 |
</meta> |
| 447 |
</publish> |
| 448 |
|
| 449 |
</resRec> |
| 450 |
""" |
| 451 |
|
| 452 |
# include this in a resource element to have minimal metadata to pass |
| 453 |
# meta validation. |
| 454 |
MINIMAL_META = """ |
| 455 |
<meta> |
| 456 |
title:x |
| 457 |
creationDate:2020-03-05T12:13:14Z |
| 458 |
description:z |
| 459 |
subject:a |
| 460 |
</meta>""" |
| 461 |
|