Skip to content

Commit

Permalink
create subgraph tests for group handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
levalleux-ludo committed Feb 28, 2024
1 parent cc82d49 commit 6af2e4a
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 40 deletions.
9 changes: 8 additions & 1 deletion packages/subgraph/src/mappings/exchange-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigInt } from "@graphprotocol/graph-ts";
import { BigInt, log } from "@graphprotocol/graph-ts";
import {
BuyerCommitted,
VoucherCanceled,
Expand Down Expand Up @@ -29,6 +29,9 @@ export function handleBuyerCommittedEvent(event: BuyerCommitted): void {
}

const offer = Offer.load(exchangeFromEvent.offerId.toString());
log.debug("exchangeFromEvent.offerId.toString() {}", [
exchangeFromEvent.offerId.toString()
]);

if (offer) {
offer.quantityAvailable = offer.quantityAvailable.minus(BigInt.fromI32(1));
Expand All @@ -39,6 +42,10 @@ export function handleBuyerCommittedEvent(event: BuyerCommitted): void {

exchange.seller = offer.seller;
exchange.disputeResolver = offer.disputeResolver;
} else {
log.warning("Unable to find Offer with id '{}'", [
exchangeFromEvent.offerId.toString()
]);
}

exchange.buyer = exchangeFromEvent.buyerId.toString();
Expand Down
18 changes: 3 additions & 15 deletions packages/subgraph/tests/account-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@ import {
createSellerCreatedEventLegacy,
createSellerUpdateAppliedEvent,
mockCreateProduct,
createSeller
createSeller,
mockOffer
} from "./mocks";
import { getSellerMetadataEntityId } from "../src/entities/metadata/seller";
import {
getSalesChannelDeploymentId,
getSalesChannelId
} from "../src/entities/metadata/seller/salesChannels";
import {
Offer,
ProductV1Product,
SalesChannel,
SellerMetadata
} from "../generated/schema";
import { getMetadataEntityId } from "../src/entities/metadata/utils";
import { saveMetadata } from "../src/entities/metadata/handler";
import { getProductId } from "../src/entities/metadata/product-v1/product";

Expand Down Expand Up @@ -275,22 +274,11 @@ test("add/remove product salesChannels", () => {
assert.assertTrue((salesChannelProductB as SalesChannel).tag == "DCL");

// mirrored values from `tests/metadata/product-v1-full.json`
const metadataUuid = "ecf2a6dc-555b-41b5-aca8-b7e29eebbb30";
const productUuid = "77593bb2-f797-11ec-b939-0242ac120002";
const productVersion = 1;

const metadataHash = "QmPK1s3pNYLi9ERiq3BDxKa4XosgWwFRQUydHUtz4YgpqB";
mockIpfsFile(metadataHash, "tests/metadata/product-v1-full.json");

const offerId = 1;
const offer = new Offer(offerId.toString());
offer.sellerId = BigInt.fromString(sellerId);
offer.quantityAvailable = BigInt.fromI32(1);
offer.metadataUri = metadataHash;
offer.metadataHash = metadataHash;
offer.save();
const offer = mockOffer(offerId.toString(), sellerId);

const metadataId = getMetadataEntityId(offerId.toString());
saveMetadata(offer, BigInt.fromI32(1651574093));
const productId = getProductId(
sellerId,
Expand Down
48 changes: 36 additions & 12 deletions packages/subgraph/tests/exchange-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Exchange } from "./../generated/schema";
import { Exchange, Offer } from "./../generated/schema";
import {
beforeEach,
test,
Expand All @@ -7,7 +7,7 @@ import {
mockIpfsFile
} from "matchstick-as/assembly/index";
import { BigInt } from "@graphprotocol/graph-ts";
import { Offer, BaseMetadataEntity } from "../generated/schema";
import { BaseMetadataEntity } from "../generated/schema";
import {
handleBuyerCommittedEvent,
handleVoucherExtendedEvent
Expand All @@ -21,28 +21,37 @@ beforeEach(() => {
test("handle BuyerCommittedEvent", () => {
const metadataHash = "QmPK1s3pNYLi9ERiq3BDxKa4XosgWwFRQUydHUtz4YgpqB";
mockIpfsFile(metadataHash, "tests/metadata/base.json");

const offerId = 1;
const buyerId = 2;
const exchangeId = 3;
const sellerId = "1";
const offer = new Offer(offerId.toString());
// note: mockOffer() does not work in this test, no idea why
offer.sellerId = BigInt.fromString(sellerId);
offer.quantityAvailable = BigInt.fromI32(1);
offer.numberOfCommits = BigInt.fromI32(0);
offer.metadataUri = metadataHash;
offer.metadataUri = `ipfs://${metadataHash}`;
offer.metadataHash = metadataHash;
offer.save();

assert.fieldEquals("Offer", offerId.toString(), "quantityAvailable", "1");
assert.fieldEquals("Offer", offerId.toString(), "numberOfCommits", "0");

const metadata = new BaseMetadataEntity(offerId.toString() + "-metadata");
metadata.quantityAvailable = offer.quantityAvailable;
metadata.quantityAvailable = (offer as Offer).quantityAvailable;
metadata.numberOfCommits = BigInt.fromI32(0);
metadata.save();

const buyerCommittedEvent = createBuyerCommittedEvent(offerId, 2, 3);
const buyerCommittedEvent = createBuyerCommittedEvent(
offerId,
buyerId,
exchangeId
);

handleBuyerCommittedEvent(buyerCommittedEvent);

assert.fieldEquals("Offer", "1", "quantityAvailable", "0");
assert.fieldEquals("Offer", "1", "numberOfCommits", "1");
assert.fieldEquals("Offer", offerId.toString(), "quantityAvailable", "0");
assert.fieldEquals("Offer", offerId.toString(), "numberOfCommits", "1");
assert.fieldEquals(
"BaseMetadataEntity",
"1-metadata",
Expand All @@ -55,8 +64,13 @@ test("handle BuyerCommittedEvent", () => {
"numberOfCommits",
"1"
);
assert.fieldEquals("Exchange", "3", "id", "3");
assert.fieldEquals("Exchange", "3", "state", "COMMITTED");
assert.fieldEquals(
"Exchange",
exchangeId.toString(),
"id",
exchangeId.toString()
);
assert.fieldEquals("Exchange", exchangeId.toString(), "state", "COMMITTED");
});

test("handle VoucherExtendedEvent", () => {
Expand All @@ -77,6 +91,16 @@ test("handle VoucherExtendedEvent", () => {

handleVoucherExtendedEvent(voucherExtendedEvent);

assert.fieldEquals("Exchange", "3", "id", exchangeId.toString());
assert.fieldEquals("Exchange", "3", "validUntilDate", validUntil.toString());
assert.fieldEquals(
"Exchange",
exchangeId.toString(),
"id",
exchangeId.toString()
);
assert.fieldEquals(
"Exchange",
exchangeId.toString(),
"validUntilDate",
validUntil.toString()
);
});
149 changes: 149 additions & 0 deletions packages/subgraph/tests/group-handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { test, assert } from "matchstick-as/assembly/index";
import { handleGroupCreatedEvent, handleGroupUpdatedEvent } from "../src/mappings/group-handler";
import { createGroupCreatedEvent, createGroupUpdatedEvent, mockOffer } from "./mocks";
import { Offer } from "../generated/schema";

const groupId = 1;
const sellerId = 2;
const offerIds = [3, 4, 5];
const offerIds_2 = [6, 7];
const method = i8(0);
const tokenType = i8(1);
const tokenAddress = "0x0123456789012345678901234567890123456789";
const tokenAddress_2 = "0x0123456789012345678901234567890000000000";
const gating = i8(2);
const minTokenId = 123;
const maxTokenId = 456;
const threshold = 6;
const maxCommits = 7;
const executedBy = "0x0abcdef1234567890abcdef12345678901234567";

test("handle GroupCreated event", () => {
for (let i = 0; i < offerIds.length; i++) {
const offerId = offerIds[i];
mockOffer(offerId.toString(), sellerId.toString());
checkOfferHasNoCondition(offerId.toString());
}
const groupCreatedEvent = createGroupCreatedEvent(
groupId,
sellerId,
offerIds,
method,
tokenType,
tokenAddress,
gating,
minTokenId,
threshold,
maxCommits,
maxTokenId,
executedBy
);
handleGroupCreatedEvent(groupCreatedEvent);
assert.fieldEquals(
"ConditionEntity",
groupId.toString(),
"id",
groupId.toString()
);
for (let i = 0; i < offerIds.length; i++) {
const offerId = offerIds[i];
assert.fieldEquals(
"Offer",
offerId.toString(),
"condition",
groupId.toString()
);
}
});

test("handle GroupUpdated event", () => {
for (let i = 0; i < offerIds.length; i++) {
const offerId = offerIds[i];
mockOffer(offerId.toString(), sellerId.toString());
checkOfferHasNoCondition(offerId.toString());
}
for (let i = 0; i < offerIds_2.length; i++) {
const offerId = offerIds_2[i];
mockOffer(offerId.toString(), sellerId.toString());
checkOfferHasNoCondition(offerId.toString());
}
const groupCreatedEvent = createGroupCreatedEvent(
groupId,
sellerId,
offerIds,
method,
tokenType,
tokenAddress,
gating,
minTokenId,
threshold,
maxCommits,
maxTokenId,
executedBy
);
handleGroupCreatedEvent(groupCreatedEvent);
assert.fieldEquals(
"ConditionEntity",
groupId.toString(),
"id",
groupId.toString()
);
assert.fieldEquals(
"ConditionEntity",
groupId.toString(),
"tokenAddress",
tokenAddress
);
for (let i = 0; i < offerIds.length; i++) {
const offerId = offerIds[i];
assert.fieldEquals(
"Offer",
offerId.toString(),
"condition",
groupId.toString()
);
}
const groupUpdatedEvent = createGroupUpdatedEvent(
groupId,
sellerId,
offerIds_2,
method,
tokenType,
tokenAddress_2,
gating,
minTokenId,
threshold,
maxCommits,
maxTokenId,
executedBy
);
handleGroupUpdatedEvent(groupUpdatedEvent);
assert.fieldEquals(
"ConditionEntity",
groupId.toString(),
"id",
groupId.toString()
);
assert.fieldEquals(
"ConditionEntity",
groupId.toString(),
"tokenAddress",
tokenAddress_2
);
for (let i = 0; i < offerIds_2.length; i++) {
const offerId = offerIds_2[i];
assert.fieldEquals(
"Offer",
offerId.toString(),
"condition",
groupId.toString()
);
}
});

function checkOfferHasNoCondition(offerId: string): void {
const offer = Offer.load(offerId);
assert.assertNotNull(offer);
const condition = (offer as Offer).condition;
assert.assertNull(condition);
}
13 changes: 2 additions & 11 deletions packages/subgraph/tests/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import {
beforeEach,
test,
assert,
mockIpfsFile,
clearStore
} from "matchstick-as/assembly/index";
import { BigInt } from "@graphprotocol/graph-ts";
import { Offer } from "../generated/schema";

import { saveMetadata } from "../src/entities/metadata/handler";
import { getMetadataEntityId } from "../src/entities/metadata/utils";
import { getProductId } from "../src/entities/metadata/product-v1/product";
import { mockOffer } from "./mocks";

beforeEach(() => {
clearStore();
Expand All @@ -22,17 +21,9 @@ test("save PRODUCT_V1 metadata product-v1-full.json", () => {
const productUuid = "77593bb2-f797-11ec-b939-0242ac120002";
const productVersion = 1;

const metadataHash = "QmPK1s3pNYLi9ERiq3BDxKa4XosgWwFRQUydHUtz4YgpqB";
mockIpfsFile(metadataHash, "tests/metadata/product-v1-full.json");

const offerId = 1;
const sellerId = "1";
const offer = new Offer(offerId.toString());
offer.sellerId = BigInt.fromString(sellerId);
offer.quantityAvailable = BigInt.fromI32(1);
offer.metadataUri = metadataHash;
offer.metadataHash = metadataHash;
offer.save();
const offer = mockOffer(offerId.toString(), sellerId);

const metadataId = getMetadataEntityId(offerId.toString());
saveMetadata(offer, BigInt.fromI32(1651574093));
Expand Down
Loading

0 comments on commit 6af2e4a

Please sign in to comment.