-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add unit test to the subgraph
- Loading branch information
Showing
5 changed files
with
156 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { afterEach, assert, beforeAll, clearStore, createMockedFunction, describe, test } from "matchstick-as"; | ||
import { Address, ethereum } from "@graphprotocol/graph-ts"; | ||
import { newTypedMockEvent } from "matchstick-as/assembly/defaults"; | ||
import { handlePortalRegistered } from "../src/portal-registry"; | ||
import { PortalRegistered as PortalRegisteredEvent } from "../generated/PortalRegistry/PortalRegistry"; | ||
|
||
describe("handlePortalRegistered()", () => { | ||
const portalAddress = "f75be6f9418710fd516fa82afb3aad07e11a0f1b"; | ||
const ownerAddress = "e75be6f9418710fd516fa82afb3aad07e11a0f1b"; | ||
const ownerName = "Verax"; | ||
const portalName = "portal name"; | ||
const portalDescription = "portal description"; | ||
|
||
beforeAll(() => { | ||
const contractAddress = Address.fromString("0xf75be6f9418710fd516fa82afb3aad07e11a0f1b"); | ||
createMockedFunction(contractAddress, "getPortalByAddress", "getPortalByAddress(address):(struct Portal)") | ||
.withArgs([ethereum.Value.fromAddress(Address.fromString(portalAddress))]) | ||
.returns([ | ||
ethereum.Value.fromTuple([ | ||
ethereum.Value.fromAddress(Address.fromString(portalAddress)), | ||
ethereum.Value.fromAddress(Address.fromString(ownerAddress)), | ||
ethereum.Value.fromAddressArray([Address.zero()]), | ||
ethereum.Value.fromBoolean(true), | ||
ethereum.Value.fromString(portalName), | ||
ethereum.Value.fromString(portalDescription), | ||
ethereum.Value.fromString(ownerName), | ||
] as ethereum.Tuple), | ||
]); | ||
}); | ||
|
||
afterEach(() => { | ||
clearStore(); | ||
}); | ||
|
||
test("Should create a new Portal entity", () => { | ||
assert.entityCount("Portal", 0); | ||
|
||
const portalRegisteredEvent = createPortalRegisteredEvent(portalAddress, portalName, portalDescription); | ||
|
||
handlePortalRegistered(portalRegisteredEvent); | ||
|
||
assert.entityCount("Portal", 1); | ||
|
||
assert.fieldEquals("Portal", "0x" + portalAddress, "id", "0x" + portalAddress); | ||
assert.fieldEquals("Portal", "0x" + portalAddress, "name", portalName); | ||
assert.fieldEquals("Portal", "0x" + portalAddress, "description", portalDescription); | ||
}); | ||
|
||
test("Should increment the portals Counter", () => { | ||
assert.entityCount("Portal", 0); | ||
|
||
const portalAddress1 = "f75be6f9418710fd516fa82afb3aad07e11a0f1b"; | ||
const portalAddress2 = "e75be6f9418710fd516fa82afb3aad07e11a0f1b"; | ||
|
||
const portalRegisteredEvent1 = createPortalRegisteredEvent(portalAddress1, portalName, portalDescription); | ||
|
||
handlePortalRegistered(portalRegisteredEvent1); | ||
|
||
assert.entityCount("Portal", 1); | ||
assert.fieldEquals("Counter", "counter", "portals", "1"); | ||
|
||
const portalRegisteredEvent2 = createPortalRegisteredEvent(portalAddress2, portalName, portalDescription); | ||
|
||
handlePortalRegistered(portalRegisteredEvent2); | ||
|
||
assert.entityCount("Portal", 2); | ||
assert.fieldEquals("Counter", "counter", "portals", "2"); | ||
}); | ||
}); | ||
|
||
function createPortalRegisteredEvent( | ||
portalAddress: string, | ||
portalName: string, | ||
portalDescription: string, | ||
): PortalRegisteredEvent { | ||
const portalRegisteredEvent = newTypedMockEvent<PortalRegisteredEvent>(); | ||
|
||
portalRegisteredEvent.parameters.push(new ethereum.EventParam("name", ethereum.Value.fromString(portalName))); | ||
portalRegisteredEvent.parameters.push( | ||
new ethereum.EventParam("description", ethereum.Value.fromString(portalDescription)), | ||
); | ||
portalRegisteredEvent.parameters.push( | ||
new ethereum.EventParam("portalAddress", ethereum.Value.fromAddress(Address.fromString(portalAddress))), | ||
); | ||
|
||
return portalRegisteredEvent; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters