33from datetime import datetime
44import unittest
55import textwrap
6+ from PIL import Image
67
78import requests_mock
89import cartopy .mpl .geoaxes
@@ -22,7 +23,7 @@ def setUp(self) -> None:
2223 @responses .activate
2324 @ImageTesting (['wms' ], tolerance = 0 )
2425 def test_ndvi (self ):
25- with requests_mock .Mocker () as m :
26+ with requests_mock .Mocker () as m , open ( "tests/responses/4326.gml" , "rb" ) as epsg4326_gml :
2627 m .post ('http://mock-instance/anonymous' , json = {
2728 "id" : "c4983c3e-9b53-47ae-bda9-382223bd5081" ,
2829 "project" : None ,
@@ -47,6 +48,8 @@ def test_ndvi(self):
4748 },
4849 request_headers = {'Authorization' : 'Bearer c4983c3e-9b53-47ae-bda9-382223bd5081' })
4950
51+ m .get ('http://epsg.io/4326.gml?download' , body = epsg4326_gml )
52+
5053 # Unfortunately, we need a separate library to catch the request from the WMS call
5154 with open ("tests/responses/wms-ndvi.png" , "rb" ) as wms_ndvi :
5255 responses .add (
@@ -90,16 +93,84 @@ def test_ndvi(self):
9093 self .assertEqual (type (ax ), cartopy .mpl .geoaxes .GeoAxesSubplot )
9194
9295 # Check requests from the mocker
93- self .assertEqual (len (m .request_history ), 3 )
96+ self .assertEqual (len (m .request_history ), 4 )
9497
9598 workflow_request = m .request_history [1 ]
9699 self .assertEqual (workflow_request .method , "POST" )
97100 self .assertEqual (workflow_request .url ,
98101 "http://mock-instance/workflow" )
99102 self .assertEqual (workflow_request .json (), workflow_definition )
100103
104+ def test_ndvi_image (self ):
105+ with requests_mock .Mocker () as m ,\
106+ open ("tests/responses/wms-ndvi.png" , "rb" ) as ndvi_png ,\
107+ open ("tests/responses/4326.gml" , "rb" ) as epsg4326_gml :
108+ m .post ('http://mock-instance/anonymous' , json = {
109+ "id" : "c4983c3e-9b53-47ae-bda9-382223bd5081" ,
110+ "project" : None ,
111+ "view" : None
112+ })
113+
114+ m .post ('http://mock-instance/workflow' ,
115+ json = {
116+ "id" : "5b9508a8-bd34-5a1c-acd6-75bb832d2d38"
117+ },
118+ request_headers = {'Authorization' : 'Bearer c4983c3e-9b53-47ae-bda9-382223bd5081' })
119+
120+ m .get ('http://mock-instance/workflow/5b9508a8-bd34-5a1c-acd6-75bb832d2d38/metadata' ,
121+ json = {
122+ "type" : "raster" ,
123+ "dataType" : "U8" ,
124+ "spatialReference" : "EPSG:4326" ,
125+ "measurement" : {
126+ "type" : "unitless"
127+ },
128+ "noDataValue" : 0.0
129+ },
130+ request_headers = {'Authorization' : 'Bearer c4983c3e-9b53-47ae-bda9-382223bd5081' })
131+
132+ m .get ('http://epsg.io/4326.gml?download' , body = epsg4326_gml )
133+
134+ # Unfortunately, we need a separate library to catch the request from the WMS call
135+ m .get (
136+ # pylint: disable=line-too-long
137+ 'http://mock-instance/wms?service=WMS&version=1.3.0&request=GetMap&layers=5b9508a8-bd34-5a1c-acd6-75bb832d2d38&time=2014-04-01T12%3A00%3A00.000%2B00%3A00&crs=EPSG%3A4326&bbox=-90.0%2C-180.0%2C90.0%2C180.0&width=200&height=100&format=image%2Fpng&styles=custom%3A%7B%22type%22%3A+%22linearGradient%22%2C+%22breakpoints%22%3A+%5B%7B%22value%22%3A+0%2C+%22color%22%3A+%5B0%2C+0%2C+0%2C+255%5D%7D%2C+%7B%22value%22%3A+255%2C+%22color%22%3A+%5B255%2C+255%2C+255%2C+255%5D%7D%5D%2C+%22noDataColor%22%3A+%5B0%2C+0%2C+0%2C+0%5D%2C+%22defaultColor%22%3A+%5B0%2C+0%2C+0%2C+0%5D%7D' ,
138+ body = ndvi_png ,
139+ )
140+
141+ ge .initialize ("http://mock-instance" )
142+
143+ workflow_definition = {
144+ "type" : "Raster" ,
145+ "operator" : {
146+ "type" : "GdalSource" ,
147+ "params" : {
148+ "dataset" : {
149+ "type" : "internal" ,
150+ "datasetId" : "36574dc3-560a-4b09-9d22-d5945f2b8093"
151+ }
152+ }
153+ }
154+ }
155+
156+ time = datetime .strptime (
157+ '2014-04-01T12:00:00.000Z' , "%Y-%m-%dT%H:%M:%S.%f%z" )
158+
159+ workflow = ge .register_workflow (workflow_definition )
160+
161+ img = workflow .wms_get_map_as_image (
162+ QueryRectangle (
163+ [- 180.0 , - 90.0 , 180.0 , 90.0 ],
164+ [time , time ],
165+ resolution = (1.8 , 1.8 )
166+ ),
167+ colorizer_min_max = (0 , 255 )
168+ )
169+
170+ self .assertEqual (img , Image .open ("tests/responses/wms-ndvi.png" ))
171+
101172 def test_wms_url (self ):
102- with requests_mock .Mocker () as m :
173+ with requests_mock .Mocker () as m , open ( "tests/responses/4326.gml" , "rb" ) as epsg4326_gml :
103174 m .post ('http://mock-instance/anonymous' , json = {
104175 "id" : "c4983c3e-9b53-47ae-bda9-382223bd5081" ,
105176 "project" : None ,
@@ -124,6 +195,8 @@ def test_wms_url(self):
124195 },
125196 request_headers = {'Authorization' : 'Bearer c4983c3e-9b53-47ae-bda9-382223bd5081' })
126197
198+ m .get ('http://epsg.io/4326.gml?download' , body = epsg4326_gml )
199+
127200 ge .initialize ("http://mock-instance" )
128201
129202 workflow_definition = {
@@ -153,7 +226,7 @@ def test_wms_url(self):
153226 self .assertEqual (
154227 # pylint: disable=line-too-long
155228 wms_curl ,
156- """curl -X GET -H "Authorization: Bearer c4983c3e-9b53-47ae-bda9-382223bd5081" 'http://mock-instance/wms?service=WMS&version=1.3.0&request=GetMap&layers=5b9508a8-bd34-5a1c-acd6-75bb832d2d38&time=2014-04-01T12%3A00%3A00.000%2B00%3A00&crs=EPSG%3A4326&bbox=-180 .0%2C-90 .0%2C180 .0%2C90 .0&width=360&height=180&format=image%2Fpng&styles='"""
229+ """curl -X GET -H "Authorization: Bearer c4983c3e-9b53-47ae-bda9-382223bd5081" 'http://mock-instance/wms?service=WMS&version=1.3.0&request=GetMap&layers=5b9508a8-bd34-5a1c-acd6-75bb832d2d38&time=2014-04-01T12%3A00%3A00.000%2B00%3A00&crs=EPSG%3A4326&bbox=-90 .0%2C-180 .0%2C90 .0%2C180 .0&width=360&height=180&format=image%2Fpng&styles='"""
157230 )
158231
159232 def test_result_descriptor (self ):
0 commit comments