Debian Codegraph @

tests/scstest.py

raw
1 """
2 Tests in the wider vicinity of cone search.
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 from gavo.helpers import testhelpers
12
13 from gavo import api
14 from gavo.helpers import trialhelpers
15 from gavo.protocols import scs
16 from gavo.web import vodal
17
18 import tresc
19
20
21 class QueryBuildingTest(testhelpers.VerboseTest,
22 metaclass=testhelpers.SamplesBasedAutoTest):
23 def _runTest(self, sample):
24 tdLit, expected = sample
25 td = api.parseFromString(api.TableDef, tdLit)
26 self.assertEqual(
27 scs.getRadialCondition(td, 10, 13, 1),
28 expected)
29
30 samples = [
31 ("""<table id="t" onDisk="True">
32 <mixin>//scs#q3cindex</mixin>
33 <column name="raj2000" ucd="pos.eq.ra;meta.main"/>
34 <column name="dej2000" ucd="pos.eq.dec;meta.main"/>
35 </table>""",
36 "q3c_radial_query(raj2000, dej2000, 10, 13, 1)"),
37 ("""<table id="t" onDisk="True">
38 <column name="ra" ucd="pos.eq.ra;meta.main"/>
39 <column name="dec" ucd="pos.eq.dec;meta.main"/>
40 </table>""",
41 "spoint(radians(ra), radians(dec))"
42 " <@ scircle(spoint(radians(10), radians(13)), radians(1))"),
43 ("""<table id="t" onDisk="True" mixin="//scs#pgs-pos-index">
44 <column name="quoted/Right Ascension" ucd="pos.eq.ra;meta.main"/>
45 <column name="dec" ucd="pos.eq.dec;meta.main"/>
46 </table>""",
47 'spoint(radians("Right Ascension"), radians(dec))'
48 " <@ scircle(spoint(radians(10), radians(13)), radians(1))"),
49 ("""<table id="t" onDisk="True">
50 <column name="ssa_location" type="spoint" ucd="pos.eq"/>
51 </table>""",
52 "ssa_location <@ scircle(spoint(radians(10), radians(13)), radians(1))"),
53
54 ]
55
56
57 class AnomalousQueryBuildingTest(testhelpers.VerboseTest):
58 tdPat = """<table id="t" onDisk="True">
59 <column name="raj2000" ucd="pos.eq.ra;meta.main"/>
60 <column name="dej2000" ucd="pos.eq.dec;meta.main"/>
61 <column name="knatter"/>
62 </table>"""
63
64 def testInjection(self):
65 td = api.parseFromString(api.TableDef, self.tdPat)
66 self.assertRaisesWithMsg(api.ReportableError,
67 "getRadialCondition's last resort alarm triggered.",
68 scs.getRadialCondition,
69 (td, "); drop table users", None, None))
70
71 def testOverriding(self):
72 td = api.parseFromString(api.TableDef, self.tdPat)
73 self.assertEqual(
74 scs.getRadialCondition(td,
75 "%(RA)s", "%(DEC)s", "%(SR)s",
76 decColName="knatter"),
77 "spoint(radians(raj2000), radians(knatter)) <@"
78 " scircle(spoint(radians(%(RA)s), radians(%(DEC)s)), radians(%(SR)s))")
79
80 def testMissingUCD(self):
81 td = api.parseFromString(api.TableDef,
82 self.tdPat.replace("pos.eq.dec;", ""))
83 self.assertRaisesWithMsg(api.StructureError,
84 "No column for pos.eq.dec;meta.main",
85 scs.getRadialCondition,
86 (td, "%(RA)s", "%(DEC)s", "%(SR)s"))
87
88
89 class _ConecatTable(tresc.RDDataResource):
90 rdName = "data/cores"
91 dataId = "import_conecat"
92
93 CONECAT_TABLE = _ConecatTable()
94
95 class ConeSearchTest(testhelpers.VerboseTest):
96 resources = [("cstable", CONECAT_TABLE),
97 ("fs", tresc.fakedSimbad)]
98
99 def testRadiusAddedSCS(self):
100 svc = api.resolveCrossId("data/cores#scs")
101 res = trialhelpers.runSvcWith(svc, "scs.xml",
102 {"RA": ["1"], "DEC": ["2"], "SR": ["3"]}
103 ).getPrimaryTable()
104 self.assertAlmostEqual(res.rows[0]["_r"], 0.5589304685425)
105 col = res.tableDef.getColumnByName("_r")
106 self.assertEqual(col.unit, "deg")
107 self.assertEqual(col.ucd, "pos.angDistance")
108
109 def testRadiusAddedForm(self):
110 svc = api.resolveCrossId("data/cores#scs")
111 res = trialhelpers.runSvcWith(svc, "form",
112 {"hscs_pos": "1, 2", "hscs_sr": str(3*60)}
113 ).getPrimaryTable()
114 self.assertAlmostEqual(res.rows[0]["_r"], 0.5589304685425)
115
116 def testRadiusAddedFormObjectres(self):
117 svc = api.resolveCrossId("data/cores#scs")
118 res = trialhelpers.runSvcWith(svc, "form",
119 {"hscs_pos": "Aldebaran", "hscs_sr": str(180*60)}
120 ).getPrimaryTable()
121 self.assertAlmostEqual(res.rows[0]["_r"], 67.9075866114036)
122
123 def testNoRadiusWithoutPos(self):
124 svc = api.resolveCrossId("data/cores#scs")
125 res = trialhelpers.runSvcWith(svc, "form", {"id": "0"}
126 ).getPrimaryTable()
127 self.assertTrue(res.rows[0]["_r"] is None)
128
129
130 class _RenderedSCSResponse(testhelpers.TestResource):
131 resources = [("conetable", CONECAT_TABLE)]
132 def make(self, deps):
133 request = trialhelpers.FakeRequest("anything?goes=here",
134 args={"RA": ["1"], "DEC": ["2"], "SR": ["3"]})
135 pg = vodal.SCSRenderer(request, api.resolveCrossId("data/cores#scs"))
136 return testhelpers.getXMLTree(pg.renderSync(request), debug=False)
137
138
139 class DataOriginTest(testhelpers.VerboseTest):
140 resources = [("tree", _RenderedSCSResponse())]
141
142 def _getINFO(self, key):
143 matches = self.tree.xpath(f"//INFO[@name='{key}']")
144 assert len(matches)==1
145 return matches[0].get("value")
146
147 def _getINFOs(self, key):
148 return set(m.get("value")
149 for m in self.tree.xpath(f"//INFO[@name='{key}']"))
150
151 # Keep the sequence of tests in sync with the sequence of items in
152 # the data origin paper so it's easy to see whether we're covering
153 # them all.
154
155 def testIVOID(self):
156 self.assertEqual(self._getINFOs("ivoid"), {
157 'ivo://x-testing/data/cores/singlearg',
158 'ivo://x-testing/data/cores/scs',
159 'ivo://x-testing/data/cores/pc',
160 'ivo://x-testing/data/cores/cstest',
161 'ivo://x-testing/data/cores/dl'})
162
163 def testPublisher(self):
164 self.assertEqual(
165 self._getINFO("publisher"),
166 "Your organisation's name")
167
168 def testServerSoftware(self):
169 self.assertEqual(self._getINFO("server_software"),
170 api.SERVER_SOFTWARE)
171
172 def testRequest(self):
173 self.assertEqual(self._getINFO("request"),
174 "http://localhost:8080/anything%3Fgoes%3Dhere?RA=1&DEC=2&SR=3")
175
176 def testRequestDate(self):
177 self.assertTrue(self._getINFO("request_date").startswith("20"))
178
179 def testContact(self):
180 self.assertEqual(
181 self._getINFO("contact"),
182 "[email protected]")
183
184 def testReferenceURL(self):
185 self.assertEqual(
186 self._getINFOs("reference_url"),
187 set(["http://localhost:8080/tableinfo/test.conecat",
188 'http://localhost:8080/data/cores/scs/info']))
189
190 def testSource(self):
191 # not given in source, make sure it doesn't fantasise something
192 self.assertEqual(
193 len(self.tree.xpath("//INFO[@name='publication_id']")),
194 0)
195
196 def testResourceVersion(self):
197 self.assertEqual(
198 self._getINFO("resource_version"),
199 "2.3.1pl2+git_deadbeef")
200
201 maxDiff = None
202 def testRights(self):
203 self.assertEqual(
204 self._getINFO("rights").strip(),
205 'To the extent possible under law, the publisher has waived all copyright and related or neighboring rights to nothing in particular. For details, see the `Creative Commons CC0 1.0 Public Domain dedication <http://creativecommons.org/publicdomain/zero/1.0/>`_. Of course, you should still give proper credit when using this data as required by good scientific practice. .. image:: /static/img/cc0.png :alt: [CC0]')
206
207 def testRightsURI(self):
208 self.assertEqual(
209 self._getINFO("rights_uri"),
210 "https://spdx.org/licenses/CC0-1.0.html")
211
212 def testCreator(self):
213 self.assertEqual(
214 self._getINFOs("creator"),
215 set([
216 'Gandalf, W.',
217 'Baggins, B.',
218 'Baggins, F.',]))
219
220
221 if __name__=="__main__":
222 testhelpers.main(QueryBuildingTest)