|
1 | 1 | extern crate couchbase_lite;
|
| 2 | +extern crate regex; |
2 | 3 |
|
3 | 4 | use couchbase_lite::index::ValueIndexConfiguration;
|
| 5 | +use regex::Regex; |
4 | 6 |
|
5 | 7 | use self::couchbase_lite::*;
|
6 | 8 |
|
@@ -64,23 +66,109 @@ fn query() {
|
64 | 66 | }
|
65 | 67 |
|
66 | 68 | #[test]
|
67 |
| -fn indexes() { |
| 69 | +fn full_index() { |
68 | 70 | utils::with_db(|db| {
|
69 | 71 | assert!(db
|
70 | 72 | .create_index(
|
71 | 73 | "new_index",
|
72 |
| - &ValueIndexConfiguration::new(QueryLanguage::JSON, r#"[[".id"]]"#), |
| 74 | + &ValueIndexConfiguration::new(QueryLanguage::JSON, r#"[[".someField"]]"#), |
73 | 75 | )
|
74 | 76 | .unwrap());
|
75 | 77 |
|
| 78 | + // Check index creation |
76 | 79 | let value = db.get_index_names().iter().next().unwrap();
|
77 | 80 | let name = value.as_string().unwrap();
|
78 | 81 | assert_eq!(name, "new_index");
|
79 | 82 |
|
| 83 | + // Check index used |
| 84 | + let query = Query::new( |
| 85 | + db, |
| 86 | + QueryLanguage::N1QL, |
| 87 | + "select _.* from _ where _.someField = 'whatever'", |
| 88 | + ) |
| 89 | + .expect("create query"); |
| 90 | + |
| 91 | + let index = Regex::new(r"USING INDEX (\w+) ") |
| 92 | + .unwrap() |
| 93 | + .captures(&query.explain().unwrap()) |
| 94 | + .map(|c| c.get(1).unwrap().as_str().to_string()) |
| 95 | + .unwrap(); |
| 96 | + |
| 97 | + assert_eq!(index, "new_index"); |
| 98 | + |
| 99 | + // Check index not used |
| 100 | + let query = Query::new( |
| 101 | + db, |
| 102 | + QueryLanguage::N1QL, |
| 103 | + "select _.* from _ where _.notSomeField = 'whatever'", |
| 104 | + ) |
| 105 | + .expect("create query"); |
| 106 | + |
| 107 | + let index = Regex::new(r"USING INDEX (\w+) ") |
| 108 | + .unwrap() |
| 109 | + .captures(&query.explain().unwrap()) |
| 110 | + .map(|c| c.get(1).unwrap().as_str().to_string()); |
| 111 | + |
| 112 | + assert!(index.is_none()); |
| 113 | + |
| 114 | + // Check index deletion |
| 115 | + assert_eq!(db.get_index_names().count(), 1); |
| 116 | + |
80 | 117 | db.delete_index("idx").unwrap();
|
81 | 118 | assert_eq!(db.get_index_names().count(), 1);
|
82 | 119 |
|
83 | 120 | db.delete_index("new_index").unwrap();
|
84 | 121 | assert_eq!(db.get_index_names().count(), 0);
|
85 | 122 | });
|
86 | 123 | }
|
| 124 | + |
| 125 | +#[test] |
| 126 | +fn partial_index() { |
| 127 | + utils::with_db(|db| { |
| 128 | + assert!(db |
| 129 | + .create_index( |
| 130 | + "new_index", |
| 131 | + &ValueIndexConfiguration::new( |
| 132 | + QueryLanguage::JSON, |
| 133 | + r#"{"WHAT": [[".id"]], "WHERE": ["=", [".someField"], "someValue"]}"# |
| 134 | + ), |
| 135 | + ) |
| 136 | + .unwrap()); |
| 137 | + |
| 138 | + // Check index creation |
| 139 | + let value = db.get_index_names().iter().next().unwrap(); |
| 140 | + let name = value.as_string().unwrap(); |
| 141 | + assert_eq!(name, "new_index"); |
| 142 | + |
| 143 | + // Check index used |
| 144 | + let query = Query::new( |
| 145 | + db, |
| 146 | + QueryLanguage::N1QL, |
| 147 | + "select _.* from _ where _.id = 'id' and _.someField='someValue'", |
| 148 | + ) |
| 149 | + .expect("create query"); |
| 150 | + |
| 151 | + let index = Regex::new(r"USING INDEX (\w+) ") |
| 152 | + .unwrap() |
| 153 | + .captures(&query.explain().unwrap()) |
| 154 | + .map(|c| c.get(1).unwrap().as_str().to_string()) |
| 155 | + .unwrap(); |
| 156 | + |
| 157 | + assert_eq!(index, "new_index"); |
| 158 | + |
| 159 | + // Check index not used |
| 160 | + let query = Query::new( |
| 161 | + db, |
| 162 | + QueryLanguage::N1QL, |
| 163 | + "select _.* from _ where _.id = 'id' and _.someField='notSomeValue'", |
| 164 | + ) |
| 165 | + .expect("create query"); |
| 166 | + |
| 167 | + let index = Regex::new(r"USING INDEX (\w+) ") |
| 168 | + .unwrap() |
| 169 | + .captures(&query.explain().unwrap()) |
| 170 | + .map(|c| c.get(1).unwrap().as_str().to_string()); |
| 171 | + |
| 172 | + assert!(index.is_none()); |
| 173 | + }); |
| 174 | +} |
0 commit comments