• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

polyphony-chat / sonata / 16378096239

18 Jul 2025 06:49PM UTC coverage: 91.622% (+17.6%) from 74.035%
16378096239

push

github

bitfl0wer
chore: fix tests, add coverage

17 of 52 branches covered (32.69%)

Branch coverage included in aggregate %.

105 of 105 new or added lines in 1 file covered. (100.0%)

5 existing lines in 3 files now uncovered.

661 of 688 relevant lines covered (96.08%)

3760.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.68
/src/database/mod.rs
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4

5
use sqlx::{
6
        PgPool,
7
        postgres::{PgConnectOptions, PgPoolOptions},
8
};
9

10
use crate::{StdResult, config::DatabaseConfig};
11

12
pub(crate) mod algorithm_identifier;
13
pub(crate) mod api_keys;
14
pub(crate) mod idcert;
15
pub(crate) mod issuer;
16
pub(crate) mod keytrials;
17
pub(crate) mod models;
18
pub(crate) mod serial_number;
19
pub(crate) mod tokens;
20

21
pub(crate) use models::*;
22

23
#[derive(Debug, Clone)]
24
/// Main Database struct. Wrapper around [PgPool].
25
pub(crate) struct Database {
26
        /// The underlying `sqlx` [PgPool].
27
        pub pool: PgPool,
28
}
29

30
impl Database {
31
        /// Connect to the PostgreSQL Database using configuration options provided
32
        /// through [DatabaseConfig], which is most commonly derived by parsing a
33
        /// [SonataConfiguration].
34
        #[cfg_attr(coverage_nightly, coverage(off))]
35
        pub async fn connect_with_config(config: &DatabaseConfig) -> StdResult<Self> {
36
                let connect_options = PgConnectOptions::new()
37
                        .host(&config.host)
38
                        .database(&config.database)
39
                        .application_name("sonata")
40
                        .password(&config.password)
41
                        .port(config.port)
42
                        .ssl_mode(match config.tls {
43
                                crate::config::TlsConfig::Disable => sqlx::postgres::PgSslMode::Disable,
44
                                crate::config::TlsConfig::Allow => sqlx::postgres::PgSslMode::Allow,
45
                                crate::config::TlsConfig::Prefer => sqlx::postgres::PgSslMode::Prefer,
46
                                crate::config::TlsConfig::Require => sqlx::postgres::PgSslMode::Require,
47
                                crate::config::TlsConfig::VerifyCa => sqlx::postgres::PgSslMode::VerifyCa,
48
                                crate::config::TlsConfig::VerifyFull => sqlx::postgres::PgSslMode::VerifyFull,
49
                        })
50
                        .username(&config.username);
51
                let pool = PgPoolOptions::new()
52
                        .max_connections(config.max_connections)
53
                        .connect_with(connect_options)
54
                        .await?;
55
                Ok(Self { pool })
56
        }
57

58
        /// Applies the migrations.
UNCOV
59
        pub(super) async fn run_migrations(&self) -> StdResult<()> {
×
UNCOV
60
                sqlx::migrate!().run(&self.pool).await.map_err(|e| e.into())
×
UNCOV
61
        }
×
62
}
63

64
#[cfg(test)]
65
mod tests {
66
        use super::*;
67
        use crate::config::TlsConfig;
68

69
        #[test]
70
        fn test_database_debug() {
1✔
71
                // We can't easily test the actual Database struct without a real connection,
72
                // but we can test that it implements Debug
73
                // This is a compile-time test to ensure Debug is implemented
74
                fn assert_debug<T: std::fmt::Debug>() {}
1✔
75
                assert_debug::<Database>();
1✔
76
        }
1✔
77

78
        #[test]
79
        fn test_database_clone() {
1✔
80
                // This is a compile-time test to ensure Clone is implemented
81
                fn assert_clone<T: Clone>() {}
1✔
82
                assert_clone::<Database>();
1✔
83
        }
1✔
84

85
        #[tokio::test]
86
        async fn test_connect_with_config_invalid() {
1✔
87
                let config = DatabaseConfig {
1✔
88
                        max_connections: 1,
1✔
89
                        database: "nonexistent".to_owned(),
1✔
90
                        username: "invalid".to_owned(),
1✔
91
                        password: "invalid".to_owned(),
1✔
92
                        port: 5432,
1✔
93
                        host: "invalid_host".to_owned(),
1✔
94
                        tls: TlsConfig::Disable,
1✔
95
                };
1✔
96

97
                // This should fail to connect
98
                let result = Database::connect_with_config(&config).await;
1✔
99
                assert!(result.is_err());
1✔
100
        }
1✔
101

102
        #[tokio::test]
103
        async fn test_connect_with_config_zero_max_connections() {
1✔
104
                let config = DatabaseConfig {
1✔
105
                        max_connections: 0, // Zero connections should cause a panic during pool creation
1✔
106
                        database: "test".to_owned(),
1✔
107
                        username: "test".to_owned(),
1✔
108
                        password: "test".to_owned(),
1✔
109
                        port: 5432,
1✔
110
                        host: "localhost".to_owned(),
1✔
111
                        tls: TlsConfig::Disable,
1✔
112
                };
1✔
113

114
                // This should panic or error due to zero max_connections
115
                let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
1✔
116
                        tokio::runtime::Runtime::new()
1✔
117
                                .unwrap()
1✔
118
                                .block_on(async { Database::connect_with_config(&config).await })
1✔
119
                }));
1✔
120
                assert!(result.is_err());
1✔
121
        }
1✔
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc