Skip to content

Commit

Permalink
implement test and update function
Browse files Browse the repository at this point in the history
  • Loading branch information
Collins C Augustine committed Dec 1, 2024
1 parent feebdf6 commit 2bd0ba2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
42 changes: 9 additions & 33 deletions land_registry/src/land_register.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -603,45 +603,21 @@ pub mod LandRegistryContract {
history
}


// fn get_inspector_pending_approvals(
// self: @ContractState, inspector: ContractAddress
// ) -> Array<Land> {
// // Verify inspector is registered
// assert(self.registered_inspectors.read(inspector), Errors::NOT_REGISTERED_INSP);

// let mut pending_lands = array![];
// let total_lands = self.land_count.read();
// let mut i: u256 = 1; // Starting from 1 since we store in lands_registry from index 1

// // Iterate through all lands
// while i < total_lands + 1 {
// let land = self.lands_registry.read(i);

// // Check if land is assigned to this inspector and is pending
// if land.inspector == inspector && land.status == LandStatus::Pending {
// pending_lands.append(land);
// }

// i += 1;
// };

// pending_lands
// }

fn get_inspector_pending_approvals(
self: @ContractState, inspector: ContractAddress, start_time: u64, end_time: u64
) -> Array<Land> {
// Verify inspector is registered
assert(self.registered_inspectors.read(inspector), Errors::NOT_REGISTERED_INSP);

let mut pending_lands = array![];
let total_lands = self.land_count.read();
let mut i: u256 = 1; // Starting from 1 since we store in lands_registry from index 1
let land_count = self.land_count.read();

let mut current_land_id: u256 = 0;
loop {
if current_land_id >= land_count {
break;
}

// Iterate through all lands
while i < total_lands + 1 {
let land = self.lands_registry.read(i);
let land = self.lands.read(current_land_id);

if land.inspector == inspector
&& land.status == LandStatus::Pending
Expand All @@ -650,7 +626,7 @@ pub mod LandRegistryContract {
pending_lands.append(land);
}

i += 1;
current_land_id += 1;
};

pending_lands
Expand Down
34 changes: 33 additions & 1 deletion land_registry/tests/test_land_register.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -1250,4 +1250,36 @@ fn test_set_land_inspector() {
"Inspector address should match the assigned address",
);
stop_cheat_caller_address(contract_address);
}
}

#[test]
#[should_panic(expected: ('Inspector not registered',))]
fn test_get_inspector_pending_approvals_unauthorized() {
let contract_address = deploy("LandRegistryContract");
let land_register_dispatcher = ILandRegistryDispatcher { contract_address };

let unauthorized = starknet::contract_address_const::<0x789>();
start_cheat_caller_address(contract_address, unauthorized);
land_register_dispatcher.get_inspector_pending_approvals(unauthorized, 1, 2);
stop_cheat_caller_address(contract_address);
}

#[test]
fn test_get_inspector_pending_approvals_empty_range() {
let contract_address = deploy("LandRegistryContract");
let land_register_dispatcher = ILandRegistryDispatcher { contract_address };

let owner_address = starknet::contract_address_const::<0x123>();
let inspector_address = starknet::contract_address_const::<0x456>();

// Register inspector
start_cheat_caller_address(contract_address, owner_address);
land_register_dispatcher.add_inspector(inspector_address);
stop_cheat_caller_address(contract_address);

// Check empty time range
start_cheat_caller_address(contract_address, inspector_address);
let pending = land_register_dispatcher.get_inspector_pending_approvals(inspector_address, 2, 1);
assert(pending.len() == 0, 'Should have no approvals');
stop_cheat_caller_address(contract_address);
}

0 comments on commit 2bd0ba2

Please sign in to comment.