Skip to content

Commit

Permalink
adding tests to contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
luiz-lvj committed Oct 6, 2024
1 parent 3796f7a commit 29029ae
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/contracts/voting/contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ garaga = { path = "../../.." }
starknet = "2.8.2"

[dev-dependencies]
snforge_std = "0.30.0"
snforge_std = "0.31.0"


[cairo]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::starknet::ContractAddress;

#[derive(Drop, Serde, starknet::Store)]
struct Voting {
pub struct Voting {
id: u64,
question: ByteArray,
reveal_date: u64, // seconds unix epoch time
Expand All @@ -23,6 +23,8 @@ pub trait IVotingContract<TContractState> {
fn get_voting_results(self: @TContractState, voting_id: u64) -> Array<u64>;
fn get_voting_choices(self: @TContractState, voting_id: u64) -> Array<ByteArray>;
fn get_voting_winner_choice_id(self: @TContractState, voting_id: u64) -> u64;
fn get_voting(self: @TContractState, voting_id: u64) -> Voting;
fn get_number_voters(self: @TContractState, voting_id: u64) -> u64;
}


Expand Down Expand Up @@ -125,6 +127,8 @@ pub mod VotingContract {
choices: Array<ByteArray>
) -> u64 {
let id: u64 = self.votings_count.read() + 1;

self.votings_count.write(id);
let creator: ContractAddress = get_caller_address();

self
Expand Down Expand Up @@ -290,5 +294,13 @@ pub mod VotingContract {

self.voting_to_choice_id_winner.entry(voting_id).read()
}

fn get_voting(self: @ContractState, voting_id: u64) -> Voting {
self.votings.entry(voting_id).read()
}

fn get_number_voters(self: @ContractState, voting_id: u64) -> u64 {
self.voting_to_voters_addresses.entry(voting_id).len()
}
}
}
55 changes: 45 additions & 10 deletions src/contracts/voting/contracts/src/tests/test_voting.cairo
Original file line number Diff line number Diff line change
@@ -1,23 +1,58 @@
use core::starknet::{ContractAddress};
use voting::contracts;

use snforge_std::{
EventSpyAssertionsTrait, spy_events, declare, DeclareResultTrait, ContractClassTrait,
start_cheat_caller_address, stop_cheat_caller_address, test_address,
start_cheat_block_timestamp_global
use voting::contracts::voting_contract::{
Voting, IVotingContractDispatcher, IVotingContractDispatcherTrait
};

fn setup_voting_contract() -> ContractAddress {
use core::starknet::{get_block_timestamp};

use snforge_std::{declare, DeclareResultTrait, ContractClassTrait};

fn setup_voting_contract() -> IVotingContractDispatcher {
let voting_contract_class = declare("VotingContract").unwrap().contract_class();

let mut calldata = array![];

let (voting_contract_address, _) = voting_contract_class.deploy(@calldata).unwrap();

voting_contract_address
IVotingContractDispatcher { contract_address: voting_contract_address }
}

#[test]
fn test_create_voting() {
let voting_contract_address = setup_voting_contract();
let voting_contract = setup_voting_contract();

let question = "What is the best programming language?";
let reveal_date = get_block_timestamp() + 1000;
let choices = array!["Cairo", "Typescript", "Rust"];

let id = voting_contract.create_voting(question, reveal_date, choices);

let choices: Array<ByteArray> = voting_contract.get_voting_choices(id);

let all_votings: Array<Voting> = voting_contract.get_all_votings();

assert(all_votings.len() == 1, 'Voting not created');
assert(choices.len() == 3, 'Choices not created');
}

#[test]
fn test_vote() {
let voting_contract = setup_voting_contract();

let question = "What is the best programming language?";
let reveal_date = get_block_timestamp() + 1000;
let choices = array!["Cairo", "Typescript", "Rust"];

let id = voting_contract.create_voting(question, reveal_date, choices);

let choice_index = 1;

voting_contract.vote(id, choice_index);

let all_votings: Array<Voting> = voting_contract.get_all_votings();

assert(all_votings.len() == 1, 'Voting not created');

let number_voters = voting_contract.get_number_voters(id);

assert(number_voters == 1, 'Number of voters is not 1');
}

0 comments on commit 29029ae

Please sign in to comment.