grpctestify 1.4.8

gRPC testing utility written in Rust
Documentation
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// EXTRACT section tests - JQ functions and metadata extraction

use grpctestify::execution::{ExecutionPlan, Workflow, WorkflowEvent};
use grpctestify::parser::{parse_gctf, parse_gctf_from_str};
use std::path::Path;

#[test]
fn test_extract_basic_jq_paths() {
    // Arrange
    let content = r#"
--- ENDPOINT ---
test.Service/Method

--- REQUEST ---
{"id": 123}

--- RESPONSE ---
{"id": 123, "name": "test", "value": 100}

--- EXTRACT ---
id = .id
name = .name
value = .value

--- ASSERTS ---
.id == 123
"#;

    // Act
    let doc = parse_gctf_from_str(content, "test.gctf").unwrap();
    let plan = ExecutionPlan::from_document(&doc);
    let workflow = Workflow::from_plan(&plan);
    let extracts = workflow.extractions();

    // Assert
    assert!(!extracts.is_empty(), "Expected at least 1 extract event");

    let result = workflow.validate();
    assert!(
        result.passed,
        "Workflow validation should pass: {:?}",
        result.errors
    );
}

#[test]
fn test_extract_string_functions() {
    // Arrange
    let content = r#"
--- ENDPOINT ---
test.Service/Method

--- REQUEST ---
{"name": "  Hello World  "}

--- RESPONSE ---
{"name": "  Hello World  ", "tags": "a,b,c"}

--- EXTRACT ---
upper = .name | upper
lower = .name | lower
trimmed = .name | trim
parts = .tags | split(",")
joined = .tags | split(",") | join("-")

--- ASSERTS ---
@len(.trimmed) > 0
"#;

    // Act
    let doc = parse_gctf_from_str(content, "test.gctf").unwrap();
    let plan = ExecutionPlan::from_document(&doc);
    let workflow = Workflow::from_plan(&plan);
    let extracts = workflow.extractions();

    // Assert
    assert!(!extracts.is_empty(), "Expected at least 1 extract event");

    let result = workflow.validate();
    assert!(
        result.passed,
        "Workflow validation should pass: {:?}",
        result.errors
    );
}

#[test]
fn test_extract_numeric_aggregations() {
    // Arrange
    let content = r#"
--- ENDPOINT ---
test.Service/Method

--- REQUEST ---
{"items": [{"price": 10}, {"price": 20}, {"price": 30}]}

--- RESPONSE ---
{"items": [{"price": 10}, {"price": 20}, {"price": 30}]}

--- EXTRACT ---
count = .items | length
avg = [.items[].price] | avg
min = [.items[].price] | min
max = [.items[].price] | max
sum = [.items[].price] | add

--- ASSERTS ---
.count == 3
"#;

    // Act
    let doc = parse_gctf_from_str(content, "test.gctf").unwrap();
    let plan = ExecutionPlan::from_document(&doc);
    let workflow = Workflow::from_plan(&plan);
    let extracts = workflow.extractions();

    // Assert
    assert!(!extracts.is_empty(), "Expected at least 1 extract event");

    let result = workflow.validate();
    assert!(
        result.passed,
        "Workflow validation should pass: {:?}",
        result.errors
    );
}

#[test]
fn test_extract_array_operations() {
    // Arrange
    let content = r#"
--- ENDPOINT ---
test.Service/Method

--- REQUEST ---
{"users": [{"name": "Alice", "active": true}, {"name": "Bob", "active": false}]}

--- RESPONSE ---
{"users": [{"name": "Alice", "active": true}, {"name": "Bob", "active": false}]}

--- EXTRACT ---
first = .users[0].name
names = [.users[].name]
active = [.users[] | select(.active == true)]
sorted = .users | sort_by(.name)
unique_names = [.users[].name] | unique

--- ASSERTS ---
@len(.names) == 2
"#;

    // Act
    let doc = parse_gctf_from_str(content, "test.gctf").unwrap();
    let plan = ExecutionPlan::from_document(&doc);
    let workflow = Workflow::from_plan(&plan);
    let extracts = workflow.extractions();

    // Assert
    assert!(!extracts.is_empty(), "Expected at least 1 extract event");

    let result = workflow.validate();
    assert!(
        result.passed,
        "Workflow validation should pass: {:?}",
        result.errors
    );
}

#[test]
fn test_extract_conditional() {
    // Arrange
    let content = r#"
--- ENDPOINT ---
test.Service/Method

--- REQUEST ---
{"status": 200}

--- RESPONSE ---
{"status": 200}

--- EXTRACT ---
label = if .status == 200 then "OK" elif .status == 404 then "Not Found" else "Error" end
default_name = .name // "Anonymous"
default_port = .port // 8080

--- ASSERTS ---
.label == "OK"
"#;

    // Act
    let doc = parse_gctf_from_str(content, "test.gctf").unwrap();
    let plan = ExecutionPlan::from_document(&doc);
    let workflow = Workflow::from_plan(&plan);
    let extracts = workflow.extractions();

    // Assert
    assert!(!extracts.is_empty(), "Expected at least 1 extract event");

    let result = workflow.validate();
    assert!(
        result.passed,
        "Workflow validation should pass: {:?}",
        result.errors
    );
}

#[test]
fn test_extract_datetime() {
    // Arrange
    let content = r#"
--- ENDPOINT ---
test.Service/Method

--- REQUEST ---
{"created_at": "2024-01-15T10:30:00Z"}

--- RESPONSE ---
{"created_at": "2024-01-15T10:30:00Z"}

--- EXTRACT ---
date_only = .created_at | split("T")[0]
time_only = .created_at | split("T")[1] | split("Z")[0]

--- ASSERTS ---
@len(.date_only) > 0
"#;

    // Act
    let doc = parse_gctf_from_str(content, "test.gctf").unwrap();
    let plan = ExecutionPlan::from_document(&doc);
    let workflow = Workflow::from_plan(&plan);
    let extracts = workflow.extractions();

    // Assert
    assert!(!extracts.is_empty(), "Expected at least 1 extract event");

    let result = workflow.validate();
    assert!(
        result.passed,
        "Workflow validation should pass: {:?}",
        result.errors
    );
}

#[test]
fn test_extract_json5_syntax() {
    // Arrange
    let content = r#"
--- ENDPOINT ---
test.Service/Method

--- REQUEST ---
{
  id: 123,
  name: "test",
}
"#;

    // Act
    let result = parse_gctf_from_str(content, "test.gctf");

    // Assert
    assert!(result.is_ok(), "JSON5 syntax should be supported");
}

#[test]
fn test_extract_workflow_events() {
    // Arrange
    let content = r#"
--- ENDPOINT ---
test.Service/Method

--- REQUEST ---
{"id": 123}

--- RESPONSE ---
{"id": 123, "token": "abc123"}

--- EXTRACT ---
id = .id
token = .token

--- ASSERTS ---
.id == 123
"#;

    // Act
    let doc = parse_gctf_from_str(content, "test.gctf").unwrap();
    let plan = ExecutionPlan::from_document(&doc);
    let workflow = Workflow::from_plan(&plan);

    // Assert
    let has_extract = workflow
        .events
        .iter()
        .any(|e| matches!(e, WorkflowEvent::Extract { .. }));
    assert!(has_extract, "Workflow should have Extract event");

    let has_extracted = workflow
        .events
        .iter()
        .any(|e| matches!(e, WorkflowEvent::Extracted { .. }));
    assert!(has_extracted, "Workflow should have Extracted event");
}

#[test]
fn test_extract_chained_operations() {
    // Arrange
    let content = r#"
--- ENDPOINT ---
shop.OrderService/GetOrder

--- REQUEST ---
{"order_id": 123}

--- RESPONSE ---
{
  "items": [
    {"name": "Item 1", "price": 10.00, "qty": 2},
    {"name": "Item 2", "price": 25.00, "qty": 1}
  ],
  "tax_rate": 0.08
}

--- EXTRACT ---
item_count = .items | length
subtotal = [.items[].price * .items[].qty] | add
tax_amount = $subtotal * .tax_rate
total = $subtotal + $tax_amount
expensive = [.items[] | select(.price > 15) | .name]

--- ASSERTS ---
.item_count == 2
"#;

    // Act
    let doc = parse_gctf_from_str(content, "test.gctf").unwrap();
    let plan = ExecutionPlan::from_document(&doc);
    let workflow = Workflow::from_plan(&plan);
    let extracts = workflow.extractions();

    // Assert
    assert!(!extracts.is_empty(), "Expected at least 1 extract event");

    let result = workflow.validate();
    assert!(
        result.passed,
        "Workflow validation should pass: {:?}",
        result.errors
    );
}

#[test]
fn test_extract_from_example_file() {
    // Arrange
    let path = "examples/advanced/extract-jq-functions.gctf";

    // Act
    if !Path::new(path).exists() {
        return;
    }

    let doc = parse_gctf(Path::new(path)).unwrap();
    let plan = ExecutionPlan::from_document(&doc);
    let workflow = Workflow::from_plan(&plan);
    let extracts = workflow.extractions();

    // Assert
    assert!(!extracts.is_empty(), "Expected extract events from example");

    let result = workflow.validate();
    assert!(
        result.passed,
        "Example workflow validation should pass: {:?}",
        result.errors
    );
}

#[test]
fn test_extract_variable_in_asserts() {
    // Arrange
    let content = r#"
--- ENDPOINT ---
test.Service/Method

--- REQUEST ---
{"expected": 100}

--- RESPONSE ---
{"value": 100}

--- EXTRACT ---
expected_value = .value

--- ASSERTS ---
.value == {{ expected_value }}
"#;

    // Act
    let doc = parse_gctf_from_str(content, "test.gctf").unwrap();
    let plan = ExecutionPlan::from_document(&doc);
    let workflow = Workflow::from_plan(&plan);

    // Assert
    assert_eq!(plan.extractions.len(), 1, "Expected 1 extraction");

    let result = workflow.validate();
    assert!(
        result.passed,
        "Workflow validation should pass: {:?}",
        result.errors
    );
}