aboutsummaryrefslogtreecommitdiffstats
path: root/log_db/tests
diff options
context:
space:
mode:
authorJan Tuomi <jan@jantuomi.fi>2025-02-10 22:05:20 +0200
committerJan Tuomi <jan@jantuomi.fi>2025-02-10 22:05:20 +0200
commitf29a9a65cb50e6d3a7d6416de576603eccd0eb30 (patch)
treea1a5e42206468d9a084e262b7b0e14fa2b611741 /log_db/tests
parent5020bc4204cb470146e4c934018b965f1cf9eba2 (diff)
Add _with_params variants to query methods
Diffstat (limited to 'log_db/tests')
-rw-r--r--log_db/tests/integration.rs126
1 files changed, 126 insertions, 0 deletions
diff --git a/log_db/tests/integration.rs b/log_db/tests/integration.rs
index c42f91c..3d71279 100644
--- a/log_db/tests/integration.rs
+++ b/log_db/tests/integration.rs
@@ -865,3 +865,129 @@ fn test_delete_by_multiple_indexes() {
let result = db.find_by(&Field::Id, &Value::Int(0)).unwrap();
assert_eq!(result.len(), 0);
}
+
+#[test]
+fn test_find_by_with_offset_and_limit() {
+ let data_dir = tmp_dir();
+
+ let mut db = DB::configure()
+ .fields(Inst::schema())
+ .primary_key(Inst::primary_key())
+ .secondary_keys(Inst::secondary_keys())
+ .from_record(Inst::from_record)
+ .into_record(Inst::into_record)
+ .data_dir(&data_dir)
+ .initialize()
+ .expect("Failed to initialize DB instance");
+
+ // Insert some records
+ for i in 0..10 {
+ db.upsert(Inst {
+ id: i,
+ name: Some("foo".to_string()),
+ data: vec![],
+ })
+ .unwrap();
+ }
+
+ // Find by name with offset and limit
+ let result = db
+ .find_by_with_params(
+ &Field::Name,
+ &Value::String("foo".to_string()),
+ &QueryParams {
+ offset: 2,
+ limit: 3,
+ },
+ )
+ .unwrap();
+
+ assert_eq!(result.len(), 3);
+ assert_eq!(result[0].id, 2);
+ assert_eq!(result[1].id, 3);
+ assert_eq!(result[2].id, 4);
+}
+
+#[test]
+fn test_batch_find_by_with_offset_and_limit() {
+ let data_dir = tmp_dir();
+
+ let mut db = DB::configure()
+ .fields(Inst::schema())
+ .primary_key(Inst::primary_key())
+ .secondary_keys(Inst::secondary_keys())
+ .from_record(Inst::from_record)
+ .into_record(Inst::into_record)
+ .data_dir(&data_dir)
+ .initialize()
+ .expect("Failed to initialize DB instance");
+
+ // Insert some records
+ for i in 0..10 {
+ db.upsert(Inst {
+ id: i,
+ name: Some("foo".to_string()),
+ data: vec![],
+ })
+ .unwrap();
+ }
+
+ // Batch find by id with offset and limit
+ let batch: Vec<Value> = (2..5).map(Value::Int).collect();
+ let result = db
+ .batch_find_by_with_params(
+ &Field::Id,
+ &batch,
+ &QueryParams {
+ offset: 1,
+ limit: 2,
+ },
+ )
+ .unwrap();
+
+ assert_eq!(result.len(), 2);
+ assert_eq!(result[0].1.id, 3);
+ assert_eq!(result[1].1.id, 4);
+}
+
+#[test]
+fn test_range_by_with_offset_and_limit() {
+ let data_dir = tmp_dir();
+
+ let mut db = DB::configure()
+ .fields(Inst::schema())
+ .primary_key(Inst::primary_key())
+ .secondary_keys(Inst::secondary_keys())
+ .from_record(Inst::from_record)
+ .into_record(Inst::into_record)
+ .data_dir(&data_dir)
+ .initialize()
+ .expect("Failed to initialize DB instance");
+
+ // Insert some records
+ for i in 0..10 {
+ db.upsert(Inst {
+ id: i,
+ name: Some("foo".to_string()),
+ data: vec![],
+ })
+ .unwrap();
+ }
+
+ // Range by id with offset and limit
+ let result = db
+ .range_by_with_params(
+ &Field::Id,
+ &Value::Int(2)..&Value::Int(8),
+ &QueryParams {
+ offset: 1,
+ limit: 3,
+ },
+ )
+ .unwrap();
+
+ assert_eq!(result.len(), 3);
+ assert_eq!(result[0].id, 3);
+ assert_eq!(result[1].id, 4);
+ assert_eq!(result[2].id, 5);
+}