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

polyphony-chat / sonata / 16393346128

19 Jul 2025 10:26PM UTC coverage: 86.57% (-4.5%) from 91.038%
16393346128

push

github

bitfl0wer
chore: add tests, fix tests

43 of 92 branches covered (46.74%)

Branch coverage included in aggregate %.

795 of 876 relevant lines covered (90.75%)

2954.79 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 actor;
13
pub(crate) mod algorithm_identifier;
14
pub(crate) mod api_keys;
15
pub(crate) mod idcert;
16
pub(crate) mod invite;
17
pub(crate) mod issuer;
18
pub(crate) mod keytrials;
19
pub(crate) mod public_key_info;
20
pub(crate) mod serial_number;
21
pub(crate) mod tokens;
22

23
pub(crate) use actor::*;
24
pub(crate) use algorithm_identifier::*;
25
pub(crate) use api_keys::*;
26
pub(crate) use idcert::*;
27
pub(crate) use invite::*;
28
pub(crate) use issuer::*;
29
pub(crate) use keytrials::*;
30
pub(crate) use public_key_info::*;
31
pub(crate) use serial_number::*;
32
pub(crate) use tokens::*;
33

34
#[derive(Debug, Clone)]
35
/// Main Database struct. Wrapper around [PgPool].
36
pub(crate) struct Database {
37
    /// The underlying `sqlx` [PgPool].
38
    pub pool: PgPool,
39
}
40

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

69
    /// Applies the migrations.
70
    pub(super) async fn run_migrations(&self) -> StdResult<()> {
×
71
        sqlx::migrate!().run(&self.pool).await.map_err(|e| e.into())
×
72
    }
×
73
}
74

75
#[cfg(test)]
76
mod tests {
77
    use super::*;
78
    use crate::config::TlsConfig;
79

80
    #[test]
81
    fn test_database_debug() {
1✔
82
        // We can't easily test the actual Database struct without a real connection,
83
        // but we can test that it implements Debug
84
        // This is a compile-time test to ensure Debug is implemented
85
        fn assert_debug<T: std::fmt::Debug>() {}
1✔
86
        assert_debug::<Database>();
1✔
87
    }
1✔
88

89
    #[test]
90
    fn test_database_clone() {
1✔
91
        // This is a compile-time test to ensure Clone is implemented
92
        fn assert_clone<T: Clone>() {}
1✔
93
        assert_clone::<Database>();
1✔
94
    }
1✔
95

96
    #[tokio::test]
97
    async fn test_connect_with_config_invalid() {
1✔
98
        let config = DatabaseConfig {
1✔
99
            max_connections: 1,
1✔
100
            database: "nonexistent".to_owned(),
1✔
101
            username: "invalid".to_owned(),
1✔
102
            password: "invalid".to_owned(),
1✔
103
            port: 5432,
1✔
104
            host: "invalid_host".to_owned(),
1✔
105
            tls: TlsConfig::Disable,
1✔
106
        };
1✔
107

108
        // This should fail to connect
109
        let result = Database::connect_with_config(&config).await;
1✔
110
        assert!(result.is_err());
1✔
111
    }
1✔
112

113
    #[tokio::test]
114
    async fn test_connect_with_config_zero_max_connections() {
1✔
115
        let config = DatabaseConfig {
1✔
116
            max_connections: 0, // Zero connections should cause a panic during pool creation
1✔
117
            database: "test".to_owned(),
1✔
118
            username: "test".to_owned(),
1✔
119
            password: "test".to_owned(),
1✔
120
            port: 5432,
1✔
121
            host: "localhost".to_owned(),
1✔
122
            tls: TlsConfig::Disable,
1✔
123
        };
1✔
124

125
        // This should panic or error due to zero max_connections
126
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
1✔
127
            tokio::runtime::Runtime::new()
1✔
128
                .unwrap()
1✔
129
                .block_on(async { Database::connect_with_config(&config).await })
1✔
130
        }));
1✔
131
        assert!(result.is_err());
1✔
132
    }
1✔
133
}
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

© 2026 Coveralls, Inc