/usr/lib/python3/dist-packages/pydap/tests/test_responses_dods.py is in python3-pydap 3.2.2+ds1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | """Test the DODS response."""
import copy
import numpy as np
from webtest import TestApp as App
from webob.headers import ResponseHeaders
from collections import OrderedDict
from pydap.lib import START_OF_SEQUENCE, END_OF_SEQUENCE, __version__
from pydap.handlers.lib import BaseHandler
from pydap.tests.datasets import (
VerySimpleSequence, SimpleSequence, SimpleGrid,
SimpleArray, NestedSequence, SimpleStructure)
from pydap.responses.dods import dods, DODSResponse
import unittest
class TestDODSResponse(unittest.TestCase):
"""Test the DODS response."""
def setUp(self):
"""Create a simple WSGI app."""
app = App(BaseHandler(VerySimpleSequence))
self.res = app.get('/.dods')
def test_dispatcher(self):
"""Test that the dispatcher works on any object."""
with self.assertRaises(StopIteration):
dods(None)
def test_status(self):
"""Test the status code."""
self.assertEqual(self.res.status, "200 OK")
def test_content_type(self):
"""Test the content type."""
self.assertEqual(self.res.content_type, "application/octet-stream")
def test_charset(self):
"""Test the charset."""
self.assertEqual(self.res.charset, None)
def test_headers(self):
"""Test the headers in the response."""
self.assertEqual(
self.res.headers,
ResponseHeaders([
('XDODS-Server', 'pydap/' + __version__),
('Content-description', 'dods_data'),
('Content-type', 'application/octet-stream'),
('Access-Control-Allow-Origin', '*'),
('Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type'),
('Content-Length', '238')
]))
def test_body(self):
"""Test response body."""
dds, xdrdata = self.res.body.split(b'\nData:\n', 1)
dds = dds.decode('ascii')
self.assertEqual(dds, """Dataset {
Sequence {
Byte byte;
Int32 int;
Float32 float;
} sequence;
} VerySimpleSequence;""")
self.assertEqual(
xdrdata,
START_OF_SEQUENCE +
b"\x00" b"\x00\x00\x00\x01" b"A \x00\x00" +
START_OF_SEQUENCE +
b"\x01" b"\x00\x00\x00\x02" b"A\xa0\x00\x00" +
START_OF_SEQUENCE +
b"\x02" b"\x00\x00\x00\x03" b"A\xf0\x00\x00" +
START_OF_SEQUENCE +
b"\x03" b"\x00\x00\x00\x04" b"B \x00\x00" +
START_OF_SEQUENCE +
b"\x04" b"\x00\x00\x00\x05" b"BH\x00\x00" +
START_OF_SEQUENCE +
b"\x05" b"\x00\x00\x00\x06" b"Bp\x00\x00" +
START_OF_SEQUENCE +
b"\x06" b"\x00\x00\x00\x07" b"B\x8c\x00\x00" +
START_OF_SEQUENCE +
b"\x07" b"\x00\x00\x00\x08" b"B\xa0\x00\x00" +
END_OF_SEQUENCE)
class TestDODSResponseGrid(unittest.TestCase):
"""Test the DODS response with a grid."""
def setUp(self):
"""Create a simple WSGI app."""
app = App(BaseHandler(SimpleGrid))
self.res = app.get('/.dods')
def test_body(self):
"""Test the response body."""
dds, xdrdata = self.res.body.split(b'\nData:\n', 1)
dds = dds.decode('ascii')
self.assertEqual(dds, """Dataset {
Grid {
Array:
Int32 SimpleGrid[y = 2][x = 3];
Maps:
Int32 x[x = 3];
Int32 y[y = 2];
} SimpleGrid;
Int32 x[x = 3];
Int32 y[y = 2];
} SimpleGrid;""")
self.assertEqual(
xdrdata,
b"\x00\x00\x00\x06" # length
b"\x00\x00\x00\x06" # length, again
b"\x00\x00\x00\x00"
b"\x00\x00\x00\x01"
b"\x00\x00\x00\x02"
b"\x00\x00\x00\x03"
b"\x00\x00\x00\x04"
b"\x00\x00\x00\x05"
b"\x00\x00\x00\x03"
b"\x00\x00\x00\x03"
b"\x00\x00\x00\x00"
b"\x00\x00\x00\x01"
b"\x00\x00\x00\x02"
b"\x00\x00\x00\x02"
b"\x00\x00\x00\x02"
b"\x00\x00\x00\x00"
b"\x00\x00\x00\x01"
b"\x00\x00\x00\x03"
b"\x00\x00\x00\x03"
b"\x00\x00\x00\x00"
b"\x00\x00\x00\x01"
b"\x00\x00\x00\x02"
b"\x00\x00\x00\x02"
b"\x00\x00\x00\x02"
b"\x00\x00\x00\x00"
b"\x00\x00\x00\x01")
class TestDODSResponseStructure(unittest.TestCase):
"""Test the DODS response with a sequence that has a string."""
def setUp(self):
"""Create a simple WSGI app."""
app = App(BaseHandler(SimpleStructure))
self.res = app.get('/.dods')
def test_body(self):
"""Test response body."""
dds, xdrdata = self.res.body.split(b'\nData:\n', 1)
dds = dds.decode('ascii')
self.assertEqual(dds, """Dataset {
Structure {
Int16 b;
Byte ub;
Int32 i32;
UInt32 ui32;
Int16 i16;
UInt16 ui16;
Float32 f32;
Float64 f64;
String s;
String u;
String U;
} types;
} SimpleStructure;""")
expected = OrderedDict([
# -10 signed byte upconv. to Int32 for transfer
('b', (np.array(-10, dtype=np.byte)
.astype('>i')
.tostring())),
# 10 usigned byte padded to multiple of 4 bytes for transfer
('ub', (np.array(10, dtype=np.ubyte)
.tostring()) + b'\x00\x00\x00'),
('i32', (np.array(-10, dtype=np.int32)
.astype('>i').tostring())),
('ui32', (np.array(10, dtype=np.uint32)
.astype('>I').tostring())),
# -10 int16 upconv. to int32 for transfer
('i16', (np.array(-10, dtype=np.int16)
.astype('>i')
.newbyteorder('>').tostring())),
# 10 uint16 upconv. to uint32 for transfer
('ui16', (np.array(10, dtype=np.uint16)
.astype('>I')
.tostring())),
('f32', (np.array(100, dtype=np.float32)
.astype('>f')
.tostring())),
('f64', (np.array(1000, dtype=np.float64)
.astype('>d')
.tostring())),
('s', b'\x00\x00\x00$This is a data test string (pass 0).\x00'),
('u', b'\x00\x00\x13http://www.dods.org\x00'),
('U', b'\x00\x00\x00\x0ctest unicode')])
for key in expected:
string = expected[key]
assert xdrdata.startswith(string)
xdrdata = xdrdata[len(string):]
assert xdrdata == b''
class TestDODSResponseSequence(unittest.TestCase):
"""Test the DODS response with a sequence that has a string."""
def setUp(self):
"""Create a simple WSGI app."""
app = App(BaseHandler(SimpleSequence))
self.res = app.get('/.dods?cast.id,cast.lon')
def test_body(self):
"""Test response body."""
dds, xdrdata = self.res.body.split(b'\nData:\n', 1)
dds = dds.decode('ascii')
self.assertEqual(dds, """Dataset {
Sequence {
String id;
Int32 lon;
} cast;
} SimpleSequence;""")
self.assertEqual(
xdrdata,
START_OF_SEQUENCE +
b"\x00\x00\x00\x01" # length of the string (=1)
b"1\x00\x00\x00" # string zero-paddded to 4 bytes
b"\x00\x00\x00d" + # lon
START_OF_SEQUENCE +
b"\x00\x00\x00\x01"
b"2\x00\x00\x00"
b"\x00\x00\x00\xc8" +
END_OF_SEQUENCE)
class TestDODSResponseArray(unittest.TestCase):
"""Test the DODS response with arrays of bytes and strings."""
def setUp(self):
"""Create a simple WSGI app."""
self.app = App(BaseHandler(SimpleArray))
def test_body(self):
"""Test response body."""
res = self.app.get("/.dods")
dds, xdrdata = res.body.split(b'\nData:\n', 1)
dds = dds.decode('ascii')
self.assertEqual(dds, """Dataset {
Byte byte[byte = 5];
String string[string = 2];
Int16 short;
} SimpleArray;""")
self.assertEqual(
xdrdata,
b"\x00\x00\x00\x05" # length of the byte array
b"\x00\x00\x00\x05" # length, again
b"\x00"
b"\x01"
b"\x02"
b"\x03"
b"\x04"
b"\x00\x00\x00" # zero-padding to 4 bytes
b"\x00\x00\x00\x02" # length of the array, only once (string)
b"\x00\x00\x00\x03" # length of the first string
b"one\x00" # string padded to 4 bytes
b"\x00\x00\x00\x03"
b"two\x00"
b"\x00\x00\x00\x01") # shorts are encoded in 4 bytes
def test_calculate_size_short(self):
"""Test that size is calculated correctly with shorts."""
res = self.app.get("/.dods?short")
self.assertEqual(res.headers["content-length"], "52")
class TestDODSResponseArrayterator(unittest.TestCase):
"""Test the DODS response when encountering an Arrayterator.
In several cases the DODS response will work on an Arrayterator, which is
a wrapper over arrays that allows iteration over them in blocks, instead of
reading everything into memory.
"""
def test_grid(self):
"""Test a grid."""
modified = copy.copy(SimpleGrid)
modified.SimpleGrid.SimpleGrid.data = np.lib.arrayterator.Arrayterator(
modified.SimpleGrid.SimpleGrid.data, 2)
app = App(BaseHandler(modified))
res = app.get("/.dods?SimpleGrid.SimpleGrid")
header_string = b"""Dataset {
Structure {
Int32 SimpleGrid[y = 2][x = 3];
} SimpleGrid;
} SimpleGrid;
Data:
"""
self.assertEqual(res.body, (header_string +
b"\x00\x00\x00\x06"
b"\x00\x00\x00\x06"
b"\x00\x00\x00\x00"
b"\x00\x00\x00\x01"
b"\x00\x00\x00\x02"
b"\x00\x00\x00\x03"
b"\x00\x00\x00\x04"
b"\x00\x00\x00\x05"))
class TestDODSResponseNestedSequence(unittest.TestCase):
"""Test the DODS response with nested sequences."""
def test_iteration(self):
"""Test direct iteration over data."""
response = DODSResponse(NestedSequence)
output = b"".join(response)
self.assertEqual(
output, b"""Dataset {
Sequence {
Int32 lat;
Int32 lon;
Int32 elev;
Sequence {
Int32 time;
Int32 slp;
Int32 wind;
} time_series;
} location;
} NestedSequence;
Data:
""" +
START_OF_SEQUENCE +
b"\x00\x00\x00\x01"
b"\x00\x00\x00\x01"
b"\x00\x00\x00\x01" +
START_OF_SEQUENCE +
b"\x00\x00\x00\n"
b"\x00\x00\x00\x0b"
b"\x00\x00\x00\x0c" +
START_OF_SEQUENCE +
b"\x00\x00\x00\x15"
b"\x00\x00\x00\x16"
b"\x00\x00\x00\x17" +
END_OF_SEQUENCE +
START_OF_SEQUENCE +
b"\x00\x00\x00\x02"
b"\x00\x00\x00\x04"
b"\x00\x00\x00\x04" +
START_OF_SEQUENCE +
b"\x00\x00\x00\x0f"
b"\x00\x00\x00\x10"
b"\x00\x00\x00\x11" +
END_OF_SEQUENCE +
START_OF_SEQUENCE +
b"\x00\x00\x00\x03"
b"\x00\x00\x00\x06"
b"\x00\x00\x00\t" +
START_OF_SEQUENCE +
b"\x00\x00\x00\x04"
b"\x00\x00\x00\x08"
b"\x00\x00\x00\x10" +
START_OF_SEQUENCE +
b"\x00\x00\x00\x1f"
b"\x00\x00\x00 "
b"\x00\x00\x00!" +
START_OF_SEQUENCE +
b"\x00\x00\x00)"
b"\x00\x00\x00*"
b"\x00\x00\x00+" +
START_OF_SEQUENCE +
b"\x00\x00\x003"
b"\x00\x00\x004"
b"\x00\x00\x005" +
START_OF_SEQUENCE +
b"\x00\x00\x00="
b"\x00\x00\x00>"
b"\x00\x00\x00?" +
END_OF_SEQUENCE +
END_OF_SEQUENCE)
def test_body(self):
"""Test response body."""
app = App(BaseHandler(NestedSequence))
res = app.get("/.dods")
dds, xdrdata = res.body.split(b'\nData:\n', 1)
dds = dds.decode('ascii')
self.assertEqual(dds, """Dataset {
Sequence {
Int32 lat;
Int32 lon;
Int32 elev;
Sequence {
Int32 time;
Int32 slp;
Int32 wind;
} time_series;
} location;
} NestedSequence;""")
self.assertEqual(
xdrdata,
START_OF_SEQUENCE +
b"\x00\x00\x00\x01"
b"\x00\x00\x00\x01"
b"\x00\x00\x00\x01" +
START_OF_SEQUENCE +
b"\x00\x00\x00\n"
b"\x00\x00\x00\x0b"
b"\x00\x00\x00\x0c" +
START_OF_SEQUENCE +
b"\x00\x00\x00\x15"
b"\x00\x00\x00\x16"
b"\x00\x00\x00\x17" +
END_OF_SEQUENCE +
START_OF_SEQUENCE +
b"\x00\x00\x00\x02"
b"\x00\x00\x00\x04"
b"\x00\x00\x00\x04" +
START_OF_SEQUENCE +
b"\x00\x00\x00\x0f"
b"\x00\x00\x00\x10"
b"\x00\x00\x00\x11" +
END_OF_SEQUENCE +
START_OF_SEQUENCE +
b"\x00\x00\x00\x03"
b"\x00\x00\x00\x06"
b"\x00\x00\x00\t" +
START_OF_SEQUENCE +
b"\x00\x00\x00\x04"
b"\x00\x00\x00\x08"
b"\x00\x00\x00\x10" +
START_OF_SEQUENCE +
b"\x00\x00\x00\x1f"
b"\x00\x00\x00 "
b"\x00\x00\x00!" +
START_OF_SEQUENCE +
b"\x00\x00\x00)"
b"\x00\x00\x00*"
b"\x00\x00\x00+" +
START_OF_SEQUENCE +
b"\x00\x00\x003"
b"\x00\x00\x004"
b"\x00\x00\x005" +
START_OF_SEQUENCE +
b"\x00\x00\x00="
b"\x00\x00\x00>"
b"\x00\x00\x00?" +
END_OF_SEQUENCE +
END_OF_SEQUENCE)
|