gluesql_test_suite/function/
replace.rs

1use {
2    crate::*,
3    gluesql_core::{
4        error::{EvaluateError, TranslateError},
5        prelude::{Payload, Value},
6    },
7};
8
9test_case!(replace, {
10    let g = get_tester!();
11
12    g.named_test(
13        "test replace function works while creating a table simultaneously",
14        "CREATE TABLE Item (name TEXT DEFAULT REPLACE('SQL Tutorial', 'T', 'M'))",
15        Ok(Payload::Create),
16    )
17    .await;
18    g.named_test(
19        "test if the sample string gets inserted to table",
20        "INSERT INTO Item VALUES ('Tticky GlueTQL')",
21        Ok(Payload::Insert(1)),
22    )
23    .await;
24    g.named_test(
25        "check id the replace function works with the previously inserted string",
26        "SELECT REPLACE(name,'T','S') AS test FROM Item;",
27        Ok(select!(
28            "test"
29            Value::Str;
30            "Sticky GlueSQL".to_owned()
31        )),
32    )
33    .await;
34    g.named_test(
35        "test when one argument was given",
36        "SELECT REPLACE('GlueSQL') AS test FROM Item",
37        Err(TranslateError::FunctionArgsLengthNotMatching {
38            name: "REPLACE".to_owned(),
39            expected: 3,
40            found: 1,
41        }
42        .into()),
43    )
44    .await;
45    g.named_test(
46        "test when two arguments were given",
47        "SELECT REPLACE('GlueSQL','G') AS test FROM Item",
48        Err(TranslateError::FunctionArgsLengthNotMatching {
49            name: "REPLACE".to_owned(),
50            expected: 3,
51            found: 2,
52        }
53        .into()),
54    )
55    .await;
56    g.named_test(
57        "test when integers were given as arguments instead of string values",
58        "SELECT REPLACE(1,1,1) AS test FROM Item",
59        Err(EvaluateError::FunctionRequiresStringValue("REPLACE".to_owned()).into()),
60    )
61    .await;
62    g.named_test(
63        "test when null was given as argument",
64        "SELECT REPLACE(name, null,null) AS test FROM Item",
65        Ok(select_with_null!(test; Value::Null)),
66    )
67    .await;
68    g.named_test(
69        "test if the table can be created will null value",
70        "CREATE TABLE NullTest (name TEXT null)",
71        Ok(Payload::Create),
72    )
73    .await;
74    g.named_test(
75        "test if null can be inserted",
76        "INSERT INTO NullTest VALUES (null)",
77        Ok(Payload::Insert(1)),
78    )
79    .await;
80    g.named_test(
81        "test if replace works in null value",
82        "SELECT REPLACE(name, 'G','T') AS test FROM NullTest",
83        Ok(select_with_null!(test; Value::Null)),
84    )
85    .await;
86    g.run("DELETE FROM Item").await;
87});