Skip to content
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

Fix: allow $in operator to be used with stored array #129

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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: 0 additions & 1 deletion src/polodb_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ keywords = ["database", "embedded", "cross-platform"]
[lib]
name = "polodb_core"
path = "lib.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
libc = "0.2"
Expand Down
15 changes: 15 additions & 0 deletions src/polodb_core/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ pub struct RegexError {
pub options: String,
}

#[derive(Debug)]
pub struct AllError {
pub field_key: String,
pub field_value: String,
pub field_type: String,
}

#[derive(Error, Debug)]
pub enum Error {
#[error("unexpected id type, expected: {0}, actual: {1}")]
Expand Down Expand Up @@ -253,6 +260,8 @@ pub enum Error {
UnknownAggregationOperation(String),
#[error("invalid aggregation stage: {0:?}")]
InvalidAggregationStage(Box<Document>),
#[error("the field {} is not an array; value: {}, type: {}", .0.field_key, .0.field_value, .0.field_type)]
AllError(Box<AllError>),
}

impl Error {
Expand Down Expand Up @@ -325,6 +334,12 @@ impl From<RegexError> for Error {
}
}

impl From<AllError> for Error {
fn from(value: AllError) -> Self {
Error::AllError(Box::new(value))
}
}

#[cfg(test)]
mod tests {
use crate::Error;
Expand Down
78 changes: 78 additions & 0 deletions src/polodb_core/tests/test_all.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use bson::{doc, Document};
use polodb_core::Database;

mod common;

use common::prepare_db;

#[test]
fn test_all() {
vec![
(prepare_db("test-all").unwrap(), true),
(Database::open_memory().unwrap(), false),
]
.iter()
.for_each(|(db, _)| {
let metrics = db.metrics();
metrics.enable();

let collection = db.collection::<Document>("config");
let docs = vec![
doc! {
"_id": "c1",
"value": ["c1", "c2", "c3"],
},
doc! {
"_id": "invalid",
"value": ["c1", "c2", "c4"],
},
];
collection.insert_many(&docs).unwrap();

let res = collection
.find(doc! {
"value": {
"$all": ["c2", "c3"],
}
})
.unwrap();

assert_eq!(res.count(), docs.len() - 1);
});
}

#[test]
fn test_all_error() {
vec![
(prepare_db("test-all-error").unwrap(), true),
(Database::open_memory().unwrap(), false),
]
.iter()
.for_each(|(db, _)| {
let metrics = db.metrics();
metrics.enable();

let collection = db.collection::<Document>("config");
let docs = vec![
doc! {
"_id": "c1",
"arr": ["c1", "c2", "c3"],
},
doc! {
"_id": "invalid2",
"value": "not-valid-value",
},
];
collection.insert_many(&docs).unwrap();

let mut res = collection
.find(doc! {
"value": {
"$all": ["c2", "c3"],
}
})
.unwrap();

assert!(res.next().unwrap().is_err());
});
}
2 changes: 1 addition & 1 deletion src/polodb_core/tests/test_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn test_create_collection_and_find_by_pkey() {

let result = collection
.find(doc! {
"_id": first_key.clone(),
"_id": first_key,
})
.unwrap()
.collect::<Result<Vec<Document>>>()
Expand Down
82 changes: 82 additions & 0 deletions src/polodb_core/tests/test_in.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use bson::{doc, Document};
use polodb_core::Database;

mod common;

use common::prepare_db;

#[test]
fn test_in_array() {
vec![
(prepare_db("test-all-array").unwrap(), true),
(Database::open_memory().unwrap(), false),
]
.iter()
.for_each(|(db, _)| {
let metrics = db.metrics();
metrics.enable();

let collection = db.collection::<Document>("config");
let docs = vec![
doc! {
"_id": "c1",
"value": ["c1", "c2", "c3"],
},
doc! {
"_id": "c2",
"value": ["c2", "c4"],
},
doc! {
"_id": "invalid",
"value": ["c5", "c6", "c4"],
},
];
collection.insert_many(&docs).unwrap();

let res = collection
.find(doc! {
"value": {
"$in": ["c2", "c52"],
}
})
.unwrap();

assert_eq!(res.count(), docs.len() - 1);
});
}

#[test]
fn test_in() {
vec![
(prepare_db("test-in").unwrap(), true),
(Database::open_memory().unwrap(), false),
]
.iter()
.for_each(|(db, _)| {
let metrics = db.metrics();
metrics.enable();

let collection = db.collection::<Document>("config");
let docs = vec![
doc! {
"_id": "c1",
"value": 18,
},
doc! {
"_id": "invalid",
"value": 50,
},
];
collection.insert_many(&docs).unwrap();

let res = collection
.find(doc! {
"value": {
"$in": [20, 18, 30],
}
})
.unwrap();

assert_eq!(res.count(), docs.len() - 1);
});
}
2 changes: 1 addition & 1 deletion src/polodb_core/tests/test_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ fn test_delete_with_index() {
let count = col.count_documents().unwrap();
assert_eq!(count, 0);

let result = col.find_one(doc! {
col.find_one(doc! {
"age": 33
}).unwrap();

Expand Down
Loading
Loading