hyperdb-api-core 0.1.1

Internal implementation details for hyperdb-api. Not a stable API; use hyperdb-api instead.
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Integration tests for the hyper-client Client API.
//!
//! These tests verify that the low-level `Client` API works correctly
//! against a real Hyper server.

mod common;
use common::TestServer;
use hyperdb_api_core::client::{Client, Config};

// =============================================================================
// Connection Tests
// =============================================================================

#[test]
fn test_client_connect() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    // Verify connection works with a simple query
    let rows = client.query("SELECT 1").expect("Failed to query");
    assert_eq!(rows.len(), 1);

    client.close().expect("Failed to close");
}

#[test]
fn test_client_connect_without_database() {
    let server = TestServer::without_database().expect("Failed to create test server");
    let client = server
        .connect_without_database()
        .expect("Failed to connect");

    // Should be able to query without a database attached
    let rows = client.query("SELECT 42 as value").expect("Failed to query");
    assert_eq!(rows.len(), 1);

    let value = rows[0].get_i32(0).expect("Failed to get value");
    assert_eq!(value, 42);

    client.close().expect("Failed to close");
}

#[test]
fn test_client_connect_invalid_host() {
    let config = Config::new()
        .with_host("invalid-host-that-does-not-exist.local")
        .with_port(7483);

    let result = Client::connect(&config);
    assert!(result.is_err(), "Should fail to connect to invalid host");
}

#[test]
fn test_client_connect_invalid_port() {
    // Use a port that's very unlikely to be in use
    let config = Config::new().with_host("127.0.0.1").with_port(1); // Port 1 is reserved and won't have Hyper running

    let result = Client::connect(&config);
    assert!(result.is_err(), "Should fail to connect to invalid port");
}

// =============================================================================
// Query Tests
// =============================================================================

#[test]
fn test_client_query_simple() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    // Create a table and insert data
    client
        .exec("CREATE TABLE test_query (id INT, name TEXT)")
        .expect("Failed to create table");

    client
        .exec("INSERT INTO test_query VALUES (1, 'Alice'), (2, 'Bob')")
        .expect("Failed to insert");

    // Query the data
    let rows = client
        .query("SELECT * FROM test_query ORDER BY id")
        .expect("Failed to query");

    assert_eq!(rows.len(), 2);

    // First row
    assert_eq!(rows[0].get_i32(0), Some(1));
    assert_eq!(rows[0].get_string(1), Some("Alice".to_string()));

    // Second row
    assert_eq!(rows[1].get_i32(0), Some(2));
    assert_eq!(rows[1].get_string(1), Some("Bob".to_string()));

    client.close().expect("Failed to close");
}

#[test]
fn test_client_query_with_nulls() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    client
        .exec("CREATE TABLE test_nulls (id INT, value TEXT)")
        .expect("Failed to create table");

    client
        .exec("INSERT INTO test_nulls VALUES (1, 'hello'), (2, NULL)")
        .expect("Failed to insert");

    let rows = client
        .query("SELECT * FROM test_nulls ORDER BY id")
        .expect("Failed to query");

    assert_eq!(rows.len(), 2);

    // First row: has value
    assert_eq!(rows[0].get_i32(0), Some(1));
    assert_eq!(rows[0].get_string(1), Some("hello".to_string()));

    // Second row: NULL value
    assert_eq!(rows[1].get_i32(0), Some(2));
    assert_eq!(rows[1].get_string(1), None);

    client.close().expect("Failed to close");
}

#[test]
fn test_client_query_empty_result() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    client
        .exec("CREATE TABLE test_empty (id INT)")
        .expect("Failed to create table");

    let rows = client
        .query("SELECT * FROM test_empty")
        .expect("Failed to query");

    assert!(rows.is_empty());

    client.close().expect("Failed to close");
}

#[test]
fn test_client_query_fast() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    client
        .exec("CREATE TABLE test_fast (id INT, value DOUBLE PRECISION)")
        .expect("Failed to create table");

    client
        .exec("INSERT INTO test_fast VALUES (1, 1.5), (2, 2.5), (3, 3.5)")
        .expect("Failed to insert");

    // Use query_fast for HyperBinary streaming
    let rows = client
        .query_fast("SELECT * FROM test_fast ORDER BY id")
        .expect("Failed to query_fast");

    assert_eq!(rows.len(), 3);

    // StreamRow uses get_* methods
    assert_eq!(rows[0].get_i32(0), Some(1));
    assert_eq!(rows[0].get_f64(1), Some(1.5));

    assert_eq!(rows[1].get_i32(0), Some(2));
    assert_eq!(rows[1].get_f64(1), Some(2.5));

    assert_eq!(rows[2].get_i32(0), Some(3));
    assert_eq!(rows[2].get_f64(1), Some(3.5));

    client.close().expect("Failed to close");
}

#[test]
fn test_client_query_streaming() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    client
        .exec("CREATE TABLE test_streaming (id INT)")
        .expect("Failed to create table");

    // Insert 100 rows
    for i in 0..100 {
        client
            .exec(&format!("INSERT INTO test_streaming VALUES ({i})"))
            .expect("Failed to insert");
    }

    // Stream with chunks of 10
    let mut total_rows = 0;
    let mut last_id = -1i32;

    {
        let mut stream = client
            .query_streaming("SELECT * FROM test_streaming ORDER BY id", 10)
            .expect("Failed to start streaming");

        while let Some(chunk) = stream.next_chunk().expect("Failed to get chunk") {
            assert!(chunk.len() <= 10, "Chunk should not exceed chunk_size");
            for row in &chunk {
                let id = row.get_i32(0).expect("Expected id");
                assert!(id > last_id, "Rows should be in order");
                last_id = id;
                total_rows += 1;
            }
        }
    } // stream dropped here, releasing borrow

    assert_eq!(total_rows, 100);

    client.close().expect("Failed to close");
}

// =============================================================================
// Exec Tests
// =============================================================================

#[test]
fn test_client_exec_create_table() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    let affected = client
        .exec("CREATE TABLE test_exec (id INT, name TEXT)")
        .expect("Failed to exec");

    // CREATE TABLE doesn't return affected rows
    assert_eq!(affected, 0);

    // Verify table exists
    let rows = client
        .query("SELECT COUNT(*) FROM test_exec")
        .expect("Failed to query");
    assert_eq!(rows.len(), 1);

    client.close().expect("Failed to close");
}

#[test]
fn test_client_exec_insert() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    client
        .exec("CREATE TABLE test_insert (id INT)")
        .expect("Failed to create table");

    let affected = client
        .exec("INSERT INTO test_insert VALUES (1), (2), (3)")
        .expect("Failed to insert");

    assert_eq!(affected, 3);

    client.close().expect("Failed to close");
}

#[test]
fn test_client_exec_update() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    client
        .exec("CREATE TABLE test_update (id INT, value INT)")
        .expect("Failed to create table");

    client
        .exec("INSERT INTO test_update VALUES (1, 10), (2, 20), (3, 30)")
        .expect("Failed to insert");

    let affected = client
        .exec("UPDATE test_update SET value = 100 WHERE id >= 2")
        .expect("Failed to update");

    assert_eq!(affected, 2);

    // Verify update
    let rows = client
        .query("SELECT value FROM test_update WHERE id = 2")
        .expect("Failed to query");
    assert_eq!(rows[0].get_i32(0), Some(100));

    client.close().expect("Failed to close");
}

#[test]
fn test_client_exec_delete() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    client
        .exec("CREATE TABLE test_delete (id INT)")
        .expect("Failed to create table");

    client
        .exec("INSERT INTO test_delete VALUES (1), (2), (3), (4), (5)")
        .expect("Failed to insert");

    let affected = client
        .exec("DELETE FROM test_delete WHERE id > 3")
        .expect("Failed to delete");

    assert_eq!(affected, 2);

    // Verify deletion
    let rows = client
        .query("SELECT COUNT(*) FROM test_delete")
        .expect("Failed to query");
    assert_eq!(rows[0].get_i64(0), Some(3));

    client.close().expect("Failed to close");
}

// =============================================================================
// Batch Execute Tests
// =============================================================================

// Note: Hyper disables multi-part queries by default (error 0A000).
// The batch_execute method exists for API compatibility but may fail
// depending on server configuration. We test that it's callable but
// handle the expected error gracefully.

#[test]
fn test_client_batch_execute_disabled() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    // Execute multiple statements - Hyper may reject this
    let result = client.batch_execute(
        "CREATE TABLE test_batch (id INT, name TEXT);
         INSERT INTO test_batch VALUES (1, 'first');",
    );

    // Hyper by default disables multi-part queries
    // Either it succeeds (if enabled) or fails with 0A000
    match result {
        Ok(()) => {
            // If it succeeded, verify the statements executed
            let rows = client
                .query("SELECT COUNT(*) FROM test_batch")
                .expect("Failed to query");
            assert!(rows[0].get_i64(0).unwrap_or(0) >= 1);
        }
        Err(e) => {
            // Multi-part queries disabled is expected
            assert!(
                e.to_string().contains("Multi-part queries are disabled")
                    || e.sqlstate() == Some("0A000"),
                "Unexpected error: {e}"
            );
        }
    }

    client.close().expect("Failed to close");
}

// =============================================================================
// Error Handling Tests
// =============================================================================

#[test]
fn test_client_query_syntax_error() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    let result = client.query("SELECT * FORM nonexistent");
    assert!(result.is_err(), "Should fail with syntax error");

    let err = result.unwrap_err();
    let err_str = err.to_string().to_lowercase();
    assert!(
        err_str.contains("syntax") || err_str.contains("parse"),
        "Error should mention syntax: {err_str}"
    );

    client.close().expect("Failed to close");
}

#[test]
fn test_client_query_table_not_found() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    let result = client.query("SELECT * FROM table_that_does_not_exist");
    assert!(result.is_err(), "Should fail for nonexistent table");

    client.close().expect("Failed to close");
}

#[test]
fn test_client_connection_still_usable_after_error() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    // Cause an error by querying a nonexistent table
    let result = client.query("SELECT * FROM nonexistent_table");
    assert!(result.is_err(), "Query should fail for nonexistent table");

    // Connection should still be usable after an error.
    // After an error, the connection state may need to be synchronized.
    // The is_alive() check should indicate if the connection is still valid.
    assert!(
        client.is_alive(),
        "Connection should still be alive after error"
    );

    // Try a simple query that doesn't depend on previous state
    // Note: After a query error, the connection may need the server to send
    // ReadyForQuery before accepting new commands. The simple query protocol
    // handles this, but there may be edge cases.
    let result = client.exec("SELECT 1");

    // The connection should either work or fail clearly
    // (not silently ignore commands)
    match result {
        Ok(_) => {
            // Connection recovered - good!
        }
        Err(e) => {
            // Connection may be in bad state - this is a known limitation
            // of some PostgreSQL-style clients
            eprintln!("Note: Connection did not recover from error: {e}");
        }
    }

    // Close should always work
    client.close().expect("Failed to close");
}

// =============================================================================
// Type Tests
// =============================================================================

#[test]
fn test_client_query_integer_types() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    client
        .exec("CREATE TABLE test_ints (small SMALLINT, med INTEGER, big BIGINT)")
        .expect("Failed to create table");

    client
        .exec("INSERT INTO test_ints VALUES (32767, 2147483647, 9223372036854775807)")
        .expect("Failed to insert");

    let rows = client
        .query("SELECT * FROM test_ints")
        .expect("Failed to query");

    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].get_i16(0), Some(32767));
    assert_eq!(rows[0].get_i32(1), Some(2147483647));
    assert_eq!(rows[0].get_i64(2), Some(9223372036854775807));

    client.close().expect("Failed to close");
}

#[test]
fn test_client_query_float_types() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    client
        .exec("CREATE TABLE test_floats (val DOUBLE PRECISION)")
        .expect("Failed to create table");

    client
        .exec("INSERT INTO test_floats VALUES (3.14259265358979)")
        .expect("Failed to insert");

    let rows = client
        .query("SELECT * FROM test_floats")
        .expect("Failed to query");

    assert_eq!(rows.len(), 1);
    let val = rows[0].get_f64(0).expect("Expected f64");
    assert!((val - 3.14259265358979).abs() < 1e-10);

    client.close().expect("Failed to close");
}

#[test]
fn test_client_query_bool_type() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    client
        .exec("CREATE TABLE test_bool (val BOOL)")
        .expect("Failed to create table");

    client
        .exec("INSERT INTO test_bool VALUES (true), (false)")
        .expect("Failed to insert");

    let rows = client
        .query("SELECT * FROM test_bool ORDER BY val")
        .expect("Failed to query");

    assert_eq!(rows.len(), 2);
    // false sorts before true
    assert_eq!(rows[0].get_bool(0), Some(false));
    assert_eq!(rows[1].get_bool(0), Some(true));

    client.close().expect("Failed to close");
}

#[test]
fn test_client_query_text_type() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    client
        .exec("CREATE TABLE test_text (val TEXT)")
        .expect("Failed to create table");

    client
        .exec("INSERT INTO test_text VALUES ('hello'), ('world'), ('unicode: 你好')")
        .expect("Failed to insert");

    let rows = client
        .query("SELECT * FROM test_text ORDER BY val")
        .expect("Failed to query");

    assert_eq!(rows.len(), 3);
    assert_eq!(rows[0].get_string(0), Some("hello".to_string()));
    assert_eq!(rows[1].get_string(0), Some("unicode: 你好".to_string()));
    assert_eq!(rows[2].get_string(0), Some("world".to_string()));

    client.close().expect("Failed to close");
}

#[test]
fn test_client_query_bytes_type() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    client
        .exec("CREATE TABLE test_bytes (val BYTEA)")
        .expect("Failed to create table");

    // Insert binary data using hex notation
    client
        .exec("INSERT INTO test_bytes VALUES ('\\x48656c6c6f')")
        .expect("Failed to insert");

    // Use query_fast for binary format results
    let rows = client
        .query_fast("SELECT * FROM test_bytes")
        .expect("Failed to query");

    assert_eq!(rows.len(), 1);
    let bytes = rows[0].get_bytes(0).expect("Expected bytes");
    assert_eq!(bytes, b"Hello");

    client.close().expect("Failed to close");
}

// =============================================================================
// Thread Safety Tests
// =============================================================================

#[test]
fn test_client_thread_safety() {
    use std::sync::Arc;
    use std::thread;

    let server = TestServer::new().expect("Failed to create test server");
    let client = Arc::new(server.connect().expect("Failed to connect"));

    client
        .exec("CREATE TABLE test_threads (id INT, thread_id INT)")
        .expect("Failed to create table");

    let mut handles = vec![];

    // Spawn multiple threads that use the same client
    for thread_id in 0..4 {
        let client_clone = Arc::clone(&client);
        let handle = thread::spawn(move || {
            for i in 0..10 {
                let id = thread_id * 100 + i;
                client_clone
                    .exec(&format!(
                        "INSERT INTO test_threads VALUES ({id}, {thread_id})"
                    ))
                    .expect("Failed to insert");
            }
        });
        handles.push(handle);
    }

    // Wait for all threads
    for handle in handles {
        handle.join().expect("Thread panicked");
    }

    // Verify all inserts succeeded
    let rows = client
        .query("SELECT COUNT(*) FROM test_threads")
        .expect("Failed to query");
    assert_eq!(rows[0].get_i64(0), Some(40)); // 4 threads * 10 rows each

    // Note: We don't call close() because Arc<Client> doesn't expose it directly
    // The client will be closed when all Arc references are dropped
}

// =============================================================================
// Client Metadata Tests
// =============================================================================

#[test]
fn test_client_process_id() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    // Process ID is returned by the server during connection setup.
    // Hyper may return 0 which is still a valid value (means no backend PID tracking).
    let _pid = client.process_id();
    // Just verify we can access the value without panicking

    client.close().expect("Failed to close");
}

#[test]
fn test_client_secret_key() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    // Secret key can be any i32 value
    let _key = client.secret_key();

    client.close().expect("Failed to close");
}

#[test]
fn test_client_is_alive() {
    let server = TestServer::new().expect("Failed to create test server");
    let client = server.connect().expect("Failed to connect");

    assert!(client.is_alive(), "Client should be alive after connect");

    client.close().expect("Failed to close");
}