From 3b5b48c043fcc005fdecfab840afc43032b1d3d1 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 6 Dec 2022 14:49:36 +0100 Subject: [PATCH] Query project count as i64 instead of i32 when gathering metrics Using the latter will cause a type mismatch when performing the query. --- crates/collab/src/db.rs | 2 +- crates/collab/src/db/tests.rs | 65 +++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/crates/collab/src/db.rs b/crates/collab/src/db.rs index 915acb00eb..1cda33c00c 100644 --- a/crates/collab/src/db.rs +++ b/crates/collab/src/db.rs @@ -1472,7 +1472,7 @@ impl Database { .into_values::<_, QueryAs>() .one(&*tx) .await? - .unwrap_or(0) as usize) + .unwrap_or(0i64) as usize) }) .await } diff --git a/crates/collab/src/db/tests.rs b/crates/collab/src/db/tests.rs index 9e70ae4b05..baa3f87060 100644 --- a/crates/collab/src/db/tests.rs +++ b/crates/collab/src/db/tests.rs @@ -402,6 +402,71 @@ test_both_dbs!(test_metrics_id_postgres, test_metrics_id_sqlite, db, { assert_ne!(metrics_id1, metrics_id2); }); +test_both_dbs!( + test_project_count_postgres, + test_project_count_sqlite, + db, + { + let user1 = db + .create_user( + &format!("admin@example.com"), + true, + NewUserParams { + github_login: "admin".into(), + github_user_id: 0, + invite_count: 0, + }, + ) + .await + .unwrap(); + let user2 = db + .create_user( + &format!("user@example.com"), + false, + NewUserParams { + github_login: "user".into(), + github_user_id: 1, + invite_count: 0, + }, + ) + .await + .unwrap(); + + let room_id = RoomId::from_proto( + db.create_room(user1.user_id, ConnectionId(0), "") + .await + .unwrap() + .id, + ); + db.call(room_id, user1.user_id, ConnectionId(0), user2.user_id, None) + .await + .unwrap(); + db.join_room(room_id, user2.user_id, ConnectionId(1)) + .await + .unwrap(); + assert_eq!(db.project_count_excluding_admins().await.unwrap(), 0); + + db.share_project(room_id, ConnectionId(1), &[]) + .await + .unwrap(); + assert_eq!(db.project_count_excluding_admins().await.unwrap(), 1); + + db.share_project(room_id, ConnectionId(1), &[]) + .await + .unwrap(); + assert_eq!(db.project_count_excluding_admins().await.unwrap(), 2); + + // Projects shared by admins aren't counted. + db.share_project(room_id, ConnectionId(0), &[]) + .await + .unwrap(); + assert_eq!(db.project_count_excluding_admins().await.unwrap(), 2); + + db.leave_room(ConnectionId(1)).await.unwrap(); + assert_eq!(db.project_count_excluding_admins().await.unwrap(), 0); + } +); + #[test] fn test_fuzzy_like_string() { assert_eq!(Database::fuzzy_like_string("abcd"), "%a%b%c%d%");