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

feat: Improve test coverage #1477

Merged
merged 3 commits into from
Apr 24, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/land/test/common/ERC721.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,37 @@ export function shouldCheckForERC721(setupLand, Contract: string) {
.to.emit(LandAsOther, 'Transfer')
.withArgs(other, ZeroAddress, 0);
});

it('should approve operator with approvalForAll to burn quad', async function () {
const {
LandContract,
LandAsAdmin,
LandAsMinter,
LandAsOther,
LandAsOther1,
deployer,
other,
other1,
} = await loadFixture(setupLand);

await LandAsMinter.mintQuad(other, 1, 0, 0, '0x');
await LandAsAdmin.setSuperOperator(deployer, true);
await LandContract.setApprovalForAllFor(other, other1, true);

await expect(LandAsOther1.burnFrom(other, 0))
.to.emit(LandAsOther, 'Transfer')
.withArgs(other, ZeroAddress, 0);
});

it('should revert burning tokens by unauthorized operator', async function () {
const {LandAsMinter, other, LandAsOther1} =
await loadFixture(setupLand);

await LandAsMinter.mintQuad(other, 1, 0, 0, '0x');
await expect(LandAsOther1.burnFrom(other, 0)).to.be.revertedWith(
'UNAUTHORIZED_BURN',
);
});
});

describe('batchTransfer', function () {
Expand Down
51 changes: 46 additions & 5 deletions packages/land/test/common/TransferQuad.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ export function shouldCheckTransferQuad(setupLand, Contract: string) {
);
});

it('should revert transfer quad when to is a contract and not a ERC721 receiver', async function () {
const {LandAsOther, TestERC721TokenReceiver, LandAsMinter, other} =
await loadFixture(setupLand);
await TestERC721TokenReceiver.returnWrongBytes();
await LandAsMinter.mintQuad(other, 6, 0, 0, '0x');
await expect(
LandAsOther.transferQuad(other, TestERC721TokenReceiver, 6, 0, 0, '0x'),
).to.be.revertedWith('erc721 batchTransfer rejected');
});

it('should transfer quads to a contract supporting ERC721 mandatory receiver', async function () {
const {LandAsMinter, LandAsOther, other, TestERC721TokenReceiver} =
await loadFixture(setupLand);
Expand Down Expand Up @@ -825,11 +835,13 @@ export function shouldCheckTransferQuad(setupLand, Contract: string) {
).to.be.revertedWith('Invalid x coordinate');
});

it('should revert when x coordinate is out of bounds', async function () {
const {LandAsMinter, deployer} = await loadFixture(setupLand);
await expect(
LandAsMinter.mintAndTransferQuad(deployer, 3, 441, 441, '0x'),
).to.be.revertedWith('x out of bounds');
it('should mint and transfer quad when a part of quad is already minted', async function () {
const {LandContract, LandAsMinter, landMinter, other} =
await loadFixture(setupLand);
await LandAsMinter.mintQuad(landMinter, 1, 0, 0, '0x');
expect(await LandContract.balanceOf(other)).to.be.equal(0);
await LandAsMinter.mintAndTransferQuad(other, 3, 0, 0, '0x');
expect(await LandContract.balanceOf(other)).to.be.equal(3 * 3);
});

it('should transfer an already minted quad using mintAndTransferQuad', async function () {
Expand Down Expand Up @@ -929,6 +941,35 @@ export function shouldCheckTransferQuad(setupLand, Contract: string) {
).to.be.revertedWith('erc721 batchTransfer rejected');
});

it('should transfer quads to a contract supporting ERC721 receiver', async function () {
const {LandAsMinter, LandAsOther, other, TestERC721TokenReceiver} =
await loadFixture(setupLand);

const quadSize = 6;
await LandAsMinter.mintQuad(other, quadSize, 0, 0, '0x');

expect(await LandAsOther.balanceOf(other)).to.be.equal(
quadSize * quadSize,
);
expect(await LandAsOther.balanceOf(TestERC721TokenReceiver)).to.be.equal(
0,
);

await LandAsOther.batchTransferQuad(
other,
TestERC721TokenReceiver,
[quadSize],
[0],
[0],
'0x',
);

expect(await LandAsOther.balanceOf(other)).to.be.equal(0);
expect(await LandAsOther.balanceOf(TestERC721TokenReceiver)).to.be.equal(
quadSize * quadSize,
);
});

it('should reverts transfers batch of quads when to is zero address', async function () {
const {LandContract, LandAsMinter, deployer} =
await loadFixture(setupLand);
Expand Down
49 changes: 49 additions & 0 deletions packages/land/test/common/WithMetadataRegistry.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ export function shouldCheckForMetadataRegistry(setupLand, Contract: string) {
);
});

it('should return metadata for Land when MetadataRegistry is not set', async function () {
const tokenId = 23n + 97n * 408n;
const neighborhoodId = 0n;
const neighborhoodName = 'unknown';
const {LandContractWithoutMetadataRegistry} =
await loadFixture(setupLand);

expect(
await LandContractWithoutMetadataRegistry.getMetadata(tokenId),
).to.deep.equal([false, neighborhoodId, neighborhoodName]);
});

it('should return metadata', async function () {
const tokenId = 23n + 97n * 408n;
const neighborhoodId = 37n;
Expand All @@ -59,5 +71,42 @@ export function shouldCheckForMetadataRegistry(setupLand, Contract: string) {
neighborhoodName,
]);
});

it('should return false for isPremium when metadataRegistry is not set for a land contract', async function () {
const tokenId = 23n + 97n * 408n;
const {LandContractWithoutMetadataRegistry} =
await loadFixture(setupLand);
expect(
await LandContractWithoutMetadataRegistry.isPremium(tokenId),
).to.be.equal(false);
});

it('should check if a token isPremium or not', async function () {
const tokenId = 23n + 97n * 408n;
const {LandContract, MetadataRegistryAsAdmin} =
await loadFixture(setupLand);
await MetadataRegistryAsAdmin.setPremium(tokenId, true);
expect(await LandContract.isPremium(tokenId)).to.be.equal(true);
});

it('should return 0 for getNeighborhoodId when metadataRegistry is not set for a land contract', async function () {
const tokenId = 23n + 97n * 408n;
const {LandContractWithoutMetadataRegistry} =
await loadFixture(setupLand);
expect(
await LandContractWithoutMetadataRegistry.getNeighborhoodId(tokenId),
).to.be.equal(0);
});

it('should return neighborhoodId for a token', async function () {
const tokenId = 23n + 97n * 408n;
const neighborhoodId = 37n;
const {LandContract, MetadataRegistryAsAdmin} =
await loadFixture(setupLand);
await MetadataRegistryAsAdmin.setNeighborhoodId(tokenId, neighborhoodId);
expect(await LandContract.getNeighborhoodId(tokenId)).to.be.equal(
neighborhoodId,
);
});
});
}
13 changes: 13 additions & 0 deletions packages/land/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export async function setupLandContract() {

await LandContract.initialize(landAdmin);

const LandContractWithoutMetadataRegistry = await LandFactory.deploy();
await LandContractWithoutMetadataRegistry.initialize(landAdmin);

// deploy mocks
const TestERC721TokenReceiverFactory = await ethers.getContractFactory(
'ERC721TokenReceiverMock',
Expand Down Expand Up @@ -133,6 +136,7 @@ export async function setupLandContract() {
LandAsOther,
LandAsOther1,
LandAsOther2,
LandContractWithoutMetadataRegistry,
MetadataRegistryContract,
MetadataRegistryContract2,
MetadataRegistryAsAdmin,
Expand Down Expand Up @@ -295,6 +299,14 @@ export async function setupPolygonLandContract() {
},
);

const LandContractWithoutMetadataRegistry = await upgrades.deployProxy(
PolygonLandFactory,
[await landAdmin.getAddress()],
{
initializer: 'initialize',
},
);

// mock contract deploy
const TestERC721TokenReceiverFactory = await ethers.getContractFactory(
'ERC721TokenReceiverMock',
Expand Down Expand Up @@ -350,6 +362,7 @@ export async function setupPolygonLandContract() {
LandAsOther,
LandAsOther1,
LandAsOther2,
LandContractWithoutMetadataRegistry,
MetadataRegistryContract,
MetadataRegistryContract2,
MetadataRegistryAsAdmin,
Expand Down