def test_body(self): """Test the generated DAS response.""" app = App(BaseHandler(SimpleStructure)) res = app.get('/.das') self.assertEqual( res.text, """Attributes { types { String key "value"; nested { String string "bar"; Int32 list 42, 43; Int32 array 1; Float64 float 1000; } b { } i32 { } ui32 { } i16 { } ui16 { } f32 { } f64 { } s { } u { } } } """)
def setUp(self): # create dataset dataset = DatasetType("test") dataset["a.b"] = BaseType("a.b", np.array(1)) # create WSGI app self.app = BaseHandler(dataset)
def test_no_ce(self): data = np.rec.fromrecords(D1.Drifters.data.tolist(), names=D1.Drifters.keys()) projection, selection = parse_ce('') dataset = BaseHandler(D1).parse(projection, selection) np.testing.assert_array_equal(data, dataset.Drifters.data)
def setUp(self): # create dataset self.dataset = DatasetType("test") self.dataset["foo["] = BaseType("foo[", np.array(1)) # create WSGI app self.app = BaseHandler(self.dataset)
def setUp(self): """Create a WSGI app""" self.app = BaseHandler(SimpleSequence) self.local = SimpleSequence.cast.data dataset = DAPHandler("http://localhost:8001/", self.app).dataset self.remote = dataset.cast.data
def setUp(self): """Create a WSGI app""" self.app = BaseHandler(SimpleArray) self.data = BaseProxy("http://localhost:8001/", "byte", np.dtype("b"), (5, ), application=self.app)
def test_filtering(self): data = np.rec.fromrecords(D1.Drifters.data.tolist(), names=D1.Drifters.keys()) filtered = data[data['longitude'] < 999] projection, selection = parse_ce('Drifters.longitude<999') dataset = BaseHandler(D1).parse(projection, selection) np.testing.assert_array_equal(filtered, dataset.Drifters.data)
def setUp(self): """Create a WSGI app with array data""" self.app = BaseHandler(SimpleArray) self.data = BaseProxy("http://localhost:8001/", "short", np.dtype(">i"), (), application=self.app)
def setUp(self): """Create a WSGI app with array data""" dataset = DatasetType("test") dataset["s"] = BaseType("s", np.array(["one", "two", "three"])) self.app = BaseHandler(dataset) self.data = BaseProxy( "http://localhost:8001/", "s", np.dtype("|S5"), (3,), application=self.app)
def setUp(self): """Create a WSGI app with array data""" dataset = DatasetType("test") self.original_data = np.array([["This ", "is "], ["a ", "test"]], dtype='<U5') dataset["s"] = BaseType("s", self.original_data) self.app = BaseHandler(dataset) self.data = DAPHandler("http://localhost:8001/", self.app).dataset.s
def test_environ_loader_without_template(self): """Test that global environment is used.""" env = Environment() self.assertIsNone(env.loader) app = BaseHandler(VerySimpleSequence) req = Request.blank('/.html') req.environ["pydap.jinja2.environment"] = env res = req.get_response(app) self.assertNotEqual(res.text, "global")
def setUp(self): """Create a WSGI app with array data""" dataset = DatasetType("test") data = np.array("This is a test", dtype='S') dataset["s"] = BaseType("s", data) self.app = BaseHandler(dataset) self.data = BaseProxy( "http://localhost:8001/", "s", np.dtype("|S14"), (), application=self.app)
def test_environ_loader_with_template(self): """Test that global environment is used.""" loader = DictLoader({'html.html': 'global'}) env = Environment(loader=loader) app = BaseHandler(VerySimpleSequence) req = Request.blank('/.html') req.environ["pydap.jinja2.environment"] = env res = req.get_response(app) self.assertEqual(res.text, "global")
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_projection(self): """Test a simple function call on a projection.""" app = TestApp(ServerSideFunctions(BaseHandler(SimpleGrid))) res = app.get("/.asc?mean(x)") self.assertEqual(res.text, """Dataset { Float64 x; } SimpleGrid; --------------------------------------------- x 1 """)
def test_nested_projection(self): """Test a nested function call.""" app = TestApp(ServerSideFunctions(BaseHandler(SimpleGrid))) res = app.get("/.asc?mean(mean(SimpleGrid.SimpleGrid,0),0)") self.assertEqual(res.text, """Dataset { Float64 SimpleGrid; } SimpleGrid; --------------------------------------------- SimpleGrid 2.5 """)
def ssf_app(): """Test the local implementation of server-side functions. Calling server-side functions is implemented using a lazy mechanism where arbitrary names are mapped to remove calls. The resulting dataset is only evaluated when ``__getitem__`` or ``__getattr__`` are called, allowing nested calls to be evaluated only once. """ from pydap.wsgi.ssf import ServerSideFunctions return ServerSideFunctions(BaseHandler(SimpleGrid))
def test_open(sequence_type_data): """Test that LocalTestServerSSL works properly""" TestDataset = DatasetType('Test') TestDataset['sequence'] = sequence_type_data with LocalTestServerSSL(BaseHandler(TestDataset)) as server: dataset = open_url(server.url) seq = dataset['sequence'] retrieved_data = [line for line in seq] np.testing.assert_array_equal( np.array(retrieved_data, dtype=sequence_type_data.data.dtype), np.array(sequence_type_data.data[:], dtype=sequence_type_data.data.dtype))
def test_projection_clash(self): """Test a function call creating a variable with a conflicting name.""" app = TestApp(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_timeout(sequence_type_data): """Test that timeout works properly""" TestDataset = DatasetType('Test') TestDataset['sequence'] = sequence_type_data TestDataset['byte'] = BaseType('byte', 0) application = BaseHandler(TestDataset) # Explictly add latency on the devel server # to guarantee that it timeouts def wrap_mocker(func): def mock_add_latency(*args, **kwargs): time.sleep(1e-1) return func(*args, **kwargs) return mock_add_latency application = wrap_mocker(application) with LocalTestServer(application) as server: url = ("http://0.0.0.0:%s/" % server.port) # test open_url assert open_url(url) == TestDataset with pytest.raises(HTTPError) as e: open_url(url, timeout=1e-5) assert 'Timeout' in str(e) # test open_dods with pytest.raises(HTTPError): open_dods(url + '.dods?sequence', timeout=1e-5) assert 'Timeout' in str(e) # test sequenceproxy dataset = open_url(url) seq = dataset['sequence'] assert isinstance(seq.data, SequenceProxy) # Change the timeout of the sequence proxy: seq.data.timeout = 1e-5 with pytest.raises(HTTPError) as e: next(seq.iterdata()) assert 'Timeout' in str(e) # test baseproxy: dat = dataset['byte'] assert isinstance(dat.data, BaseProxy) # Change the timeout of the baseprox proxy: dat.data.timeout = 1e-5 with pytest.raises(HTTPError) as e: dat[:] assert 'Timeout' in str(e)
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_body(self): """Test the generated DDS response.""" app = TestApp(BaseHandler(SimpleGrid)) res = app.get('/.dds') self.assertEqual( res.text, """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; """)
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)
def test_body(self): """Test the generated ASCII response.""" app = App(BaseHandler(SimpleGrid)) res = app.get('/.asc') self.assertEqual( res.text, """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; --------------------------------------------- SimpleGrid.SimpleGrid [0][0] 0 [0][1] 1 [0][2] 2 [1][0] 3 [1][1] 4 [1][2] 5 SimpleGrid.x [0] 0 [1] 1 [2] 2 SimpleGrid.y [0] 0 [1] 1 x [0] 0 [1] 1 [2] 2 y [0] 0 [1] 1 """)
def test_body(self): """Test the generated DAS response.""" app = App(BaseHandler(FaultyGrid)) res = app.get('/.das') self.assertEqual( res.text, """Attributes { String description "A faulty grid for testing."; FaultyGrid { } x { String axis "X"; Int32 code 1; } y { String axis "Y"; } } """)
def test_body(self): """Test the generated DAS response.""" app = App(BaseHandler(SimpleGrid)) res = app.get('/.das') self.assertEqual( res.text, """Attributes { String description "A simple grid for testing."; SimpleGrid { } x { String axis "X"; String units "degrees_east"; } y { String axis "Y"; String units "degrees_north"; } } """)
def test_body(self): """Test the generated DDS response.""" app = TestApp(BaseHandler(SimpleStructure)) res = app.get('/.dds') self.assertEqual( res.text, """Dataset { Structure { Byte b; Int32 i32; UInt32 ui32; Int16 i16; UInt16 ui16; Float32 f32; Float64 f64; String s; String u; } types; } SimpleStructure; """)
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_selection(self): """Test a selection server-side function.""" app = TestApp(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_verify_open_url(sequence_type_data): """Test that open_url raises the correct SSLError""" warnings.simplefilter("always") TestDataset = DatasetType('Test') TestDataset['sequence'] = sequence_type_data TestDataset['byte'] = BaseType('byte', 0) application = BaseHandler(TestDataset) with LocalTestServerSSL(application, ssl_context='adhoc') as server: try: open_url(server.url, verify=False, session=requests.Session()) except (ssl.SSLError, requests.exceptions.SSLError): pytest.fail("SSLError should not be raised.") with pytest.raises(requests.exceptions.SSLError): open_url(server.url, session=requests.Session()) if not (sys.version_info >= (3, 0) and sys.version_info < (3, 4, 4)): # verify is disabled by default for python 3 before 3.4.4: with pytest.raises(requests.exceptions.SSLError): open_url(server.url)