-
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
8 changed files
with
197 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ generated | |
subgraph.yaml | ||
.bin | ||
.latest.json | ||
.docker | ||
|
||
# Misc | ||
.DS_Store |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,100 @@ | ||
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, PortalRegistry } from "../generated/PortalRegistry/PortalRegistry"; | ||
|
||
const portalRegistryAddress = Address.fromString("506f88a5Ca8D5F001f2909b029738A40042e42a6"); | ||
|
||
describe("handlePortalRegistered()", () => { | ||
const portalAddress = "f75be6f9418710fd516fa82afb3aad07e11a0f1b"; | ||
const ownerAddress = "e75be6f9418710fd516fa82afb3aad07e11a0f1b"; | ||
const ownerName = "Verax"; | ||
const portalName = "portal name"; | ||
const portalDescription = "portal description"; | ||
|
||
beforeAll(() => { | ||
const tupleArray: Array<ethereum.Value> = [ | ||
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), | ||
]; | ||
const tuple = changetype<ethereum.Tuple>(tupleArray); | ||
const tupleValue = ethereum.Value.fromTuple(tuple); | ||
|
||
createMockedFunction( | ||
portalRegistryAddress, | ||
"getPortalByAddress", | ||
"getPortalByAddress(address):((address,address,address[],bool,string,string,string))", | ||
) | ||
.withArgs([ethereum.Value.fromAddress(Address.fromString(portalAddress))]) | ||
.returns([tupleValue]); | ||
|
||
const contract = PortalRegistry.bind(Address.fromString(portalAddress)); | ||
const result = contract.getPortalByAddress(Address.fromString(portalAddress)); | ||
|
||
assert.tupleEquals(tuple, result); | ||
}); | ||
|
||
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.address = portalRegistryAddress; | ||
|
||
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; | ||
} |
Oops, something went wrong.