1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2025 Oxibase Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! DDL Function Tests
//!
//! Tests for CREATE/DROP FUNCTION DDL execution edge cases and error handling.
use oxibase::Database;
#[cfg(test)]
mod ddl_function_tests {
use super::*;
#[test]
fn test_create_function_invalid_language() {
let db = Database::open("memory://invalid_lang_test").unwrap();
// Try to create function with unsupported language
let result = db.execute(
r#"
CREATE FUNCTION test_func() RETURNS INTEGER
LANGUAGE INVALID_LANG AS 'return 42'
"#,
(),
);
// May succeed at creation and fail at execution
if result.is_ok() {
let exec_result: Result<i64, _> = db.query_one("SELECT test_func()", ());
assert!(
exec_result.is_err(),
"Should fail when executing with unsupported language"
);
} else {
// If creation fails, that's also acceptable
assert!(result.is_err());
}
}
#[test]
fn test_create_function_duplicate_without_if_not_exists() {
let db = Database::open("memory://duplicate_func_test").unwrap();
// Create function
db.execute(
r#"
CREATE FUNCTION test_func() RETURNS INTEGER
LANGUAGE RHAI AS '42'
"#,
(),
)
.unwrap();
// Try to create again without IF NOT EXISTS - should fail
let result = db.execute(
r#"
CREATE FUNCTION test_func() RETURNS INTEGER
LANGUAGE RHAI AS '43'
"#,
(),
);
assert!(result.is_err());
}
#[test]
fn test_create_function_duplicate_with_if_not_exists() {
let db = Database::open("memory://duplicate_if_not_exists_test").unwrap();
// Create function
db.execute(
r#"
CREATE FUNCTION test_func() RETURNS INTEGER
LANGUAGE RHAI AS '42'
"#,
(),
)
.unwrap();
// Try to create again with IF NOT EXISTS - should succeed
let result = db.execute(
r#"
CREATE FUNCTION IF NOT EXISTS test_func() RETURNS INTEGER
LANGUAGE RHAI AS '43'
"#,
(),
);
assert!(result.is_ok());
// Function should still return original value (not replaced)
let value: i64 = db.query_one("SELECT test_func()", ()).unwrap();
assert_eq!(value, 42);
}
#[test]
fn test_create_function_invalid_syntax_validation() {
let db = Database::open("memory://syntax_validation_test").unwrap();
// Try to create function with invalid syntax in body
let result = db.execute(
r#"
CREATE FUNCTION invalid_func() RETURNS INTEGER
LANGUAGE RHAI AS '1 +'
"#,
(),
);
// May succeed at creation and fail at execution
if result.is_ok() {
let exec_result: Result<i64, _> = db.query_one("SELECT invalid_func()", ());
assert!(
exec_result.is_err(),
"Should fail when executing invalid syntax"
);
} else {
// If creation fails, that's also acceptable
assert!(result.is_err());
}
}
#[test]
fn test_create_function_empty_body() {
let db = Database::open("memory://empty_body_test").unwrap();
// Try to create function with empty body
let result = db.execute(
r#"
CREATE FUNCTION empty_func() RETURNS INTEGER
LANGUAGE RHAI AS ''
"#,
(),
);
// This might succeed or fail depending on backend validation
// At minimum, it should not crash
if result.is_ok() {
// If it succeeds, executing should handle empty script gracefully
let exec_result: Result<i64, _> = db.query_one("SELECT empty_func()", ());
// Should either return NULL or error gracefully
assert!(exec_result.is_ok() || exec_result.is_err());
}
}
// Note: Schema-qualified functions may not be fully implemented yet
// #[test]
// fn test_create_function_schema_not_exists() {
// let db = Database::open("memory://schema_not_exists_test").unwrap();
// // Implementation depends on schema support
// }
#[test]
fn test_drop_function_not_exists() {
let db = Database::open("memory://drop_not_exists_test").unwrap();
// Try to drop non-existent function
let result = db.execute("DROP FUNCTION nonexistent_func", ());
assert!(result.is_err());
let err_msg = result.unwrap_err().to_string();
assert!(err_msg.contains("not found") || err_msg.contains("does not exist"));
}
#[test]
fn test_drop_function_if_exists() {
let db = Database::open("memory://drop_if_exists_test").unwrap();
// Drop non-existent function with IF EXISTS - should succeed
let result = db.execute("DROP FUNCTION IF EXISTS nonexistent_func", ());
assert!(result.is_ok());
}
#[test]
fn test_create_function_case_insensitive_language() {
let db = Database::open("memory://case_insensitive_lang_test").unwrap();
// Test case insensitive language names
let result = db.execute(
r#"
CREATE FUNCTION test_func() RETURNS INTEGER
LANGUAGE rhai AS '42'
"#,
(),
);
assert!(result.is_ok());
}
#[test]
fn test_create_function_malformed_signature() {
let db = Database::open("memory://malformed_sig_test").unwrap();
// Missing closing parenthesis
let result = db.execute(
r#"
CREATE FUNCTION test_func(a INTEGER RETURNS INTEGER
LANGUAGE RHAI AS '42'
"#,
(),
);
assert!(result.is_err());
}
#[test]
fn test_create_function_with_dollar_quotes() {
let db = Database::open("memory://dollar_quotes_test").unwrap();
// Create function using $$
db.execute(
r#"
CREATE FUNCTION test_func_dollar() RETURNS INTEGER
LANGUAGE RHAI AS $$
let a = 20;
let b = 22;
return a + b;
$$
"#,
(),
)
.unwrap();
let value: i64 = db.query_one("SELECT test_func_dollar()", ()).unwrap();
assert_eq!(value, 42);
// Create function using triple backticks
db.execute(
r#"
CREATE FUNCTION test_func_backticks() RETURNS TEXT
LANGUAGE RHAI AS ```
return "hello " + "world";
```
"#,
(),
)
.unwrap();
let text: String = db.query_one("SELECT test_func_backticks()", ()).unwrap();
assert_eq!(text, "hello world");
// Create function using tagged python quotes but use RHAI to ensure execution succeeds
db.execute(
r#"
CREATE FUNCTION test_func_python() RETURNS TEXT
LANGUAGE RHAI AS $py$
return "rhai rocks"
$py$
"#,
(),
)
.unwrap();
let pytext: String = db.query_one("SELECT test_func_python()", ()).unwrap();
assert_eq!(pytext, "rhai rocks");
}
}