Skip to content

Commit 113d703

Browse files
committed
test: upserting redelegations
1 parent 21591e5 commit 113d703

1 file changed

Lines changed: 50 additions & 42 deletions

File tree

chain/src/repository/pos.rs

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -840,76 +840,84 @@ mod tests {
840840
.expect("Failed to run test");
841841
}
842842

843-
/// Test that the function correctly handles epoch 0 input.
843+
/// Test if upserting redelegations updates the end_epoch correctly
844+
/// when there is a conflict
844845
#[tokio::test]
845-
async fn test_clear_redelegations_with_empty_addresses() {
846+
async fn test_upsert_redelegations_with_conflict() {
846847
let db = TestDb::new();
847848

848849
db.run_test(|conn| {
849-
let validator = Validator::fake();
850-
let redelegations = (0..10)
851-
.map(|_| Redelegation::fake(validator.clone().address))
850+
let fake_validator = Validator::fake();
851+
let fake_redelegations_len = 10;
852+
let mut fake_redelegations: Vec<Redelegation> = (0
853+
..fake_redelegations_len)
854+
.map(|_| Redelegation::fake(fake_validator.clone().address))
852855
.collect();
853856

854-
seed_redelegations(conn, validator, redelegations)?;
855-
clear_redelegations(conn, 0)?;
856-
857-
let queried_redelegations = query_redelegations(conn);
858-
859-
assert_eq!(queried_redelegations.len(), 10);
860-
861-
anyhow::Ok(())
862-
})
863-
.await
864-
.expect("Failed to run test");
865-
}
857+
seed_redelegations(
858+
conn,
859+
fake_validator.clone(),
860+
fake_redelegations.clone(),
861+
)?;
866862

867-
/// Test that the clear_redelegations function does nothing when there are
868-
/// not redelegations in the db.
869-
#[tokio::test]
870-
async fn test_clear_redelegations_with_no_redelegations() {
871-
let db = TestDb::new();
863+
let new_epoch = 123 as Epoch;
864+
fake_redelegations
865+
.iter_mut()
866+
.for_each(|r| r.end_epoch = new_epoch);
872867

873-
db.run_test(|conn| {
874-
clear_redelegations(conn, 99999)?;
868+
insert_redelegations(conn, fake_redelegations)?;
875869

876870
let queried_redelegations = query_redelegations(conn);
871+
let queried_redelegations_len = queried_redelegations.len();
877872

878-
assert_eq!(queried_redelegations.len(), 0);
873+
assert_eq!(queried_redelegations_len, fake_redelegations_len);
874+
assert_eq!(
875+
queried_redelegations
876+
.into_iter()
877+
.map(|r| r.end_epoch as Epoch)
878+
.collect::<Vec<_>>(),
879+
vec![new_epoch; queried_redelegations_len]
880+
);
879881

880882
anyhow::Ok(())
881883
})
882884
.await
883885
.expect("Failed to run test");
884886
}
885887

886-
/// Test that the clear_redelegations function removes the correct
887-
/// redelegations from the db.
888+
/// Test if upserting redelegations adds new redelegations if there are no conflicts
888889
#[tokio::test]
889-
async fn test_clear_redelegations() {
890+
async fn test_upsert_redelegations_without_conflict() {
890891
let db = TestDb::new();
891892

892893
db.run_test(|conn| {
893-
let validator = Validator::fake();
894-
let redelegations: Vec<Redelegation> = (0..10)
895-
.map(|i| {
896-
let red = Redelegation::fake(validator.clone().address);
897-
Redelegation {
898-
end_epoch: i as Epoch,
899-
..red
900-
}
901-
})
894+
let fake_validator = Validator::fake();
895+
let fake_redelegations_len = 10;
896+
let fake_redelegations: Vec<Redelegation> = (0
897+
..fake_redelegations_len)
898+
.map(|_| Redelegation::fake(fake_validator.clone().address))
902899
.collect();
903900

904-
seed_redelegations(conn, validator.clone(), redelegations.clone())?;
901+
seed_redelegations(
902+
conn,
903+
fake_validator.clone(),
904+
fake_redelegations,
905+
)?;
906+
907+
let new_redelegation =
908+
Redelegation::fake(fake_validator.clone().address);
909+
let new_redelegations = vec![new_redelegation.clone()];
905910

906-
clear_redelegations(conn, 5)?;
911+
insert_redelegations(conn, new_redelegations)?;
907912

908913
let queried_redelegations = query_redelegations(conn);
914+
let queried_redelegations_len = queried_redelegations.len();
909915

910-
// We removed all redelegations with epoch <= 5, so we have 6,7,8,9
911-
// left
912-
assert_eq!(queried_redelegations.len(), 4);
916+
assert_eq!(queried_redelegations_len, fake_redelegations_len + 1);
917+
assert_eq!(
918+
queried_redelegations.last().unwrap().end_epoch,
919+
new_redelegation.end_epoch as i32
920+
);
913921

914922
anyhow::Ok(())
915923
})

0 commit comments

Comments
 (0)