sal-postgresclient 0.1.0

SAL PostgreSQL Client - PostgreSQL client wrapper with connection management and Rhai integration
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
use sal_postgresclient::*;
use std::collections::HashMap;
use std::env;

#[cfg(test)]
mod postgres_client_tests {
    use super::*;

    #[test]
    fn test_env_vars() {
        // Save original environment variables to restore later
        let original_host = env::var("POSTGRES_HOST").ok();
        let original_port = env::var("POSTGRES_PORT").ok();
        let original_user = env::var("POSTGRES_USER").ok();
        let original_password = env::var("POSTGRES_PASSWORD").ok();
        let original_db = env::var("POSTGRES_DB").ok();

        // Set test environment variables
        env::set_var("POSTGRES_HOST", "test-host");
        env::set_var("POSTGRES_PORT", "5433");
        env::set_var("POSTGRES_USER", "test-user");
        env::set_var("POSTGRES_PASSWORD", "test-password");
        env::set_var("POSTGRES_DB", "test-db");

        // Test with invalid port
        env::set_var("POSTGRES_PORT", "invalid");

        // Test with unset values
        env::remove_var("POSTGRES_HOST");
        env::remove_var("POSTGRES_PORT");
        env::remove_var("POSTGRES_USER");
        env::remove_var("POSTGRES_PASSWORD");
        env::remove_var("POSTGRES_DB");

        // Restore original environment variables
        if let Some(host) = original_host {
            env::set_var("POSTGRES_HOST", host);
        }
        if let Some(port) = original_port {
            env::set_var("POSTGRES_PORT", port);
        }
        if let Some(user) = original_user {
            env::set_var("POSTGRES_USER", user);
        }
        if let Some(password) = original_password {
            env::set_var("POSTGRES_PASSWORD", password);
        }
        if let Some(db) = original_db {
            env::set_var("POSTGRES_DB", db);
        }
    }

    #[test]
    fn test_postgres_config_builder() {
        // Test the PostgreSQL configuration builder

        // Test default values
        let config = PostgresConfigBuilder::new();
        assert_eq!(config.host, "localhost");
        assert_eq!(config.port, 5432);
        assert_eq!(config.user, "postgres");
        assert_eq!(config.password, None);
        assert_eq!(config.database, "postgres");
        assert_eq!(config.application_name, None);
        assert_eq!(config.connect_timeout, None);
        assert_eq!(config.ssl_mode, None);

        // Test setting values
        let config = PostgresConfigBuilder::new()
            .host("pg.example.com")
            .port(5433)
            .user("test-user")
            .password("test-password")
            .database("test-db")
            .application_name("test-app")
            .connect_timeout(30)
            .ssl_mode("require");

        assert_eq!(config.host, "pg.example.com");
        assert_eq!(config.port, 5433);
        assert_eq!(config.user, "test-user");
        assert_eq!(config.password, Some("test-password".to_string()));
        assert_eq!(config.database, "test-db");
        assert_eq!(config.application_name, Some("test-app".to_string()));
        assert_eq!(config.connect_timeout, Some(30));
        assert_eq!(config.ssl_mode, Some("require".to_string()));
    }

    #[test]
    fn test_connection_string_building() {
        // Test building connection strings

        // Test default connection string
        let config = PostgresConfigBuilder::new();
        let conn_string = config.build_connection_string();
        assert!(conn_string.contains("host=localhost"));
        assert!(conn_string.contains("port=5432"));
        assert!(conn_string.contains("user=postgres"));
        assert!(conn_string.contains("dbname=postgres"));
        assert!(!conn_string.contains("password="));

        // Test with all options
        let config = PostgresConfigBuilder::new()
            .host("pg.example.com")
            .port(5433)
            .user("test-user")
            .password("test-password")
            .database("test-db")
            .application_name("test-app")
            .connect_timeout(30)
            .ssl_mode("require");

        let conn_string = config.build_connection_string();
        assert!(conn_string.contains("host=pg.example.com"));
        assert!(conn_string.contains("port=5433"));
        assert!(conn_string.contains("user=test-user"));
        assert!(conn_string.contains("password=test-password"));
        assert!(conn_string.contains("dbname=test-db"));
        assert!(conn_string.contains("application_name=test-app"));
        assert!(conn_string.contains("connect_timeout=30"));
        assert!(conn_string.contains("sslmode=require"));
    }

    #[test]
    fn test_reset_mock() {
        // This is a simplified test that doesn't require an actual PostgreSQL server

        // Just verify that the reset function doesn't panic
        if let Err(_) = reset() {
            // If PostgreSQL is not available, this is expected to fail
            // So we don't assert anything here
        }
    }
}

// Integration tests that require a real PostgreSQL server
// These tests will be skipped if PostgreSQL is not available
#[cfg(test)]
mod postgres_installer_tests {
    use super::*;
    use sal_virt::nerdctl::Container;

    #[test]
    fn test_postgres_installer_config() {
        // Test default configuration
        let config = PostgresInstallerConfig::default();
        assert_eq!(config.container_name, "postgres");
        assert_eq!(config.version, "latest");
        assert_eq!(config.port, 5432);
        assert_eq!(config.username, "postgres");
        assert_eq!(config.password, "postgres");
        assert_eq!(config.data_dir, None);
        assert_eq!(config.env_vars.len(), 0);
        assert_eq!(config.persistent, true);

        // Test builder pattern
        let config = PostgresInstallerConfig::new()
            .container_name("my-postgres")
            .version("15")
            .port(5433)
            .username("testuser")
            .password("testpass")
            .data_dir("/tmp/pgdata")
            .env_var("POSTGRES_INITDB_ARGS", "--encoding=UTF8")
            .persistent(false);

        assert_eq!(config.container_name, "my-postgres");
        assert_eq!(config.version, "15");
        assert_eq!(config.port, 5433);
        assert_eq!(config.username, "testuser");
        assert_eq!(config.password, "testpass");
        assert_eq!(config.data_dir, Some("/tmp/pgdata".to_string()));
        assert_eq!(config.env_vars.len(), 1);
        assert_eq!(
            config.env_vars.get("POSTGRES_INITDB_ARGS").unwrap(),
            "--encoding=UTF8"
        );
        assert_eq!(config.persistent, false);
    }

    #[test]
    fn test_postgres_installer_error() {
        // Test IoError
        let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
        let installer_error = PostgresInstallerError::IoError(io_error);
        assert!(format!("{}", installer_error).contains("I/O error"));

        // Test NerdctlError
        let nerdctl_error = PostgresInstallerError::NerdctlError("Container not found".to_string());
        assert!(format!("{}", nerdctl_error).contains("Nerdctl error"));

        // Test PostgresError
        let postgres_error =
            PostgresInstallerError::PostgresError("Database not found".to_string());
        assert!(format!("{}", postgres_error).contains("PostgreSQL error"));
    }

    #[test]
    fn test_install_postgres_with_defaults() {
        // This is a unit test that doesn't actually install PostgreSQL
        // It just tests the configuration and error handling

        // Test with default configuration
        let config = PostgresInstallerConfig::default();

        // We expect this to fail because nerdctl is not available
        let result = install_postgres(config);
        assert!(result.is_err());

        // Check that the error is a NerdctlError or IoError
        match result {
            Err(PostgresInstallerError::NerdctlError(_)) => {
                // This is fine, we expected a NerdctlError
            }
            Err(PostgresInstallerError::IoError(_)) => {
                // This is also fine, we expected an error
            }
            _ => panic!("Expected NerdctlError or IoError"),
        }
    }

    #[test]
    fn test_install_postgres_with_custom_config() {
        // Test with custom configuration
        let config = PostgresInstallerConfig::new()
            .container_name("test-postgres")
            .version("15")
            .port(5433)
            .username("testuser")
            .password("testpass")
            .data_dir("/tmp/pgdata")
            .env_var("POSTGRES_INITDB_ARGS", "--encoding=UTF8")
            .persistent(true);

        // We expect this to fail because nerdctl is not available
        let result = install_postgres(config);
        assert!(result.is_err());

        // Check that the error is a NerdctlError or IoError
        match result {
            Err(PostgresInstallerError::NerdctlError(_)) => {
                // This is fine, we expected a NerdctlError
            }
            Err(PostgresInstallerError::IoError(_)) => {
                // This is also fine, we expected an error
            }
            _ => panic!("Expected NerdctlError or IoError"),
        }
    }

    #[test]
    fn test_create_database() {
        // Create a mock container
        // In a real test, we would use mockall to create a mock container
        // But for this test, we'll just test the error handling

        // We expect this to fail because the container is not running
        let result = create_database(
            &Container {
                name: "test-postgres".to_string(),
                container_id: None,
                image: Some("postgres:15".to_string()),
                config: HashMap::new(),
                ports: Vec::new(),
                volumes: Vec::new(),
                env_vars: HashMap::new(),
                network: None,
                network_aliases: Vec::new(),
                cpu_limit: None,
                memory_limit: None,
                memory_swap_limit: None,
                cpu_shares: None,
                restart_policy: None,
                health_check: None,
                detach: false,
                snapshotter: None,
            },
            "testdb",
        );

        assert!(result.is_err());

        // Check that the error is a PostgresError
        match result {
            Err(PostgresInstallerError::PostgresError(msg)) => {
                assert!(msg.contains("Container is not running"));
            }
            _ => panic!("Expected PostgresError"),
        }
    }

    #[test]
    fn test_execute_sql() {
        // Create a mock container
        // In a real test, we would use mockall to create a mock container
        // But for this test, we'll just test the error handling

        // We expect this to fail because the container is not running
        let result = execute_sql(
            &Container {
                name: "test-postgres".to_string(),
                container_id: None,
                image: Some("postgres:15".to_string()),
                config: HashMap::new(),
                ports: Vec::new(),
                volumes: Vec::new(),
                env_vars: HashMap::new(),
                network: None,
                network_aliases: Vec::new(),
                cpu_limit: None,
                memory_limit: None,
                memory_swap_limit: None,
                cpu_shares: None,
                restart_policy: None,
                health_check: None,
                detach: false,
                snapshotter: None,
            },
            "testdb",
            "SELECT 1",
        );

        assert!(result.is_err());

        // Check that the error is a PostgresError
        match result {
            Err(PostgresInstallerError::PostgresError(msg)) => {
                assert!(msg.contains("Container is not running"));
            }
            _ => panic!("Expected PostgresError"),
        }
    }

    #[test]
    fn test_is_postgres_running() {
        // Create a mock container
        // In a real test, we would use mockall to create a mock container
        // But for this test, we'll just test the error handling

        // We expect this to return false because the container is not running
        let result = is_postgres_running(&Container {
            name: "test-postgres".to_string(),
            container_id: None,
            image: Some("postgres:15".to_string()),
            config: HashMap::new(),
            ports: Vec::new(),
            volumes: Vec::new(),
            env_vars: HashMap::new(),
            network: None,
            network_aliases: Vec::new(),
            cpu_limit: None,
            memory_limit: None,
            memory_swap_limit: None,
            cpu_shares: None,
            restart_policy: None,
            health_check: None,
            detach: false,
            snapshotter: None,
        });

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), false);
    }
}

#[cfg(test)]
mod postgres_integration_tests {
    use super::*;
    use std::time::Duration;

    // Helper function to check if PostgreSQL is available
    fn is_postgres_available() -> bool {
        match get_postgres_client() {
            Ok(_) => true,
            Err(_) => false,
        }
    }

    #[test]
    fn test_postgres_client_integration() {
        if !is_postgres_available() {
            println!("Skipping PostgreSQL integration tests - PostgreSQL server not available");
            return;
        }

        println!("Running PostgreSQL integration tests...");

        // Test basic operations
        test_basic_postgres_operations();

        // Test error handling
        test_error_handling();
    }

    #[test]
    fn test_connection_pool() {
        if !is_postgres_available() {
            println!("Skipping PostgreSQL connection pool tests - PostgreSQL server not available");
            return;
        }

        run_connection_pool_test();
    }

    fn run_connection_pool_test() {
        println!("Running PostgreSQL connection pool tests...");

        // Test creating a connection pool
        let config = PostgresConfigBuilder::new()
            .use_pool(true)
            .pool_max_size(5)
            .pool_min_idle(1)
            .pool_connection_timeout(Duration::from_secs(5));

        let pool_result = config.build_pool();
        assert!(pool_result.is_ok());

        let pool = pool_result.unwrap();

        // Test getting a connection from the pool
        let conn_result = pool.get();
        assert!(conn_result.is_ok());

        // Test executing a query with the connection
        let mut conn = conn_result.unwrap();
        let query_result = conn.query("SELECT 1", &[]);
        assert!(query_result.is_ok());

        // Test the global pool
        let global_pool_result = get_postgres_pool();
        assert!(global_pool_result.is_ok());

        // Test executing queries with the pool
        let create_table_query = "
            CREATE TEMPORARY TABLE pool_test (
                id SERIAL PRIMARY KEY,
                name TEXT NOT NULL
            )
        ";

        let create_result = execute_with_pool(create_table_query, &[]);
        assert!(create_result.is_ok());

        // Test with parameters
        let insert_result = execute_with_pool(
            "INSERT INTO pool_test (name) VALUES ($1) RETURNING id",
            &[&"test_pool"],
        );
        assert!(insert_result.is_ok());

        // Test with QueryParams
        let mut params = QueryParams::new();
        params.add_str("test_pool_params");

        let insert_params_result = execute_with_pool_params(
            "INSERT INTO pool_test (name) VALUES ($1) RETURNING id",
            &params,
        );
        assert!(insert_params_result.is_ok());

        // Test query functions
        let query_result = query_with_pool("SELECT * FROM pool_test", &[]);
        assert!(query_result.is_ok());
        let rows = query_result.unwrap();
        assert_eq!(rows.len(), 2);

        // Test query_one
        let query_one_result =
            query_one_with_pool("SELECT * FROM pool_test WHERE name = $1", &[&"test_pool"]);
        assert!(query_one_result.is_ok());

        // Test query_opt
        let query_opt_result =
            query_opt_with_pool("SELECT * FROM pool_test WHERE name = $1", &[&"nonexistent"]);
        assert!(query_opt_result.is_ok());
        assert!(query_opt_result.unwrap().is_none());

        // Test resetting the pool
        let reset_result = reset_pool();
        assert!(reset_result.is_ok());

        // Test getting the pool again after reset
        let pool_after_reset = get_postgres_pool();
        assert!(pool_after_reset.is_ok());
    }

    fn test_basic_postgres_operations() {
        if !is_postgres_available() {
            return;
        }

        // Create a test table
        let create_table_query = "
            CREATE TEMPORARY TABLE test_table (
                id SERIAL PRIMARY KEY,
                name TEXT NOT NULL,
                value INTEGER
            )
        ";

        let create_result = execute(create_table_query, &[]);
        assert!(create_result.is_ok());

        // Insert data
        let insert_query = "
            INSERT INTO test_table (name, value)
            VALUES ($1, $2)
            RETURNING id
        ";

        let insert_result = query(insert_query, &[&"test_name", &42]);
        assert!(insert_result.is_ok());

        let rows = insert_result.unwrap();
        assert_eq!(rows.len(), 1);

        let id: i32 = rows[0].get(0);
        assert!(id > 0);

        // Query data
        let select_query = "
            SELECT id, name, value
            FROM test_table
            WHERE id = $1
        ";

        let select_result = query_one(select_query, &[&id]);
        assert!(select_result.is_ok());

        let row = select_result.unwrap();
        let name: String = row.get(1);
        let value: i32 = row.get(2);

        assert_eq!(name, "test_name");
        assert_eq!(value, 42);

        // Update data
        let update_query = "
            UPDATE test_table
            SET value = $1
            WHERE id = $2
        ";

        let update_result = execute(update_query, &[&100, &id]);
        assert!(update_result.is_ok());
        assert_eq!(update_result.unwrap(), 1); // 1 row affected

        // Verify update
        let verify_query = "
            SELECT value
            FROM test_table
            WHERE id = $1
        ";

        let verify_result = query_one(verify_query, &[&id]);
        assert!(verify_result.is_ok());

        let row = verify_result.unwrap();
        let updated_value: i32 = row.get(0);
        assert_eq!(updated_value, 100);

        // Delete data
        let delete_query = "
            DELETE FROM test_table
            WHERE id = $1
        ";

        let delete_result = execute(delete_query, &[&id]);
        assert!(delete_result.is_ok());
        assert_eq!(delete_result.unwrap(), 1); // 1 row affected
    }

    #[test]
    fn test_query_params() {
        if !is_postgres_available() {
            println!("Skipping PostgreSQL parameter tests - PostgreSQL server not available");
            return;
        }

        run_query_params_test();
    }

    #[test]
    fn test_transactions() {
        if !is_postgres_available() {
            println!("Skipping PostgreSQL transaction tests - PostgreSQL server not available");
            return;
        }

        println!("Running PostgreSQL transaction tests...");

        // Test successful transaction
        let result = transaction(|client| {
            // Create a temporary table
            client.execute(
                "CREATE TEMPORARY TABLE transaction_test (id SERIAL PRIMARY KEY, name TEXT NOT NULL)",
                &[],
            )?;

            // Insert data
            client.execute(
                "INSERT INTO transaction_test (name) VALUES ($1)",
                &[&"test_transaction"],
            )?;

            // Query data
            let rows = client.query(
                "SELECT * FROM transaction_test WHERE name = $1",
                &[&"test_transaction"],
            )?;

            assert_eq!(rows.len(), 1);
            let name: String = rows[0].get(1);
            assert_eq!(name, "test_transaction");

            // Return success
            Ok(true)
        });

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), true);

        // Test failed transaction
        let result = transaction(|client| {
            // Create a temporary table
            client.execute(
                "CREATE TEMPORARY TABLE transaction_test_fail (id SERIAL PRIMARY KEY, name TEXT NOT NULL)",
                &[],
            )?;

            // Insert data
            client.execute(
                "INSERT INTO transaction_test_fail (name) VALUES ($1)",
                &[&"test_transaction_fail"],
            )?;

            // Cause an error with invalid SQL
            client.execute("THIS IS INVALID SQL", &[])?;

            // This should not be reached
            Ok(false)
        });

        assert!(result.is_err());

        // Verify that the table was not created (transaction was rolled back)
        let verify_result = query("SELECT * FROM transaction_test_fail", &[]);

        assert!(verify_result.is_err());

        // Test transaction with pool
        let result = transaction_with_pool(|client| {
            // Create a temporary table
            client.execute(
                "CREATE TEMPORARY TABLE transaction_pool_test (id SERIAL PRIMARY KEY, name TEXT NOT NULL)",
                &[],
            )?;

            // Insert data
            client.execute(
                "INSERT INTO transaction_pool_test (name) VALUES ($1)",
                &[&"test_transaction_pool"],
            )?;

            // Query data
            let rows = client.query(
                "SELECT * FROM transaction_pool_test WHERE name = $1",
                &[&"test_transaction_pool"],
            )?;

            assert_eq!(rows.len(), 1);
            let name: String = rows[0].get(1);
            assert_eq!(name, "test_transaction_pool");

            // Return success
            Ok(true)
        });

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), true);
    }

    fn run_query_params_test() {
        println!("Running PostgreSQL parameter tests...");

        // Create a test table
        let create_table_query = "
            CREATE TEMPORARY TABLE param_test (
                id SERIAL PRIMARY KEY,
                name TEXT NOT NULL,
                value INTEGER,
                active BOOLEAN,
                score REAL
            )
        ";

        let create_result = execute(create_table_query, &[]);
        assert!(create_result.is_ok());

        // Test QueryParams builder
        let mut params = QueryParams::new();
        params.add_str("test_name");
        params.add_int(42);
        params.add_bool(true);
        params.add_float(3.14);

        // Insert data using QueryParams
        let insert_query = "
            INSERT INTO param_test (name, value, active, score)
            VALUES ($1, $2, $3, $4)
            RETURNING id
        ";

        let insert_result = query_with_params(insert_query, &params);
        assert!(insert_result.is_ok());

        let rows = insert_result.unwrap();
        assert_eq!(rows.len(), 1);

        let id: i32 = rows[0].get(0);
        assert!(id > 0);

        // Query data using QueryParams
        let mut query_params = QueryParams::new();
        query_params.add_int(id);

        let select_query = "
            SELECT id, name, value, active, score
            FROM param_test
            WHERE id = $1
        ";

        let select_result = query_one_with_params(select_query, &query_params);
        assert!(select_result.is_ok());

        let row = select_result.unwrap();
        let name: String = row.get(1);
        let value: i32 = row.get(2);
        let active: bool = row.get(3);
        let score: f64 = row.get(4);

        assert_eq!(name, "test_name");
        assert_eq!(value, 42);
        assert_eq!(active, true);
        assert_eq!(score, 3.14);

        // Test optional parameters
        let mut update_params = QueryParams::new();
        update_params.add_int(100);
        update_params.add_opt::<String>(None);
        update_params.add_int(id);

        let update_query = "
            UPDATE param_test
            SET value = $1, name = COALESCE($2, name)
            WHERE id = $3
        ";

        let update_result = execute_with_params(update_query, &update_params);
        assert!(update_result.is_ok());
        assert_eq!(update_result.unwrap(), 1); // 1 row affected

        // Verify update
        let verify_result = query_one_with_params(select_query, &query_params);
        assert!(verify_result.is_ok());

        let row = verify_result.unwrap();
        let name: String = row.get(1);
        let value: i32 = row.get(2);

        assert_eq!(name, "test_name"); // Name should be unchanged
        assert_eq!(value, 100); // Value should be updated

        // Test query_opt_with_params
        let mut nonexistent_params = QueryParams::new();
        nonexistent_params.add_int(9999); // ID that doesn't exist

        let opt_query = "
            SELECT id, name
            FROM param_test
            WHERE id = $1
        ";

        let opt_result = query_opt_with_params(opt_query, &nonexistent_params);
        assert!(opt_result.is_ok());
        assert!(opt_result.unwrap().is_none());

        // Clean up
        let delete_query = "
            DELETE FROM param_test
            WHERE id = $1
        ";

        let delete_result = execute_with_params(delete_query, &query_params);
        assert!(delete_result.is_ok());
        assert_eq!(delete_result.unwrap(), 1); // 1 row affected
    }

    fn test_error_handling() {
        if !is_postgres_available() {
            return;
        }

        // Test invalid SQL
        let invalid_query = "SELECT * FROM nonexistent_table";
        let invalid_result = query(invalid_query, &[]);
        assert!(invalid_result.is_err());

        // Test parameter type mismatch
        let mismatch_query = "SELECT $1::integer";
        let mismatch_result = query(mismatch_query, &[&"not_an_integer"]);
        assert!(mismatch_result.is_err());

        // Test query_one with no results
        let empty_query = "SELECT * FROM pg_tables WHERE tablename = 'nonexistent_table'";
        let empty_result = query_one(empty_query, &[]);
        assert!(empty_result.is_err());

        // Test query_opt with no results
        let opt_query = "SELECT * FROM pg_tables WHERE tablename = 'nonexistent_table'";
        let opt_result = query_opt(opt_query, &[]);
        assert!(opt_result.is_ok());
        assert!(opt_result.unwrap().is_none());
    }

    #[test]
    fn test_notify() {
        if !is_postgres_available() {
            println!("Skipping PostgreSQL notification tests - PostgreSQL server not available");
            return;
        }

        println!("Running PostgreSQL notification tests...");

        // Test sending a notification
        let result = notify("test_channel", "test_payload");
        assert!(result.is_ok());

        // Test sending a notification with the pool
        let result = notify_with_pool("test_channel_pool", "test_payload_pool");
        assert!(result.is_ok());
    }
}