| 1 |
""" |
| 2 |
Tests for the various modules in utils. |
| 3 |
""" |
| 4 |
|
| 5 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 |
#c |
| 7 |
#c This program is free software, covered by the GNU GPL. See the |
| 8 |
#c COPYING file in the source distribution. |
| 9 |
|
| 10 |
|
| 11 |
import os |
| 12 |
import pathlib |
| 13 |
|
| 14 |
from gavo.helpers import testhelpers |
| 15 |
|
| 16 |
from gavo import base |
| 17 |
from gavo import utils |
| 18 |
from gavo.utils import algotricks |
| 19 |
from gavo.utils import codetricks |
| 20 |
from gavo.utils import stanxml |
| 21 |
from gavo.utils import typeconversions |
| 22 |
|
| 23 |
|
| 24 |
class TopoSortTest(testhelpers.VerboseTest): |
| 25 |
def testEmpty(self): |
| 26 |
self.assertEqual(algotricks.topoSort([]), []) |
| 27 |
|
| 28 |
def testSimpleGraph(self): |
| 29 |
self.assertEqual(algotricks.topoSort([(1,2), (2,3), (3,4)]), [1,2,3,4]) |
| 30 |
|
| 31 |
def testComplexGraph(self): |
| 32 |
self.assertEqual(algotricks.topoSort([(1,2), (2,3), (1,3), (3,4), |
| 33 |
(1,4), (2,4)]), [1,2,3,4]) |
| 34 |
|
| 35 |
def testCyclicGraph(self): |
| 36 |
self.assertRaisesWithMsg(ValueError, "Graph not acyclic, cycle: 2->1", |
| 37 |
algotricks.topoSort, ([(1,2), (2,1)],)) |
| 38 |
|
| 39 |
|
| 40 |
class PrefixTest(testhelpers.VerboseTest, metaclass=testhelpers.SamplesBasedAutoTest): |
| 41 |
def _runTest(self, args): |
| 42 |
s1, s2, prefixLength = args |
| 43 |
self.assertEqual(utils.commonPrefixLength(s1, s2), prefixLength) |
| 44 |
|
| 45 |
samples = [ |
| 46 |
("abc", "abd", 2), |
| 47 |
("abc", "a", 1), |
| 48 |
("abc", "", 0), |
| 49 |
("", "abc", 0), |
| 50 |
("a", "abc", 1), |
| 51 |
("z", "abc", 0),] |
| 52 |
|
| 53 |
|
| 54 |
class IdManagerTest(testhelpers.VerboseTest): |
| 55 |
"""tests for working id manager. |
| 56 |
""" |
| 57 |
def setUp(self): |
| 58 |
self.im = utils.IdManagerMixin() |
| 59 |
|
| 60 |
def testNoDupe(self): |
| 61 |
testob = IdManagerTest |
| 62 |
self.assertEqual(self.im.makeIdFor(testob), |
| 63 |
utils.intToFunnyWord(id(testob))) |
| 64 |
self.assertRaises(ValueError, |
| 65 |
self.im.makeIdFor, |
| 66 |
testob) |
| 67 |
|
| 68 |
def testRetrieve(self): |
| 69 |
testob = "abc" |
| 70 |
theId = self.im.makeIdFor(testob) |
| 71 |
self.assertEqual(self.im.getIdFor(testob), theId) |
| 72 |
|
| 73 |
def testRefRes(self): |
| 74 |
testob = "abc" |
| 75 |
theId = self.im.makeIdFor(testob) |
| 76 |
self.assertEqual(self.im.getForId(theId), testob) |
| 77 |
|
| 78 |
def testUnknownOb(self): |
| 79 |
self.assertRaises(utils.NotFoundError, self.im.getIdFor, 1) |
| 80 |
|
| 81 |
def testUnknownId(self): |
| 82 |
self.assertRaises(utils.NotFoundError, self.im.getForId, "abc") |
| 83 |
|
| 84 |
def testSuggestion(self): |
| 85 |
testob = object() |
| 86 |
givenId = self.im.makeIdFor(testob, "ob1") |
| 87 |
self.assertEqual(givenId, "ob1") |
| 88 |
testob2 = object() |
| 89 |
id2 = self.im.makeIdFor(testob2, "ob1/") |
| 90 |
self.assertEqual(id2, "ob1-02") |
| 91 |
self.assertTrue(testob is self.im.getForId("ob1")) |
| 92 |
self.assertTrue(testob2 is self.im.getForId("ob1-02")) |
| 93 |
|
| 94 |
|
| 95 |
class LoadModuleTest(testhelpers.VerboseTest): |
| 96 |
"""tests for cli's module loader. |
| 97 |
""" |
| 98 |
def testLoading(self): |
| 99 |
ob = utils.loadInternalObject("utils.codetricks", "loadPythonModule") |
| 100 |
self.assertTrue(hasattr(ob, "__call__")) |
| 101 |
|
| 102 |
def testNotLoading(self): |
| 103 |
self.assertRaises(ImportError, utils.loadInternalObject, "noexist", "u") |
| 104 |
|
| 105 |
def testBadName(self): |
| 106 |
self.assertRaises(AttributeError, utils.loadInternalObject, |
| 107 |
"utils.codetricks", "noexist") |
| 108 |
|
| 109 |
|
| 110 |
class CachedGetterTest(testhelpers.VerboseTest): |
| 111 |
def testNormal(self): |
| 112 |
g = utils.CachedGetter(lambda c: [c], 3) |
| 113 |
self.assertEqual(g(), [3]) |
| 114 |
g().append(4) |
| 115 |
self.assertEqual(g(), [3, 4]) |
| 116 |
|
| 117 |
def testMortal(self): |
| 118 |
g = utils.CachedGetter(lambda c: [c], 3, |
| 119 |
isAlive=lambda l: len(l)<3) |
| 120 |
g().append(4) |
| 121 |
self.assertEqual(g(), [3,4]) |
| 122 |
g().append(5) |
| 123 |
self.assertEqual(g(), [3]) |
| 124 |
|
| 125 |
|
| 126 |
class SimpleTextTest(testhelpers.VerboseTest): |
| 127 |
def testFeatures(self): |
| 128 |
with testhelpers.testFile("test.txt", |
| 129 |
r"""# Test File\ |
| 130 |
this is stripped |
| 131 |
An empty line is ignored |
| 132 |
|
| 133 |
Contin\ |
| 134 |
uation lines \ |
| 135 |
# (a comment in between is ok) |
| 136 |
are concatenated |
| 137 |
""") as fName: |
| 138 |
with open(fName) as f: |
| 139 |
res = list(utils.iterSimpleText(f)) |
| 140 |
|
| 141 |
self.assertEqual(res, [ |
| 142 |
(2, "this is stripped"), |
| 143 |
(3, "An empty line is ignored"), |
| 144 |
(8, "Continuation lines are concatenated")]) |
| 145 |
|
| 146 |
def testNoTrailingBackslash(self): |
| 147 |
with testhelpers.testFile("test.txt", |
| 148 |
"""No |
| 149 |
non-finished\\ |
| 150 |
continuation\\""") as fName: |
| 151 |
with open(fName) as f: |
| 152 |
self.assertRaisesWithMsg(utils.SourceParseError, |
| 153 |
"At line 3: File ends with a backslash", |
| 154 |
lambda f: list(utils.iterSimpleText(f)), |
| 155 |
(f,)) |
| 156 |
|
| 157 |
|
| 158 |
class ToVOTableTypeTest(testhelpers.VerboseTest, |
| 159 |
metaclass=testhelpers.SamplesBasedAutoTest): |
| 160 |
def _runTest(self, sample): |
| 161 |
sqlType, voTableType = sample |
| 162 |
self.assertEqual( |
| 163 |
typeconversions.sqltypeToVOTable(sqlType), |
| 164 |
voTableType) |
| 165 |
|
| 166 |
samples = [ |
| 167 |
("double precision", ('double', None, None)), |
| 168 |
("text", ('char', "*", None)), |
| 169 |
("char", ('char', '1', None)), |
| 170 |
("unicode", ('unicodeChar', "*", None)), |
| 171 |
("double precision[2]", ('double', '2', None)), |
| 172 |
# 5 |
| 173 |
("timestamp", ("char", "19", "timestamp")), |
| 174 |
("spoint", ("double", "2", "point")), |
| 175 |
("int4range", ("int", "2", "interval")), |
| 176 |
("timestamp[5]", ("char", "19x5", "timestamp")), |
| 177 |
("scircle[5]", ("double", "3x5", "circle")), |
| 178 |
# 10 |
| 179 |
("char[1]", ("char", "1", None)), |
| 180 |
("char[12][]", ("char", "12x*", None)), |
| 181 |
# we should probably flat this as invalid |
| 182 |
("char(12)[*]", ("char", "12x*", None)), |
| 183 |
("varchar(*)", ("char", "*", None)), |
| 184 |
# we probably shouldn't let the following parse |
| 185 |
("varchar[13][15](*)", ("char", "13x15x*", None)), |
| 186 |
# 15 |
| 187 |
("varchar(*)", ("char", "*", None)), |
| 188 |
] |
| 189 |
|
| 190 |
|
| 191 |
class ToVOTableErrorTest(testhelpers.VerboseTest, |
| 192 |
metaclass=testhelpers.SamplesBasedAutoTest): |
| 193 |
def _runTest(self, sample): |
| 194 |
sqlType, message = sample |
| 195 |
self.assertRaisesWithMsg(Exception, |
| 196 |
message, |
| 197 |
typeconversions.sqltypeToVOTable, |
| 198 |
(sqlType,)) |
| 199 |
|
| 200 |
samples = [ |
| 201 |
("varchar[1", "No VOTable type for varchar[1"), |
| 202 |
("vanqual", "No VOTable type for vanqual"), |
| 203 |
("char[][]", |
| 204 |
"Arrays may only have variable length in the last dimension"), |
| 205 |
] |
| 206 |
|
| 207 |
|
| 208 |
class NoModuleAliasingTest(testhelpers.VerboseTest): |
| 209 |
def testAliasing(self): |
| 210 |
dn = base.getConfig("inputsDir") |
| 211 |
with testhelpers.testFile("klotz.py", "sentinel = 1", |
| 212 |
inDir=os.path.join(dn, "mod1")) as modsrc1: |
| 213 |
with testhelpers.testFile("klotz.py", "sentinel = 2", |
| 214 |
inDir=os.path.join(dn, "mod2")) as modsrc2: |
| 215 |
mod1, spec1 = codetricks.loadPythonModule(modsrc1[:-3]) |
| 216 |
mod2, spec2 = codetricks.loadPythonModule(modsrc2[:-3]) |
| 217 |
self.assertEqual(mod1.sentinel, 1) |
| 218 |
self.assertEqual(mod2.sentinel, 2) |
| 219 |
try: |
| 220 |
import klotz #noflake: supposed to fail |
| 221 |
except ImportError: |
| 222 |
# this must fail in order to keep "local" modules from polluting |
| 223 |
# global imports. |
| 224 |
pass |
| 225 |
else: |
| 226 |
self.fail("loadPythonModule messed up sys.modules or sys.path.") |
| 227 |
|
| 228 |
|
| 229 |
class StanXMLTest(testhelpers.VerboseTest): |
| 230 |
class Model(object): |
| 231 |
class MEl(stanxml.Element): |
| 232 |
_local = True |
| 233 |
class Root(MEl): |
| 234 |
_childSequence = ["Child", "Nilble"] |
| 235 |
class Child(MEl): |
| 236 |
_childSequence = ["Foo", None] |
| 237 |
class Other(MEl): |
| 238 |
pass |
| 239 |
class Nilble(stanxml.NillableMixin, MEl): |
| 240 |
_a_restatt = None |
| 241 |
|
| 242 |
def testNoTextContent(self): |
| 243 |
M = self.Model |
| 244 |
self.assertRaises(stanxml.ChildNotAllowed, lambda:M.Root["abc"]) |
| 245 |
|
| 246 |
def testTextContent(self): |
| 247 |
M = self.Model |
| 248 |
data = M.Root[M.Child["a\xA0bc"]] |
| 249 |
self.assertEqual(data.render(), b'<Root><Child>a\xc2\xa0bc</Child></Root>') |
| 250 |
|
| 251 |
def testRetrieveText(self): |
| 252 |
M = self.Model |
| 253 |
data = M.Other["thrown away", M.Other["mixed"], " remaining "] |
| 254 |
self.assertEqual(data.text_, " remaining ") |
| 255 |
|
| 256 |
def testNillableNil(self): |
| 257 |
M = self.Model |
| 258 |
rendered = M.Root[M.Nilble()].render() |
| 259 |
self.assertTrue(b'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' |
| 260 |
in rendered) |
| 261 |
self.assertTrue(b'Nilble xsi:nil="true"' in rendered) |
| 262 |
|
| 263 |
def testNillableNonNil(self): |
| 264 |
M = self.Model |
| 265 |
rendered = M.Root[M.Nilble["Value"]].render() |
| 266 |
self.assertTrue(b"<Nilble>Value</Nilble>" in rendered) |
| 267 |
self.assertFalse(b'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' |
| 268 |
in rendered) |
| 269 |
|
| 270 |
def testNillableAttribute(self): |
| 271 |
M = self.Model |
| 272 |
rendered = M.Root[M.Nilble(restatt="x")].render() |
| 273 |
self.assertTrue(b'<Nilble restatt="x" xsi:nil="true"></Nilble>' in rendered) |
| 274 |
|
| 275 |
def testSerialisingException(self): |
| 276 |
try: |
| 277 |
_ = 1/0 |
| 278 |
except Exception as raised: |
| 279 |
e = raised |
| 280 |
|
| 281 |
M = self.Model |
| 282 |
rendered = M.Root[M.Child[e]].render() |
| 283 |
self.assertTrue( |
| 284 |
b"<Child>EXCEPTION:\nZeroDivisionError: division by zero" |
| 285 |
in rendered) |
| 286 |
|
| 287 |
def testLocalAttribute(self): |
| 288 |
M = self.Model |
| 289 |
doc = M.Root[M.Child["x"]] |
| 290 |
doc.addAttribute("xmlns:foo", "urn:foo") |
| 291 |
doc.addAttribute("xmlns:bar", "urn:bar") |
| 292 |
|
| 293 |
self.assertEqual( |
| 294 |
doc.render(), |
| 295 |
b'<Root xmlns:bar="urn:bar" xmlns:foo="urn:foo"><Child>x</Child></Root>') |
| 296 |
|
| 297 |
|
| 298 |
class StanXMLNamespaceTest(testhelpers.VerboseTest): |
| 299 |
|
| 300 |
stanxml.registerPrefix("ns1", "http://bar.com", None) |
| 301 |
stanxml.registerPrefix("ns0", "http://foo.com", None) |
| 302 |
stanxml.registerPrefix("foo", "http://bori.ng", "http://schema.is.here") |
| 303 |
|
| 304 |
class E(object): |
| 305 |
class LocalElement(stanxml.Element): |
| 306 |
_prefix = "ns1" |
| 307 |
_local = _mayBeEmpty = True |
| 308 |
class A(LocalElement): |
| 309 |
_a_x = None |
| 310 |
class B(LocalElement): |
| 311 |
_a_y = None |
| 312 |
class NSElement(stanxml.Element): |
| 313 |
_prefix = "ns0" |
| 314 |
class C(NSElement): |
| 315 |
_a_z = "ab" |
| 316 |
class D(NSElement): |
| 317 |
_a_u = "x" |
| 318 |
_name_a_u = "foo:u" |
| 319 |
_additionalPrefixes = frozenset(["foo"]) |
| 320 |
|
| 321 |
def testTraversal(self): |
| 322 |
tree = self.E.A[self.E.B, self.E.B, self.E.A] |
| 323 |
def record(node, content, attrDict, childIter): |
| 324 |
return (node.name_, |
| 325 |
[c.apply(record) for c in childIter]) |
| 326 |
self.assertEqual(tree.apply(record), |
| 327 |
('A', [('B', []), ('B', []), ('A', [])])) |
| 328 |
|
| 329 |
def testSimpleRender(self): |
| 330 |
tree = self.E.A[self.E.B, self.E.B, self.E.A] |
| 331 |
self.assertEqual(testhelpers.cleanXML(tree.render()), |
| 332 |
'<A><B/><B/><A/></A>') |
| 333 |
|
| 334 |
def testRenderWithText(self): |
| 335 |
E = self.E |
| 336 |
tree = E.A[E.C["arg"], E.C(z="c")[E.B["muss"], E.A]] |
| 337 |
self.assertEqual(tree.render(), |
| 338 |
b'<A xmlns:ns0="http://foo.com" xmlns:ns1="http://bar.com"><ns0:C z="ab">arg</ns0:C>' |
| 339 |
b'<ns0:C z="c"><B>muss</B><A/></ns0:C></A>') |
| 340 |
|
| 341 |
def testAdditionalPrefixes(self): |
| 342 |
tree = self.E.C[self.E.D["xy"]] |
| 343 |
self.assertEqual(tree.render(includeSchemaLocation=False), |
| 344 |
b'<ns0:C xmlns:foo="http://bori.ng" xmlns:ns0="http://foo.com" z="ab"><ns0:D foo:u="x">xy</ns0:D></ns0:C>') |
| 345 |
|
| 346 |
def testSchemaLocation(self): |
| 347 |
tree = self.E.D["xy"] |
| 348 |
self.assertEqual(tree.render(), |
| 349 |
b'<ns0:D foo:u="x" xmlns:foo="http://bori.ng" xmlns:ns0="http://' |
| 350 |
b'foo.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' |
| 351 |
b'xsi:schemaLocation="http://bori.ng http://schema.is.here">xy</ns0:D>') |
| 352 |
|
| 353 |
def testEmptyPrefix(self): |
| 354 |
tree = self.E.C["bar"] |
| 355 |
self.assertEqual(tree.render(prefixForEmpty="ns0"), |
| 356 |
b'<C xmlns:ns0="http://foo.com" xmlns="http://foo.com" z="ab">bar</C>') |
| 357 |
|
| 358 |
|
| 359 |
class IAUDesignationTest(testhelpers.VerboseTest, |
| 360 |
metaclass=testhelpers.SamplesBasedAutoTest): |
| 361 |
def _runTest(self, sample): |
| 362 |
args, expected = sample |
| 363 |
res = utils.makeIAUId(*args) |
| 364 |
self.assertEqual(res, expected) |
| 365 |
|
| 366 |
samples = [ |
| 367 |
(("TJ", 0, 0), "TJ000000+000000"), |
| 368 |
(("TJ", 0, 0, 1), "TJ000000.0+000000"), |
| 369 |
(("TJ", 0, 0, 0, 1), "TJ000000+000000.0"), |
| 370 |
(("BB", 34.13333, 23.24722, 1, 0), "BB021631.9+231449"), |
| 371 |
(("BB", 34.13333, -23.24722, 1, 0), "BB021631.9-231449"), |
| 372 |
(("BB", 34.13333, -23.24722, 0, 0), "BB021631-231449"), |
| 373 |
(("BB", 34.13333, -23.24722, 0, 1), "BB021631-231449.9"), |
| 374 |
] |
| 375 |
|
| 376 |
|
| 377 |
pP = pathlib.Path |
| 378 |
|
| 379 |
class PathOpsTest(testhelpers.VerboseTest): |
| 380 |
def testRelPathBasicStr(self): |
| 381 |
self.assertEqual( |
| 382 |
utils.getRelativePath("/foo/bar/baz", "/foo"), |
| 383 |
"bar/baz") |
| 384 |
|
| 385 |
def testRelPathBasicPath(self): |
| 386 |
self.assertEqual( |
| 387 |
utils.getRelativePath( |
| 388 |
pP("/foo/bar/baz"), pP("/foo")), |
| 389 |
pP("bar/baz")) |
| 390 |
|
| 391 |
def testRelPathTrailingSlashStr(self): |
| 392 |
self.assertEqual( |
| 393 |
utils.getRelativePath("/foo/bar/baz", "/foo/"), |
| 394 |
"bar/baz") |
| 395 |
|
| 396 |
def testRelPathTrailingSlashPath(self): |
| 397 |
self.assertEqual( |
| 398 |
utils.getRelativePath( |
| 399 |
pP("/foo/bar/baz"), pP("/foo/")), |
| 400 |
pP("bar/baz")) |
| 401 |
|
| 402 |
def testNoRelPathStr(self): |
| 403 |
self.assertRaises(ValueError, |
| 404 |
utils.getRelativePath, |
| 405 |
"/foo/bar/baz", "/bar") |
| 406 |
|
| 407 |
def testNoRelPathPath(self): |
| 408 |
self.assertRaises(ValueError, |
| 409 |
utils.getRelativePath, |
| 410 |
pP("/foo/bar/baz"), pP("/bar")) |
| 411 |
|
| 412 |
def testIlliberalChars(self): |
| 413 |
self.assertRaises(ValueError, |
| 414 |
utils.getRelativePath, |
| 415 |
pP("/foo/bar/baz+quux"), pP("/bar")) |
| 416 |
|
| 417 |
def testLiberalChars(self): |
| 418 |
self.assertRaises(ValueError, |
| 419 |
utils.getRelativePath, |
| 420 |
pP("/foo/bar/baz+quux"), pP("/bar"), |
| 421 |
liberalChars=True) |
| 422 |
|
| 423 |
def testIdenticalStr(self): |
| 424 |
self.assertEqual( |
| 425 |
utils.getRelativePath( |
| 426 |
"/foo/bar", "/foo/bar"), |
| 427 |
"") |
| 428 |
|
| 429 |
def testIdenticalPath(self): |
| 430 |
self.assertEqual( |
| 431 |
utils.getRelativePath( |
| 432 |
pP('/home/msdemlei/_gavo_test/inputs'), |
| 433 |
pP('/home/msdemlei/_gavo_test/inputs')), |
| 434 |
pP(".")) |
| 435 |
|
| 436 |
|
| 437 |
if __name__=="__main__": |
| 438 |
testhelpers.main(CachedGetterTest) |