Debian Codegraph @

tests/datapacktest.py

raw
1 """
2 Tests for our datapack outputs.
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 import os
14 import shutil
15
16 from gavo import api
17 from gavo.protocols import datapack
18
19 class _DataPackageDescriptor(testhelpers.TestResource):
20 def make(self, ignored):
21 rd = datapack.getRDForDump("data/cores")
22 res = datapack.makeDescriptor(rd)
23 return res
24
25
26 class DataPackageDescriptorTest(testhelpers.VerboseTest):
27 resources = [
28 ("desc", _DataPackageDescriptor())]
29
30 def testTitleInferred(self):
31 self.assertEqual(self.desc["name"], "test")
32
33 def testDaCHSIndicator(self):
34 self.assertEqual(self.desc["dachs-resdir"], "")
35
36 def testDOI(self):
37 self.assertEqual(self.desc["id"], "10.5072/test-fixture")
38
39 def testLicense(self):
40 self.assertEqual(len(self.desc["licenses"]), 1)
41 self.assertEqual(self.desc["licenses"][0]["path"],
42 "https://spdx.org/licenses/CC0-1.0.html")
43
44 def testTitle(self):
45 self.assertEqual(self.desc["title"], "Test Fixtures")
46
47 def testDescription(self):
48 self.assertEqual(self.desc["description"], "Helpers for tests for cores.")
49
50 def testHomepage(self):
51 self.assertEqual(self.desc["homepage"],
52 "http://localhost:8080/browse/data/cores")
53
54 def testVersion(self):
55 self.assertEqual(self.desc["version"], "2.3.1pl2+git_deadbeef")
56
57 def testContributors(self):
58 self.assertEqual(self.desc["contributors"], [
59 {'role': 'author', 'title': 'Gandalf, W.'},
60 {'role': 'author', 'title': 'Baggins, B.'},
61 {'role': 'author', 'title': 'Baggins, F.'}])
62
63 def testCreated(self):
64 self.assertEqual(self.desc["created"], "2010-12-01T14:00:00Z")
65
66 def testMinimal(self):
67 rd = api.parseFromString(api.RD, '<resource schema="nirvana"/>')
68 rd.filesLoaded = []
69 rd.sourceId = "nirvana/bar"
70 rd.srcPath = rd.resdir
71 res = datapack.makeDescriptor(rd)
72 res.pop("resources")
73 self.assertEqual(res, {
74 "name": "nirvana", "dachs-resdir": "nirvana",
75 "profile": "data-package",
76 'homepage': 'http://localhost:8080/browse/nirvana/bar'})
77
78
79 class _DataPackageTestMixin:
80 def _expectFile(self, path, assertName=None):
81 for r in self.dpRscs:
82 if r["path"]==path:
83 if assertName is not None:
84 self.assertEqual(r["name"], assertName)
85 break
86 else:
87 raise AssertionError("%s not found in returned resources"%path)
88
89 def _expectNoFile(self, name):
90 for r in self.dpRscs:
91 if r["path"]==name:
92 raise AssertionError("%s is found in returned resources"
93 " although it should not be there."%name)
94
95
96 class _DataPackageResources(testhelpers.TestResource):
97 def make(self, ignored):
98 with testhelpers.messageCollector() as msg:
99 res = list(datapack.iterRDResources(
100 datapack.getRDForDump("data/test")))
101 res.append({
102 "name": "fake-warning-messages",
103 "path": "None",
104 "messages": msg})
105 return res
106
107
108 class DataPackageResourcesTest(
109 testhelpers.VerboseTest,
110 _DataPackageTestMixin):
111 resources = [("dpRscs", _DataPackageResources())]
112
113 def testRDPresent(self):
114 self.assertEqual(self.dpRscs[0]["path"], "data/test.rd")
115
116 def testDirectSource(self):
117 self._expectFile('data/adqlin.txt')
118
119 def testFITS(self):
120 self._expectFile('data/ex.fits')
121
122 def testSpectrum(self):
123 self._expectFile('data/a.imp')
124
125 def testUniqueNames(self):
126 self.assertEqual(
127 len(set(r["name"] for r in self.dpRscs)),
128 len(self.dpRscs))
129
130 def testMessage(self):
131 type, args, _ = self.dpRscs[-1]["messages"].events[0]
132 self.assertEqual(type, "Warning")
133 self.assertEqual(args[0],
134 "data data/test#productimport ignored because of dynamic ignoreSources")
135
136
137 class _ResourceCollectingResdir(testhelpers.TestResource):
138 def make(self, ignored):
139 self.respath = os.path.join(api.getConfig("inputsDir"), "dpexp")
140 os.makedirs(self.respath, exist_ok=True)
141
142 def w(n, content):
143 with open(os.path.join(self.respath, n), "w", encoding="utf-8") as f:
144 f.write(content)
145
146 w("q.rd", """<resource schema="dpexp">
147 <property name="datapack-extrafiles"
148 >["subdir/*.knack", "worn", "gibtsnicht"]</property>
149 <table id="main" onDisk="True"/>
150 <data>
151 <sources pattern="data/*.txt"/>
152 <directGrammar cBooster="res/booster.c"/>
153 <make table="main"/>
154 </data>
155 <service customPage="cp">
156 <template key="fixed">template.html</template>
157 <customCore module="cm"/>
158 </service>
159 </resource>
160 """)
161
162 w("cm.py", "from gavo import api\nclass Core(api.Core): pass\n")
163 w("template.html", "<html/>")
164 w("cp.py", "MainPage = None")
165
166 os.makedirs(self.respath+"/res", exist_ok=True)
167 for n in ["worn", "res/booster.c", "README"]:
168 w(n, "")
169
170 os.makedirs(self.respath+"/subdir", exist_ok=True)
171 for n in ["subdir/nix", "subdir/a.knack", "subdir/b.knack"]:
172 w(n, "")
173
174 return "dpexp/q"
175
176 def clean(self, ignored):
177 shutil.rmtree(self.respath)
178
179 _resourceCollectingResdir = _ResourceCollectingResdir()
180
181
182 class _CollectedResources(testhelpers.TestResource):
183 resources = [("rdid", _resourceCollectingResdir)]
184
185 def make(self, deps):
186 rd = datapack.getRDForDump(deps["rdid"])
187 return list(datapack.iterRDResources(rd))
188
189
190 class DataPackageExtraResourcesTest(
191 testhelpers.VerboseTest,
192 _DataPackageTestMixin):
193 resources = [("dpRscs", _CollectedResources())]
194
195 def testRD(self):
196 self.assertEqual(self.dpRscs[0]["path"], "q.rd")
197 self.assertEqual(self.dpRscs[0]["name"], "dachs-resource-descriptor")
198 self.assertEqual(self.dpRscs[0]["mediatype"], "text/xml")
199
200 def testREADME(self):
201 self._expectFile("README", "readme")
202
203 def testManualWildcard(self):
204 self._expectFile("subdir/a.knack")
205 self._expectFile("subdir/b.knack")
206
207 def testManualLiteral(self):
208 self._expectFile("worn")
209
210 def testManualNoOverinclusion(self):
211 self._expectNoFile("subdir/nix")
212
213 def testManualNoMissingFiles(self):
214 self._expectNoFile("gibtsnicht")
215
216 def testBoosterIncluded(self):
217 self._expectFile("res/booster.c")
218
219 def testCustomPageIncluded(self):
220 self._expectFile("cp.py")
221
222 def testTemplateIncluded(self):
223 self._expectFile("template.html")
224
225 def testCustomCoreIncluded(self):
226 self._expectFile("cm.py")
227
228
229 if __name__=="__main__":
230 testhelpers.main(DataPackageExtraResourcesTest)