Skip to content

Commit

Permalink
Added test on getDeltaReport
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineludeau committed Dec 15, 2023
1 parent f4e5d02 commit 05aab92
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 10 deletions.
42 changes: 36 additions & 6 deletions lib/api/address/__mocks__/address-data-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ export const addressMock = [
coordinates: [1.23, 2.34]
}
}],
updateDate: new Date('2023-04-12')
updateDate: new Date('2023-04-12'),
meta: {
idfix: {
hash: 'hash1'
}
}
},
{
id: '00000000-0000-4fff-9fff-00000000000b',
Expand All @@ -28,7 +33,12 @@ export const addressMock = [
coordinates: [1.24, 2.34]
}
}],
updateDate: new Date('2023-04-12')
updateDate: new Date('2023-04-12'),
meta: {
idfix: {
hash: 'hash2'
}
}
},
{
id: '00000000-0000-4fff-9fff-00000000000c',
Expand All @@ -43,7 +53,12 @@ export const addressMock = [
coordinates: [1.25, 2.34]
}
}],
updateDate: new Date('2023-04-12')
updateDate: new Date('2023-04-12'),
meta: {
idfix: {
hash: 'hash3'
}
}
}
]

Expand Down Expand Up @@ -84,7 +99,12 @@ export const bddAddressMock = [
coordinates: [1.25, 2.34]
}
}],
updateDate: new Date('2023-04-12')
updateDate: new Date('2023-04-12'),
meta: {
idfix: {
hash: 'hash4'
}
}
},
{
id: '00000000-0000-4fff-9fff-00000000002b',
Expand All @@ -99,7 +119,12 @@ export const bddAddressMock = [
coordinates: [1.25, 2.34]
}
}],
updateDate: new Date('2023-04-12')
updateDate: new Date('2023-04-12'),
meta: {
idfix: {
hash: 'hash5'
}
}
},
{
id: '00000000-0000-4fff-9fff-00000000002c',
Expand All @@ -114,6 +139,11 @@ export const bddAddressMock = [
coordinates: [1.25, 2.34]
}
}],
updateDate: new Date('2023-04-12')
updateDate: new Date('2023-04-12'),
meta: {
idfix: {
hash: 'hash6'
}
}
}
]
2 changes: 1 addition & 1 deletion lib/api/address/__mocks__/address-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export async function getAddresses(addressIDs) {
}

export async function getAllAddressIDsWithHashFromDistrict(districtID) {
return bddAddressMock.filter(({districtID: districtIDAddress}) => districtIDAddress === districtID).map(({id}) => id)
return bddAddressMock.filter(({districtID: districtIDAddress}) => districtIDAddress === districtID).map(({id, meta}) => ({id, hash: meta?.idfix?.hash}))
}

export async function getAllAddressIDsOutsideDistrict(addressIDs, districtID) {
Expand Down
39 changes: 38 additions & 1 deletion lib/api/address/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {addressMock, addressMockForPatch, bddAddressMock} from './__mocks__/addr
jest.unstable_mockModule('./models.js', async () => import('./__mocks__/address-models.js'))
jest.unstable_mockModule('../common-toponym/models.js', async () => import('../common-toponym/__mocks__/common-toponym-models.js'))
jest.unstable_mockModule('../district/models.js', async () => import('../district/__mocks__/district-models.js'))
const {checkAddressesIDsRequest, checkAddressesRequest} = await import('./utils.js')
const {checkAddressesIDsRequest, checkAddressesRequest, getDeltaReport} = await import('./utils.js')

const addressesValidationSchema = object({
isValid: bool().required(),
Expand Down Expand Up @@ -165,3 +165,40 @@ describe('checkAddressesRequest', () => {
expect(addressesValidation?.isValid).toBe(true)
})
})

describe('getDeltaReport', () => {
it('Should return all ids to create and all ids from bdd to delete', async () => {
const districtID = '00000000-0000-4fff-9fff-000000000000'
const data = addressMock.map(({id, meta}) => ({id, hash: meta?.idfix?.hash}))
const result = await getDeltaReport(data, districtID)
expect(result).toEqual({
idsToCreate: addressMock.map(({id}) => id),
idsToUpdate: [],
idsToDelete: bddAddressMock.map(({id}) => id),
idsUnauthorized: []
})
})
it('Should not return anything to update as hashes are equals', async () => {
const districtID = '00000000-0000-4fff-9fff-000000000000'
const data = bddAddressMock.map(({id, meta}) => ({id, hash: meta?.idfix?.hash}))
const result = await getDeltaReport(data, districtID)
expect(result).toEqual({
idsToCreate: [],
idsToUpdate: [],
idsToDelete: [],
idsUnauthorized: []
})
})
it('Should return only one id to update as hashes are different', async () => {
const districtID = '00000000-0000-4fff-9fff-000000000000'
const data = bddAddressMock.map(({id, meta}) => ({id, hash: meta?.idfix?.hash}))
data[0].hash = 'new-hash'
const result = await getDeltaReport(data, districtID)
expect(result).toEqual({
idsToCreate: [],
idsToUpdate: [bddAddressMock[0].id],
idsToDelete: [],
idsUnauthorized: []
})
})
})
2 changes: 1 addition & 1 deletion lib/api/common-toponym/__mocks__/common-toponym-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export async function getCommonToponyms(commonToponymIDs) {
}

export async function getAllCommonToponymIDsWithHashFromDistrict(districtID) {
return bddCommonToponymMock.filter(({districtID: districtIDCommonToponym}) => districtIDCommonToponym === districtID).map(({id}) => id)
return bddCommonToponymMock.filter(({districtID: districtIDCommonToponym}) => districtIDCommonToponym === districtID).map(({id, meta}) => ({id, hash: meta?.idfix?.hash}))
}

export async function getAllCommonToponymIDsOutsideDistrict(commonToponymIDs, districtID) {
Expand Down
39 changes: 38 additions & 1 deletion lib/api/common-toponym/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {commonToponymMock, commonToponymMockForPatch, bddCommonToponymMock} from

jest.unstable_mockModule('./models.js', async () => import('./__mocks__/common-toponym-models.js'))
jest.unstable_mockModule('../district/models.js', async () => import('../district/__mocks__/district-models.js'))
const {checkCommonToponymsRequest, checkCommonToponymsIDsRequest} = await import('./utils.js')
const {checkCommonToponymsRequest, checkCommonToponymsIDsRequest, getDeltaReport} = await import('./utils.js')

const commonToponymsValidationSchema = object({
isValid: bool().required(),
Expand Down Expand Up @@ -140,3 +140,40 @@ describe('checkCommonToponymsRequest', () => {
expect(commonToponymsValidation?.isValid).toBe(true)
})
})

describe('getDeltaReport', () => {
it('Should return all ids to create and all ids from bdd to delete', async () => {
const districtID = '00000000-0000-4fff-9fff-000000000000'
const data = commonToponymMock.map(({id, meta}) => ({id, hash: meta?.idfix?.hash}))
const result = await getDeltaReport(data, districtID)
expect(result).toEqual({
idsToCreate: commonToponymMock.map(({id}) => id),
idsToUpdate: [],
idsToDelete: bddCommonToponymMock.map(({id}) => id),
idsUnauthorized: []
})
})
it('Should not return anything to update as hashes are equals', async () => {
const districtID = '00000000-0000-4fff-9fff-000000000000'
const data = bddCommonToponymMock.map(({id, meta}) => ({id, hash: meta?.idfix?.hash}))
const result = await getDeltaReport(data, districtID)
expect(result).toEqual({
idsToCreate: [],
idsToUpdate: [],
idsToDelete: [],
idsUnauthorized: []
})
})
it('Should return only one id to update as hashes are different', async () => {
const districtID = '00000000-0000-4fff-9fff-000000000000'
const data = bddCommonToponymMock.map(({id, meta}) => ({id, hash: meta?.idfix?.hash}))
data[0].hash = 'new-hash'
const result = await getDeltaReport(data, districtID)
expect(result).toEqual({
idsToCreate: [],
idsToUpdate: [bddCommonToponymMock[0].id],
idsToDelete: [],
idsUnauthorized: []
})
})
})

0 comments on commit 05aab92

Please sign in to comment.