-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathtest_response_formatter.cpp
More file actions
393 lines (302 loc) · 13.1 KB
/
test_response_formatter.cpp
File metadata and controls
393 lines (302 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <nlohmann/json.hpp>
#include "../test_helpers.hpp"
#include "include/config.hpp"
#include "include/query_generator.hpp"
#include "include/response_formatter.hpp"
using namespace pg_ai;
using namespace pg_ai::config;
using namespace pg_ai::test_utils;
using json = nlohmann::json;
class ResponseFormatterTest : public ::testing::Test {
protected:
QueryResult createBasicResult() {
return QueryResult{.generated_query = "SELECT * FROM users",
.explanation = "Retrieves all users",
.warnings = {},
.row_limit_applied = false,
.suggested_visualization = "table",
.success = true,
.error_message = ""};
}
QueryResult createResultWithWarnings() {
return QueryResult{.generated_query = "SELECT * FROM large_table",
.explanation = "Query may be slow",
.warnings = {"Consider adding LIMIT", "Full table scan"},
.row_limit_applied = true,
.suggested_visualization = "table",
.success = true,
.error_message = ""};
}
Configuration createConfig(bool formatted,
bool showExplanation,
bool showWarnings,
bool showVisualization) {
Configuration config;
config.use_formatted_response = formatted;
config.show_explanation = showExplanation;
config.show_warnings = showWarnings;
config.show_suggested_visualization = showVisualization;
return config;
}
};
// Test plain text output - basic query only
TEST_F(ResponseFormatterTest, PlainTextBasicQuery) {
auto result = createBasicResult();
auto config = createConfig(false, false, false, false);
std::string output = ResponseFormatter::formatResponse(result, config);
EXPECT_EQ(output, "-- Query:\nSELECT * FROM users");
}
// Test plain text output with explanation
TEST_F(ResponseFormatterTest, PlainTextWithExplanation) {
auto result = createBasicResult();
auto config = createConfig(false, true, false, false);
std::string output = ResponseFormatter::formatResponse(result, config);
EXPECT_THAT(output, testing::HasSubstr("SELECT * FROM users"));
EXPECT_THAT(output, testing::HasSubstr("-- Explanation:"));
EXPECT_THAT(output, testing::HasSubstr("Retrieves all users"));
}
// Test plain text wrapping for long explanation
TEST_F(ResponseFormatterTest, PlainTextLongExplanationWrapping) {
auto result = createBasicResult();
result.explanation =
"This is a very long explanation that definitely exceeds seventy "
"characters in length and should be wrapped across multiple lines to "
"ensure proper formatting and readability in the output";
auto config = createConfig(false, true, false, false);
std::string output = ResponseFormatter::formatResponse(result, config);
EXPECT_THAT(output, testing::HasSubstr("-- Explanation:"));
EXPECT_THAT(output, testing::HasSubstr("This is a very long explanation"));
EXPECT_THAT(output, testing::HasSubstr("ensure proper formatting"));
std::istringstream stream(output);
std::string line;
int explanation_lines = 0;
bool in_explanation = false;
while (std::getline(stream, line)) {
if (line.find("-- Explanation:") != std::string::npos) {
in_explanation = true;
continue;
}
if (in_explanation) {
if (line.find("-- ") == 0) {
explanation_lines++;
EXPECT_LE(line.length(), 70);
} else {
break;
}
}
}
EXPECT_GT(explanation_lines, 1);
}
// Test plain text output with single warning
TEST_F(ResponseFormatterTest, PlainTextWithSingleWarning) {
auto result = createBasicResult();
result.warnings = {"Performance may be slow"};
auto config = createConfig(false, false, true, false);
std::string output = ResponseFormatter::formatResponse(result, config);
EXPECT_THAT(output, testing::HasSubstr("-- Warning:"));
EXPECT_THAT(output, testing::HasSubstr("Performance may be slow"));
}
// Test plain text output with multiple warnings
TEST_F(ResponseFormatterTest, PlainTextWithMultipleWarnings) {
auto result = createResultWithWarnings();
auto config = createConfig(false, false, true, false);
std::string output = ResponseFormatter::formatResponse(result, config);
EXPECT_THAT(output, testing::HasSubstr("-- Warnings:"));
EXPECT_THAT(output, testing::HasSubstr("1. Consider adding LIMIT"));
EXPECT_THAT(output, testing::HasSubstr("2. Full table scan"));
}
TEST_F(ResponseFormatterTest, PlainTextLongWarningWrapping) {
auto result = createBasicResult();
result.warnings = {
"This warning message is intentionally very long to test the text "
"wrapping functionality and ensure that it properly breaks across "
"multiple lines without exceeding the maximum width limit"};
auto config = createConfig(false, false, true, false);
std::string output = ResponseFormatter::formatResponse(result, config);
EXPECT_THAT(output, testing::HasSubstr("-- Warning:"));
std::istringstream stream(output);
std::string line;
int warning_lines = 0;
while (std::getline(stream, line)) {
if (line.find("-- ") == 0) {
warning_lines++;
EXPECT_LE(line.length(), 70);
}
}
EXPECT_GT(warning_lines, 1);
}
// Test plain text output with visualization
TEST_F(ResponseFormatterTest, PlainTextWithVisualization) {
auto result = createBasicResult();
result.suggested_visualization = "bar_chart";
auto config = createConfig(false, false, false, true);
std::string output = ResponseFormatter::formatResponse(result, config);
EXPECT_THAT(output, testing::HasSubstr("-- Suggested Visualization:"));
EXPECT_THAT(output, testing::HasSubstr("bar_chart"));
}
// Test plain text output with row limit note
TEST_F(ResponseFormatterTest, PlainTextWithRowLimitNote) {
auto result = createBasicResult();
result.row_limit_applied = true;
auto config = createConfig(false, false, false, false);
std::string output = ResponseFormatter::formatResponse(result, config);
EXPECT_THAT(output,
testing::HasSubstr("Row limit was automatically applied"));
}
// Test plain text output with all options enabled
TEST_F(ResponseFormatterTest, PlainTextAllOptions) {
auto result = createResultWithWarnings();
result.suggested_visualization = "line_chart";
auto config = createConfig(false, true, true, true);
std::string output = ResponseFormatter::formatResponse(result, config);
EXPECT_THAT(output, testing::HasSubstr("SELECT * FROM large_table"));
EXPECT_THAT(output, testing::HasSubstr("-- Explanation:"));
EXPECT_THAT(output, testing::HasSubstr("-- Warnings:"));
EXPECT_THAT(output, testing::HasSubstr("-- Suggested Visualization:"));
EXPECT_THAT(output,
testing::HasSubstr("Row limit was automatically applied"));
}
// Test JSON output - basic query
TEST_F(ResponseFormatterTest, JSONBasicQuery) {
auto result = createBasicResult();
auto config = createConfig(true, false, false, false);
std::string output = ResponseFormatter::formatResponse(result, config);
json j = json::parse(output);
EXPECT_EQ(j["query"], "SELECT * FROM users");
EXPECT_TRUE(j["success"]);
EXPECT_FALSE(j.contains("explanation"));
EXPECT_FALSE(j.contains("warnings"));
EXPECT_FALSE(j.contains("suggested_visualization"));
}
// Test JSON output with explanation
TEST_F(ResponseFormatterTest, JSONWithExplanation) {
auto result = createBasicResult();
auto config = createConfig(true, true, false, false);
std::string output = ResponseFormatter::formatResponse(result, config);
json j = json::parse(output);
EXPECT_EQ(j["query"], "SELECT * FROM users");
EXPECT_EQ(j["explanation"], "Retrieves all users");
}
// Test JSON output with warnings
TEST_F(ResponseFormatterTest, JSONWithWarnings) {
auto result = createResultWithWarnings();
auto config = createConfig(true, false, true, false);
std::string output = ResponseFormatter::formatResponse(result, config);
json j = json::parse(output);
EXPECT_TRUE(j.contains("warnings"));
EXPECT_TRUE(j["warnings"].is_array());
EXPECT_EQ(j["warnings"].size(), 2);
EXPECT_EQ(j["warnings"][0], "Consider adding LIMIT");
EXPECT_EQ(j["warnings"][1], "Full table scan");
}
// Test JSON output with visualization
TEST_F(ResponseFormatterTest, JSONWithVisualization) {
auto result = createBasicResult();
result.suggested_visualization = "pie_chart";
auto config = createConfig(true, false, false, true);
std::string output = ResponseFormatter::formatResponse(result, config);
json j = json::parse(output);
EXPECT_EQ(j["suggested_visualization"], "pie_chart");
}
// Test JSON output with row_limit_applied
TEST_F(ResponseFormatterTest, JSONWithRowLimit) {
auto result = createBasicResult();
result.row_limit_applied = true;
auto config = createConfig(true, false, false, false);
std::string output = ResponseFormatter::formatResponse(result, config);
json j = json::parse(output);
EXPECT_TRUE(j["row_limit_applied"]);
}
// Test JSON output does NOT include row_limit_applied when false
TEST_F(ResponseFormatterTest, JSONNoRowLimitWhenFalse) {
auto result = createBasicResult();
result.row_limit_applied = false;
auto config = createConfig(true, false, false, false);
std::string output = ResponseFormatter::formatResponse(result, config);
json j = json::parse(output);
EXPECT_FALSE(j.contains("row_limit_applied"));
}
// Test JSON output with all options
TEST_F(ResponseFormatterTest, JSONAllOptions) {
auto result = createResultWithWarnings();
result.suggested_visualization = "scatter";
auto config = createConfig(true, true, true, true);
std::string output = ResponseFormatter::formatResponse(result, config);
json j = json::parse(output);
EXPECT_EQ(j["query"], "SELECT * FROM large_table");
EXPECT_TRUE(j["success"]);
EXPECT_EQ(j["explanation"], "Query may be slow");
EXPECT_EQ(j["warnings"].size(), 2);
EXPECT_EQ(j["suggested_visualization"], "scatter");
EXPECT_TRUE(j["row_limit_applied"]);
}
// Test empty explanation is not included
TEST_F(ResponseFormatterTest, EmptyExplanationNotIncluded) {
auto result = createBasicResult();
result.explanation = "";
auto config =
createConfig(true, true, false, false); // show_explanation = true
std::string output = ResponseFormatter::formatResponse(result, config);
json j = json::parse(output);
// Empty explanation should not be included even with show_explanation = true
EXPECT_FALSE(j.contains("explanation"));
}
// Test empty warnings array is not included
TEST_F(ResponseFormatterTest, EmptyWarningsNotIncluded) {
auto result = createBasicResult();
result.warnings.clear();
auto config = createConfig(true, false, true, false); // show_warnings = true
std::string output = ResponseFormatter::formatResponse(result, config);
json j = json::parse(output);
EXPECT_FALSE(j.contains("warnings"));
}
// Test empty visualization is not included
TEST_F(ResponseFormatterTest, EmptyVisualizationNotIncluded) {
auto result = createBasicResult();
result.suggested_visualization = "";
auto config = createConfig(true, false, false,
true); // show_suggested_visualization = true
std::string output = ResponseFormatter::formatResponse(result, config);
json j = json::parse(output);
EXPECT_FALSE(j.contains("suggested_visualization"));
}
// Test JSON output is pretty-printed (contains newlines)
TEST_F(ResponseFormatterTest, JSONIsPrettyPrinted) {
auto result = createBasicResult();
auto config = createConfig(true, true, false, false);
std::string output = ResponseFormatter::formatResponse(result, config);
// Pretty-printed JSON should contain newlines
EXPECT_THAT(output, testing::HasSubstr("\n"));
}
// Test that documentation links appear in formatted response
TEST_F(ResponseFormatterTest, IncludesPostgresDocumentationLink) {
QueryResult result = createBasicResult();
result.explanation =
"PostgreSQL indexes improve query performance. "
"See https://www.postgresql.org/docs/current/indexes.html";
Configuration config = createConfig(false, true, false, false);
std::string formatted =
pg_ai::ResponseFormatter::formatResponse(result, config);
EXPECT_NE(formatted.find("postgresql.org/docs"), std::string::npos);
}
// Test specific documentation page
TEST_F(ResponseFormatterTest, IndexDocumentationLinkPresent) {
QueryResult result = createBasicResult();
result.explanation =
"Documentation: https://www.postgresql.org/docs/current/indexes.html";
Configuration config = createConfig(false, true, false, false);
std::string formatted =
pg_ai::ResponseFormatter::formatResponse(result, config);
EXPECT_NE(formatted.find("indexes.html"), std::string::npos);
}
// Test when no documentation link exists
TEST_F(ResponseFormatterTest, HandlesMissingDocumentationLink) {
QueryResult result = createBasicResult();
result.explanation = "PostgreSQL indexes improve performance.";
Configuration config = createConfig(false, true, false, false);
std::string formatted =
pg_ai::ResponseFormatter::formatResponse(result, config);
EXPECT_EQ(formatted.find("postgresql.org/docs"), std::string::npos);
}