| 1 |
""" |
| 2 |
Tests for value mapping |
| 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 datetime |
| 12 |
import types |
| 13 |
|
| 14 |
from gavo.helpers import testhelpers |
| 15 |
|
| 16 |
from gavo import base |
| 17 |
from gavo import formal |
| 18 |
from gavo import rscdef |
| 19 |
from gavo.base import valuemappers |
| 20 |
from gavo.protocols import products #noflake: for registration |
| 21 |
from gavo.utils import pgsphere |
| 22 |
from gavo.web import htmltable |
| 23 |
|
| 24 |
|
| 25 |
class QuotingTest(testhelpers.VerboseTest, |
| 26 |
metaclass=testhelpers.SamplesBasedAutoTest): |
| 27 |
def _runTest(self, sample): |
| 28 |
name, doesNeed = sample |
| 29 |
self.assertEqual(valuemappers.needsQuoting(name), doesNeed) |
| 30 |
|
| 31 |
samples = [ |
| 32 |
("RA(J2000)", True), |
| 33 |
("ABS", True), |
| 34 |
("AbS", True), |
| 35 |
("r", False), |
| 36 |
("_r", True), |
| 37 |
("someThingNormal", False), |
| 38 |
("foo.bar", True), |
| 39 |
] |
| 40 |
|
| 41 |
|
| 42 |
class AnnotationTest(testhelpers.VerboseTest): |
| 43 |
def testBasic(self): |
| 44 |
col = valuemappers.AnnotatedColumn(base.parseFromString(rscdef.Column, |
| 45 |
'<column name="abc" type="integer" displayHint="sf=2" required="True"/>')) |
| 46 |
self.assertEqual(col.original.name, "abc") |
| 47 |
self.assertEqual(col["name"], "abc") |
| 48 |
self.assertTrue(col["displayHint"] is col.original.displayHint) |
| 49 |
self.assertEqual(col["datatype"], "int") |
| 50 |
self.assertEqual(col["arraysize"], None) |
| 51 |
self.assertTrue(col["id"] is None) |
| 52 |
|
| 53 |
def testSetting(self): |
| 54 |
col = valuemappers.AnnotatedColumn(base.parseFromString(rscdef.Column, |
| 55 |
'<column name="abc" type="integer" displayHint="sf=2"/>')) |
| 56 |
col["name"] = "changed" |
| 57 |
self.assertEqual(col.original.name, "abc") |
| 58 |
self.assertEqual(col["name"], "changed") |
| 59 |
|
| 60 |
def testWrapping(self): |
| 61 |
col = valuemappers.AnnotatedColumn(testhelpers.getTestRD( |
| 62 |
).getById("pgs_siaptable").getColumnByName("dateObs")) |
| 63 |
self.assertEqual(col["ucd"], "time;obs.exposure") |
| 64 |
|
| 65 |
|
| 66 |
class MapperBasicTest(testhelpers.VerboseTest): |
| 67 |
def testFactorySequence(self): |
| 68 |
m1 = lambda cp: lambda val: "First" |
| 69 |
m2 = lambda cp: lambda val: "Second" |
| 70 |
mf = valuemappers.ValueMapperFactoryRegistry() |
| 71 |
mf.registerFactory(m1) |
| 72 |
self.assertEqual(mf.getMapper({})(0), "First", |
| 73 |
"Registring mappers doesn't work") |
| 74 |
mf.registerFactory(m2) |
| 75 |
self.assertEqual(mf.getMapper({})(0), "Second", |
| 76 |
"Factories registred later are not tried first") |
| 77 |
|
| 78 |
|
| 79 |
class _MapsToMixin(testhelpers.VerboseTest): |
| 80 |
def assertMapsTo(self, colDef, inValue, expectedValue, |
| 81 |
expectedAttributes=[]): |
| 82 |
column = base.parseFromString(rscdef.Column, |
| 83 |
"<column %s</column>"%colDef) |
| 84 |
annCol = valuemappers.AnnotatedColumn(column) |
| 85 |
res = valuemappers.defaultMFRegistry.getMapper(annCol)(inValue) |
| 86 |
if isinstance(expectedValue, float): |
| 87 |
self.assertAlmostEqual(expectedValue, res, places=3) |
| 88 |
else: |
| 89 |
self.assertEqual(expectedValue, res) |
| 90 |
|
| 91 |
for key, value in expectedAttributes: |
| 92 |
self.assertEqual(annCol[key], value) |
| 93 |
|
| 94 |
|
| 95 |
class _EnumeratedMapperTest(_MapsToMixin, |
| 96 |
metaclass=testhelpers.SamplesBasedAutoTest): |
| 97 |
def _runTest(self, sample): |
| 98 |
self.assertMapsTo(*sample) |
| 99 |
|
| 100 |
samples = [] |
| 101 |
|
| 102 |
|
| 103 |
class StandardMapperTest(_EnumeratedMapperTest): |
| 104 |
samples = [ |
| 105 |
# 0 |
| 106 |
('name="d" type="date">', |
| 107 |
datetime.date(2003, 5, 4), "2003-05-04", |
| 108 |
[("xtype", "timestamp")]), |
| 109 |
('name="d" type="date" unit="yr">', |
| 110 |
datetime.date(2003, 5, 4), 2003.33607118, |
| 111 |
[("datatype", "double")]), |
| 112 |
('name="d" type="date" unit="d">', |
| 113 |
datetime.date(2003, 5, 4), 2452763.5), |
| 114 |
('name="d" type="timestamp" unit="d">', |
| 115 |
datetime.datetime(2003, 5, 4, 20, 23), 2452764.34931), |
| 116 |
('name="d" type="date" unit="d" ucd="VOX:Image_MJDateObs">', |
| 117 |
datetime.date(2003, 5, 4), 52763.0), |
| 118 |
# 5 |
| 119 |
('name="d" type="date" unit="yr">', |
| 120 |
None, None), |
| 121 |
('name="d" type="integer"><values nullLiteral="-1"/>', |
| 122 |
None, None), |
| 123 |
('name= "b" type= "spoint">', pgsphere.SPoint.fromDegrees(2, 12), |
| 124 |
(2., 12.)), |
| 125 |
('name="d" unit="d" type="timestamp">', |
| 126 |
datetime.datetime(2005, 6, 4, 23, 12, 21), |
| 127 |
2453526.4669097224), |
| 128 |
('name="d" unit="d" type="timestamp">', |
| 129 |
datetime.datetime(1952, 1, 3, 3, 59, 1), |
| 130 |
2434014.6659837961), |
| 131 |
# 10 |
| 132 |
('name="d" unit="d" type="timestamp">', |
| 133 |
datetime.datetime(2000, 12, 31, 12, 00, 00), |
| 134 |
2451910.0), |
| 135 |
('name="d" unit="d" type="timestamp">', |
| 136 |
datetime.datetime(2000, 12, 31, 11, 59, 59), |
| 137 |
2451909.999988426), |
| 138 |
('name="d" unit="d" xtype="mjd">', |
| 139 |
54320.2, |
| 140 |
54320.2), |
| 141 |
('name="d" type="timestamp" xtype="timestamp">', |
| 142 |
datetime.datetime(2000, 12, 31, 11, 59, 59), |
| 143 |
"2000-12-31T11:59:59"), |
| 144 |
('name="c" type="scircle" xtype="adql:REGION">', |
| 145 |
pgsphere.SCircle.fromDALI([10, 15, 3]), |
| 146 |
"Circle UNKNOWNFrame 10. 15. 3."), |
| 147 |
# 15 |
| 148 |
('name="b" type= "spoint" xtype="adql:POINT">', |
| 149 |
pgsphere.SPoint.fromDegrees(2, 12), |
| 150 |
"Position UNKNOWNFrame 2. 12."), |
| 151 |
] |
| 152 |
|
| 153 |
|
| 154 |
class _MappingTestBase(testhelpers.VerboseTest, |
| 155 |
metaclass=testhelpers.SamplesBasedAutoTest): |
| 156 |
mfRegistry = None |
| 157 |
|
| 158 |
def _getMapped(self, colDef, inValue): |
| 159 |
column = base.parseFromString(rscdef.Column, |
| 160 |
"<column %s</column>"%colDef) |
| 161 |
annCol = valuemappers.AnnotatedColumn(column) |
| 162 |
return annCol, self.mfRegistry.getMapper(annCol)(inValue) |
| 163 |
|
| 164 |
def _runTest(self, args): |
| 165 |
colDef, inValue, expected, propDict = args |
| 166 |
annCol, res = self._getMapped(colDef, inValue) |
| 167 |
|
| 168 |
if isinstance(res, types.GeneratorType): |
| 169 |
# that's stan being returned from the HTML mapper |
| 170 |
rendered = formal.flattenSync(res).decode("utf-8") |
| 171 |
self.assertEqual(rendered, expected) |
| 172 |
else: |
| 173 |
self.assertEqual(res, expected) |
| 174 |
|
| 175 |
for key, value in propDict.items(): |
| 176 |
self.assertEqual(annCol[key], value) |
| 177 |
|
| 178 |
|
| 179 |
class HTMLMapperTest(_MappingTestBase): |
| 180 |
mfRegistry = htmltable._htmlMFRegistry |
| 181 |
|
| 182 |
samples = [ |
| 183 |
('name="d" unit="d" type="double precision" displayHint="type=humanDate">', |
| 184 |
2451909.999988426, |
| 185 |
"2000-12-31T11:59:59Z", |
| 186 |
{"xtype": "timestamp", "arraysize": "*", "unit": ""}), |
| 187 |
('name="d" unit="d" ucd="vox:MJDTrash" displayHint="type=humanDate">', |
| 188 |
51909.499988, |
| 189 |
"2000-12-31T11:59:59Z", |
| 190 |
{"xtype": "timestamp", "arraysize": "*", "unit": ""}), |
| 191 |
('name="d" unit="d" xtype="mjd" displayHint="type=humanDate">', |
| 192 |
51909.499988, |
| 193 |
"2000-12-31T11:59:59Z", |
| 194 |
{"xtype": "timestamp", "arraysize": "*", "unit": ""}), |
| 195 |
('name="d" unit="yr" displayHint="type=humanDate">', |
| 196 |
1994.25, |
| 197 |
"1994-04-02T07:30:00Z", |
| 198 |
{"xtype": "timestamp", "arraysize": "*", "unit": ""}), |
| 199 |
('name="d" unit="s" displayHint="type=humanDate">', |
| 200 |
1e9, |
| 201 |
"2001-09-09T01:46:40Z", |
| 202 |
{"xtype": "timestamp", "arraysize": "*", "unit": ""}), |
| 203 |
# 05 |
| 204 |
('name="d" type="timestamp" displayHint="type=humanDate">', |
| 205 |
datetime.datetime(2001, 9, 9, 1, 46, 40), |
| 206 |
"2001-09-09 01:46:40", |
| 207 |
{"xtype": "timestamp", "arraysize": "*", "unit": ""}), |
| 208 |
('name="d" type="date" displayHint="type=humanDate">', |
| 209 |
datetime.date(2001, 9, 9), |
| 210 |
"2001-09-09 00:00:00", |
| 211 |
{"xtype": "timestamp", "arraysize": "*", "unit": ""}), |
| 212 |
('name="d" type="date" displayHint="type=humanDay">', |
| 213 |
datetime.date(2001, 9, 9), |
| 214 |
"2001-09-09", |
| 215 |
{"xtype": "timestamp", "arraysize": "*", "unit": ""}), |
| 216 |
('name="d" type="double precision" displayHint="type=dms,sf=3">', |
| 217 |
245.3002, |
| 218 |
"+245 18 00.720", |
| 219 |
{"unit": "d:m:s"}), |
| 220 |
('name="a" type="real[2]" unit="deg/pix"' |
| 221 |
' displayHint="displayUnit=arcsec/pix,sf=4">', |
| 222 |
[0.002, 0.004], |
| 223 |
"[7.2000, 14.4000]", |
| 224 |
{"unit": "arcsec/pix"}), |
| 225 |
# 10 |
| 226 |
('name="src" type="text" displayHint="type=bibcode">', |
| 227 |
"2014ApJ...786L...5A, 2015A&C....10...88D", |
| 228 |
'<a href="https://ui.adsabs.harvard.edu/abs/2014ApJ...786L...5A/abstract">2014ApJ...786L...5A</a> <a href="https://ui.adsabs.harvard.edu/abs/2015A%26C....10...88D/abstract">2015A&C....10...88D</a> ', |
| 229 |
{}), |
| 230 |
('name="src" type="text" ucd="meta.bib.bibcode">', |
| 231 |
"2014ApJ...786L...5A, 2015A&C....10...88D", |
| 232 |
'<a href="https://ui.adsabs.harvard.edu/abs/2014ApJ...786L...5A/abstract">2014ApJ...786L...5A</a> <a href="https://ui.adsabs.harvard.edu/abs/2015A%26C....10...88D/abstract">2015A&C....10...88D</a> ', |
| 233 |
{}), |
| 234 |
('name="nu" displayHint="spectralUnit=m,sf=1" unit="MHz">', |
| 235 |
70, |
| 236 |
"4.3", |
| 237 |
{"unit": "m"}), |
| 238 |
('name="a" type="double precision" unit="km/s"' |
| 239 |
' displayHint="displayUnit=m/s">', |
| 240 |
None, |
| 241 |
"N/A", |
| 242 |
{"unit": "m/s"}), |
| 243 |
] |
| 244 |
|
| 245 |
def testBadSpectralUnitMessage(self): |
| 246 |
self.assertRaisesWithMsg( |
| 247 |
base.IncompatibleUnits, |
| 248 |
"km/s is not a spectral unit understood here", |
| 249 |
self._getMapped, |
| 250 |
('name="nu" displayHint="spectralUnit=J" unit="km/s">', 0)) |
| 251 |
|
| 252 |
def testDMMJD(self): |
| 253 |
td = base.parseFromString(rscdef.TableDef, |
| 254 |
"""<table id="t"><dm> |
| 255 |
(votable:Coords) { time: { |
| 256 |
frame: {time0: "MJD-origin"} |
| 257 |
location: @mytime}}</dm> |
| 258 |
<column name="mytime" unit="d" displayHint="type=humanDate"/> |
| 259 |
</table>""") |
| 260 |
annCol = valuemappers.AnnotatedColumn(td.getColumnByName("mytime")) |
| 261 |
self.assertEqual( |
| 262 |
self.mfRegistry.getMapper(annCol)(54300), |
| 263 |
"2007-07-19T00:00:00Z") |
| 264 |
|
| 265 |
def testWithAnchor(self): |
| 266 |
rendered = formal.flattenSync( |
| 267 |
self._getMapped( |
| 268 |
'name="l" type="text" displayHint="type=product">' |
| 269 |
'<property key="anchorText">Image</property>', "a/b/c")[1]) |
| 270 |
|
| 271 |
self.assertEqual(rendered, b'<a href="http://localhost:8080/getproduct' |
| 272 |
b'/a/b/c" onmouseover="insertPreview(this, null)"' |
| 273 |
b' class="productlink">Image</a>') |
| 274 |
|
| 275 |
|
| 276 |
class VOTableMapperTest(_MappingTestBase): |
| 277 |
mfRegistry = valuemappers.defaultMFRegistry |
| 278 |
|
| 279 |
samples = [ |
| 280 |
('name="pos" unit="deg" type="spoint">', |
| 281 |
pgsphere.SPoint.fromDegrees(90, 45), |
| 282 |
(90.0, 45.0), |
| 283 |
{"xtype": "point", "datatype": "double", "arraysize": '2'}), |
| 284 |
('name="dint" type="timestamp[2]">', |
| 285 |
[datetime.datetime(2008, 0o4, 0o1), datetime.datetime(2008, 0o5, 0o1)], |
| 286 |
[datetime.datetime(2008, 0o4, 0o1), datetime.datetime(2008, 0o5, 0o1)], |
| 287 |
{"xtype": "timestamp-interval", "datatype": "char", "arraysize": '19x2'}), |
| 288 |
('name="dint" type="timestamp[3]">', |
| 289 |
[datetime.datetime(2008, 0o4, 0o1), |
| 290 |
datetime.datetime(2008, 0o5, 0o1), |
| 291 |
datetime.datetime(2008, 0o6, 0o7),], |
| 292 |
[datetime.datetime(2008, 0o4, 0o1), |
| 293 |
datetime.datetime(2008, 0o5, 0o1), |
| 294 |
datetime.datetime(2008, 0o6, 0o7),], |
| 295 |
{"xtype": "timestamp", "datatype": "char", "arraysize": '19x3'}), |
| 296 |
('name="poly" type="spoly">', |
| 297 |
pgsphere.SPoly.fromDALI([1, 2, 2, 3, 3, 2, 1, 2]), |
| 298 |
[1.0, 2.0, 2.0, 3.0, 3.0, 2.0, 1.0, 2.0], |
| 299 |
{"xtype": "polygon", "datatype": "double", "arraysize": "*"}), |
| 300 |
] |
| 301 |
|
| 302 |
|
| 303 |
class ProductMapperTest(_MapsToMixin, |
| 304 |
metaclass=testhelpers.SamplesBasedAutoTest): |
| 305 |
def _runTest(self, sample): |
| 306 |
colDef, prodName, encoded = sample |
| 307 |
colDef = colDef+' type="text">' |
| 308 |
prodLink = "http://localhost:8080/getproduct/"+encoded |
| 309 |
self.assertMapsTo(colDef, prodName, prodLink) |
| 310 |
|
| 311 |
samples = [ |
| 312 |
('name="accref" displayHint="type=product"', "gobba", "gobba"), |
| 313 |
('name="uuxj" utype="ssa:Access.Reference"', |
| 314 |
"gobba", "gobba"), |
| 315 |
('name="uuxj" displayHint="type=product"', |
| 316 |
"gobba", "gobba"), |
| 317 |
('name="uuxj" ucd="VOX:Image_AccessReference"', |
| 318 |
"gobba", "gobba"), |
| 319 |
('name="accref" displayHint="type=product"', |
| 320 |
"wierdo+name/goes somewhere&is/bad", |
| 321 |
"wierdo%2Bname/goes%20somewhere%26is/bad"), |
| 322 |
] |
| 323 |
|
| 324 |
|
| 325 |
if __name__=="__main__": |
| 326 |
testhelpers.main(HTMLMapperTest) |