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

polyphony-chat / sonata / 16250718729

13 Jul 2025 03:34PM UTC coverage: 63.362% (-16.7%) from 80.109%
16250718729

push

github

bitfl0wer
feat(squashme): Further work on register endpoint with TODOs

9 of 20 branches covered (45.0%)

Branch coverage included in aggregate %.

0 of 12 new or added lines in 1 file covered. (0.0%)

28 existing lines in 7 files now uncovered.

285 of 444 relevant lines covered (64.19%)

418.69 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::{
6
        PgPool,
7
        postgres::{PgConnectOptions, PgPoolOptions},
8
};
9

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

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

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

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

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

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

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

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

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

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

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

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