/usr/lib/python3/dist-packages/pydap/tests/test_wsgi_ssf.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 | """Tests for the server-side function WSGI middleware."""
import sys
from six import next
from webtest import TestApp as App
import numpy as np
from pydap.model import SequenceType, BaseType
from pydap.handlers.lib import BaseHandler
from pydap.tests.datasets import SimpleSequence, SimpleGrid
from pydap.wsgi.ssf import ServerSideFunctions
from pydap.exceptions import ServerError
import unittest
class TestMiddleware(unittest.TestCase):
"""Tests for the server-side function middleware."""
def test_das(self):
"""Test that DAS requests are ignored."""
# create a simple app
app = App(ServerSideFunctions(BaseHandler(SimpleSequence)))
# test a DAS response with a non-existing function
app.get("/.das?non_existing_function(sequence)")
# now test a DDS response
with self.assertRaises(KeyError):
app.get("/.dds?non_existing_function(sequence)")
def test_no_parsed_response(self):
"""Test that non-parsed responses work or raise error.
Pydap returns WSGI responses that contain the "parsed" dataset, so it
can be manipulated by middleware.
"""
app = App(
ServerSideFunctions(Accumulator(BaseHandler(SimpleGrid))))
# a normal request should work, even though server-side functions are
# not working in the WSGI pipeline
app.get("/.dds")
# this will fail, since it's impossible to build the response
with self.assertRaises(ServerError):
app.get("/.dds?mean(x)")
def test_selection(self):
"""Test a selection server-side function."""
app = App(ServerSideFunctions(BaseHandler(SimpleSequence)))
res = app.get(
"/.asc?density(cast.salinity,cast.temperature,cast.pressure)>1025")
self.assertEqual(res.text, """Dataset {
Sequence {
String id;
Int32 lon;
Int32 lat;
Int32 depth;
Int32 time;
Int32 temperature;
Int32 salinity;
Int32 pressure;
} cast;
} SimpleSequence;
---------------------------------------------
cast.id, cast.lon, cast.lat, cast.depth, cast.time, cast.temperature, cast.salinity, cast.pressure
"2", 200, 10, 500, 1, 15, 35, 100
""")
def test_operators(self):
"""Test different operators on selection using a dummy function."""
app = App(
ServerSideFunctions(BaseHandler(SimpleSequence), double=double))
res = app.get("/.asc?cast.lat&double(cast.lat)>10")
self.assertEqual(res.text, """Dataset {
Sequence {
Int32 lat;
} cast;
} SimpleSequence;
---------------------------------------------
cast.lat
10
""")
res = app.get("/.asc?cast.lat&double(cast.lat)>=20")
self.assertEqual(res.text, """Dataset {
Sequence {
Int32 lat;
} cast;
} SimpleSequence;
---------------------------------------------
cast.lat
10
""")
res = app.get("/.asc?cast.lat&double(cast.lat)<10")
self.assertEqual(res.text, """Dataset {
Sequence {
Int32 lat;
} cast;
} SimpleSequence;
---------------------------------------------
cast.lat
-10
""")
res = app.get("/.asc?cast.lat&double(cast.lat)<=-20")
self.assertEqual(res.text, """Dataset {
Sequence {
Int32 lat;
} cast;
} SimpleSequence;
---------------------------------------------
cast.lat
-10
""")
res = app.get("/.asc?cast.lat&double(cast.lat)=20")
self.assertEqual(res.text, """Dataset {
Sequence {
Int32 lat;
} cast;
} SimpleSequence;
---------------------------------------------
cast.lat
10
""")
res = app.get("/.asc?cast.lat&double(cast.lat)!=20")
self.assertEqual(res.text, """Dataset {
Sequence {
Int32 lat;
} cast;
} SimpleSequence;
---------------------------------------------
cast.lat
-10
""")
def test_selection_no_comparison(self):
"""Test function calls in the selection without comparison.
Some functions, like ``bounds``, have no comparison. In this case, the
selection is implicitely applied by comparing the result to 1.
"""
app = App(ServerSideFunctions(BaseHandler(SimpleSequence)))
res = app.get(
"/.asc?cast.lat,cast.lon&"
"bounds(0,360,-90,0,0,500,00Z01JAN1900,00Z01JAN2000)")
self.assertEqual(res.text, """Dataset {
Sequence {
Int32 lat;
Int32 lon;
} cast;
} SimpleSequence;
---------------------------------------------
cast.lat, cast.lon
-10, 100
""")
def test_projection(self):
"""Test a simple function call on a projection."""
app = App(ServerSideFunctions(BaseHandler(SimpleGrid)))
res = app.get("/.asc?mean(x)")
self.assertEqual(res.text, """Dataset {
Float64 x;
} SimpleGrid;
---------------------------------------------
x
1
""")
def test_projection_clash(self):
"""Test a function call creating a variable with a conflicting name."""
app = App(ServerSideFunctions(BaseHandler(SimpleGrid)))
res = app.get("/.asc?mean(x),x")
self.assertEqual(res.text, """Dataset {
Int32 x[x = 3];
} SimpleGrid;
---------------------------------------------
x
[0] 0
[1] 1
[2] 2
""")
def test_nested_projection(self):
"""Test a nested function call."""
app = App(ServerSideFunctions(BaseHandler(SimpleGrid)))
res = app.get("/.asc?mean(mean(SimpleGrid.SimpleGrid,0),0)")
self.assertEqual(res.text, """Dataset {
Float64 SimpleGrid;
} SimpleGrid;
---------------------------------------------
SimpleGrid
2.5
""")
class Accumulator(object):
"""A WSGI middleware that breaks streaming."""
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
return list(self.app(environ, start_response))
def double(dataset, var):
"""A dummy function that doubles a value.
The value must be in a sequence. Return a new sequence with the value
doubled.
"""
# sequence is the first variable
sequence = next(dataset.children())
# get a single variable and double its value
selection = sequence[var.name]
rows = [(value*2,) for value in selection]
# create output sequence
out = SequenceType("result")
out["double"] = BaseType("double")
out.data = np.rec.fromrecords(rows, names=["double"])
return out
|