surrealmx 0.20.0

An embedded, in-memory, lock-free, transaction-based, key-value database engine
Documentation
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Copyright © SurrealDB Ltd
//
// 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.

//! Error handling tests for SurrealMX.
//!
//! Tests error conditions and proper error handling behavior.

use surrealmx::{Database, Error};

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

// =============================================================================
// Transaction Closed Errors
// =============================================================================

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn operations_after_commit_fail() {
	let db = Database::new();

	let mut tx = db.transaction(true);
	tx.set("key", "value").unwrap();
	tx.commit().unwrap();

	// All operations after commit should fail
	assert!(matches!(tx.get("key"), Err(Error::TxClosed)));
	assert!(matches!(tx.set("key2", "value2"), Err(Error::TxClosed)));
	assert!(matches!(tx.put("key3", "value3"), Err(Error::TxClosed)));
	assert!(matches!(tx.del("key"), Err(Error::TxClosed)));
	assert!(matches!(tx.exists("key"), Err(Error::TxClosed)));
	assert!(matches!(tx.commit(), Err(Error::TxClosed)));
	assert!(matches!(tx.cancel(), Err(Error::TxClosed)));
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn operations_after_cancel_fail() {
	let db = Database::new();

	let mut tx = db.transaction(true);
	tx.set("key", "value").unwrap();
	tx.cancel().unwrap();

	// All operations after cancel should fail
	assert!(matches!(tx.get("key"), Err(Error::TxClosed)));
	assert!(matches!(tx.set("key2", "value2"), Err(Error::TxClosed)));
	assert!(matches!(tx.commit(), Err(Error::TxClosed)));
	assert!(matches!(tx.cancel(), Err(Error::TxClosed)));
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn double_commit_fails() {
	let db = Database::new();

	let mut tx = db.transaction(true);
	tx.set("key", "value").unwrap();

	assert!(tx.commit().is_ok(), "First commit should succeed");
	assert!(matches!(tx.commit(), Err(Error::TxClosed)), "Second commit should fail with TxClosed");
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn double_cancel_fails() {
	let db = Database::new();

	let mut tx = db.transaction(true);
	tx.set("key", "value").unwrap();

	assert!(tx.cancel().is_ok(), "First cancel should succeed");
	assert!(matches!(tx.cancel(), Err(Error::TxClosed)), "Second cancel should fail with TxClosed");
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn commit_after_cancel_fails() {
	let db = Database::new();

	let mut tx = db.transaction(true);
	tx.set("key", "value").unwrap();
	tx.cancel().unwrap();

	assert!(matches!(tx.commit(), Err(Error::TxClosed)), "Commit after cancel should fail");
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn cancel_after_commit_fails() {
	let db = Database::new();

	let mut tx = db.transaction(true);
	tx.set("key", "value").unwrap();
	tx.commit().unwrap();

	assert!(matches!(tx.cancel(), Err(Error::TxClosed)), "Cancel after commit should fail");
}

// =============================================================================
// Not Writable Errors
// =============================================================================

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn write_operations_on_read_transaction_fail() {
	let db = Database::new();

	// Create some data first
	{
		let mut tx = db.transaction(true);
		tx.set("existing_key", "value").unwrap();
		tx.commit().unwrap();
	}

	// Start read-only transaction
	let mut read_tx = db.transaction(false);

	// All write operations should fail
	assert!(
		matches!(read_tx.set("key", "value"), Err(Error::TxNotWritable)),
		"set on read tx should fail"
	);
	assert!(
		matches!(read_tx.put("key", "value"), Err(Error::TxNotWritable)),
		"put on read tx should fail"
	);
	assert!(
		matches!(read_tx.del("existing_key"), Err(Error::TxNotWritable)),
		"del on read tx should fail"
	);
	assert!(
		matches!(read_tx.putc("key", "value", Some("check")), Err(Error::TxNotWritable)),
		"putc on read tx should fail"
	);
	assert!(
		matches!(read_tx.delc("existing_key", Some("value")), Err(Error::TxNotWritable)),
		"delc on read tx should fail"
	);

	// Read operations should still work
	assert!(read_tx.get("existing_key").is_ok());
	assert!(read_tx.exists("existing_key").is_ok());

	read_tx.cancel().unwrap();
}

// =============================================================================
// Key Already Exists Errors
// =============================================================================

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn put_existing_key_fails() {
	let db = Database::new();

	// Create initial key
	let mut tx = db.transaction(true);
	tx.set("key", "original").unwrap();
	tx.commit().unwrap();

	// Try to put (insert) same key
	let mut tx = db.transaction(true);
	let result = tx.put("key", "new_value");
	assert!(matches!(result, Err(Error::KeyAlreadyExists)), "put existing key should fail");
	tx.cancel().unwrap();
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn put_key_in_same_transaction_fails() {
	let db = Database::new();

	let mut tx = db.transaction(true);
	tx.set("key", "first").unwrap();

	let result = tx.put("key", "second");
	assert!(matches!(result, Err(Error::KeyAlreadyExists)), "put after set in same tx should fail");
	tx.cancel().unwrap();
}

// =============================================================================
// Value Not Expected Errors
// =============================================================================

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn putc_wrong_check_value_fails() {
	let db = Database::new();

	// Create key with known value
	let mut tx = db.transaction(true);
	tx.set("key", "actual_value").unwrap();
	tx.commit().unwrap();

	// Try putc with wrong check
	let mut tx = db.transaction(true);
	let result = tx.putc("key", "new_value", Some("wrong_check"));
	assert!(matches!(result, Err(Error::ValNotExpectedValue)), "putc with wrong check should fail");
	tx.cancel().unwrap();
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn delc_wrong_check_value_fails() {
	let db = Database::new();

	// Create key with known value
	let mut tx = db.transaction(true);
	tx.set("key", "actual_value").unwrap();
	tx.commit().unwrap();

	// Try delc with wrong check
	let mut tx = db.transaction(true);
	let result = tx.delc("key", Some("wrong_check"));
	assert!(matches!(result, Err(Error::ValNotExpectedValue)), "delc with wrong check should fail");
	tx.cancel().unwrap();
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn putc_none_check_on_existing_key_fails() {
	let db = Database::new();

	// Create key
	let mut tx = db.transaction(true);
	tx.set("key", "exists").unwrap();
	tx.commit().unwrap();

	// Try putc with None check (expects key to not exist)
	let mut tx = db.transaction(true);
	let result = tx.putc::<_, _, &[u8]>("key", "new_value", None);
	assert!(
		matches!(result, Err(Error::ValNotExpectedValue)),
		"putc with None check on existing key should fail"
	);
	tx.cancel().unwrap();
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn delc_none_check_on_existing_key_fails() {
	let db = Database::new();

	// Create key
	let mut tx = db.transaction(true);
	tx.set("key", "exists").unwrap();
	tx.commit().unwrap();

	// Try delc with None check (expects key to not exist)
	let mut tx = db.transaction(true);
	let result = tx.delc::<_, &[u8]>("key", None);
	assert!(
		matches!(result, Err(Error::ValNotExpectedValue)),
		"delc with None check on existing key should fail"
	);
	tx.cancel().unwrap();
}

// =============================================================================
// No Savepoint Errors
// =============================================================================

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn rollback_without_savepoint_fails() {
	let db = Database::new();

	let mut tx = db.transaction(true);
	tx.set("key", "value").unwrap();

	let result = tx.rollback_to_savepoint();
	assert!(matches!(result, Err(Error::NoSavepoint)), "rollback without savepoint should fail");
	tx.cancel().unwrap();
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn rollback_after_all_savepoints_consumed_fails() {
	let db = Database::new();

	let mut tx = db.transaction(true);

	// Set one savepoint
	tx.set_savepoint().unwrap();
	tx.set("key", "value").unwrap();

	// Rollback once (consumes the savepoint)
	tx.rollback_to_savepoint().unwrap();

	// Second rollback should fail
	let result = tx.rollback_to_savepoint();
	assert!(matches!(result, Err(Error::NoSavepoint)), "second rollback should fail");
	tx.cancel().unwrap();
}

// =============================================================================
// Conflict Errors
// =============================================================================

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn write_conflict_error() {
	let db = Database::new();

	// Two transactions writing same key
	let mut tx1 = db.transaction(true);
	let mut tx2 = db.transaction(true);

	tx1.set("key", "tx1").unwrap();
	tx2.set("key", "tx2").unwrap();

	// First commits
	tx1.commit().unwrap();

	// Second should fail with write conflict
	let result = tx2.commit();
	assert!(
		matches!(result, Err(Error::KeyWriteConflict)),
		"concurrent write to same key should conflict"
	);
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn read_conflict_error_ssi() {
	let db = Database::new();

	// Create initial data
	let mut tx = db.transaction(true);
	tx.set("key", "initial").unwrap();
	tx.commit().unwrap();

	// tx1 reads with SSI
	let mut tx1 = db.transaction(true).with_serializable_snapshot_isolation();
	let _ = tx1.get("key").unwrap();

	// tx2 modifies and commits
	let mut tx2 = db.transaction(true).with_serializable_snapshot_isolation();
	tx2.set("key", "modified").unwrap();
	tx2.commit().unwrap();

	// tx1 tries to write and commit
	tx1.set("other", "data").unwrap();
	let result = tx1.commit();

	assert!(matches!(result, Err(Error::KeyReadConflict)), "SSI should detect read conflict");
}

// =============================================================================
// Version In Future Error
// =============================================================================

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn get_at_future_version_fails() {
	let db = Database::new();

	// Create data
	let mut tx = db.transaction(true);
	tx.set("key", "value").unwrap();
	tx.commit().unwrap();

	// Try to read at a future version
	let tx = db.transaction(false);
	let current_version = tx.version();

	// Future version
	let future_version = current_version + 1_000_000_000; // Way in the future

	let result = tx.get_at_version("key", future_version);
	assert!(matches!(result, Err(Error::VersionInFuture)), "reading future version should fail");
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn exists_at_future_version_fails() {
	let db = Database::new();

	let mut tx = db.transaction(true);
	tx.set("key", "value").unwrap();
	tx.commit().unwrap();

	let tx = db.transaction(false);
	let future_version = tx.version() + 1_000_000_000;

	let result = tx.exists_at_version("key", future_version);
	assert!(matches!(result, Err(Error::VersionInFuture)), "exists at future version should fail");
}

// =============================================================================
// Iterator/Cursor with Closed Transaction
// =============================================================================

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn cursor_after_cancel_fails() {
	let db = Database::new();

	let mut tx = db.transaction(true);
	tx.set("key", "value").unwrap();
	tx.cancel().unwrap();

	let result = tx.cursor("a".."z");
	assert!(matches!(result, Err(Error::TxClosed)));
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn keys_iter_after_commit_fails() {
	let db = Database::new();

	let mut tx = db.transaction(true);
	tx.set("key", "value").unwrap();
	tx.commit().unwrap();

	let result = tx.keys_iter("a".."z");
	assert!(matches!(result, Err(Error::TxClosed)));
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn scan_iter_after_cancel_fails() {
	let db = Database::new();

	let mut tx = db.transaction(false);
	tx.cancel().unwrap();

	let result = tx.scan_iter("a".."z");
	assert!(matches!(result, Err(Error::TxClosed)));
}

// =============================================================================
// Getm with Closed Transaction
// =============================================================================

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn getm_after_close_fails() {
	let db = Database::new();

	let mut tx = db.transaction(false);
	tx.cancel().unwrap();

	let result = tx.getm(vec!["key1", "key2"]);
	assert!(matches!(result, Err(Error::TxClosed)));
}

// =============================================================================
// Edge Cases
// =============================================================================

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn operations_on_dropped_transaction() {
	let db = Database::new();

	// Transaction drops without commit or cancel
	{
		let mut tx = db.transaction(true);
		tx.set("key", "value").unwrap();
		// tx drops here without commit
	}

	// Data should not be committed
	let mut tx = db.transaction(false);
	assert!(tx.get("key").unwrap().is_none(), "Dropped transaction should not commit");
	tx.cancel().unwrap();
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn set_savepoint_on_closed_transaction() {
	let db = Database::new();

	let mut tx = db.transaction(true);
	tx.commit().unwrap();

	let result = tx.set_savepoint();
	assert!(matches!(result, Err(Error::TxClosed)));
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn rollback_on_closed_transaction() {
	let db = Database::new();

	let mut tx = db.transaction(true);
	tx.set_savepoint().unwrap();
	tx.cancel().unwrap();

	let result = tx.rollback_to_savepoint();
	assert!(matches!(result, Err(Error::TxClosed)));
}