Debian Codegraph @

tests/corestest.py

raw
1 """
2 Tests related to building and running cores.
3
4 This should probably take quite a bit more from servicetest.py; tests
5 more concerned with interfacing the cores should go there, anyway
6 """
7
8 #c Copyright 2008-2025, the GAVO project <[email protected]>
9 #c
10 #c This program is free software, covered by the GNU GPL. See the
11 #c COPYING file in the source distribution.
12
13
14 from gavo.helpers import testhelpers
15
16 from gavo import base
17 from gavo import rscdesc
18 from gavo import svcs
19 from gavo import utils
20 from gavo.helpers import trialhelpers
21 from gavo.protocols import scs
22
23 import tresc
24
25
26 class DbCoreTest(testhelpers.VerboseTest):
27 def testColumnQuerying(self):
28 c = base.parseFromString(svcs.DBCore,
29 """<dbCore queriedTable="//tap#columns"/>""")
30 it = svcs.CoreArgs(c.inputTable, {"size": "20", "table_name": "foo"}, None)
31 self.assertEqual(
32 c._getSQLWhere(it, svcs.emptyQueryMeta), (
33 '(table_name=%(table_name0)s) AND ("size"=%(size0)s)',
34 {'table_name0': 'foo', 'size0': '20'}))
35
36
37 class PythonCoreTest(testhelpers.VerboseTest):
38 def testBasic(self):
39 svc = base.resolveCrossId("data/cores#pc")
40 res = trialhelpers.runSvcWith(svc, "form",
41 dict(opre="1", opim="1", powers=["2 3 4"])
42 ).getPrimaryTable()
43 self.assertEqual(res.rows[0]["re"], 0)
44 self.assertEqual(res.rows[1]["im"], 2.0)
45 self.assertAlmostEqual(res.rows[2]["log_value"], 1.3862943611198906)
46 self.assertEqual(len(res.rows), 3)
47
48 def testDefaulting(self):
49 svc = base.resolveCrossId("data/cores#pc")
50 res = trialhelpers.runSvcWith(svc, "form", dict(opre="1", opim="1")
51 ).getPrimaryTable()
52 self.assertEqual(len(res), 3)
53
54 def testMissing(self):
55 svc = base.resolveCrossId("data/cores#pc")
56 self.assertRaisesWithMsg(base.ValidationError,
57 "Field opre: Required parameter 'opre' missing.",
58 trialhelpers.runSvcWith,
59 (svc, "form", dict(opim="1", powers="2 3 4")))
60
61
62 class CustomCoreTest(testhelpers.VerboseTest):
63 def testRunning(self):
64 svc = base.resolveCrossId("data/cores#cc")
65 res = trialhelpers.runSvcWith(svc, "qp", dict(x="10"))
66 self.assertEqual(res, ('text/plain', 'xxxxxxxxxx'))
67
68 def testInputTable(self):
69 core = base.resolveCrossId("data/cores#cc").core
70 self.assertEqual(
71 core.inputTable.inputKeys.getColumnByName("x").type,
72 "integer")
73
74 def testInputTableWithRDDef(self):
75 core = base.parseFromString(rscdesc.RD,
76 """<resource schema="data">
77 <service id="u"><customCore module="testcore" id="hu">
78 <inputTable>
79 <inputKey name="y" type="text"/></inputTable></customCore>
80 </service></resource>""").getById("hu")
81 self.assertEqual(
82 core.inputTable.inputKeys.getColumnByName("y").type,
83 "text")
84
85 def testMissingModuleAttribute(self):
86 self.assertRaisesWithMsg(
87 base.StructureError,
88 'At IO:\'<resource schema="data"> <service id="u"><customCore/...\','
89 " (2, 33): Attribute module is mandatory",
90 base.parseFromString,
91 (rscdesc.RD,
92 """<resource schema="data">
93 <service id="u"><customCore/></service></resource>"""))
94
95 def testMissingModuleFile(self):
96 self.assertRaisesWithMsg(
97 base.StructureError,
98 utils.EqualingRE(r'At IO:\'<resource schema="data"> <service id="u"><customCore ...\', \(2, 48\): Cannot load custom core .*/_gavo_test/inputs/data/knenk: \[Errno 2\] No such file or directory: \'.*/_gavo_test/inputs/data/knenk.py\''),
99 base.parseFromString,
100 (rscdesc.RD,
101 """<resource schema="data">
102 <service id="u"><customCore module="knenk"/></service></resource>"""))
103
104 def testBadModuleFile(self):
105 with testhelpers.testFile(
106 "borken.py",
107 "return\n",
108 inDir=base.getConfig("inputsDir") / "data"):
109 self.assertRaisesWithMsg(
110 base.StructureError,
111 utils.EqualingRE(r'At IO:\'<resource schema="data"> <service id="u"><customCore ...\', \(2, 50\): Cannot load custom core .*/_gavo_test/inputs/data/borken: \'return\' outside function \(borken.py, line 1\)'),
112 base.parseFromString,
113 (rscdesc.RD,
114 """<resource schema="data">
115 <service id="u"><customCore module="borken"/>
116 </service></resource>"""))
117
118
119 class HumanCoordParseTest(testhelpers.VerboseTest,
120 metaclass=testhelpers.SamplesBasedAutoTest):
121 resources = [("fs", tresc.fakedSimbad)]
122
123 def _runTest(self, sample):
124 literal, parsed = sample
125 self.assertEqual(scs.parseHumanSpoint(literal), parsed)
126
127 samples = [
128 ("23.5, -21.75", (23.5, -21.75)),
129 ("23.5 -21.75", (23.5, -21.75)),
130 ("23 30, 11 15 30.6", (352.5, 11.2585)),
131 ("23:30:45, 11:15:30.6", (352.6875, 11.2585)),
132 ("Aldebaran", (68.9375, 16.46875)),]
133
134 def testException(self):
135 self.assertRaisesWithMsg(base.ValidationError,
136 "Unidentified Field: $&& zefixx is neither a RA,DEC pair nor"
137 " a simbad resolvable object.",
138 scs.parseHumanSpoint,
139 ("$&& zefixx",))
140
141
142 class FancyQueryCoreTest(testhelpers.VerboseTest):
143 def testBasic(self):
144 svc = base.parseFromString(svcs.Service,
145 r"""<service id="c" allowed="api">
146 <meta name="id">testing 1 2 3</meta>
147 <fancyQueryCore queriedTable="//tap#tables"
148 query="select table_name, '\metaString{id}' as id
149 from tap_schema.tables %s">
150 <outputTable autoCols="table_name">
151 <outputField name="id" type="text"/>
152 </outputTable>
153 <condDesc buildFrom="table_name"/>
154 </fancyQueryCore>
155 </service>""")
156 rows = trialhelpers.runSvcWith(
157 svc, "api", {"table_name": "tap_schema.tables"}).getPrimaryTable().rows
158 self.assertEqual(
159 rows,
160 [{'table_name': 'tap_schema.tables', 'id': 'testing 1 2 3'}])
161
162
163 if __name__=="__main__":
164 testhelpers.main(DbCoreTest)