Skip to content

Commit

Permalink
add tests for FromSql<Jsonb, Sqlite>
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marc Le Roux committed Sep 27, 2024
1 parent 2af1253 commit 7fe0c67
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions diesel/src/sqlite/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,128 @@ mod tests {
assert!(res);
}

#[test]
fn jsonb_from_sql_null() {
let conn = &mut connection();
let res = diesel::select(sql::<Jsonb>("jsonb('null')"))
.get_result::<serde_json::Value>(conn)
.unwrap();
assert_eq!(res, serde_json::json!(null));
}

#[test]
fn jsonb_from_sql_true() {
let conn = &mut connection();
let res = diesel::select(sql::<Jsonb>("jsonb('true')"))
.get_result::<serde_json::Value>(conn)
.unwrap();
assert_eq!(res, serde_json::json!(true));
}

#[test]
fn jsonb_from_sql_false() {
let conn = &mut connection();
let res = diesel::select(sql::<Jsonb>("jsonb('false')"))
.get_result::<serde_json::Value>(conn)
.unwrap();
assert_eq!(res, serde_json::json!(false));
}

#[test]
fn jsonb_from_sql_int() {
let conn = &mut connection();
let res = diesel::select(sql::<Jsonb>("jsonb('42')"))
.get_result::<serde_json::Value>(conn)
.unwrap();
assert_eq!(res, serde_json::json!(42));
}

#[test]
fn jsonb_from_sql_float() {
let conn = &mut connection();
let res = diesel::select(sql::<Jsonb>("jsonb('3.14')"))
.get_result::<serde_json::Value>(conn)
.unwrap();
assert_eq!(res, serde_json::json!(3.14));
}

#[test]
fn jsonb_from_sql_object() {
let conn = &mut connection();
let res = diesel::select(sql::<Jsonb>("jsonb('{\"key\": \"value\"}')"))
.get_result::<serde_json::Value>(conn)
.unwrap();
assert_eq!(res, serde_json::json!({"key": "value"}));
}

#[test]
fn jsonb_from_sql_array() {
let conn = &mut connection();
let res = diesel::select(sql::<Jsonb>("jsonb('[1, 2, 3]')"))
.get_result::<serde_json::Value>(conn)
.unwrap();
assert_eq!(res, serde_json::json!([1, 2, 3]));
}

#[test]
fn jsonb_from_sql_nested_objects() {
let conn = &mut connection();
let res = diesel::select(sql::<Jsonb>("jsonb('{\"outer\": {\"inner\": 42}}')"))
.get_result::<serde_json::Value>(conn)
.unwrap();
assert_eq!(res, serde_json::json!({"outer": {"inner": 42}}));
}

#[test]
fn jsonb_from_sql_nested_arrays() {
let conn = &mut connection();
let res = diesel::select(sql::<Jsonb>("jsonb('[[1, 2], [3, 4]]')"))
.get_result::<serde_json::Value>(conn)
.unwrap();
assert_eq!(res, serde_json::json!([[1, 2], [3, 4]]));
}

#[test]
fn jsonb_from_sql_nested_arrays_in_objects() {
let conn = &mut connection();
let res = diesel::select(sql::<Jsonb>("jsonb('{\"array\": [1, 2, 3]}')"))
.get_result::<serde_json::Value>(conn)
.unwrap();
assert_eq!(res, serde_json::json!({"array": [1, 2, 3]}));
}

#[test]
fn jsonb_from_sql_nested_objects_in_arrays() {
let conn = &mut connection();
let res = diesel::select(sql::<Jsonb>(
"jsonb('[{\"key1\": \"value1\"}, {\"key2\": \"value2\"}]')",
))
.get_result::<serde_json::Value>(conn)
.unwrap();
assert_eq!(
res,
serde_json::json!([{"key1": "value1"}, {"key2": "value2"}])
);
}

#[test]
fn jsonb_from_sql_text() {
let conn = &mut connection();
let res = diesel::select(sql::<Jsonb>("jsonb('\"hello\"')"))
.get_result::<serde_json::Value>(conn)
.unwrap();
assert_eq!(res, serde_json::json!("hello"));
}

#[test]
fn jsonb_from_sql_textj() {
let conn = &mut connection();
let res = diesel::select(sql::<Jsonb>("jsonb('\"hello\\nworld\"')"))
.get_result::<serde_json::Value>(conn)
.unwrap();
assert_eq!(res, serde_json::json!("hello\nworld"));
}

#[test]
fn bad_json_from_sql() {
let conn = &mut connection();
Expand Down

0 comments on commit 7fe0c67

Please sign in to comment.