| 1 |
""" |
| 2 |
Tests for the configuration infrastructure. |
| 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 warnings |
| 13 |
from io import StringIO |
| 14 |
|
| 15 |
from gavo.helpers import testhelpers |
| 16 |
|
| 17 |
from gavo import api |
| 18 |
from gavo import base |
| 19 |
from gavo.base import config |
| 20 |
from gavo.utils import fancyconfig |
| 21 |
|
| 22 |
|
| 23 |
# The tests from fancyconfig itself |
| 24 |
_, tests = fancyconfig._getTestSuite() |
| 25 |
for _index, _t in enumerate(tests): |
| 26 |
globals()["FancyconfigTest%d"%_index] = _t |
| 27 |
|
| 28 |
|
| 29 |
class RCParseTest(testhelpers.VerboseTest): |
| 30 |
# NOTE: if these things actually change something in config, they |
| 31 |
# must undo these changes (at least on success) |
| 32 |
|
| 33 |
def _parseFragment(self, fragment): |
| 34 |
config._config.addFromFp(StringIO(fragment), origin="testsuite") |
| 35 |
|
| 36 |
def testAuthorityRejected(self): |
| 37 |
self.assertRaisesWithMsg(fancyconfig.BadConfigValue, |
| 38 |
"[ivoa]authority must match [a-zA-Z0-9][a-zA-Z0-9._~-]{2,}$ ('more than three normal characters')", |
| 39 |
self._parseFragment, |
| 40 |
("[ivoa]\nauthority:ivo://x-invalid\n",)) |
| 41 |
|
| 42 |
def testUnknownItemWarns(self): |
| 43 |
with warnings.catch_warnings(record=True) as ws: |
| 44 |
with testhelpers.testFile("testconfig.rc", |
| 45 |
"[web]\nextramad: invalid") as p: |
| 46 |
fancyconfig.readConfiguration(config._config, p, None) |
| 47 |
self.assertEqual([str(w.message) for w in ws], |
| 48 |
['Unknown configuration item [web] extramad ignored.']) |
| 49 |
|
| 50 |
def testUTF8(self): |
| 51 |
with testhelpers.tempConfig(("web", "sitename", "kaputt")): |
| 52 |
with testhelpers.testFile("testconfig.rc", |
| 53 |
b"[web]\nsitename: Universit\xc3\xa4t") as p: |
| 54 |
fancyconfig.readConfiguration(config._config, p, None) |
| 55 |
self.assertEqual(api.getConfig("web", "sitename"), |
| 56 |
'Universit\xe4t') |
| 57 |
|
| 58 |
|
| 59 |
class UserConfigTest(testhelpers.VerboseTest): |
| 60 |
def testSimpleResolution(self): |
| 61 |
s = api.resolveCrossId("%#_test-rmk", None) |
| 62 |
self.assertEqual(s.maps[0].key, "foo") |
| 63 |
|
| 64 |
def testOverriding(self): |
| 65 |
userConfigPath = os.path.join( |
| 66 |
api.getConfig("configDir"), "userconfig.rd") |
| 67 |
base.caches.clearForName(str(userConfigPath)[:-3]) |
| 68 |
with open(userConfigPath, "w") as f: |
| 69 |
f.write("""<resource schema="__system"> |
| 70 |
<rowmaker id="_test-rmk"><map key="bar">@bar</map> |
| 71 |
</rowmaker></resource>\n""") |
| 72 |
try: |
| 73 |
s = api.resolveCrossId("%#_test-rmk", None) |
| 74 |
self.assertEqual(s.maps[0].key, "bar") |
| 75 |
finally: |
| 76 |
os.unlink(userConfigPath) |
| 77 |
base.caches.clearForName(str(userConfigPath)[:-3]) |
| 78 |
|
| 79 |
def testNonexisting(self): |
| 80 |
self.assertRaisesWithMsg(api.NotFoundError, |
| 81 |
"Element with id 'dashiergibtsnicht' could not be located" |
| 82 |
" in etc/userconfig.rd", |
| 83 |
api.resolveCrossId, |
| 84 |
("%#dashiergibtsnicht", None)) |
| 85 |
|
| 86 |
def _resetURLCaches(self): |
| 87 |
base.getHTTPBase.cache_clear() |
| 88 |
base.getHTTPSBase.cache_clear() |
| 89 |
|
| 90 |
def testBasesHTTP(self): |
| 91 |
self._resetURLCaches() |
| 92 |
with testhelpers.tempConfig( |
| 93 |
("web", "serverURL", "http://knall.org:4030/foo/bar")): |
| 94 |
self.assertEqual(base.getHTTPBase(), |
| 95 |
"http://knall.org:4030/foo/bar") |
| 96 |
self.assertEqual(base.getHTTPSBase(), |
| 97 |
"https://knall.org/foo/bar") |
| 98 |
self._resetURLCaches() |
| 99 |
|
| 100 |
def testBasesHTTPS(self): |
| 101 |
self._resetURLCaches() |
| 102 |
with testhelpers.tempConfig( |
| 103 |
("web", "serverURL", "https://knall.org/foo/bar")): |
| 104 |
self.assertEqual(base.getHTTPBase(), |
| 105 |
"http://knall.org/foo/bar") |
| 106 |
self.assertEqual(base.getHTTPSBase(), |
| 107 |
"https://knall.org/foo/bar") |
| 108 |
self._resetURLCaches() |
| 109 |
|
| 110 |
|
| 111 |
def _modernise(): |
| 112 |
if "test-modernise" in api.getConfig("future"): |
| 113 |
return "Modern" |
| 114 |
else: |
| 115 |
return "Old-Fashioned" |
| 116 |
|
| 117 |
|
| 118 |
class FutureTest(testhelpers.VerboseTest): |
| 119 |
def testModern(self): |
| 120 |
with testhelpers.tempConfig(("general", "future", "test-modernise")): |
| 121 |
self.assertEqual(_modernise(), "Modern") |
| 122 |
|
| 123 |
def testOldFashioned(self): |
| 124 |
with testhelpers.tempConfig(("general", "future", "test-other")): |
| 125 |
self.assertEqual(_modernise(), "Old-Fashioned") |
| 126 |
|
| 127 |
def testNoFuture(self): |
| 128 |
self.assertEqual(_modernise(), "Old-Fashioned") |
| 129 |
|
| 130 |
|
| 131 |
class LocalNSTest(testhelpers.VerboseTest): |
| 132 |
def testNoLocalDefined(self): |
| 133 |
self.assertRaisesWithMsg(api.ReportableError, |
| 134 |
api.EqualingRE("No file .*gavo_test/etc/local.py found"), |
| 135 |
lambda: api.local.whatever, |
| 136 |
()) |
| 137 |
|
| 138 |
def testLocalReloading(self): |
| 139 |
with testhelpers.testFile("local.py", |
| 140 |
"MAGIC_LEVEL = 'moderate'\n", |
| 141 |
inDir=api.getConfig("configDir")): |
| 142 |
api.reloadLocal() |
| 143 |
|
| 144 |
self.assertEqual(api.local.MAGIC_LEVEL, 'moderate') |
| 145 |
|
| 146 |
api.reloadLocal() |
| 147 |
self.assertRaisesWithMsg(api.ReportableError, |
| 148 |
api.EqualingRE("No file .*gavo_test/etc/local.py found"), |
| 149 |
lambda: api.local.MAGIC_LEVEL, |
| 150 |
()) |
| 151 |
|
| 152 |
|
| 153 |
if __name__=="__main__": |
| 154 |
testhelpers.main(UserConfigTest) |