| 1 |
""" |
| 2 |
Tests for slap tables and services |
| 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 |
import unittest |
| 14 |
from ctypes.util import find_library |
| 15 |
|
| 16 |
from gavo import api |
| 17 |
from gavo.helpers import testtricks |
| 18 |
from gavo.helpers import trialhelpers |
| 19 |
from gavo.protocols import linetap |
| 20 |
from gavo.web import vodal |
| 21 |
|
| 22 |
import tresc |
| 23 |
|
| 24 |
if find_library(linetap._InChIBinding._libname): |
| 25 |
def skipIfNoInChI(obj): |
| 26 |
return obj |
| 27 |
else: |
| 28 |
def skipIfNoInChI(obj): |
| 29 |
return unittest.skip("No libinchi")(obj) |
| 30 |
|
| 31 |
|
| 32 |
class _SLAPTable(tresc.RDDataResource): |
| 33 |
rdName = "data/slaptest" |
| 34 |
dataId = "import" |
| 35 |
|
| 36 |
slapTestTable = _SLAPTable() |
| 37 |
|
| 38 |
|
| 39 |
class TestWithSLAPTable(testhelpers.VerboseTest): |
| 40 |
resources = [("table", slapTestTable)] |
| 41 |
|
| 42 |
def runService(self, params): |
| 43 |
res = trialhelpers.runSvcWith( |
| 44 |
api.resolveCrossId("data/slaptest#s"), "slap.xml", params) |
| 45 |
return res |
| 46 |
|
| 47 |
|
| 48 |
class ImportingTest(TestWithSLAPTable): |
| 49 |
def testValues(self): |
| 50 |
line = list(self.table.connection.queryToDicts( |
| 51 |
"select * from test.slaptest where chemical_element='Mo'"))[0] |
| 52 |
self.assertAlmostEqual(line.pop("initial_level_energy"), |
| 53 |
1.65537152014313e-26) |
| 54 |
self.assertAlmostEqual(line.pop("final_level_energy"), |
| 55 |
4.96611457698311e-18) |
| 56 |
self.assertEqual(line, { |
| 57 |
'id_status': 'identified', |
| 58 |
'initial_name': 'Upper Level', |
| 59 |
'linename': 'Mo 400 A', |
| 60 |
'final_name': 'Lower Level', 'pub': '2003junk.yard.0123X', |
| 61 |
'chemical_element': 'Mo', 'wavelength': 4e-08}) |
| 62 |
|
| 63 |
def testSimpleMeta(self): |
| 64 |
self.assertEqual( |
| 65 |
self.table.tableDef.getColumnByName("wavelength").utype, |
| 66 |
"ssldm:Line.wavelength.value") |
| 67 |
|
| 68 |
|
| 69 |
class _SLAPMetadata(testhelpers.TestResource): |
| 70 |
resources = [("slapTable", slapTestTable)] |
| 71 |
|
| 72 |
def make(self, dependents): |
| 73 |
request = trialhelpers.FakeRequest("", args={"FORMAt": ["Metadata"]}) |
| 74 |
rendered = request.processWithRoot(vodal.SLAPRenderer(request, |
| 75 |
api.resolveCrossId("data/slaptest#s"))) |
| 76 |
tree = testhelpers.getXMLTree(rendered, debug=False) |
| 77 |
return tree |
| 78 |
|
| 79 |
|
| 80 |
class ServiceMetadataTest(testhelpers.VerboseTest): |
| 81 |
resources = [("tree", _SLAPMetadata())] |
| 82 |
|
| 83 |
def testWavelength(self): |
| 84 |
params = self.tree.xpath("//PARAM[@name='INPUT:wavelength']") |
| 85 |
param = testhelpers.pickSingle(params) |
| 86 |
self.assertEqual(param.get("utype"), "ssldm:Line.wavelength.value") |
| 87 |
self.assertEqual(param.get("unit"), "m") |
| 88 |
|
| 89 |
def testServiceKey(self): |
| 90 |
param = self.tree.xpath("//PARAM[@name='INPUT:verb']")[0] |
| 91 |
self.assertEqual(len(param.xpath("VALUES/OPTION")), 3) |
| 92 |
|
| 93 |
def testFieldsDeclared(self): |
| 94 |
param = self.tree.xpath("//FIELD[@ID='wavelength']")[0] |
| 95 |
self.assertEqual(param.get("utype"), "ssldm:Line.wavelength.value") |
| 96 |
|
| 97 |
def testQueryStatus(self): |
| 98 |
self.assertEqual( |
| 99 |
self.tree.xpath("RESOURCE/INFO[@name='QUERY_STATUS']")[0 |
| 100 |
].get("value"), "OK") |
| 101 |
|
| 102 |
|
| 103 |
class ServiceConstraintsTest(TestWithSLAPTable): |
| 104 |
def testChemicalElement(self): |
| 105 |
res = self.runService({"CHEMICAL_ELEMENT": ["Mo"]} |
| 106 |
).getPrimaryTable().rows |
| 107 |
self.assertEqual(testhelpers.pickSingle(res)["chemical_element"], "Mo") |
| 108 |
|
| 109 |
def testChemicalElements(self): |
| 110 |
res = self.runService({"CHEMICAL_ELEMENT": ["Mo,Bi"]} |
| 111 |
).getPrimaryTable().rows |
| 112 |
self.assertEqual(len(res), 2) |
| 113 |
self.assertEqual(set(r["chemical_element"] for r in res), |
| 114 |
set(["Mo", "Bi"])) |
| 115 |
|
| 116 |
def testWavelength(self): |
| 117 |
res = self.runService({"WAVELENGTH": ["4e-8/1e-7"]} |
| 118 |
).getPrimaryTable().rows |
| 119 |
self.assertEqual(testhelpers.pickSingle(res)["chemical_element"], "Mo") |
| 120 |
|
| 121 |
def testInitialEnergyLower(self): |
| 122 |
res = self.runService({"INITIAL_LEVEL_ENERGY": ["1.1e-29/"]} |
| 123 |
).getPrimaryTable().rows |
| 124 |
self.assertEqual(len(res), 2) |
| 125 |
self.assertEqual(set(r["chemical_element"] for r in res), |
| 126 |
set(["Mo", "Bi"])) |
| 127 |
|
| 128 |
def testInitialEnergyUpper(self): |
| 129 |
res = self.runService({"FINAL_LEVEL_ENERGY": ["/5e-18"]} |
| 130 |
).getPrimaryTable().rows |
| 131 |
self.assertEqual(len(res), 2) |
| 132 |
self.assertEqual(set(r["chemical_element"] for r in res), |
| 133 |
set(["Mo", "H"])) |
| 134 |
|
| 135 |
def testMAXREC(self): |
| 136 |
res = self.runService({"MAXREC": ["1"]} |
| 137 |
).getPrimaryTable().rows |
| 138 |
self.assertEqual(len(res), 1) |
| 139 |
|
| 140 |
|
| 141 |
class _SLAPRegistryRecord(testhelpers.TestResource): |
| 142 |
def make(self, dependents): |
| 143 |
from gavo.registry import capabilities |
| 144 |
from gavo.web import vosi |
| 145 |
publication = api.resolveCrossId("data/slaptest#s").publications[0] |
| 146 |
res = vosi.CAP.capabilities[ |
| 147 |
capabilities.getCapabilityElement(publication)].render() |
| 148 |
return testhelpers.getXMLTree(res, debug=False), res |
| 149 |
|
| 150 |
|
| 151 |
class CapabilityTest(testhelpers.VerboseTest, testtricks.XSDTestMixin): |
| 152 |
resources = [("cap", _SLAPRegistryRecord())] |
| 153 |
|
| 154 |
def testValid(self): |
| 155 |
self.assertValidates(self.cap[1]) |
| 156 |
|
| 157 |
def testType(self): |
| 158 |
self.assertEqual(self.cap[0][0].get( |
| 159 |
"{http://www.w3.org/2001/XMLSchema-instance}type"), |
| 160 |
"slap:SimpleLineAccess") |
| 161 |
|
| 162 |
def testStandardId(self): |
| 163 |
self.assertEqual(self.cap[0][0].get("standardID"), |
| 164 |
"ivo://ivoa.net/std/slap") |
| 165 |
|
| 166 |
def testComplianceLevel(self): |
| 167 |
self.assertEqual(self.cap[0][0].xpath("complianceLevel")[0].text, |
| 168 |
"full") |
| 169 |
|
| 170 |
def testDataSource(self): |
| 171 |
self.assertEqual(self.cap[0][0].xpath("dataSource")[0].text, |
| 172 |
"theoretical") |
| 173 |
|
| 174 |
def testTestQuery(self): |
| 175 |
self.assertEqual(self.cap[0][0].xpath("testQuery/queryDataCmd")[0].text, |
| 176 |
"MAXREC=1") |
| 177 |
|
| 178 |
|
| 179 |
@skipIfNoInChI |
| 180 |
class InchiTest(testhelpers.VerboseTest): |
| 181 |
def testNoLib(self): |
| 182 |
try: |
| 183 |
oldval = linetap._InChIBinding._libname |
| 184 |
linetap._InChIBinding._libname = "nonexistinglib" |
| 185 |
self.assertRaisesWithMsg(api.ReportableError, |
| 186 |
"No libinchi found.", |
| 187 |
linetap._InChIBinding, |
| 188 |
()) |
| 189 |
finally: |
| 190 |
linetap._InChIBinding._libname = oldval |
| 191 |
|
| 192 |
def testInChIOk(self): |
| 193 |
self.assertEqual( |
| 194 |
# this *really* ought to work in strict mode, but alas, the library |
| 195 |
# complains about a FAIL_I2I right now. Too lazy to investigate |
| 196 |
# right now. |
| 197 |
linetap.getILib().checkInChI("InChI=1S/CH4/h1H4", 0), |
| 198 |
0) |
| 199 |
|
| 200 |
def testInChINotOk(self): |
| 201 |
self.assertEqual( |
| 202 |
linetap.getILib().checkInChI("InCh=1S/CH44"), |
| 203 |
3) |
| 204 |
|
| 205 |
def testGetInChIKeyOk(self): |
| 206 |
self.assertEqual( |
| 207 |
linetap.getILib().getInChIKey("InChI=1S/CH4/h1H4"), |
| 208 |
"VNWKTOKETHGBQD-UHFFFAOYSA-N") |
| 209 |
|
| 210 |
def testGetInChiKeyNotOk(self): |
| 211 |
self.assertRaisesWithMsg(linetap.InChIError, |
| 212 |
"Could not generate InChIKey: 3", |
| 213 |
linetap.getILib().getInChIKey, |
| 214 |
("",)) |
| 215 |
|
| 216 |
def testNormalizeInchi(self): |
| 217 |
self.assertEqual( |
| 218 |
linetap.getILib().normalizeInChI("InChI=1S/CH4/h1H4"), |
| 219 |
"InChI=1S/CH4/h1H4") |
| 220 |
|
| 221 |
def testAtomCreation(self): |
| 222 |
a = linetap.getILib().getAtom("Ta", 181) |
| 223 |
self.assertEqual(a.x, 0) |
| 224 |
self.assertEqual(a.elname, b"Ta") |
| 225 |
self.assertEqual(a.isotopic_mass, 181) |
| 226 |
self.assertEqual(a.charge, 0) |
| 227 |
|
| 228 |
def testAtomCreationWithCharge(self): |
| 229 |
a = linetap.getILib().getAtom("Ta", 181, -3) |
| 230 |
self.assertEqual(a.charge, -3) |
| 231 |
|
| 232 |
def testInputCreation(self): |
| 233 |
ii = linetap.getILib() |
| 234 |
mol = ii.getInput([ |
| 235 |
ii.getAtom("O", 16), |
| 236 |
ii.getAtom("H", 1), |
| 237 |
ii.getAtom("H", 2)]) |
| 238 |
mol.connect(0, 1) |
| 239 |
mol.connect(0, 2) |
| 240 |
|
| 241 |
self.assertEqual(mol.num_atoms, 3) |
| 242 |
|
| 243 |
self.assertEqual(mol.atom[0].elname, b"O") |
| 244 |
self.assertEqual(mol.atom[0].num_bonds, 2) |
| 245 |
self.assertEqual(mol.atom[0].neighbor[0], 1) |
| 246 |
self.assertEqual(mol.atom[0].neighbor[1], 2) |
| 247 |
|
| 248 |
self.assertEqual(mol.atom[1].num_bonds, 1) |
| 249 |
self.assertEqual(mol.atom[1].neighbor[0], 0) |
| 250 |
self.assertEqual(mol.atom[2].neighbor[0], 0) |
| 251 |
|
| 252 |
def testInChICreationIso(self): |
| 253 |
ii = linetap.getILib() |
| 254 |
mol = ii.getInput([ |
| 255 |
ii.getAtom("O", 16), |
| 256 |
ii.getAtom("H", 1), |
| 257 |
ii.getAtom("H", 1)]) |
| 258 |
mol.connect(0, 1) |
| 259 |
mol.connect(0, 2) |
| 260 |
|
| 261 |
self.assertEqual(ii.getInChI(mol), "InChI=1S/H2O/h1H2/i1+0/hH2") |
| 262 |
|
| 263 |
def testInChICreation(self): |
| 264 |
ii = linetap.getILib() |
| 265 |
mol = ii.getInput([ |
| 266 |
ii.getAtom("H"), |
| 267 |
ii.getAtom("H"), |
| 268 |
ii.getAtom("C"), |
| 269 |
ii.getAtom("H"), |
| 270 |
ii.getAtom("H")]) |
| 271 |
mol.connect(2, 0) |
| 272 |
mol.connect(2, 1) |
| 273 |
mol.connect(2, 3) |
| 274 |
mol.connect(2, 4) |
| 275 |
|
| 276 |
self.assertEqual(ii.getInChI(mol), "InChI=1S/CH4/h1H4") |
| 277 |
|
| 278 |
def testFailedCreation(self): |
| 279 |
ii = linetap.getILib() |
| 280 |
mol = ii.getInput([ii.getAtom("Ta")]) |
| 281 |
self.assertRaisesWithMsg(linetap.InChIError, |
| 282 |
"Atom indexes must be < 1 here", |
| 283 |
mol.connect, |
| 284 |
(0, 1)) |
| 285 |
|
| 286 |
|
| 287 |
@skipIfNoInChI |
| 288 |
class MolCodeTest(testhelpers.VerboseTest, |
| 289 |
metaclass=testhelpers.SamplesBasedAutoTest): |
| 290 |
def testErrorReporting(self): |
| 291 |
self.assertRaisesWithMsg( |
| 292 |
ValueError, |
| 293 |
"Invalid molecule code token: 'totalermurks'", |
| 294 |
linetap.getILib().getInChIFromCode, |
| 295 |
("12C H H totalermurks",)) |
| 296 |
|
| 297 |
def _runTest(self, sample): |
| 298 |
molcode, expInchi, expWeight = sample |
| 299 |
parsed, foundInchi = linetap.getILib().getInChIFromCode(molcode) |
| 300 |
self.assertEqual(expInchi, foundInchi) |
| 301 |
self.assertEqual(expWeight, parsed.getTotalWeight()) |
| 302 |
|
| 303 |
samples = [ |
| 304 |
("H", "InChI=1S/H", 1), |
| 305 |
("H 1-2 H", "InChI=1S/H2/h1H", 2), |
| 306 |
("O 1=2 O", "InChI=1S/O2/c1-2", 32), |
| 307 |
("N 1#2 N", "InChI=1S/N2/c1-2", 28), |
| 308 |
("H H O 1-3 2-3", "InChI=1S/H2O/h1H2", 18), |
| 309 |
#5 |
| 310 |
("O O C 1=3 2=3", "InChI=1S/CO2/c2-1-3", 44), |
| 311 |
("H 1-2 C 2#3 C 3-4 C H H H 4-5 4-6 4-7", |
| 312 |
"InChI=1S/C3H4/c1-3-2/h1H,2H3", 40), |
| 313 |
("H 1-2 N 2=3 C 3-4 H 3-5 H", "InChI=1S/CH3N/c1-2/h2H,1H2", 29), |
| 314 |
("35S 1-2 F 1-3 F 1-4 F 1-5 F 1-6 F 1-7 F", |
| 315 |
"InChI=1S/F6S/c1-7(2,3,4,5)6/i7+3", 149), |
| 316 |
("34S+ 1-2 F 1-3 F 1-4 F 1-5 F 1-6 F", |
| 317 |
"InChI=1S/F5S/c1-6(2,3,4)5/q+1/i6+2", 129), |
| 318 |
#10 |
| 319 |
("55Fe++++", "InChI=1S/Fe/q+4/i1-1", 55), |
| 320 |
("D 1-2 T", "InChI=1S/H2/h1H/i1+2D", 5), |
| 321 |
] |
| 322 |
|
| 323 |
|
| 324 |
@skipIfNoInChI |
| 325 |
class InChIParsingTest(testhelpers.VerboseTest): |
| 326 |
def testNoInchi(self): |
| 327 |
self.assertRaisesWithMsg(linetap.InChIError, |
| 328 |
"No structural data provided", |
| 329 |
linetap.getILib().parseInChI, |
| 330 |
("keininchi",)) |
| 331 |
|
| 332 |
def testBadLayer(self): |
| 333 |
self.assertRaisesWithMsg(linetap.InChIError, |
| 334 |
"No structural data provided", |
| 335 |
linetap.getILib().parseInChI, |
| 336 |
("InChI=1S/CH4/Breakthis",)) |
| 337 |
|
| 338 |
def testNoMainLayer(self): |
| 339 |
self.assertRaisesWithMsg(linetap.InChIError, |
| 340 |
"No structural data provided", |
| 341 |
linetap.getILib().parseInChI, |
| 342 |
("InChI=1S",)) |
| 343 |
self.assertRaisesWithMsg(linetap.InChIError, |
| 344 |
"No structural data provided", |
| 345 |
linetap.getILib().parseInChI, |
| 346 |
("InChI=1/",)) |
| 347 |
|
| 348 |
def testGetChargeNoLayer(self): |
| 349 |
with linetap.getILib().parseInChI( |
| 350 |
"InChI=1S/C2H6O/c1-2-3/h3H,2H2,1H3") as mol: |
| 351 |
self.assertEqual(mol.getTotalCharge(), 0) |
| 352 |
|
| 353 |
def testGetChargeSimple(self): |
| 354 |
with linetap.getILib().parseInChI("InChI=1/Al/q+1") as mol: |
| 355 |
self.assertEqual( |
| 356 |
mol.getTotalCharge(), 1) |
| 357 |
|
| 358 |
|
| 359 |
@skipIfNoInChI |
| 360 |
class InChIMolweightTest(testhelpers.VerboseTest, |
| 361 |
metaclass=testhelpers.SamplesBasedAutoTest): |
| 362 |
def _runTest(self, sample): |
| 363 |
inchi, expectedMass = sample |
| 364 |
with linetap.getILib().parseInChI(inchi) as mol: |
| 365 |
self.assertEqual(mol.getTotalWeight(), expectedMass) |
| 366 |
|
| 367 |
samples = [ |
| 368 |
("InChI=1S/H", 1), |
| 369 |
("InChI=1S/H2/h1H", 2), |
| 370 |
("InChI=1S/H2O/h1H2", 18), |
| 371 |
("InChI=1S/H2O/h1H2/i/hD", 19), |
| 372 |
("InChI=1S/H2O/h1H2/i/hD2", 20), |
| 373 |
# 05 |
| 374 |
("InChI=1S/H2O/h1H2/i/hTD", 21), |
| 375 |
("InChI=1S/CHBOSSi/c3-1-2-4(1)5/h5H", 100), |
| 376 |
("InChI=1S/CHBOSSi/c3-1-2-4(1)5/h5H/i1+2,2-2,4+3", 103), |
| 377 |
("InChI=1S/F6S/c1-7(2,3,4,5)6", 146), |
| 378 |
] |
| 379 |
|
| 380 |
|
| 381 |
if __name__=="__main__": |
| 382 |
testhelpers.main(ImportingTest) |