Skip to content

Commit

Permalink
test: Add dynlist example
Browse files Browse the repository at this point in the history
  • Loading branch information
ohsayan committed Jul 24, 2024
1 parent 753b69f commit 8d637fe
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ impl Row {
}
}

impl From<Vec<Value>> for Row {
fn from(values: Vec<Value>) -> Self {
Self { values }
}
}

#[derive(Debug, PartialEq, Clone)]
/// A response returned by the server
pub enum Response {
Expand Down Expand Up @@ -410,6 +416,12 @@ impl<T: FromRow> Deref for Rows<T> {
#[derive(Debug, PartialEq, Clone)]
pub struct RList<T: FromValue = Value>(Vec<T>);

impl<T: FromValue> From<Vec<T>> for RList<T> {
fn from(values: Vec<T>) -> Self {
Self(values)
}
}

impl<T: FromValue> RList<T> {
/// Returns the values of the list
pub fn into_values(self) -> Vec<T> {
Expand Down
83 changes: 83 additions & 0 deletions tests/custom_query_resp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use skytable::{
query,
query::{QList, SQParam},
response::{FromResponse, RList, Response, Row, Value},
};

/*
our model looks like:
create model myapp.data(
username: string,
password: string,
notes: list { type: string },
)
*/

#[derive(Debug, PartialEq)]
struct BookmarkUser {
username: String,
password: String,
notes: Vec<String>,
}

impl BookmarkUser {
fn test_user() -> Self {
Self {
username: "sayan".to_owned(),
password: "pass123".to_owned(),
notes: vec![
"https://example.com".to_owned(),
"https://skytable.org".to_owned(),
"https://docs.skytable.org".to_owned(),
],
}
}
}

impl<'a> SQParam for &'a BookmarkUser {
fn append_param(&self, q: &mut Vec<u8>) -> usize {
self.username.append_param(q)
+ self.password.append_param(q)
+ QList::new(&self.notes).append_param(q)
}
}

impl FromResponse for BookmarkUser {
fn from_response(resp: Response) -> skytable::ClientResult<Self> {
let (username, password, notes) = resp.parse::<(String, String, RList<String>)>()?;
Ok(Self {
username,
password,
notes: notes.into_values(),
})
}
}

#[test]
fn dynlist_q() {
let bu = BookmarkUser::test_user();
let q = query!(
"insert into myapp.data { username: ?, password: ?, notes: ? }",
&bu
);
assert_eq!(q.param_cnt(), 3);
}

#[test]
fn dynlist_r() {
// assume that this is the response we got from the server (as a row); this may look messy but in a real-world application, the library does this for you
// under the hood, so don't worry! you'll never have to write this yourself!
let resp_from_server = Response::Row(Row::from(vec![
Value::String("sayan".to_owned()),
Value::String("pass123".to_owned()),
Value::List(vec![
Value::String("https://example.com".to_owned()),
Value::String("https://skytable.org".to_owned()),
Value::String("https://docs.skytable.org".to_owned()),
]),
]));
// now this is our "fetch code"
let user: BookmarkUser = resp_from_server.parse().unwrap();
assert_eq!(user, BookmarkUser::test_user());
}

0 comments on commit 8d637fe

Please sign in to comment.