1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
"""
Tests for direct grammars (a.k.a. C boosters).
"""
#c Copyright 2008-2024, the GAVO project <gavo@ari.uni-heidelberg.de>
#c
#c This program is free software, covered by the GNU GPL. See the
#c COPYING file in the source distribution.
import os
import re
import unittest
from gavo.helpers import testhelpers
from gavo import base
from gavo import rsc
from gavo import rscdesc
from gavo import utils
from gavo.grammars import directgrammar
import tresc
class DirectGrammarTest(testhelpers.VerboseTest):
# this is for direct grammars that can't be automatically made:
# Just make sure they're producing something resembling C source.
rd = testhelpers.getTestRD("dgs")
def _assertCommonItems(self, src):
self.assertTrue(src.startswith("#include"))
self.assertTrue("fi_i, /* I, integer */" in src)
self.assertTrue("writeHeader(destination);" in src)
def testColGrammar(self):
src = directgrammar.getSource("data/dgs#col")
self._assertCommonItems(src)
self.assertTrue("parseFloat(inputLine, F(fi_f), start, len);" in src)
def testSplitGrammar(self):
src = directgrammar.getSource("data/dgs#split")
self._assertCommonItems(src)
self.assertTrue('char *curCont;' in src)
self.assertTrue('curCont = strtok(inputLine, "|");' in src)
self.assertTrue('curCont = strtok(NULL, "|");' in src)
def testBinGrammar(self):
src = directgrammar.getSource("data/dgs#bin")
self._assertCommonItems(src)
self.assertTrue('#define FIXED_RECORD_SIZE 50' in src)
self.assertTrue('MAKE_INT(fi_i, *(int32_t*)(inputLine+));' in src)
self.assertTrue('bytesRead = fread(inputLine, 1, FIXED_RECORD_SIZE, inF);'
in src)
def testSourcePlausible(self):
src = directgrammar.getSource("data/dgs#fits")
self._assertCommonItems(src)
self.assertTrue("if (COL_DESCS[i].fitsType==TSTRING) {" in src)
self.assertTrue("MAKE_BIGINT(fi_b, ((long long*)(data[1]))[rowIndex]);"
in src)
def testFITSSecondExtension(self):
src = directgrammar.getSource("data/dgs#fits2nd")
self.assertTrue("fits_movabs_hdu(fitsInput, 2+1," in src)
self.assertTrue("FITSColDesc COL_DESCS[1] = {\n"
"{.cSize = sizeof(long long), .fitsType = TLONGLONG, .index=1, .arraysize=1}\n};"
in src)
def testFITSWithAdditionalCols(self):
src = directgrammar.getSource("data/dgs#fitsplus")
self._assertCommonItems(src)
self.assertTrue("FITSColDesc COL_DESCS[5] = {" in src)
self.assertTrue("#define QUERY_N_PARS 6")
self.assertTrue("MAKE_NULL(fi_artificial);"
" /* MAKE_TEXT(fi_artificial, FILL IN VALUE); */" in src)
# XXX TODO: tests for column reordering, skipping unused columns in FITS
directgrammar.CBooster.silence_for_test = True
class _FITSBoosterImportedTable(testhelpers.TestResource):
resources = [("conn", tresc.dbConnection)]
def make(self, deps):
conn = deps["conn"]
dd = base.caches.getRD("data/dgs").getById("impfits")
self.srcName = dd.grammar.cBooster
with open(self.srcName, "w", encoding="utf-8") as f:
f.write(directgrammar.getSource("data/dgs#fits"))
data = rsc.makeData(dd, connection=conn)
table = data.getPrimaryTable()
rows = list(table.iterQuery(table.tableDef))
return rows, data.getPrimaryTable()
def clean(self, res):
os.unlink(self.srcName)
res[1].drop()
class FITSDirectGrammarTest(testhelpers.VerboseTest):
resources = [("imped", _FITSBoosterImportedTable())]
def testInteger(self):
self.assertEqual(self.imped[0][0]["i"], 450000)
def testBigint(self):
self.assertEqual(self.imped[0][0]["b"], 4009249430)
def testFloat(self):
self.assertAlmostEqual(self.imped[0][0]["f"], 3.2)
def testDouble(self):
self.assertEqual(self.imped[0][0]["d"], 5e120)
def testTextAndMap(self):
self.assertEqual(self.imped[0][0]["t"], "foobar")
def testUnknownNULL(self):
self.assertEqual(self.imped[0][0]["artificial"], None)
try:
from gavo.grammars import hdf5grammar #noflake: just testing if it's there
def skipIfNoH5py(obj):
return obj
except ImportError:
def skipIfNoH5py(obj):
return unittest.skip("No h5py")(obj)
_VAEX_RD_TEMPLATE = """
<resource schema="data">
<table id="testing" onDisk="True"><column name="a"/></table>
<data id="import">
{}
<make table="testing"/>
</data>
</resource>"""
class _HDF5vaexBoosterSource(testhelpers.TestResource):
def make(self, deps):
rd = base.parseFromString(rscdesc.RD, _VAEX_RD_TEMPLATE.format(
'<sources pattern="fromvaex.hdf5"/>'
'<directGrammar id="boost" cBooster="res/testing.c" type="hdf5vaex"/>'))
try:
return directgrammar.buildSource(
rd.getById("boost"), rd.getById("testing"))
except base.ReportableError:
# probably no h5py
return None
@skipIfNoH5py
class HDF5vaexBoosterCodegenTest(testhelpers.VerboseTest):
resources = [("src", _HDF5vaexBoosterSource())]
def testBasic(self):
typedef = re.search("(?s)typedef(.*?)InRec;", self.src.original)
self.assertEqual(typedef.group(0),
"typedef struct InRec_s {\n"
" float parallax;\n"
" uint64_t source_id;\n"
"} InRec;")
def testWithoutSource(self):
rd = base.parseFromString(rscdesc.RD, _VAEX_RD_TEMPLATE.format(
'<directGrammar id="boost" cBooster="res/testing.c" type="hdf5vaex"/>'))
self.assertRaisesWithMsg(base.StructureError,
"Cannot make HDF5 vaex booster without a sources element on the embedding data.",
directgrammar.buildSource,
(rd.getById("boost"), rd.getById("testing")))
def testWithoutMatchingSource(self):
rd = base.parseFromString(rscdesc.RD, _VAEX_RD_TEMPLATE.format(
'<sources pattern="data/does-not-exist"/>'
'<directGrammar id="boost" cBooster="res/testing.c" type="hdf5vaex"/>'))
self.assertRaisesWithMsg(base.StructureError,
"Building an HDF5 booster requires at least one matching source.",
directgrammar.buildSource,
(rd.getById("boost"), rd.getById("testing")))
def testWithWrongDataset(self):
rd = base.parseFromString(rscdesc.RD, _VAEX_RD_TEMPLATE.format(
'<sources pattern="fromvaex.hdf5"/>'
'<directGrammar id="boost" cBooster="res/testing.c" type="hdf5vaex">'
' <property name="dataset">road/to/nowhere</property>'
'</directGrammar>'))
self.assertRaisesWithMsg(base.StructureError,
utils.EqualingRE("Cannot access dataset road/to/nowhere in .*data/fromvaex.hdf5. Override the grammar's dataset property to point it to the right dataset."),
directgrammar.buildSource,
(rd.getById("boost"), rd.getById("testing")))
_VAEX_RD = """
<resource schema="data">
<table id="exvaex" onDisk="True" temporary="True">
<column name="parallax"/>
<column name="source_id" type="bigint"/>
</table>
<data id="import">
<sources pattern="fromvaex.hdf5"/>
<directGrammar id="booster" type="hdf5vaex" cBooster="tmp.c">
<property name="chunkSize">1</property>
</directGrammar>
<make table="exvaex"/>
</data>
</resource>"""
class _VAEXBoosterImportedTable(testhelpers.TestResource):
resources = [("conn", tresc.dbConnection)]
def make(self, deps):
conn = deps["conn"]
rd = base.parseFromString(rscdesc.RD, _VAEX_RD)
dd = rd.getById("import")
self.srcName = dd.grammar.cBooster
try:
with open(self.srcName, "w", encoding="utf-8") as f:
f.write(directgrammar.buildSource(dd.grammar, rd.getById("exvaex")))
except base.ReportableError:
# probably no h5py
return None
data = rsc.makeData(dd, connection=conn)
table = data.getPrimaryTable()
rows = list(table.iterQuery(table.tableDef))
return rows, data.getPrimaryTable()
def clean(self, res):
os.unlink(self.srcName)
if res.original is not None:
res[1].drop()
@skipIfNoH5py
class HDF5vaexBoosterImportTest(testhelpers.VerboseTest):
resources = [("imped", _VAEXBoosterImportedTable())]
def testAllIn(self):
self.assertEqual(len(self.imped[0]), 2)
def testBigint(self):
self.assertEqual(self.imped[0][0]["source_id"], 4464711039265053952)
def testFloat(self):
self.assertAlmostEqual(self.imped[0][1]["parallax"], 1.84249)
if __name__=="__main__":
testhelpers.main(DirectGrammarTest)
|