2020
2121import datetime
2222
23+ from click .testing import CliRunner
2324from dateutil .relativedelta import relativedelta
2425from flask import Response
2526
27+ from cube_builder import __version__
28+ from cube_builder .cli import cli
29+
2630
2731def _assert_json_request (response : Response , status_code = 200 ):
2832 assert response .status_code == status_code
@@ -31,6 +35,12 @@ def _assert_json_request(response: Response, status_code=200):
3135 return response .json
3236
3337
38+ def test_index_api (client ):
39+ response = client .get ('/' )
40+ assert response .status_code == 200
41+ assert response .json ['description' ] == 'Cube Builder' and response .json ['version' ] == __version__
42+
43+
3444def test_create_grid (client , json_data ):
3545 response = client .post ('/create-grids' , json = json_data ['grid-bdc-md.json' ])
3646 assert response .status_code == 201
@@ -43,6 +53,11 @@ def test_get_grid(client, json_data):
4353 assert len (grids ) > 0
4454
4555
56+ def test_load_initial_data ():
57+ res = CliRunner ().invoke (cli , args = ['load-data' ])
58+ assert res .exit_code == 0
59+
60+
4661def test_create_cube (client , json_data ):
4762 json_cube = json_data ['lc8-16d-stk.json' ]
4863 response = client .post ('/cubes' , json = json_cube )
@@ -81,3 +96,57 @@ def test_list_periods_continuous_month(client):
8196 assert end == (ref_date + offset )
8297
8398 ref_date += offset + relativedelta (days = 1 )
99+
100+
101+ def test_datacube_status (client , json_data ):
102+ json_cube = json_data ['lc8-16d-stk.json' ]
103+ identifier = f"{ json_cube ['datacube' ]} -{ json_cube ['version' ]} "
104+ response = client .get ('/cube-status' , query_string = {'cube_name' : identifier })
105+ _assert_json_request (response , status_code = 200 )
106+
107+ # Test invalid request
108+ response = client .get ('/cube-status' )
109+ _assert_json_request (response , status_code = 400 )
110+
111+
112+ def test_list_cubes (client ):
113+ cube_info = _get_first_cube (client )
114+
115+ response = client .get (f'/cubes/{ cube_info ["id" ]} ' )
116+ cube = _assert_json_request (response , 200 )
117+ assert cube ['name' ] == cube_info ['name' ]
118+
119+
120+ def test_update_cube_meta (client ):
121+ cube = _get_first_cube (client )
122+
123+ props = dict (
124+ title = "New Cube - Updated" ,
125+ public = True
126+ )
127+ response = client .put (f'/cubes/{ cube ["id" ]} ' , json = props )
128+ res = _assert_json_request (response , 200 )
129+ assert res ['message' ] == 'Updated cube!'
130+ updated_cube = _get_first_cube (client )
131+
132+ assert updated_cube ['title' ] == props ['title' ]
133+ assert updated_cube ['is_public' ] == props ['public' ]
134+
135+ # invalid parameter
136+ props ['public' ] = 'invalid'
137+ response = client .put (f'/cubes/{ cube ["id" ]} ' , json = props )
138+ _assert_json_request (response , 400 )
139+
140+
141+ def test_list_cube_tiles (client ):
142+ cube = _get_first_cube (client )
143+
144+ response = client .get (f'/cubes/{ cube ["id" ]} /tiles' )
145+ _assert_json_request (response , 200 )
146+
147+
148+ def _get_first_cube (client ):
149+ response = client .get ('/cubes' )
150+ cubes = _assert_json_request (response , 200 )
151+ assert len (cubes ) > 0
152+ return cubes [0 ]
0 commit comments