File: datapacktest.py

package info (click to toggle)
gavodachs 2.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 8,972 kB
  • sloc: python: 100,078; xml: 3,014; javascript: 2,360; ansic: 918; sh: 216; makefile: 31
file content (230 lines) | stat: -rw-r--r-- 6,232 bytes parent folder | download
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
"""
Tests for our datapack outputs.
"""

#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.


from gavo.helpers import testhelpers

import os
import shutil

from gavo import api
from gavo.protocols import datapack

class _DataPackageDescriptor(testhelpers.TestResource):
	def make(self, ignored):
		rd = datapack.getRDForDump("data/cores")
		res = datapack.makeDescriptor(rd)
		return res


class DataPackageDescriptorTest(testhelpers.VerboseTest):
	resources = [
		("desc", _DataPackageDescriptor())]

	def testTitleInferred(self):
		self.assertEqual(self.desc["name"], "test")
	
	def testDaCHSIndicator(self):
		self.assertEqual(self.desc["dachs-resdir"], "")
	
	def testDOI(self):
		self.assertEqual(self.desc["id"], "10.5072/test-fixture")
	
	def testLicense(self):
		self.assertEqual(len(self.desc["licenses"]), 1)
		self.assertEqual(self.desc["licenses"][0]["path"],
			"https://spdx.org/licenses/CC0-1.0.html")

	def testTitle(self):
		self.assertEqual(self.desc["title"], "Test Fixtures")
	
	def testDescription(self):
		self.assertEqual(self.desc["description"], "Helpers for tests for cores.")
	
	def testHomepage(self):
		self.assertEqual(self.desc["homepage"],
			"http://localhost:8080/browse/data/cores")

	def testVersion(self):
		self.assertEqual(self.desc["version"], "2.3.1pl2+git_deadbeef")

	def testContributors(self):
		self.assertEqual(self.desc["contributors"], [
			{'role': 'author', 'title': 'Gandalf, W.'},
			{'role': 'author', 'title': 'Baggins, B.'},
			{'role': 'author', 'title': 'Baggins, F.'}])

	def testCreated(self):
		self.assertEqual(self.desc["created"], "2010-12-01T14:00:00Z")

	def testMinimal(self):
		rd = api.parseFromString(api.RD, '<resource schema="nirvana"/>')
		rd.filesLoaded = []
		rd.sourceId = "nirvana/bar"
		rd.srcPath = rd.resdir
		res = datapack.makeDescriptor(rd)
		res.pop("resources")
		self.assertEqual(res, {
			"name": "nirvana", "dachs-resdir": "nirvana",
			"profile": "data-package",
			'homepage': 'http://localhost:8080/browse/nirvana/bar'})


class _DataPackageTestMixin:
	def _expectFile(self, path, assertName=None):
		for r in self.dpRscs:
			if r["path"]==path:
				if assertName is not None:
					self.assertEqual(r["name"], assertName)
				break
		else:
			raise AssertionError("%s not found in returned resources"%path)

	def _expectNoFile(self, name):
		for r in self.dpRscs:
			if r["path"]==name:
				raise AssertionError("%s is found in returned resources"
					" although it should not be there."%name)


class _DataPackageResources(testhelpers.TestResource):
	def make(self, ignored):
		with testhelpers.messageCollector() as msg:
			res = list(datapack.iterRDResources(
				datapack.getRDForDump("data/test")))
		res.append({
			"name": "fake-warning-messages",
			"path": "None",
			"messages": msg})
		return res


class DataPackageResourcesTest(
		testhelpers.VerboseTest,
		_DataPackageTestMixin):
	resources = [("dpRscs", _DataPackageResources())]

	def testRDPresent(self):
		self.assertEqual(self.dpRscs[0]["path"], "data/test.rd")

	def testDirectSource(self):
		self._expectFile('data/adqlin.txt')

	def testFITS(self):
		self._expectFile('data/ex.fits')

	def testSpectrum(self):
		self._expectFile('data/a.imp')

	def testUniqueNames(self):
		self.assertEqual(
			len(set(r["name"] for r in self.dpRscs)),
			len(self.dpRscs))
	
	def testMessage(self):
		type, args, _ = self.dpRscs[-1]["messages"].events[0]
		self.assertEqual(type, "Warning")
		self.assertEqual(args[0],
			"data data/test#productimport ignored because of dynamic ignoreSources")


class _ResourceCollectingResdir(testhelpers.TestResource):
	def make(self, ignored):
		self.respath = os.path.join(api.getConfig("inputsDir"), "dpexp")
		os.makedirs(self.respath, exist_ok=True)

		def w(n, content):
			with open(os.path.join(self.respath, n), "w", encoding="utf-8") as f:
				f.write(content)

		w("q.rd", """<resource schema="dpexp">
			<property name="datapack-extrafiles"
				>["subdir/*.knack", "worn", "gibtsnicht"]</property>
			<table id="main" onDisk="True"/>
			<data>
				<sources pattern="data/*.txt"/>
				<directGrammar cBooster="res/booster.c"/>
				<make table="main"/>
			</data>
			<service customPage="cp">
				<template key="fixed">template.html</template>
				<customCore module="cm"/>
			</service>
			</resource>
		""")
		
		w("cm.py", "from gavo import api\nclass Core(api.Core): pass\n")
		w("template.html", "<html/>")
		w("cp.py", "MainPage = None")

		os.makedirs(self.respath+"/res", exist_ok=True)
		for n in ["worn",  "res/booster.c", "README"]:
			w(n, "")

		os.makedirs(self.respath+"/subdir", exist_ok=True)
		for n in ["subdir/nix", "subdir/a.knack", "subdir/b.knack"]:
			w(n, "")

		return "dpexp/q"

	def clean(self, ignored):
		shutil.rmtree(self.respath)

_resourceCollectingResdir = _ResourceCollectingResdir()


class _CollectedResources(testhelpers.TestResource):
	resources = [("rdid", _resourceCollectingResdir)]

	def make(self, deps):
		rd = datapack.getRDForDump(deps["rdid"])
		return list(datapack.iterRDResources(rd))


class DataPackageExtraResourcesTest(
		testhelpers.VerboseTest,
		_DataPackageTestMixin):
	resources = [("dpRscs", _CollectedResources())]

	def testRD(self):
		self.assertEqual(self.dpRscs[0]["path"], "q.rd")
		self.assertEqual(self.dpRscs[0]["name"], "dachs-resource-descriptor")
		self.assertEqual(self.dpRscs[0]["mediatype"], "text/xml")

	def testREADME(self):
		self._expectFile("README", "readme")

	def testManualWildcard(self):
		self._expectFile("subdir/a.knack")
		self._expectFile("subdir/b.knack")

	def testManualLiteral(self):
		self._expectFile("worn")
	
	def testManualNoOverinclusion(self):
		self._expectNoFile("subdir/nix")
	
	def testManualNoMissingFiles(self):
		self._expectNoFile("gibtsnicht")

	def testBoosterIncluded(self):
		self._expectFile("res/booster.c")
	
	def testCustomPageIncluded(self):
		self._expectFile("cp.py")

	def testTemplateIncluded(self):
		self._expectFile("template.html")

	def testCustomCoreIncluded(self):
		self._expectFile("cm.py")


if __name__=="__main__":
	testhelpers.main(DataPackageExtraResourcesTest)