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

Check for required fields #358

Merged
merged 4 commits into from
Jul 13, 2022
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
412 changes: 231 additions & 181 deletions mocks/as/gravity-tests.txt

Large diffs are not rendered by default.

100 changes: 80 additions & 20 deletions mocks/as/token-lock-wallet-tests.txt
Original file line number Diff line number Diff line change
@@ -1,45 +1,105 @@
import { assert, test, newMockEvent, dataSourceMock } from "matchstick-as/assembly/index"
import { BigInt, DataSourceContext, Value } from "@graphprotocol/graph-ts"
import { assert, test, newMockEvent, dataSourceMock, describe, beforeAll, afterAll, logStore, clearStore } from "matchstick-as/assembly/index"
import { Address, BigInt, Bytes, DataSourceContext, store, Value } from "@graphprotocol/graph-ts"

import { handleApproveTokenDestinations } from "../../src/token-lock-wallet"
import { ApproveTokenDestinations } from "../../generated/templates/GraphTokenLockWallet/GraphTokenLockWallet"
import { NameSignalTransaction, TokenLockWallet, GraphAccount } from "../../generated/schema"
import { NameSignalTransaction, TokenLockWallet, GraphAccount, Subgraph } from "../../generated/schema"
import { mockGraphAccount, mockNameSignalTransaction } from "./utils"

test("Data source simple mocking example", () => {
describe("dataSourceMock", () => {
beforeAll(() => {
let addressString = "0xA16081F360e3847006dB660bae1c6d1b2e17eC2A"
let wallet = new TokenLockWallet(addressString)
let address = Address.fromString(addressString)

let wallet = new TokenLockWallet(address.toHexString())
// The following values should be set, because they are required fields,
// and since graph-cli 0.30.0 codegen does not generate default values for required fields anymore
wallet.manager = Address.zero()
wallet.initHash = Address.zero() as Bytes
wallet.beneficiary = Address.zero()
wallet.tokenDestinationsApproved = false
wallet.token = Bytes.fromHexString("0xc944e90c64b2c07662a292be6244bdf05cda44a7") // GRT
wallet.managedAmount = BigInt.fromI32(0)
wallet.startTime = BigInt.fromI32(0)
wallet.endTime = BigInt.fromI32(0)
wallet.periods = BigInt.fromI32(0)
wallet.releaseStartTime = BigInt.fromI32(0)
wallet.vestingCliffTime = BigInt.fromI32(0)
wallet.tokensReleased = BigInt.fromI32(0)
wallet.tokensWithdrawn = BigInt.fromI32(0)
wallet.tokensRevoked = BigInt.fromI32(0)
wallet.blockNumberCreated = BigInt.fromI32(0)
wallet.txHash = Address.zero() as Bytes

wallet.save()

let context = new DataSourceContext()
context.set("contextVal", Value.fromI32(325))

dataSourceMock.setReturnValues(addressString, "rinkeby", context)
})

afterAll(() => {
dataSourceMock.resetValues()
})

test("Simple dataSource mocking example", () => {
let addressString = "0xA16081F360e3847006dB660bae1c6d1b2e17eC2A"
let address = Address.fromString(addressString)

let event = changetype<ApproveTokenDestinations>(newMockEvent())
let wallet = TokenLockWallet.load(address.toHexString())!

assert.assertNull(wallet.tokenDestinationsApproved)

assert.assertTrue(!wallet.tokenDestinationsApproved)

handleApproveTokenDestinations(event)

wallet = TokenLockWallet.load(addressString)!
wallet = TokenLockWallet.load(address.toHexString())!

assert.assertTrue(wallet.tokenDestinationsApproved)
assert.bigIntEquals(wallet.tokensReleased, BigInt.fromI32(325))

dataSourceMock.resetValues()
})
})

test("Derived fields example test", () => {
let mainAccount = new GraphAccount("12")
mainAccount.save()
let operatedAccount = new GraphAccount("1")
operatedAccount.operators = ["12"]
operatedAccount.save()
let nst = new NameSignalTransaction("1234")
nst.signer = "12";
nst.save()
describe("@derivedFrom fields", () => {
beforeAll(() => {
mockGraphAccount("12")
mockGraphAccount("1")
})

afterAll(() => {
clearStore()
})

test("Derived fields example test", () => {
let mainAccount = GraphAccount.load("12")!

assert.assertNull(mainAccount.get("nameSignalTransactions"))
assert.assertNull(mainAccount.get("operatorOf"))

let operatedAccount = GraphAccount.load("1")!
operatedAccount.operators = [mainAccount.id]
operatedAccount.save()

mockNameSignalTransaction("1234", mainAccount.id)
mockNameSignalTransaction("2", mainAccount.id)

mainAccount = GraphAccount.load("12")!

assert.i32Equals(1, mainAccount.nameSignalTransactions.length)
assert.assertNotNull(mainAccount.get("nameSignalTransactions"))
assert.assertNotNull(mainAccount.get("operatorOf"))
assert.i32Equals(2, mainAccount.nameSignalTransactions.length)
assert.stringEquals("1", mainAccount.operatorOf[0])

mockNameSignalTransaction("2345", mainAccount.id)

let nst = NameSignalTransaction.load("1234")!
nst.signer = "11"
nst.save()

store.remove("NameSignalTransaction", "2")

mainAccount = GraphAccount.load("12")!
assert.i32Equals(1, mainAccount.nameSignalTransactions.length)
})
})
3 changes: 2 additions & 1 deletion mocks/ipfs/cat.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"id": "1",
"imageUrl": "https://i.ytimg.com/vi/MELP46s8Cic/maxresdefault.jpg"
"imageUrl": "https://example.com/image1.png",
"owner": "0x0000000000000000000000000000000000000001"
}
12 changes: 9 additions & 3 deletions mocks/ipfs/map.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
[
{
"id": "1"
"id": "1",
"imageUrl": "https://example.com/image1.jpg",
"owner": "0x0000000000000000000000000000000000000001"
},
{
"id": "2"
"id": "2",
"imageUrl": "https://example.com/image2.jpg",
"owner": "0x0000000000000000000000000000000000000002"
},
{
"id": "3"
"id": "3",
"imageUrl": "https://example.com/image3.jpg",
"owner": "0x0000000000000000000000000000000000000003"
}
]
Loading