Skip to content

Add unit tests for partial indexes #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "3.0.17-0"
enum_primitive = "*"
tempdir = "*"
lazy_static = "1.4.0"
regex = "1.10.4"

[dev-dependencies.cargo-husky]
version = "1"
Expand Down
92 changes: 90 additions & 2 deletions tests/query_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extern crate couchbase_lite;
extern crate regex;

use couchbase_lite::index::ValueIndexConfiguration;
use regex::Regex;

use self::couchbase_lite::*;

Expand Down Expand Up @@ -64,23 +66,109 @@ fn query() {
}

#[test]
fn indexes() {
fn full_index() {
utils::with_db(|db| {
assert!(db
.create_index(
"new_index",
&ValueIndexConfiguration::new(QueryLanguage::JSON, r#"[[".id"]]"#),
&ValueIndexConfiguration::new(QueryLanguage::JSON, r#"[[".someField"]]"#),
)
.unwrap());

// Check index creation
let value = db.get_index_names().iter().next().unwrap();
let name = value.as_string().unwrap();
assert_eq!(name, "new_index");

// Check index used
let query = Query::new(
db,
QueryLanguage::N1QL,
"select _.* from _ where _.someField = 'whatever'",
)
.expect("create query");

let index = Regex::new(r"USING INDEX (\w+) ")
.unwrap()
.captures(&query.explain().unwrap())
.map(|c| c.get(1).unwrap().as_str().to_string())
.unwrap();

assert_eq!(index, "new_index");

// Check index not used
let query = Query::new(
db,
QueryLanguage::N1QL,
"select _.* from _ where _.notSomeField = 'whatever'",
)
.expect("create query");

let index = Regex::new(r"USING INDEX (\w+) ")
.unwrap()
.captures(&query.explain().unwrap())
.map(|c| c.get(1).unwrap().as_str().to_string());

assert!(index.is_none());

// Check index deletion
assert_eq!(db.get_index_names().count(), 1);

db.delete_index("idx").unwrap();
assert_eq!(db.get_index_names().count(), 1);

db.delete_index("new_index").unwrap();
assert_eq!(db.get_index_names().count(), 0);
});
}

#[test]
fn partial_index() {
utils::with_db(|db| {
assert!(db
.create_index(
"new_index",
&ValueIndexConfiguration::new(
QueryLanguage::JSON,
r#"{"WHAT": [[".id"]], "WHERE": ["=", [".someField"], "someValue"]}"#
),
)
.unwrap());

// Check index creation
let value = db.get_index_names().iter().next().unwrap();
let name = value.as_string().unwrap();
assert_eq!(name, "new_index");

// Check index used
let query = Query::new(
db,
QueryLanguage::N1QL,
"select _.* from _ where _.id = 'id' and _.someField='someValue'",
)
.expect("create query");

let index = Regex::new(r"USING INDEX (\w+) ")
.unwrap()
.captures(&query.explain().unwrap())
.map(|c| c.get(1).unwrap().as_str().to_string())
.unwrap();

assert_eq!(index, "new_index");

// Check index not used
let query = Query::new(
db,
QueryLanguage::N1QL,
"select _.* from _ where _.id = 'id' and _.someField='notSomeValue'",
)
.expect("create query");

let index = Regex::new(r"USING INDEX (\w+) ")
.unwrap()
.captures(&query.explain().unwrap())
.map(|c| c.get(1).unwrap().as_str().to_string());

assert!(index.is_none());
});
}
Loading