Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Unit Tests for VoucherToppedUpEvent #29

Merged
merged 6 commits into from
Feb 12, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add unit tests for :
- VoucherHub Events :
- `VoucherDebited` (#28)
- `VoucherToppedUp` (#29)

## v1.1.0

Expand Down
27 changes: 27 additions & 0 deletions tests/unit/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
VoucherCreated,
VoucherDebited,
VoucherDrained,
VoucherToppedUp,
VoucherTypeCreated,
VoucherTypeDescriptionUpdated,
VoucherTypeDurationUpdated,
Expand Down Expand Up @@ -288,6 +289,32 @@ export function createVoucherDebitedEvent(
return event;
}

export function createVoucherToppedUpEvent(
voucher: Address,
expiration: BigInt,
value: BigInt,
): VoucherToppedUp {
let mockEvent = newMockEvent();
let event = new VoucherToppedUp(
mockEvent.address,
mockEvent.logIndex,
mockEvent.transactionLogIndex,
mockEvent.logType,
mockEvent.block,
mockEvent.transaction,
mockEvent.parameters,
mockEvent.receipt,
);

event.parameters = EventParamBuilder.init()
.address('voucher', voucher)
.bigInt('expiration', expiration)
.bigInt('value', value)
.build();

return event;
}

/**
* Utility functions to create and save entities.
*/
Expand Down
102 changes: 102 additions & 0 deletions tests/unit/voucherhub/VoucherToppedUpEvent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { Address, BigDecimal, BigInt } from '@graphprotocol/graph-ts';
import { assert, beforeEach, clearStore, describe, test } from 'matchstick-as/assembly/index';
import { getEventId, toNanoRLC } from '../../../src/utils';
import { handleVoucherToppedUp } from '../../../src/voucherHub';
import {
createAndSaveVoucher,
createAndSaveVoucherType,
createVoucherToppedUpEvent,
} from '../utils/utils';

// Shared constants
const VOUCHER_TYPE_ID = '1';
const VOUCHER_DESCRIPTION = 'Test Voucher Type';
const VOUCHER_DURATION = BigInt.fromI32(86400);
const VOUCHER_TYPE_ELIGIBLE_ASSETS: string[] = [];

const VOUCHER_OWNER = '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd';
const VOUCHER_VALUE = BigDecimal.fromString('100.123');
const VOUCHER_BALANCE = BigDecimal.fromString('50.456');
const VOUCHER_EXPIRATION = BigInt.fromI32(999999);
const VOUCHER_ADDRESS = '0x1234567890123456789012345678901234567890';

describe('VoucherToppedUpEvent', () => {
beforeEach(() => {
clearStore();

// Initialize required entities
createAndSaveVoucherType(
VOUCHER_TYPE_ID,
VOUCHER_DESCRIPTION,
VOUCHER_DURATION,
VOUCHER_TYPE_ELIGIBLE_ASSETS,
);
});

test('Should handle topUp correctly', () => {
// --- GIVEN
createAndSaveVoucher(
VOUCHER_ADDRESS,
VOUCHER_TYPE_ID,
VOUCHER_OWNER,
VOUCHER_VALUE,
VOUCHER_BALANCE,
VOUCHER_EXPIRATION,
[],
);
const topUpValue = BigDecimal.fromString('20.3');
const newExpiration = BigInt.fromI32(1234567);

// --- WHEN
const event = createVoucherToppedUpEvent(
Address.fromString(VOUCHER_ADDRESS),
newExpiration,
toNanoRLC(topUpValue),
);
handleVoucherToppedUp(event);

// --- THEN
assert.fieldEquals('Voucher', VOUCHER_ADDRESS, 'value', topUpValue.toString());
assert.fieldEquals(
'Voucher',
VOUCHER_ADDRESS,
'balance',
VOUCHER_BALANCE.plus(topUpValue).toString(),
);
assert.fieldEquals('Voucher', VOUCHER_ADDRESS, 'expiration', newExpiration.toString());
// Verify other fields remain unchanged
assert.fieldEquals('Voucher', VOUCHER_ADDRESS, 'voucherType', VOUCHER_TYPE_ID);
assert.fieldEquals('Voucher', VOUCHER_ADDRESS, 'owner', VOUCHER_OWNER);

// Check that a VoucherTopUp entity was created
const topUpEntityId = getEventId(event);
assert.fieldEquals('VoucherTopUp', topUpEntityId, 'value', topUpValue.toString());
// Verify other fields remain unchanged
assert.fieldEquals('VoucherTopUp', topUpEntityId, 'voucher', VOUCHER_ADDRESS);
getEventId;
assert.fieldEquals(
'VoucherTopUp',
topUpEntityId,
'timestamp',
event.block.timestamp.toString(),
);
});

test('Should handle topUp for non-existent voucher', () => {
// --- GIVEN
const nonExistentVoucherAddress = '0x0000000000000000000000000000000000000001';
assert.entityCount('Voucher', 0);

// --- WHEN
const event = createVoucherToppedUpEvent(
Address.fromString(nonExistentVoucherAddress),
VOUCHER_EXPIRATION,
toNanoRLC(VOUCHER_BALANCE),
);
handleVoucherToppedUp(event);

// --- THEN
assert.entityCount('Voucher', 0);
assert.entityCount('VoucherTopUp', 0);
});
});