a database layer insipred by caqti and ecto
at main 5.3 kB view raw
1open Repodb 2 3let test_int_literal () = 4 let sql = Expr.to_sql (Expr.int 42) in 5 Alcotest.(check string) "int literal" "42" sql 6 7let test_string_literal () = 8 let sql = Expr.to_sql (Expr.string "hello") in 9 Alcotest.(check string) "string literal" "'hello'" sql 10 11let test_string_escaping () = 12 let sql = Expr.to_sql (Expr.string "it's") in 13 Alcotest.(check string) "escaped string" "'it''s'" sql 14 15let test_bool_true () = 16 let sql = Expr.to_sql (Expr.bool true) in 17 Alcotest.(check string) "bool true" "TRUE" sql 18 19let test_bool_false () = 20 let sql = Expr.to_sql (Expr.bool false) in 21 Alcotest.(check string) "bool false" "FALSE" sql 22 23let test_equality () = 24 let sql = Expr.to_sql Expr.(int 1 = int 1) in 25 Alcotest.(check string) "equality" "(1 = 1)" sql 26 27let test_inequality () = 28 let sql = Expr.to_sql Expr.(int 1 <> int 2) in 29 Alcotest.(check string) "inequality" "(1 <> 2)" sql 30 31let test_less_than () = 32 let sql = Expr.to_sql Expr.(int 1 < int 2) in 33 Alcotest.(check string) "less than" "(1 < 2)" sql 34 35let test_greater_than () = 36 let sql = Expr.to_sql Expr.(int 2 > int 1) in 37 Alcotest.(check string) "greater than" "(2 > 1)" sql 38 39let test_and () = 40 let sql = Expr.to_sql Expr.(bool true && bool false) in 41 Alcotest.(check string) "AND" "(TRUE AND FALSE)" sql 42 43let test_or () = 44 let sql = Expr.to_sql Expr.(bool true || bool false) in 45 Alcotest.(check string) "OR" "(TRUE OR FALSE)" sql 46 47let test_not () = 48 let sql = Expr.to_sql Expr.(not_ (bool true)) in 49 Alcotest.(check string) "NOT" "(NOT TRUE)" sql 50 51let test_addition () = 52 let sql = Expr.to_sql Expr.(int 1 + int 2) in 53 Alcotest.(check string) "addition" "(1 + 2)" sql 54 55let test_subtraction () = 56 let sql = Expr.to_sql Expr.(int 5 - int 3) in 57 Alcotest.(check string) "subtraction" "(5 - 3)" sql 58 59let test_multiplication () = 60 let sql = Expr.to_sql Expr.(int 2 * int 3) in 61 Alcotest.(check string) "multiplication" "(2 * 3)" sql 62 63let test_division () = 64 let sql = Expr.to_sql Expr.(int 6 / int 2) in 65 Alcotest.(check string) "division" "(6 / 2)" sql 66 67let test_like () = 68 let sql = Expr.to_sql (Expr.like (Expr.string "hello") "%ello") in 69 Alcotest.(check string) "LIKE" "('hello' LIKE '%ello')" sql 70 71let test_ilike () = 72 let sql = Expr.to_sql (Expr.ilike (Expr.string "HELLO") "%ello") in 73 Alcotest.(check string) "ILIKE" "('HELLO' ILIKE '%ello')" sql 74 75let test_is_null () = 76 let sql = Expr.to_sql (Expr.is_null (Expr.raw "column")) in 77 Alcotest.(check string) "IS NULL" "(column IS NULL)" sql 78 79let test_is_not_null () = 80 let sql = Expr.to_sql (Expr.is_not_null (Expr.raw "column")) in 81 Alcotest.(check string) "IS NOT NULL" "(column IS NOT NULL)" sql 82 83let test_between () = 84 let sql = 85 Expr.to_sql (Expr.between (Expr.int 5) (Expr.int 1) (Expr.int 10)) 86 in 87 Alcotest.(check string) "BETWEEN" "(5 BETWEEN 1 AND 10)" sql 88 89let test_in_list () = 90 let sql = 91 Expr.to_sql 92 (Expr.in_list (Expr.int 1) [ Expr.int 1; Expr.int 2; Expr.int 3 ]) 93 in 94 Alcotest.(check string) "IN" "(1 IN (1, 2, 3))" sql 95 96let test_count () = 97 let sql = Expr.to_sql Expr.count_all in 98 Alcotest.(check string) "COUNT(*)" "COUNT(*)" sql 99 100let test_lower () = 101 let sql = Expr.to_sql (Expr.lower (Expr.string "HELLO")) in 102 Alcotest.(check string) "LOWER" "LOWER('HELLO')" sql 103 104let test_upper () = 105 let sql = Expr.to_sql (Expr.upper (Expr.string "hello")) in 106 Alcotest.(check string) "UPPER" "UPPER('hello')" sql 107 108let test_concat () = 109 let sql = Expr.to_sql (Expr.concat [ Expr.string "a"; Expr.string "b" ]) in 110 Alcotest.(check string) "CONCAT" "CONCAT('a', 'b')" sql 111 112let test_coalesce () = 113 let sql = 114 Expr.to_sql 115 (Expr.coalesce 116 [ Expr.raw "nullable"; Expr.string "default" ] 117 Types.string) 118 in 119 Alcotest.(check string) "COALESCE" "COALESCE(nullable, 'default')" sql 120 121let test_cast () = 122 let sql = Expr.to_sql (Expr.cast (Expr.int 42) Types.string) in 123 Alcotest.(check string) "CAST" "CAST(42 AS TEXT)" sql 124 125let test_raw () = 126 let sql = Expr.to_sql (Expr.raw "custom_function()") in 127 Alcotest.(check string) "raw SQL" "custom_function()" sql 128 129let tests = 130 [ 131 ("int literal", `Quick, test_int_literal); 132 ("string literal", `Quick, test_string_literal); 133 ("string escaping", `Quick, test_string_escaping); 134 ("bool true", `Quick, test_bool_true); 135 ("bool false", `Quick, test_bool_false); 136 ("equality", `Quick, test_equality); 137 ("inequality", `Quick, test_inequality); 138 ("less than", `Quick, test_less_than); 139 ("greater than", `Quick, test_greater_than); 140 ("AND", `Quick, test_and); 141 ("OR", `Quick, test_or); 142 ("NOT", `Quick, test_not); 143 ("addition", `Quick, test_addition); 144 ("subtraction", `Quick, test_subtraction); 145 ("multiplication", `Quick, test_multiplication); 146 ("division", `Quick, test_division); 147 ("LIKE", `Quick, test_like); 148 ("ILIKE", `Quick, test_ilike); 149 ("IS NULL", `Quick, test_is_null); 150 ("IS NOT NULL", `Quick, test_is_not_null); 151 ("BETWEEN", `Quick, test_between); 152 ("IN list", `Quick, test_in_list); 153 ("COUNT", `Quick, test_count); 154 ("LOWER", `Quick, test_lower); 155 ("UPPER", `Quick, test_upper); 156 ("CONCAT", `Quick, test_concat); 157 ("COALESCE", `Quick, test_coalesce); 158 ("CAST", `Quick, test_cast); 159 ("raw", `Quick, test_raw); 160 ]