/usr/lib/python3/dist-packages/pydap/tests/test_handlers_lib.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 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | """Test basic handler functions."""
import copy
from six import text_type
from webtest import AppError
from webtest import TestApp as App
import numpy as np
from pydap.model import BaseType, StructureType, SequenceType
from pydap.lib import walk
from pydap.exceptions import ConstraintExpressionError
from pydap.handlers.lib import (
load_handlers, get_handler, BaseHandler, ExtensionNotSupportedError,
apply_selection, apply_projection, ConstraintExpression,
IterData)
from pydap.parsers import parse_projection
from pydap.tests.datasets import (
SimpleArray, SimpleSequence, SimpleGrid, VerySimpleSequence,
NestedSequence, SimpleStructure)
import unittest
class TestHandlersLib(unittest.TestCase):
"""Test handler loading."""
def test_load_handlers(self):
"""Test that handlers can be loaded correctly.
We use a mock working set, since by default no handlers are installed
with Pydap.
"""
handlers = load_handlers(MockWorkingSet())
self.assertTrue(MockHandler in handlers)
def test_get_handler(self):
"""Test that we can load a specific handler."""
handlers = load_handlers(MockWorkingSet())
handler = get_handler("file.foo", handlers)
self.assertIsInstance(handler, MockHandler)
def test_no_handler_available(self):
"""Test exception raised when file not supported."""
with self.assertRaises(ExtensionNotSupportedError):
get_handler("file.bar")
class TestBaseHandler(unittest.TestCase):
"""Test the base handler as a WSGI app."""
def setUp(self):
"""Create a basic WSGI app."""
self.app = App(MockHandler(SimpleArray))
def test_unconstrained_das(self):
"""DAS responses are always unconstrained."""
res = self.app.get("/.dds")
self.assertEqual(res.text, """Dataset {
Byte byte[byte = 5];
String string[string = 2];
Int16 short;
} SimpleArray;
""")
res = self.app.get("/.dds?byte")
self.assertEqual(res.text, """Dataset {
Byte byte[byte = 5];
} SimpleArray;
""")
res = self.app.get("/.das")
das = res.text
self.assertEqual(das, """Attributes {
byte {
}
string {
}
short {
}
}
""")
# check that DAS is unmodifed with constraint expression
res = self.app.get("/.das?byte")
self.assertEqual(res.text, das)
def test_exception(self):
"""
Test exception handling.
By default Pydap will capture all exceptions and return a formatted
error response.
"""
with self.assertRaises(AppError):
self.app.get("/.foo")
def test_exception_non_captured(self):
"""Test exception handling when not captured."""
app = App(MockHandler(SimpleArray), extra_environ={
"x-wsgiorg.throw_errors": True})
with self.assertRaises(KeyError):
app.get("/.foo")
def test_missing_dataset(self):
"""Test exception when dataset is not set."""
app = App(MockHandler(), extra_environ={
"x-wsgiorg.throw_errors": True})
with self.assertRaises(NotImplementedError):
app.get("/.dds")
class TestApplySelection(unittest.TestCase):
"""Test function that applies selections to the dataset."""
def setUp(self):
"""Build our own sequence.
Pydap uses lightweight copies of objects that share data. This breaks
unit tests since the same objects are reused for tests.
"""
# make a dataset with its own data
self.dataset = copy.copy(SimpleSequence)
self.dataset.cast.data = SimpleSequence.cast.data.copy()
def test_no_selection(self):
"""Test no selection in the query string."""
dataset = apply_selection("", self.dataset)
np.testing.assert_array_equal(
dataset.cast.data, self.dataset.cast.data)
def test_simple_selection(self):
"""Test a simple selection applied to the dataset."""
dataset = apply_selection(["cast.lon=100"], self.dataset)
np.testing.assert_array_equal(
dataset.cast.data,
self.dataset.cast.data[self.dataset.cast.data["lon"] == 100])
def test_multiple_selections(self):
"""Test multiple selections applied to dataset."""
dataset = apply_selection(
["cast.lon=100", "cast.lat>0"], self.dataset)
np.testing.assert_array_equal(
dataset.cast.data,
self.dataset.cast.data[
self.dataset.cast.data["lon"] == 100
][
self.dataset.cast.data["lat"] > 0
])
class TestApplyProjectionGrid(unittest.TestCase):
"""Test applying projections on a dataset with a grid."""
def setUp(self):
"""Build dataset with no shared data."""
self.dataset = copy.copy(SimpleGrid)
for var in walk(self.dataset, BaseType):
var.data = var.data.copy()
def test_no_projection(self):
"""Test no projections."""
dataset = apply_projection("", self.dataset)
self.assertEqual(list(dataset.children()), [])
def test_simple_projection(self):
"""Test simple projections."""
dataset = apply_projection(parse_projection("x"), self.dataset)
self.assertEqual(list(dataset.keys()), ["x"])
def test_simple_projection_with_index(self):
"""Test simple projections."""
dataset = apply_projection(parse_projection("x[1]"), self.dataset)
np.testing.assert_array_equal(
dataset.x.data, [1])
def test_array(self):
"""Test that the grid degenerates into a structure."""
dataset = apply_projection(
parse_projection("SimpleGrid.SimpleGrid"), self.dataset)
self.assertIsInstance(dataset.SimpleGrid, StructureType)
def test_array_slice(self):
"""Test slices applied to a grid."""
dataset = apply_projection(
parse_projection("SimpleGrid[1]"), self.dataset)
np.testing.assert_array_equal(
dataset.SimpleGrid.x.data, self.dataset.SimpleGrid[1].x.data)
np.testing.assert_array_equal(
dataset.SimpleGrid.y.data, self.dataset.SimpleGrid[1].y.data)
np.testing.assert_array_equal(
dataset.SimpleGrid.SimpleGrid.data,
self.dataset.SimpleGrid[1:2].SimpleGrid.data)
class TestApplyProjectionSequence(unittest.TestCase):
"""Test applying projections on a dataset with a sequence."""
def setUp(self):
"""Build dataset with no shared data."""
self.dataset = copy.copy(VerySimpleSequence)
self.dataset.sequence.data = VerySimpleSequence.sequence.data.copy()
def test_sequence_projection(self):
"""Test projection slicing on sequences."""
dataset = apply_projection(
parse_projection("sequence[2]"), self.dataset)
np.testing.assert_array_equal(
dataset.sequence.data, VerySimpleSequence.sequence.data[2])
class TestInvalidProjection(unittest.TestCase):
"""Test applying a projection to a structure object."""
def test_structure_projection(self):
"""Test projection slicing on a structure."""
with self.assertRaises(ConstraintExpressionError):
apply_projection(parse_projection("types[0]"), SimpleStructure)
class TestConstraintExpression(unittest.TestCase):
"""Test the constraint expression object."""
def test_str(self):
"""Test string representation."""
ce = ConstraintExpression("a>1")
self.assertEqual(str(ce), "a>1")
def test_unicode(self):
"""Test unicode representation."""
ce = ConstraintExpression("a>1")
self.assertEqual(text_type(ce), "a>1")
def test_and(self):
"""Test CE addition."""
ce1 = ConstraintExpression("a>1")
ce2 = ConstraintExpression("b>0")
ce3 = ce1 & ce2
self.assertEqual(str(ce3), "a>1&b>0")
def test_or(self):
"""Expressions cannot be ORed."""
ce1 = ConstraintExpression("a>1")
ce2 = ConstraintExpression("b>0")
with self.assertRaises(ConstraintExpressionError):
ce1 | ce2
class TestIterData(unittest.TestCase):
"""
Test the ``IterData`` class, used to store flat/nested sequence data.
A flat ``IterData`` should behave like a Numpy structured array, except
all operations are stored to be lazily evaluated when the object is
iterated over.
"""
def setUp(self):
"""Create a flat IterData."""
template = SequenceType("a")
template["b"] = BaseType("b")
template["c"] = BaseType("c")
template["d"] = BaseType("d")
self.data = IterData([(1, 2, 3), (4, 5, 6)], template)
self.array = np.array(np.rec.fromrecords([
(1, 2, 3),
(4, 5, 6),
], names=["b", "c", "d"]))
def assertIteratorEqual(self, it1, it2):
self.assertEqual(list(it1), list(it2))
def test_repr(self):
"""Test the object representation."""
self.assertEqual(
repr(self.data),
"<IterData to stream [(1, 2, 3), (4, 5, 6)]>")
def test_dtype(self):
"""Test the ``dtype`` property."""
self.assertEqual(self.data["b"].dtype, self.array["b"].dtype)
def test_iteration(self):
"""Test iteration over data."""
self.assertIteratorEqual(map(tuple, self.data), map(tuple, self.array))
self.assertIteratorEqual(self.data["b"], self.array["b"])
def test_filter(self):
"""Test filtering the object."""
self.assertIteratorEqual(
map(tuple, self.data[self.data["b"] == 1]),
map(tuple, self.array[self.array["b"] == 1]))
self.assertIteratorEqual(
map(tuple, self.data[self.data["b"] != 1]),
map(tuple, self.array[self.array["b"] != 1]))
self.assertIteratorEqual(
map(tuple, self.data[self.data["b"] >= 1]),
map(tuple, self.array[self.array["b"] >= 1]))
self.assertIteratorEqual(
map(tuple, self.data[self.data["b"] <= 1]),
map(tuple, self.array[self.array["b"] <= 1]))
self.assertIteratorEqual(
map(tuple, self.data[self.data["b"] > 1]),
map(tuple, self.array[self.array["b"] > 1]))
self.assertIteratorEqual(
map(tuple, self.data[self.data["b"] < 1]),
map(tuple, self.array[self.array["b"] < 1]))
def test_slice(self):
"""Test slicing the object."""
self.assertIteratorEqual(map(tuple, self.data[1:]),
map(tuple, self.array[1:]))
def test_integer_slice(self):
"""Test slicing with an integer.
Note that the behavior here is different from Numpy arrays, since the
data access to ``IterData`` is through iteration it has no direct index
access.
"""
self.assertIteratorEqual(self.data[0:1], self.array[0:1].tolist())
self.assertIteratorEqual(self.data[0], self.array[0:1].tolist())
def test_invalid_child(self):
"""Test accessing a non-existing child."""
with self.assertRaises(KeyError):
self.data["e"]
def test_invalid_key(self):
"""Test accessing using an invalid key."""
with self.assertRaises(KeyError):
self.data[(1, 2)]
def test_selecting_children(self):
"""Test that we can select children."""
self.assertIteratorEqual(
map(tuple, self.data[["d", "b"]]),
map(tuple, self.array[["d", "b"]]))
def test_invalid_selection(self):
"""Test invalid selections.
In theory this should never happen, since ``ConstraintExpression``
object are constructly directly from existing children.
"""
with self.assertRaises(ConstraintExpressionError):
self.data[ConstraintExpression("a.e<1")]
with self.assertRaises(ConstraintExpressionError):
self.data[ConstraintExpression("a.d<foo")]
def test_intercomparison_selection(self):
"""Test comparing children in the selection."""
self.assertIteratorEqual(
map(tuple, self.data[self.data["b"] == self.data["c"]]),
map(tuple, self.array[self.array["b"] == self.array["c"]]))
self.assertIteratorEqual(
map(tuple, self.data[self.data["b"] != self.data["c"]]),
map(tuple, self.array[self.array["b"] != self.array["c"]]))
self.assertIteratorEqual(
map(tuple, self.data[self.data["b"] >= self.data["c"]]),
map(tuple, self.array[self.array["b"] >= self.array["c"]]))
self.assertIteratorEqual(
map(tuple, self.data[self.data["b"] <= self.data["c"]]),
map(tuple, self.array[self.array["b"] <= self.array["c"]]))
self.assertIteratorEqual(
map(tuple, self.data[self.data["b"] > self.data["c"]]),
map(tuple, self.array[self.array["b"] > self.array["c"]]))
self.assertIteratorEqual(
map(tuple, self.data[self.data["b"] < self.data["c"]]),
map(tuple, self.array[self.array["b"] < self.array["c"]]))
def test_selection_not_in_projection(self):
"""Test selection with variables that are not in the projection."""
self.data[["d", "b"]]
filtered = self.data[["d", "b"]][self.data["c"] > 3]
self.assertEqual(filtered, self.array[["d", "b"]][self.array["c"] > 3])
class TestRegexp(unittest.TestCase):
"""Test regular expression match."""
def test_regexp(self):
sequence = SequenceType("sequence")
sequence["name"] = BaseType("name")
sequence.data = IterData([
("John", "Paul", "George", "Ringo"),
], sequence)
filtered = sequence[ConstraintExpression('sequence.name=~"J.*"')]
self.assertEqual(list(filtered.iterdata()), [("John",)])
class TestNestedIterData(unittest.TestCase):
"""Test ``IterData`` with nested data."""
def setUp(self):
"""Load data from test dataset."""
self.data = NestedSequence.location.data
def test_iteration(self):
"""Test basic iteration."""
self.assertEqual(list(self.data),
[(1, 1, 1, [(10, 11, 12), (21, 22, 23)]),
(2, 4, 4, [(15, 16, 17)]),
(3, 6, 9, []),
(4, 8, 16, [(31, 32, 33), (41, 42, 43),
(51, 52, 53), (61, 62, 63)])])
def test_children_data(self):
"""Test getting data from a simple child."""
self.assertEqual(list(self.data["lat"]), [1, 2, 3, 4])
def test_sequence_children_data(self):
"""Test getting data from a sequence child."""
self.assertEqual(list(self.data["time_series"]),
[[(10, 11, 12), (21, 22, 23)],
[(15, 16, 17)],
[],
[(31, 32, 33), (41, 42, 43),
(51, 52, 53), (61, 62, 63)]])
def test_deep_children_data(self):
"""Test getting data from a sequence child."""
self.assertEqual(list(self.data["time_series"]["time"]),
[[10, 21], [15], [], [31, 41, 51, 61]])
def test_selecting_children(self):
"""Test that we can select children."""
self.assertEqual(list(self.data[["time_series", "elev"]]),
[([(10, 11, 12), (21, 22, 23)], 1),
([(15, 16, 17)], 4),
([], 9),
([(31, 32, 33), (41, 42, 43),
(51, 52, 53), (61, 62, 63)], 16)])
def test_slice(self):
"""Test slicing the object."""
self.assertEqual(list(self.data[1::2]),
[(2, 4, 4, [(15, 16, 17)]),
(4, 8, 16, [(31, 32, 33), (41, 42, 43),
(51, 52, 53), (61, 62, 63)])])
def test_children_data_from_slice(self):
"""Test getting children data from a sliced sequence."""
self.assertEqual(list(self.data[1::2]["lat"]), [2, 4])
def test_sequence_children_data_from_slice(self):
"""Test getting children data from a sliced sequence."""
self.assertEqual(list(self.data[1::2]["time_series"]),
[[(15, 16, 17)], [(31, 32, 33), (41, 42, 43),
(51, 52, 53), (61, 62, 63)]])
def test_deep_slice(self):
"""Test slicing the inner sequence."""
self.assertEqual(list(self.data["time_series"][::2]),
[[(10, 11, 12), (21, 22, 23)], []])
def test_integer_slice(self):
"""Test slicing with an integer."""
self.assertEqual(list(self.data["time_series"][1]), [[(15, 16, 17)]])
def test_filter_data(self):
"""Test filtering the data."""
self.assertEqual(list(self.data[self.data["lat"] > 2]),
[(3, 6, 9, []), (4, 8, 16,
[(31, 32, 33), (41, 42, 43),
(51, 52, 53), (61, 62, 63)])])
def test_deep_filter(self):
"""Test deep filtering the data."""
self.assertEqual(list(self.data[self.data["time_series"]["slp"] > 11]),
[(1, 1, 1, [(21, 22, 23)]),
(2, 4, 4, [(15, 16, 17)]),
(3, 6, 9, []),
(4, 8, 16, [(31, 32, 33), (41, 42, 43),
(51, 52, 53), (61, 62, 63)])])
class MockWorkingSet(object):
"""A fake working set for testing handlers."""
def iter_entry_points(self, group):
"""Return a mock entry point."""
yield MockEntryPoint(MockHandler)
class MockHandler(BaseHandler):
"""A fake handler for testing."""
extensions = r"^.*\.foo$"
def __init__(self, dataset=None):
BaseHandler.__init__(self, dataset)
self.additional_headers = [
("X-debug", "True")
]
class MockEntryPoint(object):
"""A fake entry point for testing."""
def __init__(self, handler):
self.handler = handler
self.name = 'test'
self.module_name = 'pydap.handlers.test'
self.attrs = ('TestHandler', )
def load(self):
"""Return the wrapped handler."""
return self.handler
|