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

polyphony-chat / sonata / 16237829835

12 Jul 2025 12:07PM UTC coverage: 77.37% (-8.7%) from 86.093%
16237829835

push

github

bitfl0wer
fix: failing test, read config from str instead

7 of 16 branches covered (43.75%)

Branch coverage included in aggregate %.

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

8 existing lines in 2 files now uncovered.

246 of 311 relevant lines covered (79.1%)

596.58 hits per line

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

87.5
/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::PgPool;
6
use sqlx::postgres::{PgConnectOptions, PgPoolOptions};
7

8
use crate::StdResult;
9
use crate::config::DatabaseConfig;
10

11
pub(crate) mod api_keys;
12
pub(crate) mod keytrials;
13
pub(crate) mod models;
14
pub(crate) mod serial_number;
15
pub(crate) mod tokens;
16

17
pub(crate) use models::*;
18

19
#[derive(Debug, Clone)]
20
/// Main Database struct. Wrapper around [PgPool].
21
pub(crate) struct Database {
22
    /// The underlying `sqlx` [PgPool].
23
    pub pool: PgPool,
24
}
25

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

53
    /// Applies the migrations.
54
    pub(super) async fn run_migrations(&self) -> StdResult<()> {
×
UNCOV
55
        sqlx::migrate!().run(&self.pool).await.map_err(|e| e.into())
×
UNCOV
56
    }
×
57
}
58

59
#[cfg(test)]
60
mod tests {
61
    use super::*;
62
    use crate::config::TlsConfig;
63

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

73
    #[test]
74
    fn test_database_clone() {
1✔
75
        // This is a compile-time test to ensure Clone is implemented
76
        fn assert_clone<T: Clone>() {}
1✔
77
        assert_clone::<Database>();
1✔
78
    }
1✔
79

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

92
        // This should fail to connect
93
        let result = Database::connect_with_config(&config).await;
1✔
94
        assert!(result.is_err());
1✔
95
    }
1✔
96

97
    // Note: Testing actual database connections and migrations would require
98
    // either a test database or mocking, which is typically done in integration tests
99
}
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