Debian Codegraph @

tests/lovottest.py

raw
1 """
2 Tests for our low-level VOTable interface.
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 base64
12 import datetime
13 import io
14 import re
15 import struct
16
17 import numpy
18 from numpy import rec
19
20 from gavo.helpers import testhelpers
21
22 from gavo import base
23 from gavo import utils
24 from gavo import votable
25 from gavo.utils import pgsphere
26 from gavo.votable import common
27 from gavo.votable import paramval
28 from gavo.votable import V
29 from gavo.utils.plainxml import iterparse
30
31
32 class NullFlagsSerTest(testhelpers.VerboseTest,
33 metaclass=testhelpers.SamplesBasedAutoTest):
34 def _runTest(self, sample):
35 nFields, nullMap, expected = sample
36 self.assertEqual(common.NULLFlags(nFields).serialize(nullMap), expected)
37
38 samples = [
39 (1, [True], b"\x80"),
40 (1, [False], b"\x00"),
41 (8, [True, False, True, False, False, False, False, False], b"\xa0"),
42 (9, [True, False, True, False, False, False, False, False, False],
43 b"\xa0\x00"),
44 (12, [False]*4+[True]*2+[False]*5+[True], b"\x0c\x10"),
45 (16, [False]*15+[True], b"\x00\x01"),
46 (65, [False]*64+[True], b"\x00\x00\x00\x00\x00\x00\x00\x00\x80"),
47 ]
48
49
50 class NullFlagsDeserTest(testhelpers.VerboseTest,
51 metaclass=testhelpers.SamplesBasedAutoTest):
52 def _runTest(self, sample):
53 nFields, expected, input = sample
54 self.assertEqual(common.NULLFlags(nFields).deserialize(input),
55 expected)
56
57 samples = NullFlagsSerTest.samples
58
59
60 class IterParseTest(testhelpers.VerboseTest,
61 metaclass=testhelpers.SamplesBasedAutoTest):
62 """tests for our custom iterparser.
63 """
64
65 def _runTest(self, sample):
66 xml, parsed = sample
67 self.assertEqual(list(iterparse(io.BytesIO(utils.bytify(xml)))), parsed)
68
69 samples = [
70 ("<doc/>", [("start", "doc", {}), ("end", "doc", None)]),
71 ('<doc href="x"/>',
72 [("start", "doc", {"href": "x"}), ("end", "doc", None)]),
73 (b'<doc>fl\xc3\xb6ge</doc>', [
74 ("start", "doc", {}),
75 ("data", None, "fl\xf6ge"),
76 ("end", "doc", None)]),
77 (b'<doc obj="fl\xc3\xb6ge"/>',
78 [("start", "doc", {"obj": "flöge"}), ("end", "doc", None)]),
79 ('<doc><abc>'+"unu"*10000+'</abc>\n<bcd>klutz</bcd></doc>', [
80 ("start", "doc", {}),
81 ("start", "abc", {}),
82 ("data", None, "unu"*10000),
83 ("end", "abc", None),
84 ("data", None, "\n"),
85 ("start", "bcd", {}),
86 ("data", None, "klutz"),
87 ("end", "bcd", None),
88 ("end", "doc", None)]),
89 ('<doc xmlns="http://insane"/>', [
90 ("start", "doc", {'xmlns': 'http://insane'}), ("end", "doc", None),])
91 ]
92
93
94 class TrivialParseTest(testhelpers.VerboseTest):
95 """tests operating on an empty VOTable.
96 """
97 def testTrivialParse(self):
98 self.assertEqual(list(votable.parseBytes("<VOTABLE/>")), [])
99
100 def testTrivialWatchlist(self):
101 res = list(votable.parseBytes("<VOTABLE/>",
102 watchset=[V.VOTABLE]))
103 self.assertTrue(isinstance(testhelpers.pickSingle(res), V.VOTABLE))
104
105 def testTrivialWithNamespace(self):
106 res = list(votable.parseBytes(
107 '<VOTABLE xmlns="http://www.ivoa.net/xml/VOTable/v1.2"/>',
108 watchset=[V.VOTABLE]))
109 self.assertTrue(isinstance(res[0], V.VOTABLE))
110
111 def testTrivialOldNamespace(self):
112 res = list(votable.parseBytes(
113 '<VOTABLE xmlns="http://www.ivoa.net/xml/VOTable/v1.1"/>',
114 watchset=[V.VOTABLE]))
115 self.assertTrue(isinstance(res[0], V.VOTABLE))
116
117
118 class TrivialWriteTest(testhelpers.VerboseTest):
119 def testEmpty(self):
120 res = votable.asBytes(V.VOTABLE(), xmlDecl=True)
121 self.assertTrue(b"<?xml version='1.0' encoding='utf-8'?>" in res)
122 self.assertTrue(b"<VOTABLE version=" in res)
123 self.assertTrue(b"/>" in res)
124
125 def testBasicSerialisation(self):
126 res = votable.asBytes(V.VOTABLE[V.RESOURCE[V.TABLE(name="kna")[
127 V.PARAM(name="a", datatype="char", arraysize="*", value="abc"),
128 V.DATA]]])
129 self.assertTrue(b'xmlns="http://www.ivoa.net/xml/VOTable/' in res)
130 self.assertTrue(b'<TABLE name="kna">' in res)
131 self.assertTrue(b'value="abc"' in res)
132
133
134 class ErrorParseTest(testhelpers.VerboseTest):
135 """tests for more-or-less benign behaviour on input errors.
136 """
137 def testExpatReporting(self):
138 try:
139 list(votable.parseBytes("<VOTABLE>"))
140 except Exception as raised:
141 ex = raised
142 self.assertEqual(ex.__class__.__name__, "VOTableParseError")
143 self.assertEqual(str(ex),
144 "(internal source) no element found: line 1, column 9")
145
146 def testInternalReporting(self):
147 table = next(votable.parseBytes("<VOTABLE><RESOURCE><TABLE>\n"
148 '<FIELD name="x" datatype="boolean"/>\n'
149 '<DATA><TABLEDATA>\n'
150 '<TR><TDA>True</TDA></TR>\n'
151 '</TABLEDATA></DATA>\n'
152 "</TABLE></RESOURCE></VOTABLE>\n"))
153 try:
154 list(table)
155 except Exception as raised:
156 ex = raised
157 self.assertEqual(ex.__class__.__name__, "VOTableParseError")
158 self.assertEqual(str(ex),
159 'At IO:\'<VOTABLE><RESOURCE><TABLE> <FIELD name="x" datatype="...\', (4, 4):'
160 " Unexpected element TDA")
161
162
163 class TextParseTest(testhelpers.VerboseTest):
164 """tests for parsing elements with text content.
165 """
166 def _getInfoItem(self, infoLiteral):
167 return list(votable.parseBytes(
168 '<VOTABLE>%s</VOTABLE>'%infoLiteral,
169 watchset=[V.INFO]))[0]
170
171 def testEmptyInfo(self):
172 res = self._getInfoItem('<INFO name="t" value="0"/>')
173 self.assertEqual(res.value, "0")
174 self.assertEqual(res.name, "t")
175
176 def testFullInfo(self):
177 res = self._getInfoItem('<INFO name="t" value="0">abc</INFO>')
178 self.assertEqual(res.text_, "abc")
179
180 def testUnicode(self):
181 # xml defaults to utf-8
182 res = self._getInfoItem(
183 b'<INFO name="t" value="0">\xc3\xa4rn</INFO>'.decode("utf-8"))
184 self.assertEqual(res.text_, "\xe4rn")
185
186
187 class IdTest(testhelpers.VerboseTest):
188 """tests for the management of id attributes.
189 """
190 def testSimpleId(self):
191 els = list(votable.parseBytes(
192 '<VOTABLE><INFO ID="xy">abc</INFO></VOTABLE>',
193 watchset=[V.INFO, V.VOTABLE]))
194 self.assertTrue(els[0].idmap is els[1].idmap)
195 self.assertTrue(els[0].idmap["xy"] is els[0])
196
197 def testForwardReference(self):
198 iter = votable.parseBytes(
199 '<VOTABLE><INFO ID="xy" ref="z">abc</INFO>'
200 '<INFO ID="z" ref="xy">zz</INFO></VOTABLE>',
201 watchset=[V.INFO])
202 info0 = next(iter)
203 self.assertRaises(KeyError, lambda: info0.idmap[info0.ref])
204 info1 = next(iter)
205 self.assertTrue(info0.idmap[info0.ref] is info1)
206 self.assertTrue(info1.idmap[info1.ref] is info0)
207 self.assertRaises(StopIteration, iter.__next__)
208
209
210 class TabledataReadTest(testhelpers.VerboseTest,
211 metaclass=testhelpers.SamplesBasedAutoTest):
212 """tests for deserialization of TABLEDATA encoded values.
213 """
214
215 def _runTest(self, sample):
216 fielddefs, literals, expected = sample
217 table = next(votable.parseBytes(
218 '<VOTABLE><RESOURCE><TABLE>'+
219 fielddefs+
220 '<DATA><TABLEDATA>'+
221 '\n'.join('<TR>%s</TR>'%''.join('<TD>%s</TD>'%l
222 for l in row) for row in literals)+
223 '</TABLEDATA></DATA>'
224 '</TABLE></RESOURCE></VOTABLE>'))
225 self.assertEqual(list(table), expected)
226
227 samples = [(
228 '<FIELD name="x" datatype="boolean"/>',
229 [['TRue'], ['T'], ['False'], ['?']],
230 [[True], [True], [False], [None]]
231 ), (
232 '<FIELD name="x" datatype="unsignedByte"/>'
233 '<FIELD name="y" datatype="unsignedByte"><VALUES null="16"/></FIELD>',
234 [['', ''], ['0x10', '0x10'], ['10', '16'], ['', '']],
235 [[None, None], [16, 16], [10, None], [None, None]]
236 ), (
237 '<FIELD name="x" datatype="char"/>',
238 [[''], ['a'], ['&apos;'], ['\xe4'], ['&#xe4;'], ['']],
239 [[None], ['a'], ["'"], ['ä'], ['ä'], [None]],
240 ), (
241 '<FIELD name="x" datatype="short"><VALUES null="0"/></FIELD>'
242 '<FIELD name="y" datatype="int"/>'
243 '<FIELD name="z" datatype="long"><VALUES null="222399322"/></FIELD>',
244 [['0', '0', '0'], ['-3', '-300', '222399322'], ['0xff', '0xcafebabe', '0xcafebabedeadbeef'], ['', '', '']],
245 [[None, 0, 0], [-3, -300, None], [255, -889275714, -3819410105021120785], [None, None, None]]
246 ), (
247 '<FIELD name="x" datatype="float"><VALUES null="-999."/></FIELD>'
248 '<FIELD name="y" datatype="float"/>',
249 [['1', '0.5e10'], ['-999.', '']],
250 [[1.0, 5e09], [None, None]]
251 # 5
252 ), (
253 '<FIELD name="x" datatype="floatComplex"><VALUES null="-999. 0"/></FIELD>'
254 '<FIELD name="y" datatype="floatComplex"/>',
255 [['1 1', '0.5e10 -2e5'], ['-999. 0', '20']],
256 [[(1+1j), 5e09-2e5j], [None, 20+0j]]
257 ), (
258 '<FIELD name="x" datatype="boolean" arraysize="*"/>',
259 [['true false ? T'], [' T'], ['']],
260 [[[True, False, None, True]], [[True,]], [None]]
261 ), (
262 '<FIELD name="y" datatype="unsignedByte" arraysize="*">'
263 ' <VALUES null="16"/></FIELD>',
264 [['10 0x10\t 16 \n 0x16'], ['']],
265 [[[10, 16, None, 22]], [None]]
266 ), (
267 '<FIELD name="x" datatype="char" arraysize="4"/>',
268 [[''], ['auto'], ['&apos;xx&quot;'], ['\xe4'], ['&#xe4;'], ['']],
269 [[None], ['auto'], ["'xx\""], ['ä'], ['ä'], [None]],
270 ), (
271 '<FIELD name="x" datatype="short" arraysize="*"><VALUES null="0"/></FIELD>',
272 [['1 2 3 0 1'], [""]],
273 [[[1,2,3,None,1]], [None]]
274 #10
275 ), (
276 '<FIELD name="y" datatype="floatComplex" arraysize="*"/>',
277 [['1 1 0.5e10 -2e5'], [""]],
278 [[[(1+1j), 5e09-2e5j]], [None]]
279 ), (
280 '<FIELD datatype="short" arraysize="2x3"/>',
281 [['0 1 2 3 4 5']],
282 [[[[0,1],[2,3],[4,5]]]],
283 ), (
284 '<FIELD name="x" datatype="float"/>',
285 [['NaN'], ['']],
286 [[None], [None]]
287 ), (
288 '<FIELD datatype="unicodeChar" arraysize="*"/>',
289 [['\xe4'], [""]],
290 [['\xe4'], [None]]
291 ), (
292 '<FIELD datatype="float" arraysize="*"/>',
293 [[" "]],
294 [[[]]]
295 ), (
296 #15
297 '<FIELD datatype="double" arraysize="2" xtype="point"/>',
298 [[""], ["10 12"]],
299 [[None], [pgsphere.SPoint.fromDegrees(10, 12)]],
300 ), (
301 '<FIELD datatype="double" arraysize="3" xtype="circle"/>',
302 [[""], ["100 -23 0.25"]],
303 [[None], [pgsphere.SCircle.fromDALI([100, -23, 0.25])]]
304 ), (
305 '<FIELD datatype="double" arraysize="*" xtype="polygon"/>',
306 [[""], ["0 0 130 20 70 -10"]],
307 [[None], [pgsphere.SPoly.fromDALI([0, 0, 130, 20, 70, -10])]]
308 ), (
309 '<FIELD datatype="char" arraysize="*"/>',
310 [['\xe4'], [""]],
311 [['ä'], [None]] # this is fallback encoding
312 ), (
313 '<FIELD datatype="char" arraysize="1"/>',
314 [['\xe4'], [" "]],
315 [['ä'], [' ']] # this is fallback encoding
316 ), (
317 # 20
318 '<FIELD name="d" datatype="char" arraysize="1" xtype="timestamp"/>',
319 [[''], [" "], [" "]],
320 [[None], [None], [None]] # this is fallback encoding
321 ), (
322 '<FIELD name="j" datatype="char" arraysize="*" xtype="json"/>',
323 [[''], ["{'foo': 'bar'}"]],
324 [[None], ["{'foo': 'bar'}"]]
325 ),
326 ]
327
328
329 class FloatTDEncodingTest(testhelpers.VerboseTest):
330 """tests for proper handling of special float values.
331 """
332 def _decode(self, fielddefs, literals):
333 table = next(votable.parseBytes(
334 '<VOTABLE><RESOURCE><TABLE>'+
335 fielddefs+
336 '<DATA><TABLEDATA>'+
337 '\n'.join('<TR>%s</TR>'%''.join('<TD>%s</TD>'%l
338 for l in row) for row in literals)+
339 '</TABLEDATA></DATA>'
340 '</TABLE></RESOURCE></VOTABLE>'))
341 return list(table)
342
343 def testNAN(self):
344 vals = self._decode(
345 '<FIELD name="y" datatype="float"/>',
346 [['NaN']])[0]
347 self.assertTrue(vals[0] is None)
348
349 def testInfinities(self):
350 vals = self._decode(
351 '<FIELD name="y" datatype="float"/>',
352 [['+Inf'], ['-Inf']])
353 self.assertTrue(vals[0][0]==2*vals[0][0])
354 self.assertTrue(vals[1][0]==2*vals[1][0])
355
356 def testWeirdArray(self):
357 vals = self._decode(
358 '<FIELD name="y" datatype="float" arraysize="3"/>',
359 [['NaN +Inf -Inf']])[0]
360 self.assertEqual(repr(vals), '[[None, inf, -inf]]')
361
362
363 class TabledataWriteTest(testhelpers.VerboseTest,
364 metaclass=testhelpers.SamplesBasedAutoTest):
365 """tests for serializing TABLEDATA VOTables.
366 """
367
368 def _runTest(self, sample):
369 fielddefs, input, expected = sample
370 vot = V.VOTABLE[V.RESOURCE[votable.DelayedTable(
371 V.TABLE[fielddefs], input, V.TABLEDATA)]]
372 res = votable.asBytes(vot)
373 mat = re.search(b"<TABLEDATA>(.*)</TABLEDATA>", res)
374 content = mat and mat.group(1)
375 self.assertEqual(content, utils.bytify(expected))
376
377 samples = [(
378 [V.FIELD(datatype="float")],
379 [[1],[None],[float("NaN")], [3]],
380 "<TR><TD>1.0</TD></TR><TR><TD>NaN</TD></TR><TR><TD>NaN</TD></TR><TR><TD>3.0</TD></TR>"
381 ), (
382 [V.FIELD(datatype="double")],
383 [[1.52587890625e-05], [float("+Inf")]],
384 '<TR><TD>1.52587890625e-05</TD></TR><TR><TD>inf</TD></TR>'
385 ), (
386 [V.FIELD(datatype="boolean")],
387 [[True], [False], [None]],
388 '<TR><TD>1</TD></TR><TR><TD>0</TD></TR><TR><TD>?</TD></TR>'
389 ), ([
390 V.FIELD(datatype="bit"),
391 V.FIELD(datatype="unsignedByte"),
392 V.FIELD(datatype="short"),
393 V.FIELD(datatype="int"),
394 V.FIELD(datatype="long")],
395 [[0,1,2,3,4]],
396 '<TR><TD>0</TD><TD>1</TD><TD>2</TD><TD>3</TD><TD>4</TD></TR>'
397 ), (
398 [V.FIELD(datatype="unicodeChar")],
399 ['\xe4'],
400 b'<TR><TD>\xc3\xa4</TD></TR>'
401 ), (
402 # 5
403 [V.FIELD(datatype="char")],
404 ['\xe4'],
405 '<TR><TD>?</TD></TR>'
406 ), (
407 [V.FIELD(datatype="floatComplex")],
408 [[0.5+0.25j]],
409 '<TR><TD>0.5 0.25</TD></TR>'
410 ), ([
411 V.FIELD(datatype="unsignedByte")[V.VALUES(null="23")],
412 V.FIELD(datatype="unicodeChar")[V.VALUES(null="\x00")],
413 V.FIELD(datatype="float")[V.VALUES(null="-9999")]],
414 [[1, "a", 1.5], [None, None, None]],
415 '<TR><TD>1</TD><TD>a</TD><TD>1.5</TD></TR>'
416 '<TR><TD>23</TD><TD>&x00;</TD><TD>NaN</TD></TR>'
417 ), (
418 [V.FIELD(datatype="unsignedByte", arraysize="2")[V.VALUES(null="0xff")]],
419 [[[]], [[2]], [None], [[2, 3, 4]]],
420 '<TR><TD>0xff 0xff</TD></TR><TR><TD>2 0xff</TD></TR>'
421 '<TR><TD></TD></TR><TR><TD>2 3</TD></TR>'
422 ), (
423 [V.FIELD(datatype="bit", arraysize="*")],
424 [[430049293488]],
425 '<TR><TD>110010000100000111011110111010010110000</TD></TR>'
426 ), (
427 # 10
428 [V.FIELD(datatype="doubleComplex", arraysize="2")[V.VALUES(null="0 0")]],
429 [[[2+2j, None, 4+4j]]],
430 '<TR><TD>2.0 2.0 NaN NaN</TD></TR>'
431 ), (
432 [V.FIELD(datatype="double", arraysize="*")[V.VALUES(null="23")]],
433 [[None], [[None]]],
434 "<TR><TD></TD></TR><TR><TD>NaN</TD></TR>"
435 ), (
436 [V.FIELD(datatype="char", arraysize="*", xtype="adql:TIMESTAMP")],
437 [[None], [datetime.datetime(2010, 10, 12, 23, 10, 0o1)]],
438 "<TR><TD></TD></TR><TR><TD>2010-10-12T23:10:01Z</TD></TR>"
439 ),(
440 [V.FIELD(datatype="char", arraysize="*", xtype="timestamp")],
441 [[None], [datetime.datetime(2010, 10, 12, 23, 10, 0o1)]],
442 "<TR><TD></TD></TR><TR><TD>2010-10-12T23:10:01Z</TD></TR>"
443 ),(
444 [V.FIELD(datatype="double", arraysize="2", xtype="point")],
445 [[None], [pgsphere.SPoint.fromDegrees(100, 23)],
446 [[1, 2]]],
447 "<TR><TD></TD></TR><TR><TD>100.0 23.0</TD></TR><TR><TD>1.0 2.0</TD></TR>"
448 ),(
449 # 15
450 [V.FIELD(datatype="double", arraysize="3", xtype="circle")],
451 [[None], [pgsphere.SCircle.fromDALI([100, -23, 0.25])],
452 [[1, 2, 0.5]]],
453 "<TR><TD></TD></TR><TR><TD>100.0 -23.0 0.25</TD></TR>"
454 "<TR><TD>1.0 2.0 0.5</TD></TR>"
455 ), (
456 [V.FIELD(datatype="double", arraysize="*", xtype="polygon")],
457 [[None], [pgsphere.SPoly.fromDALI([0, 0, 130, 20, 70, -10])],
458 [[1, 2, 3, 4, 5, 6]]],
459 "<TR><TD></TD></TR><TR><TD>0.0 0.0 130.0 20.0 70.0 -10.0</TD></TR>"
460 "<TR><TD>1.0 2.0 3.0 4.0 5.0 6.0</TD></TR>"
461 ), (
462 [V.FIELD(datatype="char", arraysize="*", xtype="json")],
463 [['{"a": "b"}'], [None], ['["ünk", "✂"]']],
464 r'<TR><TD>{"a": "b"}</TD></TR><TR><TD></TD></TR><TR><TD>["\u00fcnk", "\u2702"]</TD></TR>'
465 ),
466 ]
467
468
469 class BinaryWriteTest(testhelpers.VerboseTest,
470 metaclass=testhelpers.SamplesBasedAutoTest):
471 """tests for serializing BINARY VOTables.
472 """
473
474 def _runTest(self, sample):
475 fielddefs, input, expected = sample
476 vot = V.VOTABLE[V.RESOURCE[votable.DelayedTable(
477 V.TABLE[fielddefs], input, V.BINARY)]]
478 mat = re.search(b'(?s)<STREAM encoding="base64">(.*)</STREAM>',
479 votable.asBytes(vot))
480 content = mat and mat.group(1)
481 self.assertEqual(base64.b64decode(content), expected)
482
483 samples = [(
484 [V.FIELD(datatype="float")],
485 [[1],[None],[common.NaN]],
486 struct.pack("!fff", 1, common.NaN, common.NaN)
487 ), (
488 [V.FIELD(datatype="double")],
489 [[1],[None],[common.NaN]],
490 struct.pack("!ddd", 1, common.NaN, common.NaN)
491 ), (
492 [V.FIELD(datatype="boolean")],
493 [[True],[False],[None]],
494 b"10?"
495 ), (
496 [V.FIELD(datatype="bit")],
497 [[1],[0]],
498 b"\x01\x00"
499 ), (
500 [V.FIELD(datatype="unsignedByte")],
501 [[20]],
502 b"\x14"
503 ), (
504 # 5
505 [V.FIELD(datatype="unsignedByte")[V.VALUES(null="23")]],
506 [[20], [None]],
507 b"\x14\x17"
508 ), (
509 [V.FIELD(datatype="short")],
510 [[20]],
511 b"\x00\x14"
512 ), (
513 [V.FIELD(datatype="int")],
514 [[-20]],
515 b"\xff\xff\xff\xec"
516 ), (
517 [V.FIELD(datatype="long")],
518 [[-20]],
519 b"\xff\xff\xff\xff\xff\xff\xff\xec"
520 ), (
521 [V.FIELD(datatype="char")[V.VALUES(null="x")]],
522 [['a'], [None]],
523 b"ax"
524 ), (
525 # 10
526 [V.FIELD(datatype="unicodeChar")[V.VALUES(null="\u1ead")]],
527 [['a'], [b'\xe4'.decode("iso-8859-1")], [None]],
528 b"\x00a\x00\xe4\x1e\xad"
529 ), (
530 [V.FIELD(datatype="floatComplex")],
531 [[6+7j], [None]],
532 struct.pack("!ff", 6, 7)+struct.pack("!ff", common.NaN, common.NaN)
533 ), (
534 [V.FIELD(datatype="bit", arraysize="17")],
535 [[1],[2**25-1]],
536 b"\x00\x00\x01\xff\xff\xff"
537 ), (
538 [V.FIELD(datatype="bit", arraysize="*")],
539 [[1],[2**25-1]],
540 b"\x00\x00\x00\x08\x01"
541 b"\x00\x00\x00\x20\x01\xff\xff\xff"
542 ), (
543 [V.FIELD(datatype="unsignedByte", arraysize="*")],
544 [[[]], [[1]], [[0, 1, 2]]],
545 b"\x00\x00\x00\x00"
546 b"\x00\x00\x00\x01\x01"
547 b"\x00\x00\x00\x03\x00\x01\x02"
548 ), (
549 # 15
550 [V.FIELD(datatype="unsignedByte", arraysize="2")[V.VALUES(null="255")]],
551 [[[]], [[1]], [[0, 1, 2]]],
552 b"\xff\xff"
553 b"\x01\xff"
554 b"\x00\x01"
555 ), (
556 [V.FIELD(datatype="short", arraysize="2*")],
557 [[[]], [[1]], [[0, 1, 2]]],
558 b"\x00\x00\x00\x00"
559 b"\x00\x00\x00\x01\x00\x01"
560 b"\x00\x00\x00\x03\x00\x00\x00\x01\x00\x02"
561 ), (
562 [V.FIELD(datatype="char", arraysize="2")],
563 [["abc"], ["a"], [None], ["öa"]],
564 b"aba ?a"
565 ), (
566 [V.FIELD(datatype="char", arraysize="*")],
567 [["abc"], ["a"], [None], ["öa"]],
568 b"\0\0\0\x03abc\0\0\0\x01a\0\0\0\0\0\0\0\x02?a"
569 ), (
570 [V.FIELD(datatype="unicodeChar", arraysize="2")],
571 [["\u00e4bc"], ["\u00e4"]],
572 b'\x00\xe4\x00b\x00\xe4\x00 '
573 ), (
574 # 20
575 [V.FIELD(datatype="short", arraysize="3x2")],
576 [[[1,2,3,4,5,6]]],
577 b'\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06'
578 ),(
579 [V.FIELD(datatype="char", arraysize="*", xtype="adql:TIMESTAMP")],
580 [[None], [datetime.datetime(2010, 10, 12, 23, 10, 0o1)]],
581 b'\x00\x00\x00\x00\x00\x00\x00\x142010-10-12T23:10:01Z'
582 ), (
583 [V.FIELD(datatype="char", arraysize="*", xtype="timestamp")],
584 [[None], [datetime.datetime(2010, 10, 12, 23, 10, 0o1)]],
585 b'\x00\x00\x00\x00\x00\x00\x00\x142010-10-12T23:10:01Z'
586 ), (
587 [V.FIELD(datatype="double", arraysize="2", xtype="point")],
588 [[None], [pgsphere.SPoint.fromDegrees(100, 23)],
589 [[1, 2]]],
590 b'\x7f\xf8\x00\x00\x00\x00\x00\x00\x7f\xf8\x00\x00\x00\x00\x00\x00'
591 b'@Y\x00\x00\x00\x00\x00\x00@7\x00\x00\x00\x00\x00\x00'
592 b'?\xf0\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00'
593 ),(
594 [V.FIELD(datatype="double", arraysize="3", xtype="circle")],
595 [[None], [pgsphere.SCircle.fromDALI([100, -23, 0.25])],
596 [[1, 2, 0.5]]],
597 b'\x7f\xf8\x00\x00\x00\x00\x00\x00\x7f\xf8\x00\x00\x00\x00\x00\x00\x7f\xf8\x00\x00\x00\x00\x00\x00'
598 b'@Y\x00\x00\x00\x00\x00\x00\xc07\x00\x00\x00\x00\x00\x00?\xd0\x00\x00\x00\x00\x00\x00'
599 b'?\xf0\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00?\xe0\x00\x00\x00\x00\x00\x00'
600 ), (
601 # 25
602 [V.FIELD(datatype="double", arraysize="*", xtype="polygon")],
603 [[None], [pgsphere.SPoly.fromDALI([0, 0, 130, 20, 70, -10])],
604 [[1, 2, 3, 4, 5, 6]]],
605 b'\x00\x00\x00\x00'
606 b'\x00\x00\x00\x06'
607 b'\x00\x00\x00\x00\x00\x00\x00\x00'
608 b'\x00\x00\x00\x00\x00\x00\x00\x00'
609 b'@`@\x00\x00\x00\x00\x00'
610 b'@4\x00\x00\x00\x00\x00\x00'
611 b'@Q\x80\x00\x00\x00\x00\x00'
612 b'\xc0$\x00\x00\x00\x00\x00\x00'
613 b'\x00\x00\x00\x06'
614 b'?\xf0\x00\x00\x00\x00\x00\x00'
615 b'@\x00\x00\x00\x00\x00\x00\x00'
616 b'@\x08\x00\x00\x00\x00\x00\x00'
617 b'@\x10\x00\x00\x00\x00\x00\x00'
618 b'@\x14\x00\x00\x00\x00\x00\x00'
619 b'@\x18\x00\x00\x00\x00\x00\x00'
620 ), (
621 [V.FIELD(datatype="char", arraysize="10x*")],
622 [[numpy.array(["grab", "grub"])]],
623 b'\x00\x00\x00\x14grab grub '
624 ), (
625 [V.FIELD(datatype="char", arraysize="*", xtype="json")],
626 [[["Phoenix", "Phönix"]]],
627 b'\x00\x00\x00\x1a["Phoenix", "Ph\\u00f6nix"]'
628 ),
629 ]
630
631
632 class BinaryReadTest(testhelpers.VerboseTest,
633 metaclass=testhelpers.SamplesBasedAutoTest):
634 """tests for deserializing BINARY VOTables.
635 """
636
637 def _runTest(self, sample):
638 fielddefs, stuff, expected = sample
639 table = next(votable.parseBytes(
640 b'<VOTABLE><RESOURCE><TABLE>'+
641 utils.bytify(fielddefs)+
642 b'<DATA><BINARY><STREAM encoding="base64">'+
643 base64.b64encode(stuff)+
644 b'</STREAM></BINARY></DATA>'
645 b'</TABLE></RESOURCE></VOTABLE>'))
646 self.assertEqual(list(table), expected)
647
648 samples = [(
649 '<FIELD datatype="boolean"/>',
650 b"10?",
651 [[True],[False],[None]],
652 ), (
653 '<FIELD datatype="bit"/>',
654 b"\x01\x00\xff",
655 [[1],[0],[1]],
656 ), (
657 '<FIELD datatype="bit" arraysize="9"/>',
658 b"\x01\x00\xff\xff",
659 [[256],[511]],
660 ), (
661 '<FIELD datatype="bit" arraysize="*"/>',
662 b"\x00\x00\x00\x03\xff\x00\x00\x00\x45"
663 b"\xff\x00\x00\x00\x00\x00\x00\x00\x01",
664 [[7],[0x1f0000000000000001]],
665 ), (
666 '<FIELD datatype="char"><VALUES null="a"/></FIELD>',
667 b"x\x00a",
668 [['x'],[''], [None]],
669 ), (
670 # 05
671 '<FIELD datatype="unicodeChar"><VALUES null="&#xbabe;"/></FIELD>',
672 b"\x00a\x23\x42\xba\xbe",
673 [['a'],['\u2342'], [None]],
674 ), (
675 '<FIELD datatype="unsignedByte"><VALUES null="12"/></FIELD>'
676 '<FIELD datatype="short"><VALUES null="12"/></FIELD>'
677 '<FIELD datatype="int"><VALUES null="12"/></FIELD>'
678 '<FIELD datatype="long"><VALUES null="12"/></FIELD>',
679 b"\x0c\x00\x0c\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x0c"
680 b"\x0d\x0d\x0d\x00\x0d\x0d\x0c\x00\xdd\x00\x00\x00\x00\x00\x0c", [
681 [None, None, None, None],
682 [13, 3341, 855308, 62205969853054988]]
683 ), (
684 '<FIELD datatype="float"/>',
685 b"\x7f\xc0\x00\x00:\x80\x00\x00",
686 [[None], [0.0009765625]]
687 ), (
688 '<FIELD datatype="double"/>',
689 b"\x7f\xf8\x00\x00\x00\x00\x00\x00?P\x00\x01\x00\x00\x00\x00",
690 [[None], [0.00097656343132257462]]
691 ), (
692 '<FIELD datatype="doubleComplex"/>',
693 b"\x7f\xf8\x00\x00\x00\x00\x00\x00?P\x00\x01\x00\x00\x00\x00"
694 b'@\x04\x00\x00\x00\x00\x00\x00?\xe0\x00\x00\x00\x00\x00\x00',
695 [[None], [2.5+0.5j]]
696 ), (
697 # 10
698 '<FIELD datatype="char" arraysize="4"><VALUES null="0000"/></FIELD>',
699 b"abcd0000",
700 [["abcd"], [None]]
701 ), (
702 '<FIELD datatype="char" arraysize="*"/>',
703 b"\x00\x00\x00\x00\x00\x00\x00\x03abc",
704 [[""], ["abc"]]
705 ), (
706 '<FIELD datatype="unicodeChar" arraysize="*"/>',
707 b"\x00\x00\x00\x03\x00a\x23\x42bc",
708 [["a\u2342\u6263"]]
709 ), (
710 '<FIELD datatype="unicodeChar" arraysize="2"/>',
711 b"\x00a\x23\x42",
712 [["a\u2342"]]
713 ), (
714 '<FIELD datatype="unsignedByte" arraysize="2"/>',
715 b'\x00\x01',
716 [[[0, 1]]],
717 ), (
718 # 15
719 '<FIELD datatype="short" arraysize="*"><VALUES null="16"/></FIELD>',
720 b'\x00\x00\x00\x03\x00\x01\x00\x10\x00\x02',
721 [[[1, None, 2]]],
722 ), (
723 '<FIELD datatype="int" arraysize="2"/>',
724 b'\x00\x00\x00\x03\x00\x01\x00\x10',
725 [[[3, 0x10010]]],
726 ), (
727 '<FIELD datatype="float" arraysize="2"/>',
728 b'\x7f\xc0\x00\x00:\x80\x00\x00',
729 [[[None, 0.0009765625]]],
730 ), (
731 '<FIELD datatype="double" arraysize="*"/>',
732 b'\x00\x00\x00\x02\x7f\xf8\x00\x00\x00\x00\x00\x00'
733 b'?P\x00\x01\x00\x00\x00\x00',
734 [[[None, 0.00097656343132257462]]]
735 ), (
736 '<FIELD datatype="float" arraysize="2"><VALUES null="2"/></FIELD>',
737 b'\x7f\xc0\x00\x00:\x80\x00\x00',
738 [[[None, 0.0009765625]]],
739 ), (
740 # 20
741 '<FIELD datatype="floatComplex" arraysize="2"/>',
742 b'\x7f\xc0\x00\x00:\x80\x00\x00'
743 b'A\x80\x00\x00A\x0c\x00\x00',
744 [[[None, 16+8.75j]]],
745 ), (
746 '<FIELD datatype="short" arraysize="2x3"/>',
747 b'\x00\x00\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05',
748 [[[[0,1],[2,3],[4,5]]]],
749 ), (
750 '<FIELD datatype="double" arraysize="2" xtype="point"/>',
751 b'\x7f\xf8\x00\x00\x00\x00\x00\x00\x7f\xf8\x00\x00\x00\x00\x00\x00',
752 [[None]],
753 ), (
754 '<FIELD datatype="double" arraysize="2" xtype="point"/>',
755 b'@Y\x00\x00\x00\x00\x00\x00@7\x00\x00\x00\x00\x00\x00',
756 [[pgsphere.SPoint.fromDegrees(100, 23)]],
757 ), (
758 '<FIELD datatype="double" arraysize="3" xtype="circle"/>',
759 b'\x7f\xf8\x00\x00\x00\x00\x00\x00'
760 b'\x7f\xf8\x00\x00\x00\x00\x00\x00'
761 b'\x7f\xf8\x00\x00\x00\x00\x00\x00',
762 [[None]]
763 ), (
764 # 25
765 '<FIELD datatype="double" arraysize="3" xtype="circle"/>',
766 b'@Y\x00\x00\x00\x00\x00\x00'
767 b'\xc07\x00\x00\x00\x00\x00\x00'
768 b'?\xd0\x00\x00\x00\x00\x00\x00',
769 [[pgsphere.SCircle.fromDALI([100, -23, 0.25])]]
770 ), (
771 '<FIELD datatype="double" arraysize="*" xtype="polygon"/>',
772 b'\x00\x00\x00\x00',
773 [[None]]
774 ), (
775 '<FIELD datatype="double" arraysize="*" xtype="polygon"/>',
776 b'\x00\x00\x00\x06'
777 b'\x00\x00\x00\x00\x00\x00\x00\x00'
778 b'\x00\x00\x00\x00\x00\x00\x00\x00'
779 b'@`@\x00\x00\x00\x00\x00'
780 b'@4\x00\x00\x00\x00\x00\x00'
781 b'@Q\x80\x00\x00\x00\x00\x00'
782 b'\xc0$\x00\x00\x00\x00\x00\x00',
783 [[pgsphere.SPoly.fromDALI([0, 0, 130, 20, 70, -10])]]
784 ), (
785 '<FIELD datatype="char" arraysize="*" xtype="timestamp"/>',
786 b'\0\0\0\0\0\0\0\x142010-10-12T23:10:01Z',
787 [[None], [datetime.datetime(2010, 10, 12, 23, 10, 0o1)]]
788 ), (
789 '<FIELD datatype="char"><VALUES null="a"/></FIELD>',
790 b"\x00a\x23\x42\xba\xbe",
791 [[''],[None], ['#'], ['B'], ['?'], ['?']],
792 ), (
793 # 30
794 '<FIELD datatype="char" arraysize="10"/>',
795 b"abc\x00tail ",
796 [["abc"]]),
797 ]
798
799
800 class Binary2WriteTest(testhelpers.VerboseTest,
801 metaclass=testhelpers.SamplesBasedAutoTest):
802 """tests for serializing BINARY2 VOTables.
803 """
804
805 def _runTest(self, sample):
806 fielddefs, input, expected = sample
807 vot = V.VOTABLE[V.RESOURCE[votable.DelayedTable(
808 V.TABLE[fielddefs], input, V.BINARY2)]]
809 mat = re.search(b'(?s)<STREAM encoding="base64">(.*)</STREAM>',
810 votable.asBytes(vot))
811 content = mat and mat.group(1)
812 self.assertEqual(base64.b64decode(content), expected)
813
814 samples = [(
815 [V.FIELD(datatype="float")],
816 [[1.],[None],[common.NaN]],
817 struct.pack("!BfBfBf", 0, 1., 0x80, common.NaN, 0, common.NaN)
818 ), (
819 [V.FIELD(datatype="double")],
820 [[1.],[None],[common.NaN]],
821 struct.pack("!BdBdBd", 0, 1., 0x80, common.NaN, 0, common.NaN)
822 ), (
823 [V.FIELD(datatype="boolean")],
824 [[True],[False],[None]],
825 b"\x001\x000\x80?"
826 ), (
827 [V.FIELD(datatype="bit")],
828 [[1],[0]],
829 b"\x00\x01\x00\x00"
830 ), (
831 [V.FIELD(datatype="unsignedByte")],
832 [[20], [None]],
833 b"\x00\x14\x80\xff"
834 ), (
835 # 5
836 [V.FIELD(datatype="unsignedByte")[V.VALUES(null="23")]],
837 [[20], [None]],
838 b"\x00\x14\x80\xff"
839 ), (
840 [V.FIELD(datatype="short")],
841 [[20],[None]],
842 b"\x00\x00\x14\x80\x00\x00"
843 ), (
844 [V.FIELD(datatype="int")],
845 [[-20], [None]],
846 b"\x00\xff\xff\xff\xec\x80\x00\x00\x00\x00"
847 ), (
848 [V.FIELD(datatype="long")],
849 [[-20], [None]],
850 b"\x00\xff\xff\xff\xff\xff\xff\xff\xec\x80\x00\x00\x00\x00\x00\x00\x00\x00"
851 ), (
852 [V.FIELD(datatype="char")[V.VALUES(null="x")]],
853 [['a'], [None]],
854 b"\x00a\x80\x00"
855 ), (
856 # 10
857 [V.FIELD(datatype="unicodeChar")[V.VALUES(null="\u1ead")]],
858 [['a'], [b'\xe4'.decode("iso-8859-1")], [None]],
859 b"\x00\x00a\x00\x00\xe4\x80\x00\x00"
860 ), (
861 [V.FIELD(datatype="floatComplex")],
862 [[6+7j], [None]],
863 struct.pack("!Bff", 0, 6, 7)+struct.pack(
864 "!Bff", 0x80, common.NaN, common.NaN)
865 ), (
866 [V.FIELD(datatype="bit", arraysize="17")],
867 [[1], [2**25-1], [None]],
868 b"\x00\x00\x00\x01\x00\xff\xff\xff\x80\x00\x00\x00\x00"
869 ), (
870 [V.FIELD(datatype="bit", arraysize="*")],
871 [[1],[2**25-1], [None]],
872 b"\x00\x00\x00\x00\x08\x01"
873 b"\x00\x00\x00\x00\x20\x01\xff\xff\xff"
874 b"\x80\x00\x00\x00\x00"
875 ), (
876 [V.FIELD(datatype="unsignedByte", arraysize="*")],
877 [[[]], [[1]], [[0, 1, 2]], [None]],
878 b"\x00\x00\x00\x00\x00"
879 b"\x00\x00\x00\x00\x01\x01"
880 b"\x00\x00\x00\x00\x03\x00\x01\x02"
881 b"\x80\x00\x00\x00\x00"
882 ), (
883 # 15
884 [V.FIELD(datatype="unsignedByte", arraysize="2")[V.VALUES(null="255")]],
885 [[[]], [[1]], [[0, 1, 2]], [None]],
886 b"\x00\xff\xff"
887 b"\x00\x01\xff"
888 b"\x00\x00\x01"
889 b"\x80\xff\xff"
890 ), (
891 [V.FIELD(datatype="short", arraysize="2*")],
892 [[[]], [[1]], [[0, 1, 2]], [None]],
893 b"\x00\x00\x00\x00\x00"
894 b"\x00\x00\x00\x00\x01\x00\x01"
895 b"\x00\x00\x00\x00\x03\x00\x00\x00\x01\x00\x02"
896 b"\x80\x00\x00\x00\x00"
897 ), (
898 [V.FIELD(datatype="char", arraysize="2")],
899 [["abc"], ["a"], [None], ["öa"]],
900 b"\x00ab\x00a\x00\x80\x00\x00\x00?a"
901 ), (
902 [V.FIELD(datatype="char", arraysize="*")],
903 [["abc"], ["a"], [None], ["öa"]],
904 b"\x00\0\0\0\x03abc\x00\0\0\0\x01a\x80\0\0\0\x00\0\0\0\0\x02?a"
905 ), (
906 [V.FIELD(datatype="unicodeChar", arraysize="2")],
907 [["\u00e4bc"], ["\u00e4"], [None]],
908 b'\x00\x00\xe4\x00b\x00\x00\xe4\x00\x00\x80\0\0\0\0'
909 ), (
910 #20
911 [V.FIELD(datatype="short", arraysize="3x2")],
912 [[[1,2,3,4,5,6]], [None]],
913 b'\x00\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06'
914 b'\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
915 ), (
916 [V.FIELD(datatype="char", arraysize="*", xtype="adql:TIMESTAMP")],
917 [[None], [datetime.datetime(2010, 10, 12, 23, 10, 0o1)]],
918 b'\x80\0\0\0\0\0\0\0\0\x142010-10-12T23:10:01Z'
919 ),(
920 [V.FIELD(datatype="char", arraysize="*", xtype="timestamp")],
921 [[None], [datetime.datetime(2010, 10, 12, 23, 10, 0o1)]],
922 b'\x80\0\0\0\0\0\0\0\0\x142010-10-12T23:10:01Z'
923 ), (
924 [V.FIELD(datatype="double", arraysize="2", xtype="point")],
925 [[None], [pgsphere.SPoint.fromDegrees(100, 23)],
926 [[1, 2]]],
927 b'\x80\x7f\xf8\x00\x00\x00\x00\x00\x00\x7f\xf8\x00\x00\x00\x00\x00\x00'
928 b'\x00@Y\x00\x00\x00\x00\x00\x00@7\x00\x00\x00\x00\x00\x00'
929 b'\x00?\xf0\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00'
930 ),(
931 [V.FIELD(datatype="double", arraysize="3", xtype="circle")],
932 [[None], [pgsphere.SCircle.fromDALI([100, -23, 0.25])],
933 [[1, 2, 0.5]]],
934 b'\x80\x7f\xf8\x00\x00\x00\x00\x00\x00\x7f\xf8\x00\x00\x00\x00\x00\x00\x7f\xf8\x00\x00\x00\x00\x00\x00'
935 b'\x00@Y\x00\x00\x00\x00\x00\x00\xc07\x00\x00\x00\x00\x00\x00?\xd0\x00\x00\x00\x00\x00\x00'
936 b'\x00?\xf0\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00?\xe0\x00\x00\x00\x00\x00\x00'
937 ), (
938 #25
939 [V.FIELD(datatype="double", arraysize="*", xtype="polygon")],
940 [[None], [pgsphere.SPoly.fromDALI([0, 0, 130, 20, 70, -10])],
941 [[1, 2, 3, 4, 5, 6]]],
942 b'\x80\x00\x00\x00\x00'
943 b'\x00\x00\x00\x00\x06'
944 b'\x00\x00\x00\x00\x00\x00\x00\x00'
945 b'\x00\x00\x00\x00\x00\x00\x00\x00'
946 b'@`@\x00\x00\x00\x00\x00'
947 b'@4\x00\x00\x00\x00\x00\x00'
948 b'@Q\x80\x00\x00\x00\x00\x00'
949 b'\xc0$\x00\x00\x00\x00\x00\x00'
950 b'\x00\x00\x00\x00\x06'
951 b'?\xf0\x00\x00\x00\x00\x00\x00'
952 b'@\x00\x00\x00\x00\x00\x00\x00'
953 b'@\x08\x00\x00\x00\x00\x00\x00'
954 b'@\x10\x00\x00\x00\x00\x00\x00'
955 b'@\x14\x00\x00\x00\x00\x00\x00'
956 b'@\x18\x00\x00\x00\x00\x00\x00'
957 ), (
958 [V.FIELD(datatype="short", arraysize="3x2")],
959 [[[1]], [[1, 2, 3, 4, 5, 6]], [[[2, 3, 4], [5, 6, 7]]],
960 [list(range(1, 23))]],
961 b'\x00'
962 b'\x00\x01\x00\x00\x00\x00'
963 b'\x00\x00\x00\x00\x00\x00'
964 b'\x00'
965 b'\x00\x01\x00\x02\x00\x03'
966 b'\x00\x04\x00\x05\x00\x06'
967 b'\x00'
968 b'\x00\x02\x00\x03\x00\x04'
969 b'\x00\x05\x00\x06\x00\x07'
970 b'\x00'
971 b'\x00\x01\x00\x02\x00\x03'
972 b'\x00\x04\x00\x05\x00\x06'
973 )
974 ]
975
976
977 class Binary2ReadTest(testhelpers.VerboseTest,
978 metaclass=testhelpers.SamplesBasedAutoTest):
979 """tests for deserializing BINARY VOTables.
980 """
981
982 def _runTest(self, sample):
983 fielddefs, stuff, expected = sample
984 table = next(votable.parseBytes(
985 b'<VOTABLE><RESOURCE><TABLE>'+
986 fielddefs.encode("ascii")+
987 b'<DATA><BINARY2><STREAM encoding="base64">'+
988 base64.b64encode(stuff)+
989 b'</STREAM></BINARY2></DATA>'
990 b'</TABLE></RESOURCE></VOTABLE>'))
991 self.assertEqual(list(table), expected)
992
993 samples = [(
994 '<FIELD datatype="boolean"/>',
995 b"\x001\x000\x80?",
996 [[True],[False],[None]],
997 ), (
998 '<FIELD datatype="bit"/>',
999 b"\x00\x01\x00\x00\x00\xff\x80\x00",
1000 [[1],[0],[1], [None]],
1001 ), (
1002 '<FIELD datatype="bit" arraysize="9"/>',
1003 b"\x00\x01\x00\x00\xff\xff\x80\x00\x00",
1004 [[256],[511], [None]],
1005 ), (
1006 '<FIELD datatype="bit" arraysize="*"/>',
1007 b"\x00\x00\x00\x00\x03\xff\x00\x00\x00\x00\x45"
1008 b"\xff\x00\x00\x00\x00\x00\x00\x00\x01\x80\x00\x00\x00\x00",
1009 [[7],[0x1f0000000000000001], [None]],
1010 ), (
1011 '<FIELD datatype="char"><VALUES null="a"/></FIELD>',
1012 b"\x00x\x00\x00\x00a\x80\x00",
1013 [['x'],[''], [None], [None]],
1014 ), (
1015 # 05
1016 '<FIELD datatype="unicodeChar"><VALUES null="&#xbabe;"/></FIELD>',
1017 b"\x00\x00a\x00\x23\x42\x00\xba\xbe\x80\x00\x00",
1018 [['a'],['\u2342'], [None], [None]],
1019 ), (
1020 '<FIELD datatype="unsignedByte"><VALUES null="12"/></FIELD>'
1021 '<FIELD datatype="short"><VALUES null="12"/></FIELD>'
1022 '<FIELD datatype="int"><VALUES null="12"/></FIELD>'
1023 '<FIELD datatype="long"><VALUES null="12"/></FIELD>',
1024 b"\x00\x0c\x00\x0c\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x0c"
1025 b"\xf0\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1026 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
1027 b"\x00\x0d\x0d\x0d\x00\x0d\x0d\x0c\x00\xdd\x00\x00\x00\x00\x00\x0c", [
1028 [None, None, None, None],
1029 [None, None, None, None],
1030 [0, 0, 0, 0],
1031 [13, 3341, 855308, 62205969853054988]]
1032 ), (
1033 '<FIELD datatype="float"/>',
1034 b"\x00\x7f\xc0\x00\x00\x00:\x80\x00\x00\x80\x00\x00\x00\x00",
1035 [[None], [0.0009765625], [None]]
1036 ), (
1037 '<FIELD datatype="double"/>',
1038 b"\x00\x7f\xf8\x00\x00\x00\x00\x00\x00\x00?P\x00\x01\x00\x00\x00\x00",
1039 [[None], [0.00097656343132257462]]
1040 ), (
1041 '<FIELD datatype="doubleComplex"/>',
1042 b"\x00\x7f\xf8\x00\x00\x00\x00\x00\x00?P\x00\x01\x00\x00\x00\x00"
1043 b'\x00@\x04\x00\x00\x00\x00\x00\x00?\xe0\x00\x00\x00\x00\x00\x00'
1044 b'\x80@\x04\x00\x00\x00\x00\x00\x00?\xe0\x00\x00\x00\x00\x00\x00',
1045 [[None], [2.5+0.5j], [None]]
1046 ), (
1047 # 10
1048 '<FIELD datatype="char" arraysize="4"><VALUES null="x"/></FIELD>',
1049 b"\x00abcd\x80\x00\x00\x00\x00",
1050 [["abcd"], [None]]
1051 ), (
1052 '<FIELD datatype="char" arraysize="*"/>',
1053 b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03abc\x80\x00\x00\x00\x00",
1054 [[""], ["abc"], [None]]
1055 ), (
1056 '<FIELD datatype="unicodeChar" arraysize="*"/>',
1057 b"\x00\x00\x00\x00\x03\x00a\x23\x42bc",
1058 [["a\u2342\u6263"]]
1059 ), (
1060 '<FIELD datatype="unicodeChar" arraysize="2"/>',
1061 b"\x00\x00a\x23\x42\x80\x00\x00\x00\x00",
1062 [["a\u2342"], [None]]
1063 ), (
1064 '<FIELD datatype="unsignedByte" arraysize="2"/>',
1065 b'\x00\x00\x01\x80\x00\x00',
1066 [[[0, 1]], [None]],
1067 ), (
1068 # 15
1069 '<FIELD datatype="short" arraysize="*"><VALUES null="16"/></FIELD>',
1070 b'\x00\x00\x00\x00\x03\x00\x01\x00\x10\x00\x02'
1071 b'\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00',
1072 [[[1, None, 2]], [None], [[]]],
1073 ), (
1074 '<FIELD datatype="int" arraysize="2"/>',
1075 b'\x00\x00\x00\x00\x03\x00\x01\x00\x10\x80\0\0\0\0\0\0\0\0',
1076 [[[3, 0x10010]], [None]],
1077 ), (
1078 '<FIELD datatype="float" arraysize="2"/>',
1079 b'\x00\x7f\xc0\x00\x00:\x80\x00\x00',
1080 [[[None, 0.0009765625]]],
1081 ), (
1082 '<FIELD datatype="double" arraysize="*"/>',
1083 b'\x00\x00\x00\x00\x02\x7f\xf8\x00\x00\x00\x00\x00\x00'
1084 b'?P\x00\x01\x00\x00\x00\x00',
1085 [[[None, 0.00097656343132257462]]]
1086 ), (
1087 '<FIELD datatype="float" arraysize="2"><VALUES null="2"/></FIELD>',
1088 b'\x00\x7f\xc0\x00\x00:\x80\x00\x00',
1089 [[[None, 0.0009765625]]],
1090 ), (
1091 # 20
1092 '<FIELD datatype="floatComplex" arraysize="2"/>',
1093 b'\x00\x7f\xc0\x00\x00:\x80\x00\x00'
1094 b'A\x80\x00\x00A\x0c\x00\x00',
1095 [[[None, 16+8.75j]]],
1096 ), (
1097 '<FIELD datatype="short" arraysize="2x3"/>',
1098 b'\x00\x00\x00\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05',
1099 [[[[0,1],[2,3],[4,5]]]],
1100 ), (
1101 '<FIELD datatype="double" arraysize="2" xtype="point"/>',
1102 b'\x80\x7f\xf8\x00\x00\x00\x00\x00\x00\x7f\xf8\x00\x00\x00\x00\x00\x00',
1103 [[None]],
1104 ), (
1105 '<FIELD datatype="double" arraysize="2" xtype="point"/>',
1106 b'\x00@Y\x00\x00\x00\x00\x00\x00@7\x00\x00\x00\x00\x00\x00',
1107 [[pgsphere.SPoint.fromDegrees(100, 23)]],
1108 ), (
1109 '<FIELD datatype="double" arraysize="3" xtype="circle"/>',
1110 b'\x80\x7f\xf8\x00\x00\x00\x00\x00\x00'
1111 b'\x7f\xf8\x00\x00\x00\x00\x00\x00'
1112 b'\x7f\xf8\x00\x00\x00\x00\x00\x00',
1113 [[None]]
1114 ), (
1115 # 25
1116 '<FIELD datatype="double" arraysize="3" xtype="circle"/>',
1117 b'\x00@Y\x00\x00\x00\x00\x00\x00'
1118 b'\xc07\x00\x00\x00\x00\x00\x00'
1119 b'?\xd0\x00\x00\x00\x00\x00\x00',
1120 [[pgsphere.SCircle.fromDALI([100, -23, 0.25])]]
1121 ), (
1122 '<FIELD datatype="double" arraysize="*" xtype="polygon"/>',
1123 b'\x80\x00\x00\x00\x00',
1124 [[None]]
1125 ), (
1126 '<FIELD datatype="double" arraysize="*" xtype="polygon"/>',
1127 b'\x00\x00\x00\x00\x06'
1128 b'\x00\x00\x00\x00\x00\x00\x00\x00'
1129 b'\x00\x00\x00\x00\x00\x00\x00\x00'
1130 b'@`@\x00\x00\x00\x00\x00'
1131 b'@4\x00\x00\x00\x00\x00\x00'
1132 b'@Q\x80\x00\x00\x00\x00\x00'
1133 b'\xc0$\x00\x00\x00\x00\x00\x00',
1134 [[pgsphere.SPoly.fromDALI([0, 0, 130, 20, 70, -10])]]
1135 ), (
1136 '<FIELD datatype="char" arraysize="10"/>',
1137 b"\x00abc\x00tail ",
1138 [["abc"]]),
1139 ]
1140
1141
1142 class Binary2RoundtripTest(testhelpers.VerboseTest,
1143 metaclass=testhelpers.SamplesBasedAutoTest):
1144 """tests for roundtripping values through BINARY2 VOTables.
1145 """
1146
1147 def _runTest(self, sample):
1148 fielddefs, input = sample
1149 vot = V.VOTABLE[V.RESOURCE[votable.DelayedTable(
1150 V.TABLE[fielddefs], input, V.BINARY2)]]
1151 valAfter = list(next(votable.parseBytes(
1152 votable.asBytes(vot))))
1153 self.assertEqual(input, valAfter)
1154
1155 samples = [
1156 ([V.FIELD(datatype="int"), V.FIELD(datatype="float")],
1157 [[5, None], [None, 4.25]]),
1158 ([V.FIELD(datatype="char"), V.FIELD(datatype="char", arraysize="*")],
1159 [['a', None], [None, 'abc']]),
1160 ([V.FIELD(datatype="boolean"), V.FIELD(datatype="doubleComplex")],
1161 [[True, None], [None, 0.25+3j]]),
1162 ([V.FIELD(datatype="int", arraysize="2")],
1163 [[[1,2]], [[-2, -3]], [None]]),
1164 ([V.FIELD(datatype="int", arraysize="2*")],
1165 [[[1,2,3,4]], [[-2, -3, -4]], [None]]),
1166 # 5
1167 ([V.FIELD(datatype="float", arraysize="2x3")],
1168 [[[[1,2],[3,4],[-2,-3]]], [None]]),
1169 ([V.FIELD(datatype="float", arraysize="2x3*")],
1170 [[[[1,2],[3,4],[-2,-3]]], [None]]),
1171 ([V.FIELD(datatype="double", arraysize="2", xtype="point")],
1172 [[pgsphere.SPoint(6, 12)], [None]]),
1173 ([V.FIELD(datatype="char", arraysize="*", xtype="timestamp")],
1174 [[datetime.datetime(2017, 9, 5, 10, 16)], [None]]),
1175 ([V.FIELD(datatype="double", arraysize="4", xtype="x-box")],
1176 [[pgsphere.SBox(
1177 pgsphere.SPoint.fromDegrees(2, -12),
1178 pgsphere.SPoint.fromDegrees(12, 30))], [None]]),
1179 # 10
1180 ([V.FIELD(datatype="char", arraysize="*", xtype="adql:POINT")],
1181 [[pgsphere.SPoint.fromDegrees(2, -12)], [None]]),
1182 # on roundtrip, we'll ASCII-escape any non-ASCII json, so don't
1183 # test for that.
1184 ([V.FIELD(datatype="char", arraysize="*", xtype="json")],
1185 [[r'"\u00e4jsonstring"'], [None]]),
1186 ]
1187
1188
1189 class NDArrayTest(testhelpers.VerboseTest):
1190 def _assertRavels(self, arrayspec, data, expected):
1191 res = votable.unravelArray(arrayspec, data)
1192 self.assertEqual(res, expected)
1193
1194 def testUnravelNull(self):
1195 self._assertRavels("*", list(range(10)), list(range(10)))
1196
1197 def testUnravelPlain(self):
1198 self._assertRavels("3x2", list(range(6)), [[0,1,2],[3,4,5]])
1199
1200 def testUnravelSkewed(self):
1201 self._assertRavels("3x2", list(range(5)), [[0,1,2],[3,4]])
1202
1203 def testUnravelOverlong(self):
1204 self._assertRavels("3x2", list(range(9)), [[0,1,2],[3,4,5],[6,7,8]])
1205
1206 def testUnravelAccpetsStar(self):
1207 self._assertRavels("3x2*", list(range(9)), [[0,1,2],[3,4,5],[6,7,8]])
1208
1209 def testUnravel3d(self):
1210 self._assertRavels("3x2x2", list(range(12)),
1211 [[[0,1,2],[3,4,5]], [[6,7,8],[9,10,11]]])
1212
1213 def assertRoundtrips(self, fieldDef, value, serialization):
1214 vot = V.VOTABLE[V.RESOURCE[votable.DelayedTable(
1215 V.TABLE[[fieldDef]], [[value]], serialization)]]
1216 valAfter = list(next(votable.parseBytes(
1217 votable.asBytes(vot))))
1218 self.assertEqual(value, valAfter[0][0])
1219
1220 def assertRoundtripsBinary(self, fieldDef, value):
1221 self.assertRoundtrips(fieldDef, value, V.BINARY)
1222
1223 def assertRoundtripsBinary2(self, fieldDef, value):
1224 self.assertRoundtrips(fieldDef, value, V.BINARY2)
1225
1226 def assertRoundtripsTabledata(self, fieldDef, value):
1227 self.assertRoundtrips(fieldDef, value, V.TABLEDATA)
1228
1229 def testFloat2DArray(self):
1230 args = (V.FIELD(name="x", datatype="double", arraysize="3x2"),
1231 [[2.25, 1.5, 0.], [1.75, -3.5, 9.]])
1232 self.assertRoundtripsBinary(*args)
1233 self.assertRoundtripsBinary2(*args)
1234 self.assertRoundtripsTabledata(*args)
1235
1236 def testTruncatedFloat2DArray(self):
1237 vot = V.VOTABLE[V.RESOURCE[votable.DelayedTable(
1238 V.TABLE[[V.FIELD(name="x", datatype="double", arraysize="3x2")]],
1239 [[
1240 [[2.25, 1.5, 0.], [1.75, -3.5]] ]], V.TABLEDATA)]]
1241 valAfter = list(next(votable.parseBytes(
1242 votable.asBytes(vot))))
1243 self.assertEqual(valAfter[0][0],
1244 [[2.25, 1.5, 0.], [1.75, -3.5, None]])
1245
1246 def testVarSizeIntArray(self):
1247 args = (V.FIELD(name="x", datatype="int", arraysize="3x2x*"),
1248 [[[2, 1, 0], [1, -3, 9]],
1249 [[3, 0, -1], [0, 4, 8]],
1250 [[1, 3, 7], [23, 1, -8]]]
1251 )
1252 self.assertRoundtripsBinary(*args)
1253 self.assertRoundtripsBinary2(*args)
1254 self.assertRoundtripsTabledata(*args)
1255
1256 def test2DCharArray(self):
1257 args = (V.FIELD(name="x", datatype="char", arraysize="12x*"),
1258 ["012345678901", "123456789012"])
1259 self.assertRoundtripsBinary(*args)
1260 self.assertRoundtripsBinary2(*args)
1261 self.assertRoundtripsTabledata(*args)
1262
1263 def test2DUnicodeArray(self):
1264 args = (V.FIELD(name="x", datatype="unicodeChar", arraysize="12x*"),
1265 ["012345678901", "123456789012"])
1266 self.assertRoundtripsBinary(*args)
1267 self.assertRoundtripsBinary2(*args)
1268 self.assertRoundtripsTabledata(*args)
1269
1270
1271 class VOTableDataValidationTest(testhelpers.VerboseTest):
1272 def testNoSurrogates(self):
1273 vot = V.VOTABLE[V.RESOURCE[votable.DelayedTable(
1274 V.TABLE[V.FIELD(datatype="char", arraysize="*", xtype="json")],
1275 [['"🄁"']], V.TABLEDATA)]]
1276 content = votable.asBytes(vot)
1277 self.assertTrue(b"Will not produce utf-8 surrogates" in content)
1278
1279
1280 class ArraySizeValidationTest(testhelpers.VerboseTest):
1281 def _decode(self, fieldbody, literal):
1282 table = next(votable.parseBytes((
1283 '<VOTABLE><RESOURCE><TABLE>'+
1284 '<FIELD name="x" '+fieldbody+'/>'
1285 '<DATA><TABLEDATA>'+
1286 '<TR><TD>'+literal+'</TD></TR>'
1287 '</TABLEDATA></DATA>'
1288 '</TABLE></RESOURCE></VOTABLE>')))
1289 return list(table)[0][0]
1290
1291 def test1DMissingRaises(self):
1292 self.assertRaisesWithMsg(votable.BadVOTableLiteral,
1293 "Invalid literal for int[2] (field x): '<1 token(s)>'",
1294 self._decode,
1295 ('datatype="int" arraysize="2"', '1'))
1296
1297 def test1DOverflowRaises(self):
1298 self.assertRaisesWithMsg(votable.BadVOTableLiteral,
1299 "Invalid literal for int[2] (field x): '<3 token(s)>'",
1300 self._decode,
1301 ('datatype="int" arraysize="2"', '1 2 3'))
1302
1303 def test1DFlexibleOk(self):
1304 self.assertEqual(self._decode('datatype="int" arraysize="2*"', '1 2 3'),
1305 [1, 2, 3])
1306
1307 def test2DMissingRaises(self):
1308 self.assertRaisesWithMsg(votable.BadVOTableLiteral,
1309 "Invalid literal for float[2x3] (field x): '<5 token(s)>'",
1310 self._decode,
1311 ('datatype="float" arraysize="2x3"', '1 2 3 4 5'))
1312
1313 def test2DMFlexibleRaises(self):
1314 self.assertRaisesWithMsg(votable.BadVOTableLiteral,
1315 "Invalid literal for float[2x*] (field x): '<5 token(s)>'",
1316 self._decode,
1317 ('datatype="float" arraysize="2x*"', '1 2 3 4 5'))
1318
1319 def test2DMFlexibleOk(self):
1320 self.assertEqual(self._decode('datatype="float" arraysize="2x*"',
1321 '1 2 3 4'),
1322 [[1., 2.], [3., 4.]])
1323
1324 def testBadArraysize(self):
1325 self.assertRaisesWithMsg(votable.VOTableError,
1326 "Invalid arraysize '*x2' specified in field or param name 'x'",
1327 self._decode,
1328 ('datatype="float" arraysize="*x2"', '1 2 3 4 5'))
1329
1330 def testMoreBadArraysize(self):
1331 self.assertRaisesWithMsg(votable.VOTableError,
1332 "Invalid arraysize fragment '*2' in field or param name 'x'",
1333 self._decode,
1334 ('datatype="float" arraysize="4x4x*2"', '1 2 3 4 5'))
1335
1336
1337 class WeirdTablesTest(testhelpers.VerboseTest):
1338 """tests with malformed tables and fringe cases.
1339 """
1340 def testEmpty(self):
1341 for data in votable.parseBytes("<VOTABLE/>"):
1342 self.fail("A table is returned for an empty VOTable")
1343
1344 def testEmptySimple(self):
1345 data, metadata = votable.load(io.BytesIO((b"<VOTABLE/>")))
1346 self.assertTrue(data is None)
1347
1348 def testBadStructure(self):
1349 it = votable.parseBytes("<VOTABLE>")
1350 self.assertRaisesWithMsg(votable.VOTableParseError,
1351 "(internal source) no element found: line 1, column 9", list, it)
1352
1353 def testLargeTabledata(self):
1354 # This test is supposed to exercise multi-chunk parsing. So,
1355 # raise the "*20" below when you raise tableparser._StreamData.minChunk
1356 vot = V.VOTABLE[V.RESOURCE[votable.DelayedTable(
1357 V.TABLE[V.FIELD(name="col1", datatype="char", arraysize="*"),],
1358 [["a"*1000]]*20, V.BINARY)]]
1359 dest = io.BytesIO()
1360 votable.write(vot, dest)
1361 dest.seek(0)
1362 data, metadata = votable.load(dest)
1363 self.assertEqual(len(data), 20)
1364 self.assertEqual(len(testhelpers.pickSingle(data[0])), 1000)
1365
1366 def testUnknownAttributesFails(self):
1367 it = votable.parseBytes('<VOTABLE><RESOURCE class="upper"/></VOTABLE>')
1368 self.assertRaisesWithMsg(votable.VOTableParseError,
1369 'At IO:\'<VOTABLE><RESOURCE class="upper"/></VOTABLE>\', (1, 9):'
1370 ' Attribute \'class\' invalid on RESOURCE',
1371 list,
1372 (it,))
1373
1374 def testBadElements(self):
1375 it = votable.parseBytes("<VOTABLE><FOO/></VOTABLE>")
1376 self.assertRaisesWithMsg(votable.VOTableParseError,
1377 "At IO:'<VOTABLE><FOO/></VOTABLE>', (1, 9):"
1378 " Unknown tag: FOO", list, it)
1379
1380 def testIgnoringBadElements(self):
1381 data, metadata = votable.load(io.StringIO(
1382 '<VOTABLE><FOO><bar><PARAM name="quux"/>"'
1383 '</bar></FOO></VOTABLE>'),
1384 raiseOnInvalid=False)
1385 self.assertTrue(data is None)
1386
1387 def testIgnoringBadAttributes(self):
1388 data, metadata = votable.load(io.StringIO(
1389 '<VOTABLE><RESOURCE class="upper"/></VOTABLE>'),
1390 raiseOnInvalid=False)
1391 self.assertTrue(data is None)
1392
1393
1394 class StringArrayTest(testhelpers.VerboseTest):
1395 """tests for the extra-special case of 2+D char arrays.
1396 """
1397 def _get2DTable(self, enc):
1398 return V.VOTABLE[V.RESOURCE[votable.DelayedTable(
1399 V.TABLE[V.FIELD(name="test", datatype="char", arraysize="2x*")],
1400 [[["ab", b"c", "def"]]], enc)]]
1401
1402 def test2dTdencWrite(self):
1403 ser = votable.asBytes(self._get2DTable(V.TABLEDATA))
1404 self.assertTrue(b"<TD>abc de</TD>" in ser)
1405
1406
1407 class UnicodeCharStringsTest(testhelpers.VerboseTest):
1408 # These are about making sure we don't bomb when someone hands us
1409 # unicode strings for char tables.
1410 def _getDataTable(self, enc):
1411 return votable.asBytes(
1412 V.VOTABLE[V.RESOURCE[votable.DelayedTable(
1413 V.TABLE[V.FIELD(name="test", datatype="char", arraysize="*")],
1414 [["\u03b2"]], enc)]])
1415
1416 def testInTD(self):
1417 self.assertTrue(b"<TD>?</TD>" in
1418 self._getDataTable(V.TABLEDATA))
1419
1420 def testInBinary(self):
1421 self.assertTrue(b'STREAM encoding="base64">AAAAAT8'
1422 in self._getDataTable(V.BINARY))
1423
1424
1425 class SimpleInterfaceTest(testhelpers.VerboseTest):
1426 def testIterDict(self):
1427 with open("test_data/importtest.vot") as f:
1428 data, metadata = votable.load(f)
1429 res = list(metadata.iterDicts(data))
1430 self.assertEqual(res[0]["FileName"], "ngc104.dat")
1431 self.assertEqual(res[1]["apex"], None)
1432
1433 def testWrite(self):
1434 with open("test_data/importtest.vot") as f:
1435 data, metadata = votable.load(f)
1436 dest = io.BytesIO()
1437 votable.save(data, metadata.votTable, dest)
1438 content = dest.getvalue()
1439 self.assertTrue(b"QFILtsN2C/ZAGBY4hllK" in content)
1440 self.assertTrue(b'name="n_VHB"' in content)
1441 self.assertTrue(b'Right Ascension (J2000)</DESCRIPTION>' in content)
1442
1443
1444 class RecordArrayTest(testhelpers.VerboseTest):
1445 def testPlain(self):
1446 data, metadata = votable.loads(b"""<VOTABLE>
1447 <RESOURCE><TABLE>
1448 <FIELD name="a" datatype="int"/>
1449 <FIELD name="b" datatype="double"/>
1450 <FIELD name="c" datatype="char" arraysize="4"/>
1451 <DATA><TABLEDATA>
1452 <TR><TD>1</TD><TD>23.25</TD><TD>abcd</TD></TR>
1453 <TR><TD>2</TD><TD>-2e6</TD><TD>x</TD></TR>
1454 </TABLEDATA></DATA></TABLE></RESOURCE></VOTABLE>""")
1455 arr = rec.array([tuple(t) for t in data],
1456 dtype=votable.makeDtype(metadata))
1457 self.assertEqual(tuple(arr[0]), (1, 23.25, b'abcd'))
1458 self.assertEqual(tuple(arr[1]), (2, -2000000.0, b'x'))
1459
1460 def testVarLengthStrings(self):
1461 data, metadata = votable.loads(b"""<VOTABLE>
1462 <RESOURCE><TABLE>
1463 <FIELD name="a" datatype="short"/>
1464 <FIELD name="c" datatype="char" arraysize="*"/>
1465 <DATA><TABLEDATA>
1466 <TR><TD>1</TD><TD>short string</TD></TR>
1467 <TR><TD>2</TD><TD>A long, long string that will have to be trunc...</TD></TR>
1468 </TABLEDATA></DATA></TABLE></RESOURCE></VOTABLE>""")
1469 arr = rec.fromrecords([tuple(t) for t in data],
1470 dtype=votable.makeDtype(metadata))
1471 self.assertEqual(arr[0][1], b'short string')
1472 self.assertEqual(arr[1][1], b'A long, long string ')
1473
1474
1475 class StanXMLTest(testhelpers.VerboseTest):
1476 # make sure VOTable work as normal stanxml trees in a pinch
1477 def testSimple(self):
1478 vot = V.VOTABLE[
1479 V.INFO(name="QUERY_STATUS", value="ERROR")["Nothing: µ-test"]]
1480 ser = vot.render()
1481 self.assertEqual(ser, b'<VOTABLE version="1.5" xmlns="http://www.ivoa.net/xml/VOTable/v1.3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ivoa.net/xml/VOTable/v1.3 http://vo.ari.uni-heidelberg.de/docs/schemata/VOTable.xsd"><INFO name="QUERY_STATUS" value="ERROR">Nothing: \xc2\xb5-test</INFO></VOTABLE>')
1482
1483
1484 class _BOMB(V._VOTElement):
1485 def isEmpty(self):
1486 return False
1487
1488 def write(self, outputFile):
1489 raise base.ReportableError("This element is a VOTable Bomb.")
1490
1491
1492 class StackUnwindingTest(testhelpers.VerboseTest):
1493 # these test that even if there's an ugly error during VOTable
1494 # serialization, some semblance of VOTable results, and also that
1495 # there's an appropriate error message
1496 def testErrorWhileMeta(self):
1497 vot = V.VOTABLE[
1498 V.RESOURCE(type="results")[
1499 V.TABLE[
1500 V.FIELD(name="junk"),],
1501 V.DEFINITIONS[_BOMB()]]]
1502 result = votable.asBytes(vot)
1503 self.assertTrue(b"</INFO></RESOURCE></VOTABLE>" in result)
1504 self.assertTrue(b"content is probably incomplete" in result)
1505 self.assertTrue(b"This element is a VOTable Bomb")
1506
1507 def testErrorWhileTABLEData(self):
1508 vot = V.VOTABLE[
1509 V.RESOURCE(type="results")[
1510 votable.DelayedTable(
1511 V.TABLE[
1512 V.FIELD(name="junk", datatype="float"),],
1513 [[0.2], ["abc"]], V.TABLEDATA)]]
1514 result = votable.asBytes(vot)
1515 self.assertTrue(b"<TD>0.2" in result)
1516 self.assertTrue(b"content is probably incomplete" in result)
1517 self.assertTrue(b"could not convert string to float: 'abc'" in result)
1518 self.assertTrue(b"</TABLEDATA>" in result)
1519 self.assertTrue(b"</RESOURCE></VOTABLE>" in result)
1520
1521 def testErrorWhileBINARY(self):
1522 vot = V.VOTABLE[
1523 V.RESOURCE(type="results")[
1524 votable.DelayedTable(
1525 V.TABLE[
1526 V.FIELD(name="junk", datatype="float"),],
1527 [[0.2], ["abc"]], V.BINARY)]]
1528 result = votable.asBytes(vot)
1529 self.assertTrue(b"PkzMzQ==" in result)
1530 self.assertTrue(b"content is probably incomplete" in result)
1531 self.assertTrue(b"is not a float" in result)
1532 self.assertTrue(b"</BINARY>" in result)
1533 self.assertTrue(b"</RESOURCE></VOTABLE>" in result)
1534
1535
1536 class ParamTypecodeGuessingTest(testhelpers.SimpleSampleComparisonTest):
1537
1538 functionToRun = staticmethod(votable.guessParamAttrsForValue)
1539
1540 samples = [
1541 (15, {"datatype": "short"}),
1542 ("23", {"datatype": "char", "arraysize": "*"}),
1543 ([1, 2], {"datatype": "short", "arraysize": "2"}),
1544 ([[1, 2], [2, 3], [4, 5]], {"datatype": "short", "arraysize": "2x3"}),
1545 (["abc", "defg", "alles Quatsch"], {"datatype": "char",
1546 "arraysize": "13x3"}),
1547 # 05
1548 ([datetime.datetime.now(), datetime.datetime.now()],
1549 {"datatype": "char", "arraysize": "20x2", "xtype": "timestamp"}),
1550 (pgsphere.SPoint.fromDegrees(10, 20),
1551 {'arraysize': '2', 'datatype': 'double precision', 'xtype': 'point'}),
1552 ([pgsphere.SPoint.fromDegrees(10, 20)],
1553 {'datatype': 'double precision', 'arraysize': '2x1', 'xtype': 'point'}),
1554 (base.NotGiven, base.NotFoundError(repr(base.NotGiven),
1555 "VOTable type code for", "paramval.py predefined types")),
1556 (150000, {"datatype": "int"}),
1557 #10
1558 (150000000000, {"datatype": "long"}),
1559 ]
1560
1561
1562 class ParamValueSerializationTest(testhelpers.SimpleSampleComparisonTest):
1563
1564 def functionToRun(self, args):
1565 datatype, arraysize, val = args
1566 param = V.PARAM(name="tmp", datatype=datatype, arraysize=arraysize)
1567 votable.serializeToParam(param, val)
1568 return param.value
1569
1570 samples = [
1571 (("int", None, None), "99"),
1572 (("char", "*", None), ""),
1573 (("char", "4", None), "xxxx"),
1574 (("float", "1", None), "NaN"),
1575 (("double", "2x2", None), "NaN NaN NaN NaN"),
1576 # 5
1577 (("long", "2x2", None), "99 99 99 99"),
1578 (("int", None, 33), "33"),
1579 (("char", "4x*", ["foobar", "wo", "nnnn"]), "foobwo nnnn"),
1580 (("int", "2x3x4", list(range(24))), '0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23'),
1581 (("double", None, 0.25), "0.25"),
1582 # 10
1583 (("double", "2", [0.25, 1]), "0.25 1.0"),
1584 ]
1585
1586 def testDatetimeValueLegacy(self):
1587 param = V.PARAM(name="tmp", datatype="char", arraysize="*",
1588 xtype="adql:TIMESTAMP")
1589 votable.serializeToParam(param,
1590 datetime.datetime(2015, 5, 19, 15, 6, 22, 25))
1591 self.assertEqual(param.value, "2015-05-19T15:06:22Z")
1592
1593 def testDatetimeValue(self):
1594 param = V.PARAM(name="tmp", datatype="char", arraysize="*",
1595 xtype="timestamp")
1596 votable.serializeToParam(param,
1597 datetime.datetime(2015, 5, 19, 15, 6, 22, 25))
1598 self.assertEqual(param.value, "2015-05-19T15:06:22Z")
1599
1600 def testDatetimeArray(self):
1601 param = V.PARAM(name="tmp", datatype="char", arraysize="19x2",
1602 xtype="timestamp")
1603 votable.serializeToParam(param,
1604 [datetime.datetime(2020, 1, 1),datetime.datetime(2021, 1, 1)])
1605 self.assertEqual(param.value, "2020-01-01T00:00:002021-01-01T00:00:00")
1606
1607 def testDatetimeNULL(self):
1608 param = V.PARAM(name="tmp", datatype="char", arraysize="*",
1609 xtype="adql:TIMESTAMP")
1610 votable.serializeToParam(param, None)
1611 self.assertEqual(param.value, "")
1612
1613 def testSPointValue(self):
1614 param = V.PARAM(name="tmp", datatype="char", arraysize="*",
1615 xtype="adql:POINT")
1616 votable.serializeToParam(param,
1617 pgsphere.SPoint.fromDegrees(10, 12))
1618 self.assertEqual(param.value, "Position UNKNOWNFrame 10. 12.")
1619
1620 def testSPointModern(self):
1621 param = V.PARAM(name="tmp", datatype="double", arraysize="2",
1622 xtype="point")
1623 votable.serializeToParam(param,
1624 pgsphere.SPoint.fromDegrees(10, 12))
1625 self.assertEqual(param.value, '10.0 12.0')
1626
1627 def testNullIsEmpty(self):
1628 param = V.PARAM(name="tmp", datatype="char", arraysize="*")
1629 self.assertTrue(b'value=""' in votable.asBytes(param))
1630
1631 def testNullOption(self):
1632 param = V.PARAM(name="tmp", datatype="int", value="2")[
1633 V.VALUES[
1634 V.OPTION(name="empty", value=""),
1635 V.OPTION(name="full", value="2")]]
1636 serialized = votable.asBytes(param)
1637 self.assertTrue(b'value="2"' in serialized)
1638 self.assertTrue(b'value=""' in serialized)
1639
1640 def testTimeIntervalSerialisation(self):
1641 param = V.PARAM(name="tmp", datatype="char", arraysize="19x2",
1642 xtype="timestamp")
1643 votable.serializeToParam(param,
1644 [utils.parseISODT("2013-12-01T10:20:30"),
1645 utils.parseISODT("2013-12-21T11:21:31")])
1646 self.assertEqual(param.value, "2013-12-01T10:20:302013-12-21T11:21:31")
1647
1648 def testTimeIntervalParsing(self):
1649 parser = paramval.getVOTParser("char", "19x2", "timestamp")
1650 self.assertEqual(
1651 parser("2013-12-01T10:20:302013-12-21T11:21:31"),
1652 [utils.parseISODT("2013-12-01T10:20:30"),
1653 utils.parseISODT("2013-12-21T11:21:31")])
1654
1655
1656 if __name__=="__main__":
1657 testhelpers.main(BinaryReadTest)