From 2b2c63613a7876d75c548ec7acf1b527923d4f77 Mon Sep 17 00:00:00 2001 From: Max Summe Date: Tue, 19 Nov 2024 14:14:03 -0800 Subject: [PATCH] Add tests --- rs/nns/governance/src/neuron/types/tests.rs | 90 +++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/rs/nns/governance/src/neuron/types/tests.rs b/rs/nns/governance/src/neuron/types/tests.rs index 3678f07600a..be721dd618d 100644 --- a/rs/nns/governance/src/neuron/types/tests.rs +++ b/rs/nns/governance/src/neuron/types/tests.rs @@ -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); +}