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