Skip to content

Commit 4b3e8dc

Browse files
salih tangelsalih tangel
authored andcommitted
fixing the pull problem, i used rebase instead and back to my works.
1 parent 6a837f6 commit 4b3e8dc

File tree

7 files changed

+120
-103
lines changed

7 files changed

+120
-103
lines changed

vector/v.net/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ MODULE_TOPDIR = ../..
44
PGM=v.net
55

66
LIBES = $(VECTORLIB) $(GISLIB) $(DBMILIB) $(PARSONLIB)
7-
DEPENDENCIES = $(VECTORDEP) $(GISDEP) $(DBMIDEP) $(PARSONDEP)
7+
DEPENDENCIES = $(VECTORDEP) $(GISDEP) $(DBMIDEP)
88
EXTRA_INC = $(VECT_INC)
99
EXTRA_CFLAGS = $(VECT_CFLAGS)
1010

vector/v.net/args.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ void define_options(struct opt *opt)
99
{
1010
char *desc;
1111

12+
opt->format = G_define_standard_option(G_OPT_F_FORMAT);
13+
opt->format->key = "format";
14+
if (!opt->format->description)
15+
opt->format->description = _("Output format");
16+
opt->format->descriptions = _("plain;Human readable text output;"
17+
"json;JSON (JavaScript Object Notation)");
18+
opt->format->options = "plain,json";
19+
opt->format->answer = "plain";
20+
opt->format->guisection = _("Print");
21+
1222
opt->input = G_define_standard_option(G_OPT_V_INPUT);
1323
opt->input->required = NO;
1424
opt->input->label = _("Name of input vector line map (arcs)");
@@ -120,18 +130,10 @@ void define_options(struct opt *opt)
120130
opt->tucfield->key = "turn_cat_layer";
121131
opt->tucfield->required = NO;
122132
opt->tucfield->guisection = _("Turntable");
123-
124-
opt->format = G_define_standard_option(G_OPT_F_FORMAT);
125-
opt->format->key = "format";
126-
opt->format->type = TYPE_STRING;
127-
opt->format->required = NO;
128-
opt->format->options = "plain,json";
129-
opt->format->answer = "plain";
130-
opt->format->description = _("Output");
131133
}
132134

133135
void parse_arguments(const struct opt *opt, int *afield, int *nfield,
134-
double *thresh, int *act)
136+
double *thresh, int *act, enum OutputFormat *format)
135137
{
136138
*afield = atoi(opt->afield_opt->answer);
137139
*nfield = atoi(opt->nfield_opt->answer);
@@ -152,6 +154,13 @@ void parse_arguments(const struct opt *opt, int *afield, int *nfield,
152154
else
153155
G_fatal_error(_("Unknown operation"));
154156

157+
if (strcmp(opt->format->answer, "json") == 0) {
158+
*format = FORMAT_JSON;
159+
}
160+
else {
161+
*format = FORMAT_PLAIN;
162+
}
163+
155164
if (*act == TOOL_NODES || *act == TOOL_CONNECT || *act == TOOL_REPORT ||
156165
*act == TOOL_NREPORT || *act == TOOL_TURNTABLE) {
157166
if (opt->input->answer == NULL)

vector/v.net/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ int main(int argc, char **argv)
3333
struct Map_info *In = NULL, *Out = NULL, *Points = NULL;
3434

3535
FILE *file_arcs;
36-
3736
int afield, nfield;
3837
int act;
3938
double thresh;
39+
enum OutputFormat format;
4040

4141
char message[4096];
4242

@@ -54,7 +54,7 @@ int main(int argc, char **argv)
5454
if (G_parser(argc, argv))
5555
exit(EXIT_FAILURE);
5656

57-
parse_arguments(&opt, &afield, &nfield, &thresh, &act);
57+
parse_arguments(&opt, &afield, &nfield, &thresh, &act, &format);
5858

5959
In = Points = Out = NULL;
6060
file_arcs = NULL;
@@ -173,7 +173,7 @@ int main(int argc, char **argv)
173173
turntable(&opt);
174174
}
175175
else { /* report */
176-
report(In, afield, nfield, act, opt.format->answer);
176+
report(In, afield, nfield, act, format);
177177
}
178178

179179
if (In)

vector/v.net/proto.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ struct opt {
1616
struct Option *format;
1717
};
1818

19+
enum OutputFormat { FORMAT_PLAIN, FORMAT_JSON };
20+
1921
/* arcs.c */
2022
int create_arcs(FILE *, struct Map_info *, struct Map_info *, int, int);
2123

2224
/* argc.c */
2325
void define_options(struct opt *);
24-
void parse_arguments(const struct opt *, int *, int *, double *, int *);
26+
void parse_arguments(const struct opt *, int *, int *, double *, int *,
27+
enum OutputFormat *);
2528

2629
/* connect.c */
2730
int connect_arcs(struct Map_info *, struct Map_info *, struct Map_info *, int,
@@ -31,6 +34,6 @@ int connect_arcs(struct Map_info *, struct Map_info *, struct Map_info *, int,
3134
int nodes(struct Map_info *, struct Map_info *, int, int);
3235

3336
/* report.c */
34-
int report(struct Map_info *, int, int, int, const char *);
37+
int report(struct Map_info *, int, int, int, enum OutputFormat);
3538

3639
void turntable(struct opt *);

vector/v.net/report.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <grass/gjson.h>
77

88
int report(struct Map_info *In, int afield, int nfield, int action,
9-
const char *format)
9+
enum OutputFormat format)
1010
{
1111
int i, j, line, nlines, ltype, node, nnodes;
1212
int cat_line, cat_node[2];
@@ -25,9 +25,11 @@ int report(struct Map_info *In, int afield, int nfield, int action,
2525

2626
if (action == TOOL_REPORT) {
2727
struct boxlist *List;
28+
2829
G_JSON_Value *root_value = NULL;
2930
G_JSON_Array *root_array = NULL;
30-
if (format && strcmp(format, "json") == 0) {
31+
32+
if (format == FORMAT_JSON) {
3133
root_value = G_json_value_init_array();
3234
root_array = G_json_array(root_value);
3335
}
@@ -75,7 +77,7 @@ int report(struct Map_info *In, int afield, int nfield, int action,
7577
G_warning(_("%d points found: %g %g %g line category: %d"),
7678
nnodes, x, y, z, cat_line);
7779
}
78-
if (root_array) {
80+
if (format == FORMAT_JSON) {
7981
G_JSON_Value *item_value = G_json_value_init_object();
8082
G_JSON_Object *item_obj = G_json_object(item_value);
8183

@@ -92,7 +94,7 @@ int report(struct Map_info *In, int afield, int nfield, int action,
9294
}
9395
}
9496

95-
if (root_value) {
97+
if (format == FORMAT_JSON) {
9698
char *json_str = G_json_serialize_to_string_pretty(root_value);
9799
if (json_str) {
98100
fprintf(stdout, "%s\n", json_str);
@@ -109,10 +111,13 @@ int report(struct Map_info *In, int afield, int nfield, int action,
109111

110112
List = Vect_new_list();
111113

112-
G_JSON_Value *root_val = (format && strcmp(format, "json") == 0)
113-
? G_json_value_init_array()
114-
: NULL;
115-
G_JSON_Array *root_arr = G_json_array(root_val);
114+
G_JSON_Value *root_val = NULL;
115+
G_JSON_Array *root_arr = NULL;
116+
117+
if (format == FORMAT_JSON) {
118+
root_val = G_json_value_init_array();
119+
root_arr = G_json_array(root_val);
120+
}
116121

117122
for (i = 1; i <= nlines; i++) {
118123

@@ -144,7 +149,7 @@ int report(struct Map_info *In, int afield, int nfield, int action,
144149
G_JSON_Value *lines_val =
145150
root_arr ? G_json_value_init_array() : NULL;
146151

147-
if (root_arr) {
152+
if (format == FORMAT_JSON) {
148153
G_json_object_set_number(
149154
G_json_value_get_object(item_val), "node_cat",
150155
Cats->cat[j]);
@@ -162,7 +167,7 @@ int report(struct Map_info *In, int afield, int nfield, int action,
162167
/* Loop through all cats of line */
163168
for (l = 0; l < Cats2->n_cats; l++) {
164169
if (Cats2->field[l] == afield) {
165-
if (root_arr)
170+
if (format == FORMAT_JSON)
166171
G_json_array_append_number(
167172
G_json_array(lines_val),
168173
Cats2->cat[l]);
@@ -175,7 +180,7 @@ int report(struct Map_info *In, int afield, int nfield, int action,
175180
}
176181
}
177182
}
178-
if (root_arr) {
183+
if (format == FORMAT_JSON) {
179184
G_json_object_set_value(
180185
G_json_value_get_object(item_val), "lines",
181186
lines_val);
@@ -187,7 +192,7 @@ int report(struct Map_info *In, int afield, int nfield, int action,
187192
}
188193
}
189194
}
190-
if (root_val) {
195+
if (format == FORMAT_JSON) {
191196
char *s = G_json_serialize_to_string_pretty(root_val);
192197
if (s) {
193198
fprintf(stdout, "%s\n", s);

vector/v.net/testsuite/test_v_net.py

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,71 @@
11
from grass.gunittest.case import TestCase
22
from grass.gunittest.main import test
33
from grass.script.core import read_command
4-
from grass.tools import Tools
4+
from grass.gunittest.gmodules import SimpleModule
55
import json
66

7+
78
class TestVNet(TestCase):
89
network = "test_vnet"
910

1011
def tearDown(self):
1112
"""Remove viewshed map after each test method"""
1213
self.runModule("g.remove", flags="f", type="vector", name=self.network)
1314

14-
def test_nreport_json(self):
15-
self.runModule("v.net", input="streets", points="schools",
16-
output=self.network, operation="connect",
17-
threshold=400, flags="c")
18-
19-
tools = Tools(quiet=True)
20-
result = tools.v_net(
15+
def test_nreport_json_output(self):
16+
"""Verify that v.net nreport produces valid and accurate JSON output"""
17+
self.runModule(
18+
"v.net",
19+
input="streets",
20+
points="schools",
21+
output=self.network,
22+
operation="connect",
23+
threshold=400,
24+
flags="c",
25+
)
26+
27+
vnet_module = SimpleModule(
28+
"v.net",
2129
input=self.network,
2230
operation="nreport",
2331
node_layer=2,
24-
format="json"
32+
format="json",
2533
)
34+
self.assertModule(vnet_module)
2635

27-
try:
28-
data = json.loads(result.text)
29-
except json.JSONDecodeError:
30-
self.fail(f"nreport produced invalid JSON: {result.text}")
31-
32-
self.assertIsInstance(data, list)
33-
self.assertGreater(len(data), 0, "nreport output is empty")
34-
35-
first_row = data[0]
36-
self.assertIn("node_cat", first_row)
37-
self.assertIn("lines", first_row)
38-
39-
self.assertIsInstance(first_row["node_cat"], int)
40-
self.assertIsInstance(first_row["lines"], list)
41-
self.assertGreater(len(first_row["lines"]), 0)
42-
43-
def test_report_json(self):
44-
tools = Tools(quiet=True)
45-
result = tools.v_net(
46-
input="streets",
36+
actual_output = json.loads(vnet_module.outputs.stdout)
37+
38+
self.assertIsInstance(actual_output, list)
39+
self.assertGreater(len(actual_output), 0, "The JSON output list is empty")
40+
41+
expected_keys = {"node_cat", "lines"}
42+
self.assertEqual(set(actual_output[0].keys()), expected_keys)
43+
self.assertIsInstance(actual_output[0]["node_cat"], int)
44+
self.assertIsInstance(actual_output[0]["lines"], list)
45+
46+
def test_report_json_output(self):
47+
"""Verify that v.net report produces valid and accurate JSON output"""
48+
vnet_module = SimpleModule(
49+
"v.net",
50+
input="streets_net",
4751
operation="report",
4852
arc_layer=1,
4953
node_layer=2,
50-
format="json"
54+
format="json",
5155
)
52-
53-
try:
54-
data = json.loads(result.text)
55-
except json.JSONDecodeError:
56-
self.fail(f"report produced invalid JSON: {result.text}")
57-
58-
self.assertIsInstance(data, list)
59-
self.assertGreater(len(data), 0, "report output is empty")
60-
61-
self.assertIn("line_cat", data[0])
62-
self.assertIsInstance(data[0]["line_cat"], int)
56+
self.assertModule(vnet_module)
57+
58+
actual_output = json.loads(vnet_module.outputs.stdout)
59+
60+
self.assertIsInstance(actual_output, list)
61+
self.assertGreater(len(actual_output), 0, "The JSON output list is empty")
62+
63+
expected_keys = {"line_cat", "start_node_cat", "end_node_cat"}
64+
self.assertEqual(set(actual_output[0].keys()), expected_keys)
65+
66+
for entry in actual_output[:10]:
67+
for key in expected_keys:
68+
self.assertIsInstance(entry[key], int)
6369

6470
def test_nodes(self):
6571
"""Test"""

0 commit comments

Comments
 (0)