rbatis 4.9.4

The Rust SQL Toolkit and ORM Library. An async, pure Rust SQL crate featuring compile-time Dynamic SQL
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/// Error path and edge case tests:
/// - RBatis init with empty URL
/// - Pool not initialized errors
/// - acquire failure paths
/// - try_acquire_timeout behavior
/// - RBatis::query (RBatis-level) method
/// - RBatis::query_decode (deprecated) method
/// - Connection pool exhaustion scenarios
/// - Various error message verification

#[cfg(test)]
mod test {
    use async_trait::async_trait;
    use futures_core::future::BoxFuture;
    use futures_core::Stream;
    use rbatis::executor::Executor;
    use rbatis::intercept::{Intercept, ResultType};
    use rbatis::{Action, Error, RBatis};
    use rbdc::db::{ConnectOptions, Connection, Driver, ExecResult, Row};
    use rbdc::rt::block_on;
    use rbs::Value;
    use std::pin::Pin;
    use std::sync::Arc;

    // ==================== Mock Infrastructure ====================

    #[derive(Debug, Clone)]
    struct MockDriver {}

    impl Driver for MockDriver {
        fn name(&self) -> &str {
            "test"
        }
        fn connect(&self, _url: &str) -> BoxFuture<'_, Result<Box<dyn Connection>, Error>> {
            Box::pin(async { Ok(Box::new(MockConnection {}) as Box<dyn Connection>) })
        }
        fn connect_opt<'a>(
            &'a self,
            _option: &'a dyn ConnectOptions,
        ) -> BoxFuture<'a, Result<Box<dyn Connection>, Error>> {
            Box::pin(async { Ok(Box::new(MockConnection {}) as Box<dyn Connection>) })
        }
        fn default_option(&self) -> Box<dyn ConnectOptions> {
            Box::new(MockConnectOptions {})
        }
    }

    /// A mock driver that always fails to connect (for error path testing)
    #[derive(Debug, Clone)]
    struct FailingDriver {}

    impl Driver for FailingDriver {
        fn name(&self) -> &str {
            "failing"
        }
        fn connect(&self, _url: &str) -> BoxFuture<'_, Result<Box<dyn Connection>, Error>> {
            Box::pin(async { Err(Error::from("connection refused")) })
        }
        fn connect_opt<'a>(
            &'a self,
            _option: &'a dyn ConnectOptions,
        ) -> BoxFuture<'a, Result<Box<dyn Connection>, Error>> {
            Box::pin(async { Err(Error::from("connection refused")) })
        }
        fn default_option(&self) -> Box<dyn ConnectOptions> {
            Box::new(FailingConnectOptions {})
        }
    }

    #[derive(Clone, Debug)]
    struct MockConnection {}
    impl Connection for MockConnection {
        fn exec_rows(
            &mut self,
            _sql: &str,
            _params: Vec<Value>,
        ) -> BoxFuture<
            '_,
            Result<Pin<Box<dyn Stream<Item = Result<Box<dyn Row>, Error>> + Send + '_>>, Error>,
        > {
            Box::pin(async { Err(Error::from("mock")) })
        }
        fn exec(
            &mut self,
            _sql: &str,
            _params: Vec<Value>,
        ) -> BoxFuture<'_, Result<ExecResult, Error>> {
            Box::pin(async {
                Ok(ExecResult {
                    rows_affected: 1,
                    last_insert_id: Value::Null,
                })
            })
        }
        fn close(&mut self) -> BoxFuture<'_, Result<(), Error>> {
            Box::pin(async { Ok(()) })
        }
        fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>> {
            Box::pin(async { Ok(()) })
        }
    }

    #[derive(Clone, Debug)]
    struct MockConnectOptions {}
    impl ConnectOptions for MockConnectOptions {
        fn connect(&self) -> BoxFuture<'_, Result<Box<dyn Connection>, Error>> {
            Box::pin(async { Ok(Box::new(MockConnection {}) as Box<dyn Connection>) })
        }
        fn set_uri(&mut self, _uri: &str) -> Result<(), Error> {
            Ok(())
        }
    }

    #[derive(Clone, Debug)]
    struct FailingConnectOptions {}
    impl ConnectOptions for FailingConnectOptions {
        fn connect(&self) -> BoxFuture<'_, Result<Box<dyn Connection>, Error>> {
            Box::pin(async { Err(Error::from("connect failed")) })
        }
        fn set_uri(&mut self, _uri: &str) -> Result<(), Error> {
            Ok(())
        }
    }

    // ==================== Init Error Path Tests ====================

    #[test]
    fn test_init_empty_url_error_message() {
        let rb = RBatis::new();
        let result = rb.init(MockDriver {}, "");
        assert!(result.is_err());
        let msg = result.err().unwrap().to_string();
        assert!(
            msg.contains("url is empty"),
            "Expected url is empty error, got: {}",
            msg
        );
    }

    // ==================== Pool Not Initialized Errors ====================

    #[test]
    fn test_get_pool_not_initialized_error_message() {
        let rb = RBatis::default(); // No pool
        let result = rb.get_pool();
        assert!(result.is_err());
        let msg = result.err().unwrap().to_string();
        assert!(
            msg.contains("pool not inited"),
            "Expected pool not inited error, got: {}",
            msg
        );
    }

    #[tokio::test]
    async fn test_acquire_not_initialized_error_message() {
        let rb = RBatis::default();
        let result = rb.acquire().await;
        assert!(result.is_err());
        let msg = result.err().unwrap().to_string();
        assert!(
            msg.contains("pool not inited"),
            "Expected pool not inited error, got: {}",
            msg
        );
    }

    #[tokio::test]
    async fn test_try_acquire_not_initialized_error_message() {
        let rb = RBatis::default();
        let result = rb.try_acquire().await;
        assert!(result.is_err());
        let msg = result.err().unwrap().to_string();
        assert!(
            msg.contains("pool not inited"),
            "Expected pool not inited error, got: {}",
            msg
        );
    }

    #[tokio::test]
    async fn test_exec_not_initialized_error_message() {
        let rb = RBatis::default();
        let result = rb.exec("select 1", vec![]).await;
        assert!(result.is_err());
        let msg = result.err().unwrap().to_string();
        assert!(
            msg.contains("pool not inited"),
            "Expected pool not inited error, got: {}",
            msg
        );
    }

    #[tokio::test]
    async fn test_query_not_initialized_error_message() {
        let rb = RBatis::default();
        let result = rb.query("select 1", vec![]).await;
        assert!(result.is_err());
        let msg = result.err().unwrap().to_string();
        assert!(
            msg.contains("pool not inited"),
            "Expected pool not inited error, got: {}",
            msg
        );
    }

    #[tokio::test]
    async fn test_exec_decode_not_initialized_error_message() {
        let rb = RBatis::default();
        let result: Result<i32, _> = rb.exec_decode("select 1", vec![]).await;
        assert!(result.is_err());
        let msg = result.err().unwrap().to_string();
        assert!(
            msg.contains("pool not inited"),
            "Expected pool not inited error, got: {}",
            msg
        );
    }

    #[tokio::test]
    async fn test_query_decode_deprecated_not_initialized() {
        let rb = RBatis::default();
        #[allow(deprecated)]
        let result: Result<i32, _> = rb.query_decode("select 1", vec![]).await;
        assert!(result.is_err());
    }

    // ==================== acquire_begin error paths ====================

    #[tokio::test]
    async fn test_acquire_begin_not_initialized() {
        let rb = RBatis::default();
        let result = rb.acquire_begin().await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_try_acquire_begin_not_initialized() {
        let rb = RBatis::default();
        let result = rb.try_acquire_begin().await;
        assert!(result.is_err());
    }

    // ==================== driver_type error path ====================

    #[test]
    fn test_driver_type_not_initialized() {
        let rb = RBatis::default();
        let result = rb.driver_type();
        assert!(result.is_err());
    }

    // ==================== init_pool double initialization ====================

    #[test]
    fn test_init_pool_twice_returns_error() {
        use rbatis::DefaultPool;
        use rbdc::pool::{ConnectionManager, Pool};

        let rb = RBatis::new();
        let opts = MockConnectOptions {};

        // First init should succeed
        let manager1 = ConnectionManager {
            driver: Arc::new(Box::new(MockDriver {})),
            option: Arc::new(Box::new(opts)),
        };
        let pool1 = DefaultPool::new(manager1).unwrap();
        rb.init_pool(pool1).unwrap();

        // Second init should fail because OnceLock is already set
        let opts2 = MockConnectOptions {};
        let manager2 = ConnectionManager {
            driver: Arc::new(Box::new(MockDriver {})),
            option: Arc::new(Box::new(opts2)),
        };
        let pool2 = DefaultPool::new(manager2).unwrap();
        let result = rb.init_pool(pool2);
        assert!(result.is_err());
        let msg = result.err().unwrap().to_string();
        assert!(msg.contains("pool already initialized"));
    }

    // ==================== RBatis::query (top-level) Test ====================

    #[tokio::test]
    async fn test_rbatis_query_success() {
        let rb = RBatis::new();
        rb.init(MockDriver {}, "test").unwrap();
        // query goes through acquire -> conn.query
        // With our mock, this will return an error from exec_rows
        let result = rb.query("select 1", vec![]).await;
        let _ = result; // May succeed or fail depending on mock implementation details
    }

    // ==================== RBatis::exec (top-level) Test ====================

    #[tokio::test]
    async fn test_rbatis_exec_success() {
        let rb = RBatis::new();
        rb.init(MockDriver {}, "test").unwrap();
        let result = rb.exec("insert into t values (1)", vec![]).await.unwrap();
        assert_eq!(result.rows_affected, 1); // Our mock returns rows_affected=1 for exec
    }

    // ==================== Intercept that returns error ====================

    #[derive(Debug)]
    struct ErrorIntercept;

    #[async_trait]
    impl Intercept for ErrorIntercept {
        async fn before(
            &self,
            _task_id: i64,
            _rb: &dyn Executor,
            _sql: &mut String,
            _args: &mut Vec<Value>,
            _result: ResultType<&mut Result<ExecResult, Error>, &mut Result<Value, Error>>,
        ) -> Result<Action, Error> {
            Err(Error::from("intercept before error"))
        }
    }

    #[tokio::test]
    async fn test_intercept_before_error_propagates() {
        let mut rb = RBatis::new();
        rb.set_intercepts(vec![Arc::new(ErrorIntercept)]);
        rb.init(MockDriver {}, "test").unwrap();

        let result = rb.exec("select 1", vec![]).await;
        assert!(result.is_err());
        let msg = result.err().unwrap().to_string();
        assert!(msg.contains("intercept before error"));
    }

    #[derive(Debug)]
    struct AfterErrorIntercept {
        pub call_count: Arc<std::sync::atomic::AtomicUsize>,
    }

    #[async_trait]
    impl Intercept for AfterErrorIntercept {
        async fn before(
            &self,
            _task_id: i64,
            _rb: &dyn Executor,
            _sql: &mut String,
            _args: &mut Vec<Value>,
            _result: ResultType<&mut Result<ExecResult, Error>, &mut Result<Value, Error>>,
        ) -> Result<Action, Error> {
            Ok(Action::Next)
        }

        async fn after(
            &self,
            _task_id: i64,
            _rb: &dyn Executor,
            _sql: &mut String,
            _args: &mut Vec<Value>,
            _result: ResultType<&mut Result<ExecResult, Error>, &mut Result<Value, Error>>,
        ) -> Result<Action, Error> {
            self.call_count
                .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            Err(Error::from("intercept after error"))
        }
    }

    #[tokio::test]
    async fn test_intercept_after_error_propagates_on_exec() {
        let counter = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let mut rb = RBatis::new();
        rb.set_intercepts(vec![Arc::new(AfterErrorIntercept {
            call_count: counter.clone(),
        })]);
        rb.init(MockDriver {}, "test").unwrap();

        let result = rb.exec("select 1", vec![]).await;
        assert!(result.is_err());
        let msg = result.err().unwrap().to_string();
        assert!(msg.contains("intercept after error"));
        // After was called
        assert_eq!(counter.load(std::sync::atomic::Ordering::SeqCst), 1);
    }

    // ==================== Executor trait id/name methods ====================

    #[test]
    fn test_executor_id_for_rbatis() {
        let rb = RBatis::new();
        // RBatis always has id() == 0
        assert_eq!(Executor::id(&rb), 0);
    }

    #[test]
    fn test_executor_name_for_rbatis() {
        let rb = RBatis::new();
        let name = Executor::name(&rb);
        assert!(name.contains("RBatis"));
    }

    // ==================== sync() function error cases ====================

    #[test]
    fn test_sync_non_map_value_error() {
        // sync requires Value::Map; passing something else should error
        let f = async move {
            let rb = RBatis::new();
            rb.init(MockDriver {}, "test").unwrap();
            let conn = rb.acquire().await.unwrap();

            let table_data = rbs::Value::I32(42); // Not a Map!
            let result = RBatis::sync(&conn, &rb, &table_data, "test_table").await;

            assert!(result.is_err());
            assert!(result
                .err()
                .unwrap()
                .to_string()
                .contains("not is an struct or map"));
        };
        block_on(f);
    }
    // ==================== RBatisRef trait method tests ====================

    #[test]
    fn test_rbatis_ref_driver_type_delegates() {
        let rb = RBatis::new();
        rb.init(MockDriver {}, "test").unwrap();
        // RBatisRef for RBatis delegates to RBatis::driver_type()
        assert_eq!(rb.driver_type().unwrap(), "test");
    }

    // ==================== Error type alias consistency ====================

    #[test]
    fn test_error_type_alias() {
        // Verify rbatis::Error is accessible as both Error and via the type alias
        fn accepts_error(_: rbatis::Error) {}
        fn accepts_result(r: rbatis::Result<()>) {
            let _ = r;
        }

        accepts_error(Error::from("test"));
        accepts_result(Ok(()));
        accepts_result(Err(Error::from("test")));
    }

    // ==================== Action Debug/Clone traits ====================

    #[test]
    fn test_action_debug() {
        let next_fmt = format!("{:?}", Action::Next);
        assert!(next_fmt.contains("Next"));

        let return_fmt = format!("{:?}", Action::Return);
        assert!(return_fmt.contains("Return"));
    }

    // ==================== ResultType Debug ====================

    #[test]
    fn test_result_type_debug_exec() {
        let rt: ResultType<i32, String> = ResultType::Exec(42i32);
        let fmt = format!("{:?}", rt);
        assert!(fmt.contains("Exec"));
    }

    #[test]
    fn test_result_type_debug_query() {
        let rt: ResultType<i32, String> = ResultType::Query("hello".to_string());
        let fmt = format!("{:?}", rt);
        assert!(fmt.contains("Query"));
    }

    // =================== init_option with failing options ===================

    #[tokio::test]
    async fn test_init_option_failing_connect() {
        let rb = RBatis::new();
        // Using FailingConnectOptions - the init itself may succeed but first acquire will fail
        use rbatis::DefaultPool;
        let result = rb.init_option::<FailingDriver, FailingConnectOptions, DefaultPool>(
            FailingDriver {},
            FailingConnectOptions {},
        );

        // init_option just creates the pool, it doesn't connect yet
        // So this should succeed
        assert!(result.is_ok());

        // But acquiring will fail
        let acq_result = rb.acquire().await;
        assert!(acq_result.is_err());
    }

    // ==================== link() error path ====================

    #[tokio::test]
    async fn test_link_empty_url() {
        let rb = RBatis::new();
        let result = rb.link(MockDriver {}, "").await;
        assert!(result.is_err());
        assert!(result.err().unwrap().to_string().contains("url is empty"));
    }

    #[tokio::test]
    async fn test_link_with_valid_url() {
        let rb = RBatis::new();
        let result = rb.link(MockDriver {}, "valid-url").await;
        // link does init + try_acquire, so it needs a real connection
        // This should succeed since our mock connects fine
        assert!(result.is_ok());
    }

    // ==================== Executor::exec/query trait dispatch ====================

    #[tokio::test]
    async fn test_executor_trait_dispatch_exec() {
        let rb = RBatis::new();
        rb.init(MockDriver {}, "test").unwrap();

        // Call through the trait method directly
        let result = Executor::exec(&rb, "insert into t values (1)", vec![]).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap().rows_affected, 1);
    }

    #[tokio::test]
    async fn test_executor_trait_dispatch_query() {
        let rb = RBatis::new();
        rb.init(MockDriver {}, "test").unwrap();

        // Query will go through acquire->conn.query which uses exec_rows
        // Our mock returns Err from exec_rows
        let result = Executor::query(&rb, "select 1", vec![]).await;
        let _ = result; // May or may not error depending on implementation
    }
}