Skip to content

Commit

Permalink
new unit tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardovivanco committed May 21, 2024
1 parent 44a09f1 commit ef660d3
Show file tree
Hide file tree
Showing 2 changed files with 280 additions and 0 deletions.
254 changes: 254 additions & 0 deletions test/tableland/DeviceDefinitionTable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,4 +946,258 @@ describe('DeviceDefinitionTable', async function () {
});
});
});

describe('updateDeviceDefinition', () => {
beforeEach(async () => {
const tx = await ddTableInstance
.connect(admin)
.createDeviceDefinitionTable(manufacturer1.address, 1);

await tablelandValidator.pollForReceiptByTransactionHash({
chainId: CURRENT_CHAIN_ID,
transactionHash: (await tx.wait())?.hash as string,
});

const txInsert = await ddTableInstance
.connect(manufacturer1)
.insertDeviceDefinition(1, C.mockDdInput1);

await tablelandValidator.pollForReceiptByTransactionHash({
chainId: CURRENT_CHAIN_ID,
transactionHash: (await txInsert.wait())?.hash as string,
});


});

context('Error handling', () => {
it('Should revert if Device Definition table does not exist', async () => {
await expect(
ddTableInstance
.connect(manufacturer1)
.updateDeviceDefinition(99, C.mockDdInput1)
).to.be.revertedWithCustomError(
ddTableInstance,
'TableDoesNotExist',
).withArgs(99);
});
it('Should revert if caller is not the manufacturer ID owner or has not the MANUFACTURER_UPDATE_DD_PRIVILEGE', async () => {
await expect(
ddTableInstance
.connect(unauthorized)
.updateDeviceDefinition(1, C.mockDdInput1)
).to.be.revertedWithCustomError(
ddTableInstance,
'Unauthorized',
).withArgs(unauthorized.address);
});
});

context('Table owner as caller', () => {
context('State', () => {
it('Should correctly update DD from the table', async () => {
const tx = await ddTableInstance
.connect(manufacturer1)
.updateDeviceDefinition(1, C.mockDdUpdate1);

await tablelandValidator.pollForReceiptByTransactionHash({
chainId: CURRENT_CHAIN_ID,
transactionHash: (await tx.wait())?.hash as string,
});

const count = await tablelandDb.prepare(
`SELECT COUNT(*) AS total FROM ${await ddTableInstance.getDeviceDefinitionTableName(1)}`
).first<{ total: number }>('total');

expect(count).to.deep.equal([1]);

const selectQuery = await tablelandDb.prepare(
`SELECT * FROM ${await ddTableInstance.getDeviceDefinitionTableName(1)} WHERE id = "${C.mockDdId1}"`
).first();

expect(selectQuery).to.deep.include({
id: C.mockDdId1,
model: C.mockDdModel1,
year: C.mockDdYear1,
metadata: C.mockDdMetadataUpdate1,
ksuid: C.mockKsuid1,
deviceType: C.mockDdDeviceType1,
imageURI: C.mockDdImageURI1
});
});
});

context('Events', () => {
it('Should emit DeviceDefinitionUpdated event with correct params', async () => {
await expect(
ddTableInstance
.connect(manufacturer1)
.updateDeviceDefinition(1, C.mockDdUpdate1)
)
.to.emit(ddTableInstance, 'DeviceDefinitionUpdated')
.withArgs(2, C.mockDdId1, C.mockDdModel1, C.mockDdYear1);
});
});
});

context('Privileged address as caller', () => {
context('State', () => {
it('Should correctly update DD from the table', async () => {
const tx = await ddTableInstance
.connect(privilegedUser1)
.updateDeviceDefinition(1, C.mockDdUpdate1);

await tablelandValidator.pollForReceiptByTransactionHash({
chainId: CURRENT_CHAIN_ID,
transactionHash: (await tx.wait())?.hash as string,
});

const count = await tablelandDb.prepare(
`SELECT COUNT(*) AS total FROM ${await ddTableInstance.getDeviceDefinitionTableName(1)}`
).first<{ total: number }>('total');

expect(count).to.deep.equal([1]);

const selectQuery = await tablelandDb.prepare(
`SELECT * FROM ${await ddTableInstance.getDeviceDefinitionTableName(1)} WHERE id = "${C.mockDdId1}"`
).first();

expect(selectQuery).to.deep.include({
id: C.mockDdId1,
model: C.mockDdModel1,
year: C.mockDdYear1,
metadata: C.mockDdMetadataUpdate1,
ksuid: C.mockKsuid1,
deviceType: C.mockDdDeviceType1,
imageURI: C.mockDdImageURI1,
});
});
});

context('Events', () => {
it('Should emit DeviceDefinitionUpdated event with correct params', async () => {
await expect(
ddTableInstance
.connect(privilegedUser1)
.updateDeviceDefinition(1, C.mockDdUpdate1)
)
.to.emit(ddTableInstance, 'DeviceDefinitionUpdated')
.withArgs(2, C.mockDdId1, C.mockDdModel1, C.mockDdYear1);
});
});
});
});

describe('deleteDeviceDefinition', () => {
beforeEach(async () => {
const tx = await ddTableInstance
.connect(admin)
.createDeviceDefinitionTable(manufacturer1.address, 1);

await tablelandValidator.pollForReceiptByTransactionHash({
chainId: CURRENT_CHAIN_ID,
transactionHash: (await tx.wait())?.hash as string,
});

const txInsert = await ddTableInstance
.connect(manufacturer1)
.insertDeviceDefinition(1, C.mockDdInput1);

await tablelandValidator.pollForReceiptByTransactionHash({
chainId: CURRENT_CHAIN_ID,
transactionHash: (await txInsert.wait())?.hash as string,
});


});

context('Error handling', () => {
it('Should revert if Device Definition table does not exist', async () => {
await expect(
ddTableInstance
.connect(manufacturer1)
.deleteDeviceDefinition(99, C.mockDdInput1.id)
).to.be.revertedWithCustomError(
ddTableInstance,
'TableDoesNotExist',
).withArgs(99);
});
it('Should revert if caller is not the manufacturer ID owner or has not the MANUFACTURER_DELETE_DD_PRIVILEGE', async () => {
await expect(
ddTableInstance
.connect(unauthorized)
.deleteDeviceDefinition(1, C.mockDdInput1.id)
).to.be.revertedWithCustomError(
ddTableInstance,
'Unauthorized',
).withArgs(unauthorized.address);
});
});

context('Table owner as caller', () => {
context('State', () => {
it('Should correctly delete DD from the table', async () => {
const tx = await ddTableInstance
.connect(manufacturer1)
.deleteDeviceDefinition(1, C.mockDdInput1.id);

await tablelandValidator.pollForReceiptByTransactionHash({
chainId: CURRENT_CHAIN_ID,
transactionHash: (await tx.wait())?.hash as string,
});

const count = await tablelandDb.prepare(
`SELECT COUNT(*) AS total FROM ${await ddTableInstance.getDeviceDefinitionTableName(1)}`
).first<{ total: number }>('total');

expect(count).to.deep.equal([0]);
});
});

context('Events', () => {
it('Should emit DeviceDefinitionDeleted event with correct params', async () => {
await expect(
ddTableInstance
.connect(manufacturer1)
.deleteDeviceDefinition(1, C.mockDdInput1.id)
)
.to.emit(ddTableInstance, 'DeviceDefinitionDeleted')
.withArgs(2, C.mockDdId1);
});
});
});

context('Privileged address as caller', () => {
context('State', () => {
it('Should correctly delete DD into the table', async () => {
const tx = await ddTableInstance
.connect(privilegedUser1)
.deleteDeviceDefinition(1, C.mockDdInput1.id);

await tablelandValidator.pollForReceiptByTransactionHash({
chainId: CURRENT_CHAIN_ID,
transactionHash: (await tx.wait())?.hash as string,
});

const count = await tablelandDb.prepare(
`SELECT COUNT(*) AS total FROM ${await ddTableInstance.getDeviceDefinitionTableName(1)}`
).first<{ total: number }>('total');

expect(count).to.deep.equal([0]);
});
});

context('Events', () => {
it('Should emit DeviceDefinitionDeleted event with correct params', async () => {
await expect(
ddTableInstance
.connect(privilegedUser1)
.deleteDeviceDefinition(1, C.mockDdInput1.id)
)
.to.emit(ddTableInstance, 'DeviceDefinitionDeleted')
.withArgs(2, C.mockDdId1);
});
});
});
});
});
26 changes: 26 additions & 0 deletions utils/constants/DeviceDefinitionTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ export const mockDdMetadata3 = {
fuel_tank_capacity_gal: '15.800000'
}

export const mockDdMetadataUpdate1 = {
mpg: '21',
mpg_city: '18',
base_msrp: '67796',
fuel_type: 'Gasoline x',
wheelbase: '109 WB x',
generation: '2',
mpg_highway: '26',
vehicle_type: 'PASSENGER CAR x',
driven_wheels: 'RWDx',
number_of_doors: '4',
powertrain_type: 'ICE',
manufacturer_code: '6AE48',
fuel_tank_capacity_gal: '18'
};

export const mockDdInput1: DeviceDefinitionInput = {
id: mockDdId1,
model: mockDdModel1,
Expand Down Expand Up @@ -98,6 +114,16 @@ export const mockDdInput3: DeviceDefinitionInput = {
imageURI: mockDdImageURI3
}

export const mockDdUpdate1: DeviceDefinitionInput = {
id: mockDdId1,
model: mockDdModel1,
year: mockDdYear1,
metadata: JSON.stringify(mockDdMetadataUpdate1),
ksuid: mockKsuid1,
deviceType: mockDdDeviceType1,
imageURI: mockDdImageURI1
}

export const mockDdInputBatch: DeviceDefinitionInput[] = [
mockDdInput1,
mockDdInput2,
Expand Down

0 comments on commit ef660d3

Please sign in to comment.