Skip to content

Commit

Permalink
fix: fix user relationship test
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterPtato committed Mar 28, 2024
1 parent b67f27e commit 30abaf3
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 41 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/bolt-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ jobs:
run: nix-shell --pure --run "bolt init --yes ci"

- name: Bolt Test
run: nix-shell --pure --run "bolt test -c 4"

- name: Tmate
if: failure()
uses: mxschmitt/action-tmate@v3
run: nix-shell --pure --run "bolt test -c 4 -t 120"

- name: Force Parallel Failure
if: failure()
Expand Down
1 change: 1 addition & 0 deletions svc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions svc/pkg/team/ops/member-relationship-get/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ rivet-operation = { path = "../../../../../lib/operation/core" }
chirp-client = { path = "../../../../../lib/chirp/client" }
prost = "0.10"

[dependencies.sqlx]
version = "0.7"
default-features = false

[dev-dependencies]
chirp-worker = { path = "../../../../../lib/chirp/worker" }

Expand Down
28 changes: 17 additions & 11 deletions svc/pkg/team/ops/member-relationship-get/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use proto::backend::pkg::*;
use rivet_operation::prelude::*;

#[derive(sqlx::FromRow)]
struct Relationship {
this_user_id: Uuid,
other_user_id: Uuid,
shared_team_ids: Vec<Uuid>,
}

#[operation(name = "team-member-relationship-get")]
async fn handle(
ctx: OperationContext<team::member_relationship_get::Request>,
Expand All @@ -19,7 +26,7 @@ async fn handle(

// Query relationships
let relationships = sql_fetch_all!(
[ctx, (Uuid, Uuid, Vec<Uuid>,)]
[ctx, Relationship]
"
SELECT
(q->>0)::UUID AS this_user_id,
Expand All @@ -29,7 +36,7 @@ async fn handle(
FROM db_team.team_members AS this_tm
INNER JOIN db_team.team_members AS other_tm ON this_tm.team_id = other_tm.team_id
WHERE this_tm.user_id = (q->>0)::UUID AND other_tm.user_id = (q->>1)::UUID
) AS mutual_team_ids
) AS shared_team_ids
FROM jsonb_array_elements($1::JSONB) AS q
",
serde_json::to_string(&query_users)?,
Expand All @@ -38,15 +45,14 @@ async fn handle(

let users = relationships
.into_iter()
.map(|(this_user_id, other_user_id, team_ids)| {
team::member_relationship_get::response::User {
this_user_id: Some(this_user_id.into()),
other_user_id: Some(other_user_id.into()),
shared_team_ids: team_ids
.into_iter()
.map(Into::<common::Uuid>::into)
.collect(),
}
.map(|x| team::member_relationship_get::response::User {
this_user_id: Some(x.this_user_id.into()),
other_user_id: Some(x.other_user_id.into()),
shared_team_ids: x
.shared_team_ids
.into_iter()
.map(Into::<common::Uuid>::into)
.collect(),
})
.collect();

Expand Down
8 changes: 7 additions & 1 deletion svc/pkg/user-follow/ops/relationship-get/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use rivet_operation::prelude::*;

#[derive(sqlx::FromRow)]
struct Relationship {
this_user_id: Uuid,
other_user_id: Uuid,
is_follower: bool,
is_following: bool,
}
Expand All @@ -27,7 +29,9 @@ async fn handle(
let relationships = sql_fetch_all!(
[ctx, Relationship]
"
SELECT
SELECT
(q->>0)::UUID AS this_user_id,
(q->>1)::UUID AS other_user_id,
exists(
SELECT 1
FROM db_user_follow.user_follows AS uf
Expand All @@ -51,6 +55,8 @@ async fn handle(
let users = relationships
.iter()
.map(|x| user_follow::relationship_get::response::User {
this_user_id: Some(x.this_user_id.into()),
other_user_id: Some(x.other_user_id.into()),
is_mutual: x.is_follower && x.is_following,
is_follower: x.is_follower,
is_following: x.is_following,
Expand Down
50 changes: 26 additions & 24 deletions svc/pkg/user-follow/ops/relationship-get/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,30 @@ async fn basic(ctx: TestCtx) {
.await
.unwrap();

res.users
.iter()
.zip(tests.iter())
.for_each(|(relationship, &(this_user, other_user))| {
assert_eq!(
follows
.iter()
.any(|x| x.0 == this_user && x.1 == other_user),
relationship.is_follower,
"bad follower"
);
assert_eq!(
follows
.iter()
.any(|x| x.1 == this_user && x.0 == other_user),
relationship.is_following,
"bad following"
);
assert_eq!(
relationship.is_follower && relationship.is_following,
relationship.is_mutual,
"bad mutual"
);
});
tracing::info!(?res, ?tests);

res.users.iter().for_each(|relationship| {
let this_user = relationship.this_user_id.unwrap().as_uuid();
let other_user = relationship.other_user_id.unwrap().as_uuid();

assert_eq!(
follows
.iter()
.any(|x| x.0 == this_user && x.1 == other_user),
relationship.is_follower,
"bad follower"
);
assert_eq!(
follows
.iter()
.any(|x| x.1 == this_user && x.0 == other_user),
relationship.is_following,
"bad following"
);
assert_eq!(
relationship.is_follower && relationship.is_following,
relationship.is_mutual,
"bad mutual"
);
});
}
3 changes: 3 additions & 0 deletions svc/pkg/user-follow/types/relationship-get.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ message Request {

message Response {
message User {
rivet.common.Uuid this_user_id = 4;
rivet.common.Uuid other_user_id = 5;

// Both users follow each other.
bool is_mutual = 1;

Expand Down

0 comments on commit 30abaf3

Please sign in to comment.