diff --git a/src/services/__mocks__/enedisDayData.json b/src/services/__mocks__/loadDayData.json similarity index 100% rename from src/services/__mocks__/enedisDayData.json rename to src/services/__mocks__/loadDayData.json diff --git a/src/services/__mocks__/loadHourData.json b/src/services/__mocks__/loadHourData.json new file mode 100644 index 0000000000000000000000000000000000000000..2804f3618144f80ebb4f8b59341e52394de75b44 --- /dev/null +++ b/src/services/__mocks__/loadHourData.json @@ -0,0 +1,62 @@ +[ + { + "id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_type": "com.grandlyon.enedis.hour", + "_rev": "1-4fe971dff073a3c3c6cc12a0246e642e", + "day": 2, + "hour": 0, + "load": 4.5, + "minute": 0, + "month": 11, + "year": 2020 + }, + { + "id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_type": "com.grandlyon.enedis.hour", + "_rev": "1-4fe971dff073a3c3c6cc12a0246e642e", + "day": 2, + "hour": 1, + "load": 1.33, + "minute": 0, + "month": 11, + "year": 2020 + }, + { + "id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_type": "com.grandlyon.enedis.hour", + "_rev": "1-4fe971dff073a3c3c6cc12a0246e642e", + "day": 2, + "hour": 2, + "load": 3.22, + "minute": 0, + "month": 11, + "year": 2020 + }, + { + "id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_type": "com.grandlyon.enedis.hour", + "_rev": "1-4fe971dff073a3c3c6cc12a0246e642e", + "day": 2, + "hour": 3, + "load": 7.82, + "minute": 0, + "month": 11, + "year": 2020 + }, + { + "id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_type": "com.grandlyon.enedis.hour", + "_rev": "1-4fe971dff073a3c3c6cc12a0246e642e", + "day": 2, + "hour": 4, + "load": 1.23, + "minute": 0, + "month": 11, + "year": 2020 + } +] diff --git a/src/services/__mocks__/enedisMinuteData.json b/src/services/__mocks__/loadMinuteData.json similarity index 100% rename from src/services/__mocks__/enedisMinuteData.json rename to src/services/__mocks__/loadMinuteData.json diff --git a/src/services/__mocks__/loadMonthData.json b/src/services/__mocks__/loadMonthData.json new file mode 100644 index 0000000000000000000000000000000000000000..fd9d2458d76eeee6d3cceeb2d7bb03e8f47975cc --- /dev/null +++ b/src/services/__mocks__/loadMonthData.json @@ -0,0 +1,38 @@ +[ + { + "id": "bf1ce3a5774e140056714c4c200c093e", + "_id": "bf1ce3a5774e140056714c4c200c093e", + "_type": "com.grandlyon.enedis.month", + "_rev": "1-4fe971dff073a3c3c6cc12a0246e642e", + "day": 1, + "hour": 0, + "load": 125.25, + "minute": 0, + "month": 9, + "year": 2020 + }, + { + "id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_type": "com.grandlyon.enedis.month", + "_rev": "1-4fe971dff073a3c3c6cc12a0246e642e", + "day": 1, + "hour": 0, + "load": 220.5, + "minute": 0, + "month": 10, + "year": 2020 + }, + { + "id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_type": "com.grandlyon.enedis.month", + "_rev": "1-4fe971dff073a3c3c6cc12a0246e642e", + "day": 1, + "hour": 0, + "load": 130.33, + "minute": 0, + "month": 11, + "year": 2020 + } +] diff --git a/src/services/__mocks__/loadYearData.json b/src/services/__mocks__/loadYearData.json new file mode 100644 index 0000000000000000000000000000000000000000..86180661bc2f561cecf5616b144d7b1caf7826b3 --- /dev/null +++ b/src/services/__mocks__/loadYearData.json @@ -0,0 +1,38 @@ +[ + { + "id": "bf1ce3a5774e140056714c4c200c093e", + "_id": "bf1ce3a5774e140056714c4c200c093e", + "_type": "com.grandlyon.enedis.year", + "_rev": "1-4fe971dff073a3c3c6cc12a0246e642e", + "day": 1, + "hour": 0, + "load": 125.25, + "minute": 0, + "month": 1, + "year": 2018 + }, + { + "id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_type": "com.grandlyon.enedis.year", + "_rev": "1-4fe971dff073a3c3c6cc12a0246e642e", + "day": 0, + "hour": 0, + "load": 220.5, + "minute": 0, + "month": 0, + "year": 2019 + }, + { + "id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_id": "cf7dc6f44a19b354f99b01ba1a0b4840", + "_type": "com.grandlyon.enedis.year", + "_rev": "1-4fe971dff073a3c3c6cc12a0246e642e", + "day": 1, + "hour": 0, + "load": 130.33, + "minute": 0, + "month": 1, + "year": 2020 + } +] diff --git a/src/services/queryRunner.service.spec.ts b/src/services/queryRunner.service.spec.ts index 28cf73a4f43a29a41679fa8c542bde572b73488f..e829f7e30a1c0782460eadc7e07c2c995d8c8b45 100644 --- a/src/services/queryRunner.service.spec.ts +++ b/src/services/queryRunner.service.spec.ts @@ -2,48 +2,604 @@ import { QueryResult } from 'cozy-client' import { FluidType } from 'enum/fluid.enum' import { TimeStep } from 'enum/timeStep.enum' import { DateTime } from 'luxon' -import { DataloadEntity } from 'models' +import { Dataload, DataloadEntity } from 'models' import QueryRunner from './queryRunner.service' import mockClient from './__mocks__/client' -import enedisDayData from './__mocks__/enedisDayData.json' -import enedisMinuteData from './__mocks__/enedisMinuteData.json' +import loadYearData from './__mocks__/loadYearData.json' +import loadMonthData from './__mocks__/loadMonthData.json' +import loadDayData from './__mocks__/loadDayData.json' +import loadHourData from './__mocks__/loadHourData.json' +import loadMinuteData from './__mocks__/loadMinuteData.json' describe('queryRunner service', () => { const queryRunner = new QueryRunner(mockClient) describe('fetchFluidData method', () => { - const mockTimePeriod = { - startDate: DateTime.fromISO('2020-11-01T00:00:00.000+01:00'), - endDate: DateTime.fromISO('2020-11-30T23:59:59.999+01:00'), - } - const expectedResult = [ - { - date: DateTime.fromISO('2020-11-01T00:00:00.000+01:00'), - value: 25.25, - }, - { - date: DateTime.fromISO('2020-11-02T00:00:00.000+01:00'), - value: 20.5, - }, - { - date: DateTime.fromISO('2020-11-03T00:00:00.000+01:00'), - value: 30.33, - }, - { - date: DateTime.fromISO('2020-11-04T00:00:00.000+01:00'), - value: 1.22, - }, - ] + it('should return the data of the elec fluid and year time step', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2018-01-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2018-01-01T00:00:00.000'), + value: 125.25, + }, + { + date: DateTime.fromISO('2019-01-01T00:00:00.000'), + value: 220.5, + }, + { + date: DateTime.fromISO('2020-01-01T00:00:00.000'), + value: 130.33, + }, + ] + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: loadYearData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + TimeStep.YEAR, + FluidType.ELECTRICITY + ) + expect(result).toEqual(expectedResult) + }) + + it('should return the data of the elec fluid and month time step', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-01-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2020-09-01T00:00:00.000'), + value: 125.25, + }, + { + date: DateTime.fromISO('2020-10-01T00:00:00.000'), + value: 220.5, + }, + { + date: DateTime.fromISO('2020-11-01T00:00:00.000'), + value: 130.33, + }, + ] + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: loadMonthData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + TimeStep.MONTH, + FluidType.ELECTRICITY + ) + expect(result).toEqual(expectedResult) + }) - it('should return the data of the selected Fluid', async () => { + it('should return the data of the elec fluid and day time step', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-11-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2020-11-01T00:00:00.000'), + value: 25.25, + }, + { + date: DateTime.fromISO('2020-11-02T00:00:00.000'), + value: 20.5, + }, + { + date: DateTime.fromISO('2020-11-03T00:00:00.000'), + value: 30.33, + }, + { + date: DateTime.fromISO('2020-11-04T00:00:00.000'), + value: 1.22, + }, + ] const mockQueryResult: QueryResult<DataloadEntity[]> = { - data: enedisDayData, + data: loadDayData, bookmark: '', next: false, skip: 0, } mockClient.query.mockResolvedValue(mockQueryResult) - const result = await queryRunner.fetchFluidData( + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + TimeStep.DAY, + FluidType.ELECTRICITY + ) + expect(result).toEqual(expectedResult) + }) + + it('should return the data of the elec fluid and half an hour time step', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-11-02T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-02T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2020-11-02T00:00:00.000'), + value: 4.5, + }, + { + date: DateTime.fromISO('2020-11-02T00:30:00.000'), + value: 1.33, + }, + { + date: DateTime.fromISO('2020-11-02T01:00:00.000'), + value: 3.22, + }, + { + date: DateTime.fromISO('2020-11-02T01:30:00.000'), + value: 7.82, + }, + { + date: DateTime.fromISO('2020-11-02T02:00:00.000'), + value: 1.23, + }, + ] + const filteredData = [...loadMinuteData] + filteredData.filter( + data => data.year === 2020 && data.month === 11 && data.day === 2 + ) + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: filteredData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + TimeStep.HALF_AN_HOUR, + FluidType.ELECTRICITY + ) + expect(result).toEqual(expectedResult) + }) + + it('should return the data of the water fluid and year time step', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2018-01-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2018-01-01T00:00:00.000'), + value: 125.25, + }, + { + date: DateTime.fromISO('2019-01-01T00:00:00.000'), + value: 220.5, + }, + { + date: DateTime.fromISO('2020-01-01T00:00:00.000'), + value: 130.33, + }, + ] + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: loadYearData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + TimeStep.YEAR, + FluidType.WATER + ) + expect(result).toEqual(expectedResult) + }) + + it('should return the data of the water fluid and month time step', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-01-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2020-09-01T00:00:00.000'), + value: 125.25, + }, + { + date: DateTime.fromISO('2020-10-01T00:00:00.000'), + value: 220.5, + }, + { + date: DateTime.fromISO('2020-11-01T00:00:00.000'), + value: 130.33, + }, + ] + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: loadMonthData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + TimeStep.MONTH, + FluidType.WATER + ) + expect(result).toEqual(expectedResult) + }) + + it('should return the data of the water fluid and day time step', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-11-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2020-11-01T00:00:00.000'), + value: 25.25, + }, + { + date: DateTime.fromISO('2020-11-02T00:00:00.000'), + value: 20.5, + }, + { + date: DateTime.fromISO('2020-11-03T00:00:00.000'), + value: 30.33, + }, + { + date: DateTime.fromISO('2020-11-04T00:00:00.000'), + value: 1.22, + }, + ] + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: loadDayData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + TimeStep.DAY, + FluidType.WATER + ) + expect(result).toEqual(expectedResult) + }) + + it('should return the day data of the water fluid and unkown time step', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-11-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2020-11-01T00:00:00.000'), + value: 25.25, + }, + { + date: DateTime.fromISO('2020-11-02T00:00:00.000'), + value: 20.5, + }, + { + date: DateTime.fromISO('2020-11-03T00:00:00.000'), + value: 30.33, + }, + { + date: DateTime.fromISO('2020-11-04T00:00:00.000'), + value: 1.22, + }, + ] + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: loadDayData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + 99, + FluidType.WATER + ) + expect(result).toEqual(expectedResult) + }) + + it('should return the data of the gas fluid and year time step', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2018-01-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2018-01-01T00:00:00.000'), + value: 125.25, + }, + { + date: DateTime.fromISO('2019-01-01T00:00:00.000'), + value: 220.5, + }, + { + date: DateTime.fromISO('2020-01-01T00:00:00.000'), + value: 130.33, + }, + ] + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: loadYearData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + TimeStep.YEAR, + FluidType.GAS + ) + expect(result).toEqual(expectedResult) + }) + + it('should return the data of the gas fluid and month time step', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-01-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2020-09-01T00:00:00.000'), + value: 125.25, + }, + { + date: DateTime.fromISO('2020-10-01T00:00:00.000'), + value: 220.5, + }, + { + date: DateTime.fromISO('2020-11-01T00:00:00.000'), + value: 130.33, + }, + ] + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: loadMonthData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + TimeStep.MONTH, + FluidType.GAS + ) + expect(result).toEqual(expectedResult) + }) + + it('should return the data of the gas fluid and day time step', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-11-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2020-11-01T00:00:00.000'), + value: 25.25, + }, + { + date: DateTime.fromISO('2020-11-02T00:00:00.000'), + value: 20.5, + }, + { + date: DateTime.fromISO('2020-11-03T00:00:00.000'), + value: 30.33, + }, + { + date: DateTime.fromISO('2020-11-04T00:00:00.000'), + value: 1.22, + }, + ] + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: loadDayData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + TimeStep.DAY, + FluidType.GAS + ) + expect(result).toEqual(expectedResult) + }) + + it('should return the data of the gas fluid and hour time step', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-11-02T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-02T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2020-11-02T00:00:00.000'), + value: 4.5, + }, + { + date: DateTime.fromISO('2020-11-02T01:00:00.000'), + value: 1.33, + }, + { + date: DateTime.fromISO('2020-11-02T02:00:00.000'), + value: 3.22, + }, + { + date: DateTime.fromISO('2020-11-02T03:00:00.000'), + value: 7.82, + }, + { + date: DateTime.fromISO('2020-11-02T04:00:00.000'), + value: 1.23, + }, + ] + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: loadHourData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + TimeStep.HOUR, + FluidType.GAS + ) + expect(result).toEqual(expectedResult) + }) + + it('should return the day data of the gas fluid and unkonwn time step', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-11-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2020-11-01T00:00:00.000'), + value: 25.25, + }, + { + date: DateTime.fromISO('2020-11-02T00:00:00.000'), + value: 20.5, + }, + { + date: DateTime.fromISO('2020-11-03T00:00:00.000'), + value: 30.33, + }, + { + date: DateTime.fromISO('2020-11-04T00:00:00.000'), + value: 1.22, + }, + ] + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: loadDayData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + 99, + FluidType.GAS + ) + expect(result).toEqual(expectedResult) + }) + + it('should return the data of the elec fluid and month time step with start year <> end year', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2019-12-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2020-09-01T00:00:00.000'), + value: 125.25, + }, + { + date: DateTime.fromISO('2020-10-01T00:00:00.000'), + value: 220.5, + }, + { + date: DateTime.fromISO('2020-11-01T00:00:00.000'), + value: 130.33, + }, + ] + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: loadMonthData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + TimeStep.MONTH, + FluidType.ELECTRICITY + ) + expect(result).toEqual(expectedResult) + }) + + it('should return the data of the elec fluid and day time step with start month <> end month', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-10-15T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-5T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2020-11-01T00:00:00.000'), + value: 25.25, + }, + { + date: DateTime.fromISO('2020-11-02T00:00:00.000'), + value: 20.5, + }, + { + date: DateTime.fromISO('2020-11-03T00:00:00.000'), + value: 30.33, + }, + { + date: DateTime.fromISO('2020-11-04T00:00:00.000'), + value: 1.22, + }, + ] + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: loadDayData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( + mockTimePeriod, + TimeStep.DAY, + FluidType.ELECTRICITY + ) + expect(result).toEqual(expectedResult) + }) + + it('should return the data of the elec fluid and day time step with time period > 31 days', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-10-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const expectedResult: Dataload[] = [ + { + date: DateTime.fromISO('2020-11-01T00:00:00.000'), + value: 25.25, + }, + { + date: DateTime.fromISO('2020-11-02T00:00:00.000'), + value: 20.5, + }, + { + date: DateTime.fromISO('2020-11-03T00:00:00.000'), + value: 30.33, + }, + { + date: DateTime.fromISO('2020-11-04T00:00:00.000'), + value: 1.22, + }, + ] + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: loadDayData, + bookmark: '', + next: false, + skip: 0, + } + mockClient.query.mockResolvedValue(mockQueryResult) + const result: Dataload[] | null = await queryRunner.fetchFluidData( mockTimePeriod, TimeStep.DAY, FluidType.ELECTRICITY @@ -52,8 +608,12 @@ describe('queryRunner service', () => { }) it('should return null when fetch data failed', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-11-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } mockClient.query.mockRejectedValue(new Error()) - const result = await queryRunner.fetchFluidData( + const result: Dataload[] | null = await queryRunner.fetchFluidData( mockTimePeriod, TimeStep.DAY, FluidType.ELECTRICITY @@ -62,7 +622,11 @@ describe('queryRunner service', () => { }) it('should return null when unknown fluid type', async () => { - const result = await queryRunner.fetchFluidData( + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-11-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const result: Dataload[] | null = await queryRunner.fetchFluidData( mockTimePeriod, TimeStep.DAY, 99 @@ -71,7 +635,11 @@ describe('queryRunner service', () => { }) it('should return null when unknown time step', async () => { - const result = await queryRunner.fetchFluidData( + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-11-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), + } + const result: Dataload[] | null = await queryRunner.fetchFluidData( mockTimePeriod, 99, FluidType.ELECTRICITY @@ -83,17 +651,17 @@ describe('queryRunner service', () => { describe('fetchFluidMaxData method', () => { it('should return the max load for elec fluid with day timestep', async () => { const mockTimePeriod = { - startDate: DateTime.fromISO('2020-11-01T00:00:00.000+01:00'), - endDate: DateTime.fromISO('2020-11-30T23:59:59.999+01:00'), + startDate: DateTime.fromISO('2020-11-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), } const mockQueryResult: QueryResult<DataloadEntity[]> = { - data: enedisDayData.sort((a, b) => b.load - a.load), + data: loadDayData.sort((a, b) => b.load - a.load), bookmark: '', next: false, skip: 0, } mockClient.query.mockResolvedValue(mockQueryResult) - const result = await queryRunner.fetchFluidMaxData( + const result: number | null = await queryRunner.fetchFluidMaxData( mockTimePeriod, TimeStep.DAY, FluidType.ELECTRICITY @@ -103,17 +671,17 @@ describe('queryRunner service', () => { it('should return the max load for elec fluid with half an hour timestep', async () => { const mockTimePeriod = { - startDate: DateTime.fromISO('2020-11-02T00:00:00.000+01:00'), - endDate: DateTime.fromISO('2020-11-2T23:59:59.999+01:00'), + startDate: DateTime.fromISO('2020-11-02T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-2T23:59:59.999'), } const mockQueryResult: QueryResult<DataloadEntity[]> = { - data: [enedisMinuteData[4]], + data: [loadMinuteData[4]], bookmark: '', next: false, skip: 0, } const mockQueryResult2: QueryResult<DataloadEntity[]> = { - data: [enedisMinuteData[0]], + data: [loadMinuteData[0]], bookmark: '', next: false, skip: 0, @@ -121,7 +689,7 @@ describe('queryRunner service', () => { mockClient.query .mockResolvedValueOnce(mockQueryResult) .mockResolvedValueOnce(mockQueryResult2) - const result = await queryRunner.fetchFluidMaxData( + const result: number | null = await queryRunner.fetchFluidMaxData( mockTimePeriod, TimeStep.HALF_AN_HOUR, FluidType.ELECTRICITY @@ -129,13 +697,41 @@ describe('queryRunner service', () => { expect(result).toBe(7.82) }) + it('should return 0 for elec fluid with half an hour timestep and no data found', async () => { + const mockTimePeriod = { + startDate: DateTime.fromISO('2020-11-02T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-2T23:59:59.999'), + } + const mockQueryResult: QueryResult<DataloadEntity[]> = { + data: [], + bookmark: '', + next: false, + skip: 0, + } + const mockQueryResult2: QueryResult<DataloadEntity[]> = { + data: [], + bookmark: '', + next: false, + skip: 0, + } + mockClient.query + .mockResolvedValueOnce(mockQueryResult) + .mockResolvedValueOnce(mockQueryResult2) + const result: number | null = await queryRunner.fetchFluidMaxData( + mockTimePeriod, + TimeStep.HALF_AN_HOUR, + FluidType.ELECTRICITY + ) + expect(result).toBe(0) + }) + it('should return null when fetch data failed', async () => { const mockTimePeriod = { - startDate: DateTime.fromISO('2020-11-01T00:00:00.000+01:00'), - endDate: DateTime.fromISO('2020-11-30T23:59:59.999+01:00'), + startDate: DateTime.fromISO('2020-11-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), } mockClient.query.mockRejectedValue(new Error()) - const result = await queryRunner.fetchFluidMaxData( + const result: number | null = await queryRunner.fetchFluidMaxData( mockTimePeriod, TimeStep.DAY, FluidType.ELECTRICITY @@ -145,10 +741,10 @@ describe('queryRunner service', () => { it('should return null when unknown fluid type', async () => { const mockTimePeriod = { - startDate: DateTime.fromISO('2020-11-01T00:00:00.000+01:00'), - endDate: DateTime.fromISO('2020-11-30T23:59:59.999+01:00'), + startDate: DateTime.fromISO('2020-11-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), } - const result = await queryRunner.fetchFluidMaxData( + const result: number | null = await queryRunner.fetchFluidMaxData( mockTimePeriod, TimeStep.DAY, 99 @@ -158,10 +754,10 @@ describe('queryRunner service', () => { it('should return null when unknown time step', async () => { const mockTimePeriod = { - startDate: DateTime.fromISO('2020-11-01T00:00:00.000+01:00'), - endDate: DateTime.fromISO('2020-11-30T23:59:59.999+01:00'), + startDate: DateTime.fromISO('2020-11-01T00:00:00.000'), + endDate: DateTime.fromISO('2020-11-30T23:59:59.999'), } - const result = await queryRunner.fetchFluidMaxData( + const result: number | null = await queryRunner.fetchFluidMaxData( mockTimePeriod, 99, FluidType.ELECTRICITY @@ -172,7 +768,7 @@ describe('queryRunner service', () => { describe('getLastDateData method', () => { it('should return the last load for elec fluid', async () => { - const lastMockData = [...enedisDayData] + const lastMockData = [...loadDayData] lastMockData.sort( (a, b) => (b.year - a.year) * 100 + (b.month - a.month) * 10 + (b.day - a.day) @@ -187,9 +783,9 @@ describe('queryRunner service', () => { const result = await queryRunner.getLastDateData(FluidType.ELECTRICITY) expect(result).toEqual( DateTime.local( - enedisDayData[3].year, - enedisDayData[3].month, - enedisDayData[3].day + loadDayData[3].year, + loadDayData[3].month, + loadDayData[3].day ) ) }) @@ -204,7 +800,7 @@ describe('queryRunner service', () => { describe('getEntries method', () => { it('should return query result for elec fluid with day timestep', async () => { const mockQueryResult: QueryResult<DataloadEntity[]> = { - data: enedisDayData, + data: loadDayData, bookmark: '', next: false, skip: 0, diff --git a/src/services/queryRunner.service.ts b/src/services/queryRunner.service.ts index f4a4e9b30ceb37a76c56753df98e1fa0c1a78d60..2ff6150ba5c47f46f06ea6d3e082dbb20e24ce8d 100644 --- a/src/services/queryRunner.service.ts +++ b/src/services/queryRunner.service.ts @@ -156,16 +156,14 @@ export default class QueryRunner { if (intervalCount >= 12) return this._default_months const monthList = [] let offset = 0 - if (timePeriod.startDate.year !== timePeriod.endDate.year) offset = 12 - for ( let m = timePeriod.startDate.month; m <= timePeriod.endDate.month + offset; m++ - ) + ) { monthList.push(m > offset ? m - offset : m) - + } return monthList } @@ -177,16 +175,14 @@ export default class QueryRunner { if (intervalCount >= 31) return this._default_days const dayList = [] let offset = 0 - if (timePeriod.startDate.month !== timePeriod.endDate.month) offset = 31 - for ( let d = timePeriod.startDate.day; d <= timePeriod.endDate.day + offset; d++ - ) + ) { dayList.push(d > offset ? d - offset : d) - + } return dayList }