From aa5495ba1c83b8bcdd1ac2b47aee56626f74298f Mon Sep 17 00:00:00 2001
From: Yaroslav Dynnikov <yaroslav.dynnikov@gmail.com>
Date: Thu, 11 May 2023 14:40:28 +0300
Subject: [PATCH] draft: make cas tests more consise

---
 src/traft/rpc/cas.rs | 57 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 56 insertions(+), 1 deletion(-)

diff --git a/src/traft/rpc/cas.rs b/src/traft/rpc/cas.rs
index 9fa5632a30..fb230c0eb0 100644
--- a/src/traft/rpc/cas.rs
+++ b/src/traft/rpc/cas.rs
@@ -14,7 +14,7 @@ use ::raft::StorageError;
 
 use tarantool::error::Error as TntError;
 use tarantool::tlua;
-use tarantool::tuple::{KeyDef, Tuple, TupleBuffer};
+use tarantool::tuple::{KeyDef, ToTupleBuffer, Tuple, TupleBuffer};
 
 use once_cell::sync::Lazy;
 
@@ -313,6 +313,47 @@ pub struct Range {
     pub key_max: Bound,
 }
 
+impl Range {
+    pub fn new(space: impl ToString) -> Self {
+        Self {
+            space: space.to_string(),
+            key_min: Bound::Unbounded,
+            key_max: Bound::Unbounded,
+        }
+    }
+
+    pub fn gt(mut self, key: impl ToTupleBuffer) -> Self {
+        let tuple = key.to_tuple_buffer().expect("cannot fail");
+        self.key_min = Bound::Excluded(tuple);
+        self
+    }
+
+    pub fn ge(mut self, key: impl ToTupleBuffer) -> Self {
+        let tuple = key.to_tuple_buffer().expect("cannot fail");
+        self.key_min = Bound::Included(tuple);
+        self
+    }
+
+    pub fn lt(mut self, key: impl ToTupleBuffer) -> Self {
+        let tuple = key.to_tuple_buffer().expect("cannot fail");
+        self.key_max = Bound::Excluded(tuple);
+        self
+    }
+
+    pub fn le(mut self, key: impl ToTupleBuffer) -> Self {
+        let tuple = key.to_tuple_buffer().expect("cannot fail");
+        self.key_max = Bound::Included(tuple);
+        self
+    }
+
+    pub fn eq(mut self, key: impl ToTupleBuffer) -> Self {
+        let tuple = key.to_tuple_buffer().expect("cannot fail");
+        self.key_min = Bound::Included(tuple.clone());
+        self.key_max = Bound::Included(tuple);
+        self
+    }
+}
+
 #[derive(Clone, Debug, ::serde::Serialize, ::serde::Deserialize, tlua::LuaRead)]
 #[serde(rename_all = "snake_case", tag = "kind", content = "value")]
 pub enum Bound {
@@ -544,6 +585,20 @@ mod tests {
             TestOp::Delete,
         ];
 
+        for op in ops {
+            assert_eq!(test2(op, Range::new("the_space")), false);
+            assert_eq!(test2(op, Range::new("the_space").le(12)), false);
+            assert_eq!(test2(op, Range::new("the_space").ge(12)), false);
+            assert_eq!(test2(op, Range::new("the_space").eq(12)), false);
+
+            assert_eq!(test2(op, Range::new("the_space").lt(12)), true);
+            assert_eq!(test2(op, Range::new("the_space").le(11)), true);
+
+            assert_eq!(test2(op, Range::new("the_space").gt(12)), true);
+            assert_eq!(test2(op, Range::new("the_space").ge(13)), true);
+
+            assert_eq!(test2(op, Range::new("other_space")), true);
+        }
         // For range ["b", "d"]
         // Test cases a,b,c,d,e
         // Error      0,1,1,1,0
-- 
GitLab