1+ import os
12import pytest
23
34import grass .script as gs
45from grass .experimental .tools import Tools
56
67
7- def test_key_value_parser (xy_dataset_session ):
8+ def test_key_value_parser_number (xy_dataset_session ):
9+ """Check that numbers are parsed as numbers"""
810 tools = Tools (session = xy_dataset_session )
911 assert tools .g_region (flags = "g" ).keyval ["nsres" ] == 1
1012
1113
12- # def test_json_parser(xy_dataset_session):
13- # print(
14- # tools.v_db_univar(map="bridges", column="YEAR_BUILT", format="json").json[
15- # "statistics"
16- # ]["mean"]
17- # )
14+ @pytest .mark .fails
15+ def test_key_value_parser_multiple_values (xy_dataset_session ):
16+ """Check that strings and floats are parsed"""
17+ tools = Tools (session = xy_dataset_session )
18+ name = "surface"
19+ tools .r_surf_gauss (output = name ) # needs seed
20+ result = tools .r_info (map = name , flags = "g" ).keyval
21+ assert result ["datatype" ] == "DCELL"
22+ assert result ["nsres" ] == 1
23+ result = tools .r_univar (map = name , flags = "g" ).keyval
24+ assert result ["mean" ] == pytest .approx (- 0.756762744552762 )
25+
26+
27+ def test_json_parser (xy_dataset_session ):
28+ """Check that JSON is parsed"""
29+ tools = Tools (session = xy_dataset_session )
30+ assert (
31+ tools .g_search_modules (keyword = "random" , flags = "j" ).json [0 ]["name" ]
32+ == "r.random"
33+ )
34+
35+
36+ def test_stdout_as_text (xy_dataset_session ):
37+ """Check that simple text is parsed and has no whitespace"""
38+ tools = Tools (session = xy_dataset_session )
39+ assert tools .g_mapset (flags = "p" ).text == "PERMANENT"
1840
19- # def test_direct_overwrite(xy_dataset_session):
20- # tools = Tools(session=xy_dataset_session)
21- # tools.r_slope_aspect(elevation="elevation", slope="slope")
22- # tools.r_slope_aspect(elevation="elevation", slope="slope", overwrite=True)
41+
42+ def test_stdout_as_space_items (xy_dataset_session ):
43+ """Check that whitespace-separated items are parsed"""
44+ tools = Tools (session = xy_dataset_session )
45+ assert tools .g_mapset (flags = "l" ).space_items == ["PERMANENT" ]
46+
47+
48+ def test_stdout_split_whitespace (xy_dataset_session ):
49+ tools = Tools (session = xy_dataset_session )
50+ assert tools .g_mapset (flags = "l" ).text_split () == ["PERMANENT" ]
51+
52+
53+ def test_stdout_split_space (xy_dataset_session ):
54+ tools = Tools (session = xy_dataset_session )
55+ # Not a good example usage, but it tests the functionality.
56+ assert tools .g_mapset (flags = "l" ).text_split (" " ) == ["PERMANENT" , "" ]
57+
58+
59+ def test_direct_overwrite (xy_dataset_session ):
60+ """Check overwrite as a parameter"""
61+ tools = Tools (session = xy_dataset_session )
62+ tools .r_random_surface (output = "surface" , seed = 42 )
63+ tools .r_random_surface (output = "surface" , seed = 42 , overwrite = True )
64+
65+
66+ def test_object_overwrite (xy_dataset_session ):
67+ """Check overwrite as parameter of the tools object"""
68+ tools = Tools (session = xy_dataset_session , overwrite = True )
69+ tools .r_random_surface (output = "surface" , seed = 42 )
70+ tools .r_random_surface (output = "surface" , seed = 42 )
71+
72+
73+ def test_no_overwrite (xy_dataset_session ):
74+ """Check that it fails without overwrite"""
75+ tools = Tools (session = xy_dataset_session )
76+ tools .r_random_surface (output = "surface" , seed = 42 )
77+ with pytest .raises (gs .CalledModuleError , match = "overwrite" ):
78+ tools .r_random_surface (output = "surface" , seed = 42 )
79+
80+
81+ def test_env_overwrite (xy_dataset_session ):
82+ """Check that overwrite from env parameter is used"""
83+ # env = xy_dataset_session.env.copy() # ideally
84+ env = os .environ .copy () # for now
85+ env ["GRASS_OVERWRITE" ] = "1"
86+ tools = Tools (session = xy_dataset_session , env = env )
87+ tools .r_random_surface (output = "surface" , seed = 42 )
88+ tools .r_random_surface (output = "surface" , seed = 42 )
89+
90+
91+ def test_global_overwrite_vs_env (xy_dataset_session ):
92+ """Check that global overwrite is not used when separate env is used"""
93+ # env = xy_dataset_session.env.copy() # ideally
94+ env = os .environ .copy () # for now
95+ os .environ ["GRASS_OVERWRITE" ] = "1" # change to xy_dataset_session.env
96+ tools = Tools (session = xy_dataset_session , env = env )
97+ tools .r_random_surface (output = "surface" , seed = 42 )
98+ with pytest .raises (gs .CalledModuleError , match = "overwrite" ):
99+ tools .r_random_surface (output = "surface" , seed = 42 )
100+ del os .environ ["GRASS_OVERWRITE" ] # check or ideally remove this
101+
102+
103+ def test_global_overwrite_vs_init (xy_dataset_session ):
104+ """Check that global overwrite is not used when separate env is used"""
105+ tools = Tools (session = xy_dataset_session )
106+ os .environ ["GRASS_OVERWRITE" ] = "1" # change to xy_dataset_session.env
107+ tools .r_random_surface (output = "surface" , seed = 42 )
108+ with pytest .raises (gs .CalledModuleError , match = "overwrite" ):
109+ tools .r_random_surface (output = "surface" , seed = 42 )
110+ del os .environ ["GRASS_OVERWRITE" ] # check or ideally remove this
23111
24112
25113def test_stdin (xy_dataset_session ):
@@ -31,9 +119,10 @@ def test_stdin(xy_dataset_session):
31119
32120def test_raises (xy_dataset_session ):
33121 tools = Tools (session = xy_dataset_session )
34- with pytest .raises (gs .CalledModuleError , match = "xstandard" ):
122+ wrong_name = "wrong_standard"
123+ with pytest .raises (gs .CalledModuleError , match = wrong_name ):
35124 tools .feed_input_to ("13.45,29.96,200" ).v_in_ascii (
36125 input = "-" ,
37126 output = "point" ,
38- format = "xstandard" ,
127+ format = wrong_name ,
39128 )
0 commit comments