Skip to content

Commit

Permalink
test: members_pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed Jul 12, 2024
1 parent 86ac85e commit 6a1bb3c
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cala-ledger-core-types/src/account_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct AccountSetMember {
pub created_at: DateTime<Utc>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "type", content = "id")]
pub enum AccountSetMemberId {
Account(AccountId),
Expand Down
126 changes: 126 additions & 0 deletions cala-ledger/tests/account_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,129 @@ async fn account_set_update() -> anyhow::Result<()> {
assert_eq!(updated_name, account_set.values().name);
Ok(())
}

#[tokio::test]
async fn members_pagination() -> anyhow::Result<()> {
let pool = helpers::init_pool().await?;
let cala_config = CalaLedgerConfig::builder()
.pool(pool)
.exec_migrations(false)
.build()?;
let cala = CalaLedger::init(cala_config).await?;
let new_journal = helpers::test_journal();
let journal = cala.journals().create(new_journal).await.unwrap();

let (one, two) = helpers::test_accounts();
let account_one = cala.accounts().create(one).await.unwrap();
let account_two = cala.accounts().create(two).await.unwrap();

let set_one = NewAccountSet::builder()
.id(AccountSetId::new())
.name("SET ONE")
.journal_id(journal.id())
.build()
.unwrap();
let set_one = cala.account_sets().create(set_one).await.unwrap();
let set_two = NewAccountSet::builder()
.id(AccountSetId::new())
.name("SET TWO")
.journal_id(journal.id())
.build()
.unwrap();
let set_two = cala.account_sets().create(set_two).await.unwrap();

let parent = NewAccountSet::builder()
.id(AccountSetId::new())
.name("parent")
.journal_id(journal.id())
.build()
.unwrap();
let parent = cala.account_sets().create(parent).await.unwrap();

cala.account_sets()
.add_member(parent.id(), account_two.id())
.await
.unwrap();

cala.account_sets()
.add_member(parent.id(), set_one.id())
.await
.unwrap();

cala.account_sets()
.add_member(parent.id(), account_one.id())
.await
.unwrap();

cala.account_sets()
.add_member(parent.id(), set_two.id())
.await
.unwrap();

let query_args = cala_ledger::query::PaginatedQueryArgs {
first: 2,
after: None,
};

let ret = cala
.account_sets()
.list_members(parent.id(), query_args)
.await?;

assert_eq!(ret.entities.len(), 2);
assert_eq!(ret.has_next_page, true);
assert_eq!(
ret.entities[0].id.clone(),
AccountSetMemberId::from(set_two.id())
);
assert_eq!(
ret.entities[1].id.clone(),
AccountSetMemberId::from(account_one.id())
);

let after_cursor = AccountSetMemberCursor {
member_created_at: ret.entities[0].created_at,
};

let query_args = cala_ledger::query::PaginatedQueryArgs {
first: 2,
after: Some(after_cursor),
};

let ret = cala
.account_sets()
.list_members(parent.id(), query_args)
.await?;
assert_eq!(ret.entities.len(), 2);
assert_eq!(ret.has_next_page, true);
assert_eq!(
ret.entities[0].id.clone(),
AccountSetMemberId::from(account_one.id())
);
assert_eq!(
ret.entities[1].id.clone(),
AccountSetMemberId::from(set_one.id())
);

let after_cursor = AccountSetMemberCursor {
member_created_at: ret.entities[1].created_at,
};

let query_args = cala_ledger::query::PaginatedQueryArgs {
first: 2,
after: Some(after_cursor),
};

let ret = cala
.account_sets()
.list_members(parent.id(), query_args)
.await?;
assert_eq!(ret.entities.len(), 1);
assert_eq!(ret.has_next_page, false);
assert_eq!(
ret.entities[0].id.clone(),
AccountSetMemberId::from(account_two.id())
);

Ok(())
}

0 comments on commit 6a1bb3c

Please sign in to comment.