-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestForFarmers.js
84 lines (70 loc) · 2.81 KB
/
TestForFarmers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("FarmManagement", function () {
let FarmManagement;
let farmManagement;
let owner;
let addr1;
let addr2;
beforeEach(async function () {
// Deploy the contract
const FarmManagementFactory = await ethers.getContractFactory("FarmManagement");
[owner, addr1, addr2] = await ethers.getSigners();
farmManagement = await FarmManagementFactory.deploy();
// Wait for the contract to be mined
await farmManagement.waitForDeployment();
});
describe("Farmer Management", function () {
it("Should add a new farmer", async function () {
await farmManagement.addFarmer("John Doe");
const farmerCount = await farmManagement.farmerCount();
expect(farmerCount).to.equal(1);
const farmer = await farmManagement.farmers(1);
expect(farmer.name).to.equal("John Doe");
});
it("Should emit FarmerAdded event", async function () {
await expect(farmManagement.addFarmer("Jane Doe"))
.to.emit(farmManagement, "FarmerAdded")
.withArgs(1, "Jane Doe");
});
});
describe("Crop Management", function () {
beforeEach(async function () {
await farmManagement.addFarmer("John Doe");
});
it("Should add a new crop and mint NFT", async function () {
const ipfsHash = "QmTest...";
await farmManagement.addCrop(1, "Wheat", 100, ipfsHash);
const crop = await farmManagement.getCrop(1, 1);
expect(crop.cropName).to.equal("Wheat");
expect(crop.quantity).to.equal(100);
expect(crop.ipfsHash).to.equal(ipfsHash);
});
it("Should emit CropAdded and NFTMinted events", async function () {
const ipfsHash = "QmTest...";
await expect(farmManagement.addCrop(1, "Corn", 200, ipfsHash))
.to.emit(farmManagement, "CropAdded")
.withArgs(1, 1, "Corn", 200, 1, ipfsHash)
.and.to.emit(farmManagement, "NFTMinted")
.withArgs(1, owner.address, ipfsHash);
});
it("Should revert when adding crop to non-existent farmer", async function () {
await expect(farmManagement.addCrop(999, "Invalid", 100, "QmTest..."))
.to.be.revertedWith("Invalid farmer ID");
});
});
describe("NFT Functionality", function () {
beforeEach(async function () {
await farmManagement.addFarmer("John Doe");
await farmManagement.addCrop(1, "Wheat", 100, "QmTest...");
});
it("Should transfer NFT ownership", async function () {
await farmManagement.transferFrom(owner.address, addr1.address, 1);
expect(await farmManagement.ownerOf(1)).to.equal(addr1.address);
});
it("Should return correct owner of crop NFT", async function () {
const nftOwner = await farmManagement.ownerOfCropNFT(1, 1);
expect(nftOwner).to.equal(owner.address);
});
});
});