Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
max-dfinity committed Nov 19, 2024
1 parent 553a709 commit 2b2c636
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions rs/nns/governance/src/neuron/types/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,3 +631,93 @@ fn test_adjust_voting_power_disabled() {
);
}
}

#[test]
fn test_conversion_from_old_ballot_storage_full() {
let principal_id = PrincipalId::new_user_test_id(42);
let created_timestamp_seconds = 1729791574;

let recent_ballots: Vec<_> = (0..100)
.map(|id| BallotInfo {
proposal_id: Some(ProposalId { id }),
vote: Vote::Yes as i32,
})
.collect();

let mut neuron = NeuronBuilder::new(
NeuronId { id: 42 },
Subaccount::try_from(vec![42u8; 32].as_slice()).unwrap(),
principal_id,
DissolveStateAndAge::NotDissolving {
dissolve_delay_seconds: 12 * ONE_MONTH_SECONDS,
aging_since_timestamp_seconds: created_timestamp_seconds + 42,
},
created_timestamp_seconds, // created
)
.with_recent_ballots(recent_ballots.clone())
.build();
neuron.recent_ballots_next_entry_index = None;

assert_eq!(neuron.recent_ballots, recent_ballots);

neuron.register_recent_ballot(Topic::NetworkEconomics, &ProposalId { id: 100 }, Vote::No);

assert_eq!(neuron.recent_ballots_next_entry_index, Some(1));

let expected_updated_ballots = {
let mut recent_ballots = recent_ballots.clone();
recent_ballots.reverse();
recent_ballots[0] = BallotInfo {
proposal_id: Some(ProposalId { id: 100 }),
vote: Vote::No as i32,
};
recent_ballots
};

assert_eq!(neuron.recent_ballots, expected_updated_ballots);
}

#[test]
fn test_conversion_from_old_ballot_storage_not_full() {
let principal_id = PrincipalId::new_user_test_id(42);
let created_timestamp_seconds = 1729791574;

let recent_ballots: Vec<_> = (0..75)
.map(|id| BallotInfo {
proposal_id: Some(ProposalId { id }),
vote: Vote::Yes as i32,
})
.collect();

let mut neuron = NeuronBuilder::new(
NeuronId { id: 42 },
Subaccount::try_from(vec![42u8; 32].as_slice()).unwrap(),
principal_id,
DissolveStateAndAge::NotDissolving {
dissolve_delay_seconds: 12 * ONE_MONTH_SECONDS,
aging_since_timestamp_seconds: created_timestamp_seconds + 42,
},
created_timestamp_seconds, // created
)
.with_recent_ballots(recent_ballots.clone())
.build();
neuron.recent_ballots_next_entry_index = None;

assert_eq!(neuron.recent_ballots, recent_ballots);

neuron.register_recent_ballot(Topic::NetworkEconomics, &ProposalId { id: 100 }, Vote::No);

assert_eq!(neuron.recent_ballots_next_entry_index, Some(76));

let expected_updated_ballots = {
let mut recent_ballots = recent_ballots.clone();
recent_ballots.reverse();
recent_ballots.push(BallotInfo {
proposal_id: Some(ProposalId { id: 100 }),
vote: Vote::No as i32,
});
recent_ballots
};

assert_eq!(neuron.recent_ballots, expected_updated_ballots);
}

0 comments on commit 2b2c636

Please sign in to comment.