Skip to content

Commit

Permalink
chore: added unit tests and CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Maikol committed Jan 19, 2024
1 parent d444049 commit 5f0600a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 28 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Tests (L1)

on:
pull_request:
types: [opened, reopened, synchronize]

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

# Install commands
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '20'
registry-url: https://registry.npmjs.org
- name: yarn add ts-node
run: yarn add ts-node
- name: yarn install
run: yarn install

# Run scripts
- name: Test
run: ./node_modules/@graphprotocol/graph-cli/bin/run test -v 0.6.0-rc.2
100 changes: 72 additions & 28 deletions tests/subgraph-availability-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,92 @@ import {
beforeAll,
afterAll
} from "matchstick-as/assembly/index"
import { Address, BigInt, Bytes } from "@graphprotocol/graph-ts"
import { NewOwnership } from "../generated/schema"
import { NewOwnership as NewOwnershipEvent } from "../generated/SubgraphAvailabilityManager/SubgraphAvailabilityManager"
import { handleNewOwnership } from "../src/subgraph-availability-manager"
import { createNewOwnershipEvent } from "./subgraph-availability-manager-utils"
import { Address, BigInt } from "@graphprotocol/graph-ts"
import { handleNewOwnership, handleNewPendingOwnership, handleOracleSet, handleOracleVote, handleVoteTimeLimitSet } from "../src/subgraph-availability-manager"
import { createNewOwnershipEvent, createNewPendingOwnershipEvent, createOracleSetEvent, createOracleVoteEvent, createVoteTimeLimitSetEvent } from "./subgraph-availability-manager-utils"

import { logStore } from 'matchstick-as/assembly/store'

// Tests structure (matchstick-as >=0.5.0)
// https://thegraph.com/docs/en/developer/matchstick/#tests-structure-0-5-0

describe("Describe entity assertions", () => {
beforeAll(() => {
let from = Address.fromString("0x0000000000000000000000000000000000000001")
let to = Address.fromString("0x0000000000000000000000000000000000000001")
let newNewOwnershipEvent = createNewOwnershipEvent(from, to)
handleNewOwnership(newNewOwnershipEvent)
})
const defaultID = "0xa16081f360e3847006db660bae1c6d1b2e17ec2a01000000"
const samID = "0x0000000000000000000000000000000000000001"
const samAddress = Address.fromString(samID)
const governorID = "0x0000000000000000000000000000000000000002"
const governorAddress = Address.fromString(governorID)
const pendingGovernorID = "0x0000000000000000000000000000000000000003"
const pendingGovernorAddress = Address.fromString(pendingGovernorID)
const oracleID = "0x0000000000000000000000000000000000000004"
const oracleAddress = Address.fromString(oracleID)
const subgraphDeploymentID = "0x0000000000000000000000000000000000000005"
const subgraphDeploymentAddress = Address.fromString(subgraphDeploymentID)

describe("OWNERSHIP", () => {
afterAll(() => {
clearStore()
})

// For more test scenarios, see:
// https://thegraph.com/docs/en/developer/matchstick/#write-a-unit-test
test("NewPendingOwnership created and stored", () => {
let newPendingOwnershipEvent = createNewPendingOwnershipEvent(governorAddress, pendingGovernorAddress)
handleNewPendingOwnership(newPendingOwnershipEvent)

assert.entityCount("NewPendingOwnership", 1)
assert.fieldEquals("NewPendingOwnership", defaultID, "from", governorID)
assert.fieldEquals("NewPendingOwnership", defaultID, "to", pendingGovernorID)
})

test("NewOwnership created and stored", () => {
let newNewOwnershipEvent = createNewOwnershipEvent(governorAddress, pendingGovernorAddress)
handleNewOwnership(newNewOwnershipEvent)

assert.entityCount("NewOwnership", 1)
assert.fieldEquals("NewOwnership", defaultID, "from", governorID)
assert.fieldEquals("NewOwnership", defaultID, "to", pendingGovernorID)
})
})

// 0xa16081f360e3847006db660bae1c6d1b2e17ec2a is the default address used in newMockEvent() function
assert.fieldEquals(
"NewOwnership",
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1",
"from",
"0x0000000000000000000000000000000000000001"
)
assert.fieldEquals(
"NewOwnership",
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a-1",
"to",
"0x0000000000000000000000000000000000000001"
describe("ORACLE", () => {
afterAll(() => {
clearStore()
})

test("OracleSet created and stored", () => {
let newOracleSetEvent = createOracleSetEvent(BigInt.fromI32(0), oracleAddress)
handleOracleSet(newOracleSetEvent)

assert.entityCount("OracleSet", 1)
assert.fieldEquals("OracleSet", defaultID, "index", "0")
assert.fieldEquals("OracleSet", defaultID, "oracle", oracleID)
})

test("OracleVote created and stored", () => {
let newOracleVoteEvent = createOracleVoteEvent(
subgraphDeploymentAddress,
true,
BigInt.fromI32(0),
BigInt.fromI32(300)
)
handleOracleVote(newOracleVoteEvent)

assert.entityCount("OracleVote", 1)
assert.fieldEquals("OracleVote", defaultID, "subgraphDeploymentID", subgraphDeploymentID)
assert.fieldEquals("OracleVote", defaultID, "deny", "true")
assert.fieldEquals("OracleVote", defaultID, "oracleIndex", "0")
assert.fieldEquals("OracleVote", defaultID, "timestamp", "300")
})
})

describe("VOTE_TIME_LIMIT", () => {
afterAll(() => {
clearStore()
})

test("VoteTimeLimit created and stored", () => {
let newVoteTimeLimitEvent = createVoteTimeLimitSetEvent(BigInt.fromI32(300))
handleVoteTimeLimitSet(newVoteTimeLimitEvent)

// More assert options:
// https://thegraph.com/docs/en/developer/matchstick/#asserts
assert.entityCount("VoteTimeLimitSet", 1)
assert.fieldEquals("VoteTimeLimitSet", defaultID, "voteTimeLimit", "300")
})
})

0 comments on commit 5f0600a

Please sign in to comment.