Skip to content

Commit

Permalink
refactor: reduce cost model test code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannis committed Oct 11, 2023
1 parent 5ad15ec commit fe5d889
Showing 1 changed file with 10 additions and 23 deletions.
33 changes: 10 additions & 23 deletions service/src/common/indexer_management/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,14 @@ mod test {
.expect("Create test instance in db");
}

async fn add_cost_models(pool: &PgPool, models: Vec<CostModel>) {
async fn add_cost_models(pool: &PgPool, models: Vec<DbCostModel>) {
for model in models {
let deployment = model.deployment;
sqlx::query!(
r#"
INSERT INTO "CostModels" (deployment, model)
VALUES ($1, $2);
"#,
format!("{deployment:#x}"),
model.deployment,
model.model,
)
.execute(pool)
Expand All @@ -235,20 +234,8 @@ mod test {
}
}

async fn add_db_cost_models(pool: &PgPool, models: Vec<DbCostModel>) {
for model in models {
sqlx::query!(
r#"
INSERT INTO "CostModels" (deployment, model)
VALUES ($1, $2);
"#,
model.deployment,
model.model,
)
.execute(pool)
.await
.expect("Create test instance in db");
}
fn to_db_models(models: Vec<CostModel>) -> Vec<DbCostModel> {
models.into_iter().map(DbCostModel::from).collect()
}

fn global_cost_model() -> DbCostModel {
Expand Down Expand Up @@ -294,7 +281,7 @@ mod test {
.collect::<HashSet<_>>();

setup_cost_models_table(&pool).await;
add_cost_models(&pool, test_models.clone()).await;
add_cost_models(&pool, to_db_models(test_models.clone())).await;

// First test: query without deployment filter
let models = cost_models(&pool, &[])
Expand Down Expand Up @@ -358,8 +345,8 @@ mod test {
let global_model = global_cost_model();

setup_cost_models_table(&pool).await;
add_cost_models(&pool, test_models.clone()).await;
add_db_cost_models(&pool, vec![global_model.clone()]).await;
add_cost_models(&pool, to_db_models(test_models.clone())).await;
add_cost_models(&pool, vec![global_model.clone()]).await;

// First test: fetch cost models without filtering by deployment
let models = cost_models(&pool, &[])
Expand Down Expand Up @@ -443,7 +430,7 @@ mod test {
#[sqlx::test]
async fn success_cost_model(pool: PgPool) {
setup_cost_models_table(&pool).await;
add_cost_models(&pool, test_data()).await;
add_cost_models(&pool, to_db_models(test_data())).await;

let deployment_id_from_bytes = DeploymentId(
"0xbd499f7673ca32ef4a642207a8bebdd0fb03888cf2678b298438e3a1ae5206ea"
Expand All @@ -470,8 +457,8 @@ mod test {
let global_model = global_cost_model();

setup_cost_models_table(&pool).await;
add_cost_models(&pool, test_models.clone()).await;
add_db_cost_models(&pool, vec![global_model.clone()]).await;
add_cost_models(&pool, to_db_models(test_models.clone())).await;
add_cost_models(&pool, vec![global_model.clone()]).await;

// Test that the behavior is correct for existing deployments
for test_model in test_models {
Expand Down

0 comments on commit fe5d889

Please sign in to comment.