Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/components/operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ pub mod OperationsComponent {
// Check if stored loan has matching ID and is not in a terminal state
loan.id == loan_id && loan.status != LoanStatus::NOT_INITIALIZED
}

fn can_renegotiate_loan(self: @ComponentState<TContractState>, loan_id: u256) -> bool {
if (!self.is_valid_loan(loan_id)) {
return false;
}

let loan = self.get_loan(loan_id);

loan.status != LoanStatus::FORECLOSED && loan.status != LoanStatus::REPAID
}
}

#[generate_trait]
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/ioperations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ pub trait IOperations<TContractState> {
fn get_loan(self: @TContractState, loan_id: u256) -> Loan;
fn is_active_loan(self: @TContractState, loan_id: u256) -> bool;
fn is_valid_loan(self: @TContractState, loan_id: u256) -> bool;
fn can_renegotiate_loan(self: @TContractState, loan_id: u256) -> bool;
}

66 changes: 65 additions & 1 deletion src/tests/test_operations.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use starknet::{ContractAddress};
use starknet::storage::{StoragePointerWriteAccess, StoragePathEntry};
use trajectfi::components::operations::OperationsComponent;
use trajectfi::components::operations::OperationsComponent::{OperationsImpl};
use trajectfi::types::{Loan, LoanStatus};

Expand Down Expand Up @@ -335,3 +334,68 @@ fn create_test_loan(id: u256, status: LoanStatus) -> Loan {
status,
}
}

#[test]
fn test_can_renegotiate_active_loan() {
// Setup test environment
let mut state: MockOperationsContract::ContractState =
MockOperationsContract::contract_state_for_testing();

// Create an active loan
let loan_id = 1_u256;
let valid_loan = create_test_loan(loan_id, LoanStatus::ONGOING);
state.operations.loans.entry(loan_id).write(valid_loan);
state.operations.loan_exists.entry(loan_id).write(true);

// Verify an active loan can be renegotiated
let result = state.can_renegotiate_loan(loan_id);
assert(result, 'Loan should be renegotiable');
}

#[test]
fn test_can_renegotiate_repaid_loan() {
// Setup test environment
let mut state: MockOperationsContract::ContractState =
MockOperationsContract::contract_state_for_testing();

// Create a repaid loan
let loan_id = 1_u256;
let valid_loan = create_test_loan(loan_id, LoanStatus::REPAID);
state.operations.loans.entry(loan_id).write(valid_loan);
state.operations.loan_exists.entry(loan_id).write(true);

// Verify a repaid loan cannot be renegotiated
let result = state.can_renegotiate_loan(loan_id);
assert(!result, 'Loan should not be renegotiable');
}

#[test]
fn test_can_renegotiate_foreclosed_loan() {
// Setup test environment
let mut state: MockOperationsContract::ContractState =
MockOperationsContract::contract_state_for_testing();

// Create a foreclosed loan
let loan_id = 1_u256;
let valid_loan = create_test_loan(loan_id, LoanStatus::FORECLOSED);
state.operations.loans.entry(loan_id).write(valid_loan);
state.operations.loan_exists.entry(loan_id).write(true);

// Verify a foreclosed loan cannot be renegotiated
let result = state.can_renegotiate_loan(loan_id);
assert(!result, 'Loan should not be renegotiable');
}

#[test]
fn test_can_renegotiate_invalid_id_loan() {
// Setup test environment
let mut state: MockOperationsContract::ContractState =
MockOperationsContract::contract_state_for_testing();

// Use a loan ID that does not exist
let invalid_id = 99_u256;

// Verify an uninitialized loan cannot be renegotiated
let result = state.can_renegotiate_loan(invalid_id);
assert(!result, 'Loan should not be renegotiable');
}