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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
|
//! The renderer: query string in, index terms out.
//!
//! SCOUT section implemented: SCOUT-NATIVE §3 (the grammar), reached through
//! the public [`gn_search_engine::grammar`] API. The grammar's own tables are
//! tested per prefix and per slot in `src/grammar.rs`; this file tests what
//! a query actually becomes, which is what an index backend consumes.
use gn_search_engine::grammar::{RenderError, RenderedTerm, SyntaxFault, TermKind, GRAMMAR};
use gn_search_engine::SearchError;
fn terms(q: &str) -> Vec<RenderedTerm> {
GRAMMAR.render(q).expect("the query renders").terms
}
fn one(q: &str) -> RenderedTerm {
let mut rendered = terms(q);
assert_eq!(rendered.len(), 1, "{q:?} must render exactly one term");
rendered.remove(0)
}
/// Every boolean prefix, one case each: the index term is the prefix plus
/// the LOWERCASED value, because the index stores only lowercase boolean
/// terms (SCOUT-NATIVE §3).
#[test]
fn every_boolean_prefix_renders_a_lowercase_exact_term() {
for (field, prefix, value) in [
("author", "A", "Williams"),
("species", "XS", "Mouse"),
("group", "XG", "BXD"),
("tissue", "XI", "Liver"),
("dataset", "XDS", "BXDPublish"),
("symbol", "XY", "Bdnf"),
("chr", "XC", "16"),
("peakchr", "XPC", "1"),
] {
let term = one(&format!("{field}:{value}"));
assert_eq!(term.prefix, prefix, "prefix for {field}");
assert_eq!(
term.term,
format!("{prefix}{}", value.to_lowercase()),
"term for {field}"
);
assert_eq!(term.kind, TermKind::Boolean, "kind for {field}");
}
}
/// Two spellings of the same boolean value render to the same term — this
/// is the behaviour that makes `dataset:BXDPublish` match an index full of
/// lowercase terms (SCOUT-NATIVE §3).
#[test]
fn boolean_value_case_does_not_change_the_term() {
assert_eq!(
one("dataset:BXDPublish").term,
one("dataset:bxdpublish").term
);
}
/// Every free-text prefix, aliases included (gn3 registers `RIF` and `WIKI`
/// as well as `rif` and `wiki`; SCOUT-NATIVE §3).
#[test]
fn every_probabilistic_prefix_renders_a_field_term() {
for (field, prefix, value) in [
("description", "XD", "Alcohol"),
("rif", "XRF", "Liver"),
("RIF", "XRF", "Liver"),
("wiki", "XWK", "Strain"),
("WIKI", "XWK", "Strain"),
] {
let term = one(&format!("{field}:{value}"));
assert_eq!(term.prefix, prefix, "prefix for {field}");
assert_eq!(
term.term,
format!("{prefix}{}", value.to_lowercase()),
"term for {field}"
);
assert_eq!(term.kind, TermKind::Probabilistic, "kind for {field}");
}
}
/// Every numeric range field, one case each: marker, raw value, slot, and
/// bounds — including the open sides the grammar allows (SCOUT-NATIVE §3).
#[test]
fn every_numeric_slot_renders_a_value_range() {
for (field, marker, slot, value, begin, end) in [
("mean", "mean:", 0u32, "1..5", Some(1.0), Some(5.0)),
("peak", "peak:", 1u32, "5..", Some(5.0), None),
("peakmb", "peakmb:", 3u32, "..20", None, Some(20.0)),
(
"additive",
"additive:",
4u32,
"-2..2",
Some(-2.0),
Some(2.0),
),
(
"year",
"year:",
5u32,
"2010..2020",
Some(2010.0),
Some(2020.0),
),
] {
let term = one(&format!("{field}:{value}"));
assert_eq!(term.prefix, marker, "marker for {field}");
assert_eq!(term.term, value, "raw value for {field}");
assert_eq!(
term.kind,
TermKind::ValueRange { slot, begin, end },
"slot {slot} for {field}"
);
}
}
/// A numeric field value WITHOUT `..` is free text, not a point range — and
/// the colon is a WORD BREAK, so `year:2014` is the two words `year` and
/// `2014`, which is what xapian builds (`native.rs:722-725`: "`mean:5` is a
/// `mean 5` phrase"; `native.rs:389-405` inspects only values containing
/// `..`). `REVIEW-FIXES.md` §d blocker 3; DECISIONS row 34.
#[test]
fn a_bare_numeric_value_renders_as_its_words() {
for (q, words) in [
("year:2014", ["year", "2014"]),
("mean:5", ["mean", "5"]),
("peak:1.5", ["peak", "1.5"]),
("additive:-2", ["additive", "-2"]),
] {
let rendered = terms(q);
assert_eq!(
rendered.len(),
2,
"{q:?} is two words, not a range: {rendered:?}"
);
for (term, word) in rendered.iter().zip(words) {
assert_eq!(term.kind, TermKind::FreeText, "{q:?} is text, not a range");
assert_eq!(term.term, word, "{q:?} word");
assert_eq!(term.prefix, "", "a text term carries no field label");
}
assert!(
GRAMMAR.render(q).is_ok(),
"{q:?} must render, not be rejected"
);
}
}
/// A non-numeric value WITHOUT `..` is not a range at all, so it is not an
/// error either: `mean:abc` renders as its two words and a 200 is reachable
/// (`native.rs:389-405` inspects only values containing `..`; the recorded
/// 422, Q52, is `mean:abc..def`).
#[test]
fn a_non_numeric_value_without_a_range_is_not_a_422() {
for q in ["mean:abc", "year:twenty", "peak:abc"] {
let rendered = terms(q);
assert_eq!(
rendered.len(),
2,
"{q:?} is the label and the value: {rendered:?}"
);
assert!(
rendered.iter().all(|term| term.kind == TermKind::FreeText),
"{q:?} is text: {rendered:?}"
);
assert!(
GRAMMAR.render(q).is_ok(),
"{q:?} must render, not be rejected"
);
}
}
/// A field token whose value the punctuation grammar does not consume is a
/// PHRASE at xapian, not an AND of its words: GN4 measures `mean:5` as a
/// `mean 5` phrase (`native.rs:721-724`) and its positionless index answers a
/// phrase with **0** hits (Q04/Q11/Q12). The rendered terms therefore carry
/// [`RenderedTerm::phrase`], so a real backend can hand the words to xapian's
/// parser as adjacency instead of ANDing them — which is the only shape that
/// can ever be parity (`REVIEW-BLOCKERS.md` §5 blocker 2; DECISIONS row 36).
#[test]
fn a_bare_field_value_is_a_phrase_not_an_and() {
for (q, words) in [("year:2014", ["year", "2014"]), ("mean:5", ["mean", "5"])] {
let rendered = terms(q);
assert_eq!(rendered.len(), 2, "{q:?} is two phrase words: {rendered:?}");
for (term, word) in rendered.iter().zip(words) {
assert_eq!(term.term, word, "{q:?} word");
assert_eq!(term.kind, TermKind::FreeText, "{q:?} is text");
assert!(
term.phrase,
"{q:?} is a phrase at GN4, not an AND of words: {rendered:?}"
);
}
}
// One word is not a phrase group: `position:` is the word `position`.
assert!(!one("position:").phrase);
// An unregistered name is not a field, so nothing is split (Q50), and a
// standalone word is not a phrase either.
assert_eq!(one("foo:bar").term, "foo:bar");
assert!(!one("foo:bar").phrase);
assert!(!one("alcohol").phrase);
// A range is a predicate, not a phrase.
assert!(!one("mean:1..2").phrase);
}
/// A quoted value is phrase content, so a `..` inside quotes is not range
/// syntax: `mean:"1..2"` is the words `mean` and `1..2`, never a slot-0
/// range and never the Q52 422 — GN4's scan skips quoted content and stops
/// the value at a quote (`native.rs:380-385`, `:398-401`).
/// `REVIEW-FIXES.md` §d blocker 4.
#[test]
fn a_quoted_value_is_never_a_range() {
let rendered = terms("mean:\"1..2\"");
assert_eq!(rendered.len(), 2, "two words: {rendered:?}");
assert_eq!(rendered[0].term, "mean");
assert_eq!(
rendered[1].term, "1..2",
"the phrase content without its quotes"
);
for term in &rendered {
assert_eq!(
term.kind,
TermKind::FreeText,
"phrase content is text, not a VALUE range"
);
}
assert!(
GRAMMAR.render("mean:\"1..2\"").is_ok(),
"a quoted range must not be a 422"
);
// The unquoted form is still the range it always was.
assert_eq!(
one("mean:1..2").kind,
TermKind::ValueRange {
slot: 0,
begin: Some(1.0),
end: Some(2.0)
}
);
}
/// Quote state is a property of the QUERY, not of the token: GN4's range guard
/// walks the whole string with an `in_quotes` state machine
/// (`native.rs:376-385`) and stops a field value at the next quote (`:399`).
/// So a range-looking token inside a quoted phrase is phrase content, and
/// `mean:1.."2"` is the one-sided range `1..` plus the phrase word `2`.
/// Neither is the Q52 422 — the per-token test made both of them one
/// (`REVIEW-BLOCKERS.md` §5 blocker 1).
#[test]
fn a_range_looking_token_inside_a_quote_is_not_a_422() {
// The token starts INSIDE a quoted region: `mean:abc..def` is phrase
// content there, not a bound, because the phrase opened before it.
let rendered = terms("\"alcohol mean:abc..def\"");
assert_eq!(rendered.len(), 2, "two phrase words: {rendered:?}");
assert_eq!(rendered[0].term, "alcohol");
assert_eq!(rendered[1].term, "mean:abc..def");
for term in &rendered {
assert_eq!(
term.kind,
TermKind::FreeText,
"phrase content is text, not a predicate: {rendered:?}"
);
assert!(term.phrase, "the words of a quoted region are a phrase");
}
assert!(
GRAMMAR.render("\"alcohol mean:abc..def\"").is_ok(),
"a quoted phrase is a 200 at GN4, never the Q52 422"
);
// The token starts outside the quotes and the value ENDS at the quote: the
// range keeps its one parsed side and the quoted tail is phrase content
// the range did not consume.
let rendered = terms("mean:1..\"2\"");
assert_eq!(
rendered.len(),
2,
"a one-sided range and a phrase word: {rendered:?}"
);
assert_eq!(
rendered[0].kind,
TermKind::ValueRange {
slot: 0,
begin: Some(1.0),
end: None
},
"the value stops at the quote, so the upper side is open"
);
assert_eq!(rendered[1].term, "2", "the quoted tail is phrase content");
assert!(rendered[1].phrase);
assert!(
GRAMMAR.render("mean:1..\"2\"").is_ok(),
"a value that ends at a quote is not a bad bound"
);
// An operator inside a quoted region is phrase content as well: GN4's
// parser reads quoted content as a phrase, never as a boolean expression.
let rendered = terms("\"alcohol AND dependence\"");
assert_eq!(rendered.len(), 3, "three phrase words: {rendered:?}");
assert!(
rendered
.iter()
.all(|term| term.kind == TermKind::FreeText && term.phrase),
"a quoted `AND` is a word, not an operator: {rendered:?}"
);
// The UNQUOTED shape is still the recorded Q52 422.
assert_eq!(
GRAMMAR
.render("mean:abc..def")
.expect_err("abc is not a number"),
RenderError::RangeBound {
field: "mean".to_string(),
value: "abc..def".to_string(),
}
);
}
/// An empty field value is free text, not match-nothing: `position:` never
/// reaches a processor, and the recorded Q53 answer is a 200 with 576 hits —
/// hits on the WORD `position`, which is why the label survives as a term
/// and its colon does not (`native.rs:264-270`; DECISIONS rows 30 and 34).
#[test]
fn an_empty_field_value_is_the_label_word() {
for (q, word) in [
("position:", "position"),
("pos:", "pos"),
("mean:", "mean"),
("author:", "author"),
("dataset:", "dataset"),
("Hs:", "Hs"),
] {
let term = one(q);
assert_eq!(term.kind, TermKind::FreeText, "{q:?} is text");
assert_eq!(term.term, word, "{q:?} keeps the label as the word");
assert_eq!(term.prefix, "", "{q:?} carries no field label");
}
// And it renders rather than being rejected.
assert!(GRAMMAR.render("position:").is_ok());
}
/// The both-open range matches nothing, which every backend reads the same
/// way (SCOUT-NATIVE §3).
#[test]
fn the_both_open_range_has_no_bounds() {
let term = one("mean:..");
assert_eq!(
term.kind,
TermKind::ValueRange {
slot: 0,
begin: None,
end: None
}
);
}
/// Slot 2: a point location expands by 50 kb on each side, in megabases —
/// the unit the slot holds (SCOUT-NATIVE §3).
#[test]
fn position_point_expands_by_fifty_kilobases() {
let term = one("position:1M");
assert_eq!(term.prefix, "position:");
assert_eq!(
term.kind,
TermKind::ValueRange {
slot: 2,
begin: Some(0.95),
end: Some(1.05)
}
);
}
/// The `pos` alias is the same rule, and the window never goes below 0
/// (SCOUT-NATIVE §3).
#[test]
fn position_never_expands_below_zero() {
let term = one("pos:10k");
assert_eq!(term.prefix, "pos:");
assert_eq!(
term.kind,
TermKind::ValueRange {
slot: 2,
begin: Some(0.0),
end: Some(0.06)
}
);
}
/// A position range keeps open sides and converts both to megabases.
#[test]
fn position_range_converts_to_megabases() {
let term = one("position:100000..200000");
assert_eq!(
term.kind,
TermKind::ValueRange {
slot: 2,
begin: Some(0.1),
end: Some(0.2)
}
);
}
/// An unparseable position declines to "match nothing" instead of an error:
/// gn3's escaped `ValueError` is not a contract, and GN4's blessed
/// behaviour for the class is a 200 with no hits (SCOUT-NATIVE §3, §6).
#[test]
fn unparseable_position_matches_nothing() {
let term = one("position:abc");
assert_eq!(
term.kind,
TermKind::ValueRange {
slot: 2,
begin: None,
end: None
}
);
}
/// A numeric bound that is not a number on a `..` range is the recorded Q52
/// diagnostic and reaches a caller as the 422-producing variant, not as a
/// panic or an empty result (SCOUT-NATIVE §6).
#[test]
fn bad_numeric_bound_is_an_invalid_query() {
let error = GRAMMAR
.render("mean:abc..def")
.expect_err("abc is not a number");
assert_eq!(
error,
RenderError::RangeBound {
field: "mean".to_string(),
value: "abc..def".to_string(),
}
);
assert_eq!(error.message(), "Unknown range operation");
assert_eq!(error.xapian_error_type(), "QueryParserError");
match gn_search_engine::search("", "mean:abc..def", "phenotype", 20, 1) {
Err(SearchError::InvalidQuery {
message,
xapian_error_type,
}) => {
assert_eq!(message, "Unknown range operation");
assert_eq!(xapian_error_type.as_deref(), Some("QueryParserError"));
}
other => panic!("expected InvalidQuery, got {other:?}"),
}
}
/// `Hs:` / `Mm:` are species ∧ chromosome ∧ slot-2 range, with no liftover:
/// `Hs:chr2:1M..1.2M` is `XShuman` + `XC2` + 1.0–1.2 Mb (Q40, 4296 recorded
/// hits; GN4's `LocationProcessor`, `native.rs:310-350`).
#[test]
fn a_human_location_renders_species_chromosome_and_position() {
let rendered = terms("Hs:chr2:1M..1.2M");
assert_eq!(rendered.len(), 3, "species, chromosome and position");
assert_eq!(rendered[0].prefix, "XS");
assert_eq!(rendered[0].term, "XShuman");
assert_eq!(rendered[0].kind, TermKind::Boolean);
assert_eq!(rendered[1].prefix, "XC");
assert_eq!(rendered[1].term, "XC2");
assert_eq!(rendered[1].kind, TermKind::Boolean);
assert_eq!(rendered[2].prefix, "Hs:");
assert_eq!(
rendered[2].kind,
TermKind::ValueRange {
slot: 2,
begin: Some(1.0),
end: Some(1.2),
}
);
}
/// The mouse shorthand is the same shape on slot 2, and a point location
/// expands by ±50 kb like `position:` (Q41 `Mm:chr1:5M`, 491 recorded hits).
#[test]
fn a_mouse_location_point_expands_like_a_position() {
let rendered = terms("Mm:chr1:5M");
assert_eq!(rendered.len(), 3);
assert_eq!(rendered[0].term, "XSmouse");
assert_eq!(rendered[1].term, "XC1");
assert_eq!(
rendered[2].kind,
TermKind::ValueRange {
slot: 2,
begin: Some(4.95),
end: Some(5.05),
}
);
// The value is lowercased before it is split, as gn3 does.
let rendered = terms("Hs:chrX:1M");
assert_eq!(rendered[1].term, "XCx");
}
/// An invalid location is the recorded Q42 class: a 422-shaped
/// `InvalidQuery` carrying `InvalidOperationError`, never a 200 and never an
/// mset crash (`native.rs:187-215`, AMENDMENT E).
#[test]
fn an_invalid_location_is_an_invalid_query() {
for q in ["Hs:foo:1M", "Mm:chr1", "Hs:chr1:abc", "Hs:1:2:3"] {
let error = GRAMMAR.render(q).expect_err("{q:?} is not a location");
assert!(
matches!(error, RenderError::InvalidLocation { .. }),
"{q:?} must be an InvalidLocation, got {error:?}"
);
assert_eq!(
error.message(),
"InvalidOperationError: Query is invalid (invalid Hs/Mm location field)"
);
assert_eq!(error.xapian_error_type(), "InvalidOperationError");
match gn_search_engine::search("", q, "phenotype", 20, 1) {
Err(SearchError::InvalidQuery {
message,
xapian_error_type,
}) => {
assert_eq!(xapian_error_type.as_deref(), Some("InvalidOperationError"));
assert!(message.starts_with("InvalidOperationError: Query is invalid"));
}
other => panic!("{q:?} must be InvalidQuery, got {other:?}"),
}
}
}
/// A dangling operator is a syntax fault — the recorded Q51 shape, which GN4
/// reports as 422 `invalid_query` with xapian's diagnostic (F6).
#[test]
fn a_dangling_operator_is_a_syntax_error() {
for q in [
"alcohol AND",
"AND alcohol",
"alcohol AND OR dependence",
// A `NOT` at the end has a term on neither side it could negate.
"alcohol AND NOT",
"a AND (NOT",
] {
let error = GRAMMAR.render(q).expect_err("{q:?} is not a query");
assert_eq!(
error,
RenderError::Syntax {
fault: SyntaxFault::DanglingOperator
}
);
assert_eq!(error.message(), "Syntax: <expression> AND <expression>");
assert_eq!(error.xapian_error_type(), "QueryParserError");
}
match gn_search_engine::search("", "alcohol AND", "phenotype", 20, 1) {
Err(SearchError::InvalidQuery { message, .. }) => {
assert_eq!(message, "Syntax: <expression> AND <expression>");
}
other => panic!("expected InvalidQuery, got {other:?}"),
}
// Operators in their legal places are untouched — including a leading
// `NOT`, whose xapian behaviour no recorded shape pins.
assert!(GRAMMAR.render("alcohol AND dependence").is_ok());
assert!(GRAMMAR.render("alcohol NOT dependence").is_ok());
assert!(GRAMMAR.render("NOT alcohol").is_ok());
assert!(GRAMMAR
.render("(alcohol OR insulin) AND species:mouse")
.is_ok());
}
/// `NOT` is a PREFIX operator: it needs no term on its left, so it is legal
/// after a binary operator too. `alcohol AND NOT dependence` and
/// `a AND (NOT b)` are the canonical xapian exclusions and must render, not
/// 422 (`REVIEW-FIXES.md` §d blocker 1 — a 422 where production answers is
/// the direction a parity claim cannot afford).
#[test]
fn a_not_after_a_binary_operator_is_not_dangling() {
for q in [
"alcohol AND NOT dependence",
"a AND (NOT b)",
"alcohol OR NOT dependence",
"NOT alcohol",
] {
GRAMMAR
.render(q)
.unwrap_or_else(|error| panic!("{q:?} must render, got {error:?}"));
assert!(GRAMMAR.render(q).is_ok(), "{q:?} must render, not be a 422");
}
let rendered = terms("alcohol AND NOT dependence");
assert_eq!(
rendered.len(),
4,
"two words and two operators: {rendered:?}"
);
assert_eq!(rendered[1].term, "AND");
assert_eq!(rendered[2].term, "NOT");
assert_eq!(rendered[2].kind, TermKind::Operator);
}
/// An unbalanced quote is the renderer's own rejection — and NOT the engine's.
///
/// This is the one renderer rule the moved engine does not share: xapian
/// parses `alcohol "dependence` as a phrase whose close it supplies, so
/// `gn_search_engine::search` accepts it (measured after the move: it reaches the
/// index and fails as `Internal`, not `InvalidQuery`). The engine is
/// authority, so this test pins the DOUBLE's rule and says so, rather than
/// claiming a production class it does not have. No recorded shape carries an
/// unterminated phrase (F6), so nothing graded moves.
#[test]
fn an_unbalanced_quote_is_the_renderers_own_syntax_error() {
let error = GRAMMAR
.render("alcohol \"dependence")
.expect_err("the phrase never closes");
assert_eq!(
error,
RenderError::Syntax {
fault: SyntaxFault::UnbalancedQuotes
}
);
assert_eq!(error.xapian_error_type(), "QueryParserError");
// A balanced phrase is not a syntax fault (the recorded Q04 shape).
assert!(GRAMMAR.render("\"alcohol dependence\"").is_ok());
}
/// Free text, operators and unregistered prefixes render as themselves;
/// structure (grouping, precedence) is the backend's (SCOUT-NATIVE §3).
#[test]
fn free_text_operators_and_unknown_prefixes_pass_through() {
let rendered = terms("alcohol AND author:Williams");
assert_eq!(rendered.len(), 3);
assert_eq!(rendered[0].kind, TermKind::FreeText);
assert_eq!(rendered[0].term, "alcohol");
assert_eq!(rendered[0].prefix, "");
assert_eq!(rendered[1].kind, TermKind::Operator);
assert_eq!(rendered[1].term, "AND");
assert_eq!(rendered[2].kind, TermKind::Boolean);
assert_eq!(rendered[2].term, "Awilliams");
// Lowercase operators are plain terms, not operators.
let rendered = terms("alcohol and");
assert_eq!(rendered[1].kind, TermKind::FreeText);
assert_eq!(rendered[1].term, "and");
// An unregistered prefix is text (recorded shape Q50: `foo:bar`).
let rendered = one("foo:bar");
assert_eq!(rendered.kind, TermKind::FreeText);
assert_eq!(rendered.term, "foo:bar");
}
/// Parentheses around a field term do not stop it being one.
#[test]
fn parentheses_are_trimmed_before_classifying() {
let rendered = one("(dataset:bxd)");
assert_eq!(rendered.kind, TermKind::Boolean);
assert_eq!(rendered.term, "XDSbxd");
}
/// The document type travels with the query, as the `XT{type}` term an
/// index filters on (SCOUT-NATIVE §3, §4).
#[test]
fn type_filter_travels_with_the_query() {
let parsed = GRAMMAR.render("alcohol").expect("renders");
assert_eq!(parsed.raw, "alcohol");
assert_eq!(parsed.type_filter, None);
assert_eq!(parsed.type_term(), None);
let typed = parsed.clone().with_type("phenotype");
assert_eq!(typed.type_filter.as_deref(), Some("phenotype"));
assert_eq!(typed.type_term().as_deref(), Some("XTphenotype"));
assert_eq!(parsed.with_type("").type_term(), None);
}
/// An empty query renders no terms — the route, not the engine, rejects it
/// with 400 (SCOUT-NATIVE §6).
#[test]
fn empty_query_renders_no_terms() {
let parsed = GRAMMAR.render(" ").expect("renders");
assert!(parsed.terms.is_empty());
assert_eq!(parsed.raw, " ");
}
|