Skip to content

Commit

Permalink
Merge pull request #50 from EmmanuelAR/feat/donator_test
Browse files Browse the repository at this point in the history
[feat] Add donators contract tests
  • Loading branch information
adrianvrj authored Aug 27, 2024
2 parents 1044bb5 + 66042c7 commit 8a9d095
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 11 deletions.
Empty file.
16 changes: 16 additions & 0 deletions contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Go Stark Me - Backend Setup 💻

### Steps to Run the Backend 🥳

1. **Navigate to the Contracts Directory 🔍**
```bash
cd gostarkme/contracts
2. **Compile gostarkme backend 🛠️**
```bash
scarb build
3. **Run gostarkme unit test**
```bash
snforge test
4. **Run code formatter 📝**
```bash
scarb fmt
10 changes: 8 additions & 2 deletions contracts/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ version = 1
name = "gostarkme"
version = "0.1.0"
dependencies = [
"openzeppelin",
"snforge_std",
]

[[package]]
name = "openzeppelin"
version = "0.10.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.10.0#d77082732daab2690ba50742ea41080eb23299d3"

[[package]]
name = "snforge_std"
version = "0.14.0"
source = "git+https://github.com/foundry-rs/starknet-foundry.git?tag=v0.14.0#e8cbecee4e31ed428c76d5173eaa90c8df796fe3"
version = "0.20.1"
source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.20.1#fea2db8f2b20148cc15ee34b08de12028eb42942"
8 changes: 5 additions & 3 deletions contracts/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ edition = "2023_11"
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[dependencies]
starknet = ">=2.6.4"
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.14.0" }
starknet = "2.6.4"
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag = "v0.10.0" }

[dev-dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.20.1" }

[[target.starknet-contract]]
sierra = true
casm = true
3 changes: 2 additions & 1 deletion contracts/src/donator.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use starknet::ContractAddress;

#[starknet::interface]
trait IDonator<TContractState> {
pub trait IDonator<TContractState> {
fn getOwner(self: @TContractState) -> ContractAddress;
fn getLevel(self: @TContractState) -> u32;
fn getTotalStarkDonations(self: @TContractState) -> u64;
Expand Down Expand Up @@ -61,6 +61,7 @@ mod Donator {
}
fn updateDonatorValues(ref self: ContractState, donated_starks: u64) {
let total_donator_pod = self.total_stark_donations.read() + donated_starks;
self.total_stark_donations.write(total_donator_pod);
if (total_donator_pod > self.max_stark_donations_to_next_level.read()) {
self.level.write(self.level.read() + 1);
self
Expand Down
10 changes: 5 additions & 5 deletions contracts/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod constants;
pub mod constants;
// FUND
mod fund;
mod fundManager;
pub mod fund;
pub mod fundManager;
// DONATOR
mod donatorManager;
mod donator;
pub mod donatorManager;
pub mod donator;
77 changes: 77 additions & 0 deletions contracts/tests/test_donator.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// *************************************************************************
// DONATOR TEST
// *************************************************************************
use starknet::{ContractAddress, contract_address_const};

use snforge_std::{declare, ContractClassTrait};
use openzeppelin::utils::serde::SerializedAppend;

use gostarkme::donator::IDonatorDispatcher;
use gostarkme::donator::IDonatorDispatcherTrait;

fn OWNER() -> ContractAddress {
contract_address_const::<'OWNER'>()
}

fn __setup__() -> ContractAddress {
let contract = declare("Donator");
let mut calldata: Array<felt252> = array![];
calldata.append_serde(OWNER());
contract.deploy(@calldata).unwrap()
}

// *************************************************************************
// TEST
// *************************************************************************
#[test]
fn test_get_owner() {
let contract_address = __setup__();
let dispatcher = IDonatorDispatcher { contract_address };
let owner = dispatcher.getOwner();
assert(owner == OWNER(), 'Invalid owner');
}

#[test]
fn test_get_level() {
let contract_address = __setup__();
let dispatcher = IDonatorDispatcher { contract_address };
let level = dispatcher.getLevel();
assert(level == 1, 'Invalid level');
}

#[test]
fn test_get_total_stark_donations() {
let contract_address = __setup__();
let dispatcher = IDonatorDispatcher { contract_address };
let total_stark_donations = dispatcher.getTotalStarkDonations();
assert(total_stark_donations == 0, 'Invalid total stark donations');
}

#[test]
fn test_get_max_stark_donations_to_next_level() {
let contract_address = __setup__();
let dispatcher = IDonatorDispatcher { contract_address };
let max_stark_donations_to_next_level = dispatcher.getMaxStarkDonationsToNextLevel();
assert(max_stark_donations_to_next_level == 10, 'Invalid total stark donations');
}

#[test]
fn test_update_donator_values() {
let contract_address = __setup__();
let dispatcher = IDonatorDispatcher { contract_address };
dispatcher.updateDonatorValues(5);
let level = dispatcher.getLevel();
let total_stark_donations = dispatcher.getTotalStarkDonations();
assert(level == 1, 'Invalid level');
assert(total_stark_donations == 5, 'Invalid total stark donations');
dispatcher.updateDonatorValues(5);
let level = dispatcher.getLevel();
assert(level == 1, 'Invalid level');
dispatcher.updateDonatorValues(1);
let level = dispatcher.getLevel();
let total_stark_donations = dispatcher.getTotalStarkDonations();
let max_stark_donations_to_next_level = dispatcher.getMaxStarkDonationsToNextLevel();
assert(level == 2, 'Invalid level');
assert(total_stark_donations == 11, 'Invalid total stark donations');
assert(max_stark_donations_to_next_level == 20, 'Invalid total stark donations');
}

0 comments on commit 8a9d095

Please sign in to comment.