Skip to content

Commit bd538c4

Browse files
committed
move field to optional + add a test with the new field where
1 parent 1de9227 commit bd538c4

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/index.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ impl ValueIndexConfiguration {
3131
/// The expressions describe each coloumn of the index. The expressions could be specified
3232
/// in a JSON Array or in N1QL syntax using comma delimiter.
3333
/// The where clause is optional and is a predicate expression defining conditions for indexing documents.
34-
pub fn new(query_language: QueryLanguage, expressions: &str, where_: &str) -> Self {
34+
pub fn new(query_language: QueryLanguage, expressions: &str, where_: Option<&str>) -> Self {
3535
let expressions_slices = from_str(expressions);
36-
let where_slices = from_str(where_);
36+
let where_slices = from_str(where_.unwrap_or(""));
3737
Self {
3838
cbl_ref: CBLValueIndexConfiguration {
3939
expressionLanguage: query_language as u32,

tests/query_tests.rs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn get_index() {
114114
default_collection
115115
.create_index(
116116
"new_index1",
117-
&ValueIndexConfiguration::new(QueryLanguage::JSON, r#"[[".someField"]]"#, ""),
117+
&ValueIndexConfiguration::new(QueryLanguage::JSON, r#"[[".someField"]]"#, None),
118118
)
119119
.unwrap()
120120
);
@@ -135,7 +135,11 @@ fn get_index() {
135135
new_coll
136136
.create_index(
137137
"new_index2",
138-
&ValueIndexConfiguration::new(QueryLanguage::JSON, r#"[[".someField2"]]"#, ""),
138+
&ValueIndexConfiguration::new(
139+
QueryLanguage::JSON,
140+
r#"[[".someField2"]]"#,
141+
None
142+
),
139143
)
140144
.unwrap()
141145
);
@@ -159,7 +163,7 @@ fn full_index() {
159163
assert!(
160164
db.create_index(
161165
"new_index",
162-
&ValueIndexConfiguration::new(QueryLanguage::JSON, r#"[[".someField"]]"#, ""),
166+
&ValueIndexConfiguration::new(QueryLanguage::JSON, r#"[[".someField"]]"#, None),
163167
)
164168
.unwrap()
165169
);
@@ -215,7 +219,55 @@ fn partial_index() {
215219
&ValueIndexConfiguration::new(
216220
QueryLanguage::JSON,
217221
r#"{"WHAT": [[".id"]], "WHERE": ["=", [".someField"], "someValue"]}"#,
218-
""
222+
None
223+
),
224+
)
225+
.unwrap()
226+
);
227+
228+
// Check index creation
229+
let value = db.get_index_names().iter().next().unwrap();
230+
let name = value.as_string().unwrap();
231+
assert_eq!(name, "new_index");
232+
233+
// Check index used
234+
let query = Query::new(
235+
db,
236+
QueryLanguage::N1QL,
237+
"select _.* from _ where _.id = 'id' and _.someField='someValue'",
238+
)
239+
.expect("create query");
240+
241+
let index = get_index_name_from_explain(&query.explain().unwrap()).unwrap();
242+
assert_eq!(index, "new_index");
243+
244+
// Check index not used
245+
let query = Query::new(
246+
db,
247+
QueryLanguage::N1QL,
248+
"select _.* from _ where _.id = 'id' and _.someField='notSomeValue'",
249+
)
250+
.expect("create query");
251+
252+
let index = Regex::new(r"USING INDEX (\w+) ")
253+
.unwrap()
254+
.captures(&query.explain().unwrap())
255+
.map(|c| c.get(1).unwrap().as_str().to_string());
256+
257+
assert!(index.is_none());
258+
});
259+
}
260+
261+
#[test]
262+
fn partial_index_with_condition_field() {
263+
utils::with_db(|db| {
264+
assert!(
265+
db.create_index(
266+
"new_index",
267+
&ValueIndexConfiguration::new(
268+
QueryLanguage::JSON,
269+
r#"[[".id"]]"#,
270+
Some(r#"["=", [".someField"], "someValue"]"#)
219271
),
220272
)
221273
.unwrap()

0 commit comments

Comments
 (0)