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

getdozer / dozer / 4020902227

pending completion
4020902227

Pull #743

github

GitHub
Merge 57279c6b6 into a12da35a5
Pull Request #743: Chore clippy fix

165 of 165 new or added lines in 60 files covered. (100.0%)

23638 of 35485 relevant lines covered (66.61%)

38417.79 hits per line

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

93.62
/dozer-admin/src/tests/util_sqlite_setup.rs
1
use crate::db::connection::DbConnection;
2
use diesel::{r2d2::ConnectionManager, RunQueryDsl, SqliteConnection};
3
use diesel_migrations::{EmbeddedMigrations, MigrationHarness};
4
use dozer_types::models::connection::DBType;
5
use r2d2::{CustomizeConnection, Pool};
6
use std::error::Error;
7
type DB = diesel::sqlite::Sqlite;
8

9
#[derive(Debug, Clone)]
×
10
pub struct TestConfigId {
11
    pub app_id: String,
12
    pub connection_ids: Vec<String>,
13
    pub source_ids: Vec<String>,
14
    pub api_ids: Vec<String>,
15
}
16
#[derive(Debug)]
×
17
struct TestConnectionCustomizer;
18

19
impl<E> CustomizeConnection<SqliteConnection, E> for TestConnectionCustomizer {
20
    fn on_acquire(&self, conn: &mut SqliteConnection) -> Result<(), E> {
170✔
21
        let setup_ids = get_setup_ids();
170✔
22
        prepare_test_db(conn, setup_ids);
170✔
23
        Ok(())
170✔
24
    }
170✔
25
    fn on_release(&self, conn: SqliteConnection) {
×
26
        std::mem::drop(conn);
×
27
    }
×
28
}
29
pub type DbPool = Pool<ConnectionManager<SqliteConnection>>;
30
pub fn establish_test_connection(database_url: String) -> DbPool {
17✔
31
    let manager = ConnectionManager::<SqliteConnection>::new(database_url);
17✔
32
    r2d2::Pool::builder()
17✔
33
        .max_size(10)
17✔
34
        .connection_customizer(Box::new(TestConnectionCustomizer))
17✔
35
        .build(manager)
17✔
36
        .expect("Failed to create DB pool.")
17✔
37
}
17✔
38

39
fn prepare_test_db(connection: &mut SqliteConnection, config_id: TestConfigId) {
170✔
40
    run_migrations(connection).unwrap();
170✔
41
    setup_data(connection, config_id)
170✔
42
}
170✔
43
fn get_db_type_supported() -> Vec<DBType> {
170✔
44
    vec![
170✔
45
        DBType::Postgres,
170✔
46
        DBType::Ethereum,
170✔
47
        DBType::Events,
170✔
48
        DBType::Snowflake,
170✔
49
        DBType::Kafka,
170✔
50
    ]
170✔
51
}
170✔
52
pub fn get_setup_ids() -> TestConfigId {
186✔
53
    let connection_ids: Vec<String> = vec![
186✔
54
        "9cd38b34-3100-4b61-99fb-ca3626b90f59".to_owned(),
186✔
55
        "2afd6d1f-f739-4f02-9683-b469011936a4".to_owned(),
186✔
56
        "dc5d0a89-7b7a-4ab1-88a0-f23ec5c73482".to_owned(),
186✔
57
        "67df73b7-a322-4ff7-86b4-d7a5b12416d9".to_owned(),
186✔
58
        "7a82ead6-bfd2-4336-805c-a7058dfac3a6".to_owned(),
186✔
59
    ];
186✔
60

186✔
61
    let source_ids: Vec<String> = vec![
186✔
62
        "ebec89f4-80c7-4519-99d3-94cf55669c2b".to_owned(),
186✔
63
        "0ea2cb76-1103-476d-935c-fe5f745bad53".to_owned(),
186✔
64
        "28732cb6-7a68-4e34-99f4-99e356daa06d".to_owned(),
186✔
65
        "bce87d76-93dc-42af-bffa-d47743f4c7fa".to_owned(),
186✔
66
        "d0356a18-77f5-479f-a690-536d086707d8".to_owned(),
186✔
67
    ];
186✔
68
    TestConfigId {
186✔
69
        app_id: "a04376da-3af3-4051-a725-ed0073b3b598".to_owned(),
186✔
70
        connection_ids,
186✔
71
        source_ids,
186✔
72
        api_ids: vec!["de3052fc-affb-46f8-b8c1-0ac69ee91a4f".to_owned()],
186✔
73
    }
186✔
74
}
186✔
75
pub fn database_url_for_test_env() -> String {
17✔
76
    String::from(":memory:")
17✔
77
}
17✔
78

79
fn fake_dbconnection(db_type: DBType) -> DbConnection {
850✔
80
    match db_type {
850✔
81
        DBType::Postgres => DbConnection {
170✔
82
            auth: r#"{"Postgres":{"user":"users","password":"postgres","host":"localhost","port":5432,"database":"postgres"}}"#.to_owned(),
170✔
83
            name: "postgres_connection".to_owned(),
170✔
84
            db_type: "postgres".to_owned(),
170✔
85
            ..Default::default()
170✔
86
        },
170✔
87
        DBType::Ethereum => DbConnection {
170✔
88
            auth: r#"{"Ethereum":{"filter":{"from_block":0,"addresses":[],"topics":[]},"wss_url":"wss:link","name":"eth_logs", "contracts": []}}"#.to_owned(),
170✔
89
            name: "eth_connection".to_owned(),
170✔
90
            db_type: "ethereum".to_owned(),
170✔
91
            ..Default::default()
170✔
92
        },
170✔
93
        DBType::Events => DbConnection {
170✔
94
            auth: r#"{"Events":{"database":"users"}}"#.to_owned(),
170✔
95
            name: "events_connection".to_owned(),
170✔
96
            db_type: "events".to_owned(),
170✔
97
            ..Default::default()
170✔
98
        },
170✔
99
        DBType::Snowflake => DbConnection {
170✔
100
            auth: r#"{"Snowflake":{"server":"tx06321.eu-north-1.aws.snowflakecomputing.com","port":"443","user":"karolisgud","password":"uQ@8S4856G9SHP6","database":"DOZER_SNOWFLAKE_SAMPLE_DATA","schema":"PUBLIC","warehouse":"TEST","driver":"{/opt/snowflake/snowflakeodbc/lib/universal/libSnowflake.dylib}"}}"#.to_owned(),
170✔
101
            name: "snowflake_connection".to_owned(),
170✔
102
            db_type: "snowflake".to_owned(),
170✔
103
            ..Default::default()
170✔
104
        },
170✔
105
        DBType::Kafka => DbConnection {
170✔
106
            auth: r#"{"Kafka":{"broker":"localhost:9092","topic":"dbserver1.public.products"}}"#.to_owned(),
170✔
107
            name: "kafka_debezium_connection".to_owned(),
170✔
108
            db_type: "kafka".to_owned(),
170✔
109
            ..Default::default()
170✔
110
        }
170✔
111
    }
112
}
850✔
113

114
fn setup_data(connection: &mut SqliteConnection, config_id: TestConfigId) {
170✔
115
    // let generated_app_id = uuid::Uuid::new_v4().to_string();
170✔
116
    // create app
170✔
117
    insert_apps(connection, config_id.app_id.to_owned());
170✔
118

170✔
119
    //let generated_postgres_connection_id = uuid::Uuid::new_v4().to_string();
170✔
120
    let db_types = get_db_type_supported();
170✔
121
    db_types.iter().enumerate().for_each(|(idx, db_type)| {
850✔
122
        let db_connection = fake_dbconnection(db_type.to_owned());
850✔
123
        if idx < config_id.connection_ids.len() {
850✔
124
            insert_connections(
850✔
125
                connection,
850✔
126
                config_id.connection_ids[idx].to_owned(),
850✔
127
                config_id.app_id.to_owned(),
850✔
128
                db_connection.db_type,
850✔
129
                db_connection.auth,
850✔
130
                db_connection.name,
850✔
131
            );
850✔
132
            insert_sources(
850✔
133
                connection,
850✔
134
                config_id.source_ids[idx].to_owned(),
850✔
135
                config_id.app_id.to_owned(),
850✔
136
                format!("source_{:}", db_type.to_owned()),
850✔
137
                format!("table_source_{:}", db_type.to_owned()),
850✔
138
                config_id.connection_ids[idx].to_owned(),
850✔
139
                "id".to_owned(),
850✔
140
            )
850✔
141
        }
×
142
    });
850✔
143

170✔
144
    //let generated_endpoint_id = uuid::Uuid::new_v4().to_string();
170✔
145
    insert_endpoints(
170✔
146
        connection,
170✔
147
        config_id.api_ids[0].to_owned(),
170✔
148
        config_id.app_id.to_owned(),
170✔
149
        "users".to_owned(),
170✔
150
        "/users".to_owned(),
170✔
151
        "select id, email, phone from users where 1=1;".to_owned(),
170✔
152
        "id".to_owned(),
170✔
153
    );
170✔
154
}
170✔
155

156
fn insert_apps(connection: &mut SqliteConnection, app_id: String) {
170✔
157
    diesel::sql_query(format!("INSERT INTO apps (id, name, created_at, updated_at) VALUES('{app_id}', \'app_name\', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);"))
170✔
158
        .execute(connection)
170✔
159
        .unwrap();
170✔
160
}
170✔
161

162
fn insert_connections(
850✔
163
    connection: &mut SqliteConnection,
850✔
164
    connection_id: String,
850✔
165
    app_id: String,
850✔
166
    db_type: String,
850✔
167
    auth: String,
850✔
168
    name: String,
850✔
169
) {
850✔
170
    diesel::sql_query(
850✔
171
        format!("INSERT INTO connections (id, app_id, auth, name, db_type, created_at, updated_at) VALUES('{connection_id}', '{app_id}', '{auth}', '{name}', '{db_type}', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);"))
850✔
172
    .execute(connection)
850✔
173
    .unwrap();
850✔
174
}
850✔
175

×
176
fn insert_sources(
850✔
177
    connection: &mut SqliteConnection,
850✔
178
    source_id: String,
850✔
179
    app_id: String,
850✔
180
    name: String,
850✔
181
    table_name: String,
850✔
182
    connection_id: String,
850✔
183
    columns: String,
850✔
184
) {
850✔
185
    diesel::sql_query(
850✔
186
        format!("INSERT INTO sources (id, app_id, name, table_name, connection_id,columns, created_at, updated_at) VALUES('{source_id}', '{app_id}', '{name}', '{table_name}', '{connection_id}','{columns}', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);"))
850✔
187
    .execute(connection)
850✔
188
    .unwrap();
850✔
189
}
850✔
190

×
191
fn insert_endpoints(
170✔
192
    connection: &mut SqliteConnection,
170✔
193
    endpoint_id: String,
170✔
194
    app_id: String,
170✔
195
    name: String,
170✔
196
    path: String,
170✔
197
    sql: String,
170✔
198
    primary_keys: String,
170✔
199
) {
170✔
200
    diesel::sql_query(
170✔
201
        format!("INSERT INTO endpoints (id, app_id, name, \"path\", \"sql\", primary_keys, created_at, updated_at) VALUES( '{endpoint_id}', '{app_id}', '{name}', '{path}','{sql}','{primary_keys}', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);"
170✔
202
        ))
170✔
203
        .execute(connection)
170✔
204
        .unwrap();
170✔
205
}
170✔
206

×
207
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
×
208

209
fn run_migrations(
210
    connection: &mut impl MigrationHarness<DB>,
211
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
212
    connection.revert_all_migrations(MIGRATIONS)?;
170✔
213
    connection.run_pending_migrations(MIGRATIONS)?;
170✔
214
    Ok(())
170✔
215
}
170✔
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