Skip to content

Commit

Permalink
Merge pull request #221 from ral-facilities/DSEGOG-92-searching-for-d…
Browse files Browse the repository at this point in the history
…ata-using-experiment-id

DSEGOG-92 DSEGOG-91 DSEGOG-90 DSEGOG-94 DSEGOG-90 DSEGOG-96 DSEGOG-97 searching for data using experiment id
  • Loading branch information
joshuadkitenge authored May 25, 2023
2 parents e1685c2 + 55f2361 commit 023a8ae
Show file tree
Hide file tree
Showing 21 changed files with 1,617 additions and 254 deletions.
460 changes: 328 additions & 132 deletions cypress/integration/table/search.spec.ts

Large diffs are not rendered by default.

39 changes: 2 additions & 37 deletions src/api/experiment.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { renderHook, waitFor } from '@testing-library/react';
import { useExperiment } from './experiment';
import { ExperimentParams } from '../app.types';
import { hooksWrapperWithProviders } from '../setupTests';
import experimentsJson from '../mocks/experiments.json';

describe('channels api functions', () => {
afterEach(() => {
Expand All @@ -18,43 +19,7 @@ describe('channels api functions', () => {
expect(result.current.isSuccess).toBeTruthy();
});

const expected: ExperimentParams[] = [
{
_id: '18325019-4',
end_date: '2020-01-06T18:00:00',
experiment_id: '18325019',
part: 4,
start_date: '2020-01-03T10:00:00',
},
{
_id: '18325019-5',
end_date: '2019-06-12T17:00:00',
experiment_id: '18325019',
part: 5,
start_date: '2019-06-12T09:00:00',
},
{
_id: '18325024-1',
end_date: '2019-03-13T18:00:00',
experiment_id: '18325024',
part: 1,
start_date: '2019-03-13T10:00:00',
},
{
_id: '18325025-1',
end_date: '2019-12-01T18:00:00',
experiment_id: '18325025',
part: 1,
start_date: '2019-12-01T10:00:00',
},
{
_id: '19510000-1',
end_date: '2019-10-01T17:00:00',
experiment_id: '19510000',
part: 1,
start_date: '2019-10-01T09:00:00',
},
];
const expected: ExperimentParams[] = experimentsJson;

expect(result.current.data).toEqual(expected);
});
Expand Down
56 changes: 56 additions & 0 deletions src/api/records.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
useIncomingRecordCount,
useRecordsPaginated,
useThumbnails,
useShotnumToDateConverter,
useDateToShotnumConverter,
} from './records';
import { PreloadedState } from '@reduxjs/toolkit';
import { RootState } from '../state/store';
Expand Down Expand Up @@ -169,6 +171,60 @@ describe('records api functions', () => {
);
});

describe('useShotnumToDateConverter', () => {
it('send a request to fetch date using ShotnumToDateConverter and returns a succesful response', async () => {
const expectedReponse = {
from: '2022-01-04T00:00:00',
to: '2022-01-18T00:00:00',
min: 4,
max: 19,
};

const { result } = renderHook(
() =>
useShotnumToDateConverter(expectedReponse.min, expectedReponse.max),
{
wrapper: hooksWrapperWithProviders(state),
}
);
await waitFor(() => {
expect(result.current.isSuccess).toBeTruthy();
});

expect(result.current.data).toEqual(expectedReponse);
});
it.todo(
'sends axios request to fetch records and throws an appropriate error on failure'
);
});

describe('useDateToShotnumConverter', () => {
it('send a request to fetch date usingDateToShotnumConverter and returns a succesful response', async () => {
const expectedReponse = {
from: '2021-12-01T00:00:00',
to: '2022-01-19T00:00:00',
min: 1,
max: 18,
};

const { result } = renderHook(
() =>
useDateToShotnumConverter(expectedReponse.from, expectedReponse.to),
{
wrapper: hooksWrapperWithProviders(state),
}
);
await waitFor(() => {
expect(result.current.isSuccess).toBeTruthy();
});

expect(result.current.data).toEqual(expectedReponse);
});
it.todo(
'sends axios request to fetch records and throws an appropriate error on failure'
);
});

describe('useIncomingRecordCount', () => {
let params: URLSearchParams;

Expand Down
113 changes: 113 additions & 0 deletions src/api/records.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
timeChannelName,
isChannelImage,
isChannelWaveform,
DateRangetoShotnumConverter,
} from '../app.types';
import { useAppSelector } from '../state/hooks';
import { selectQueryParams } from '../state/slices/searchSlice';
Expand Down Expand Up @@ -161,6 +162,118 @@ const fetchRecordCountQuery = (
.then((response) => response.data);
};

export const fetchRangeRecordConverterQuery = (
apiUrl: string,
fromDate: string | undefined,
toDate: string | undefined,
shotnumMin: number | undefined,
shotnumMax: number | undefined
): Promise<DateRangetoShotnumConverter> => {
const queryParams = new URLSearchParams();
let timestampObj = {};
if (fromDate || toDate) {
timestampObj = {
from: fromDate,
to: toDate,
};
}

if (fromDate || toDate) {
queryParams.append('date_range', JSON.stringify(timestampObj));
}

let shotnumObj = {};
if (shotnumMin || shotnumMax) {
shotnumObj = {
min: shotnumMin,
max: shotnumMax,
};
}

if (shotnumMin || shotnumMax) {
queryParams.append('shotnum_range', JSON.stringify(shotnumObj));
}

return axios
.get(`${apiUrl}/records/range_converter`, {
params: queryParams,
headers: {
Authorization: `Bearer ${readSciGatewayToken()}`,
},
})
.then((response) => {
if (response.data) {
let inputRange;
if (fromDate || toDate) {
inputRange = { from: fromDate, to: toDate };
}
if (shotnumMin || shotnumMax) {
inputRange = { min: shotnumMin, max: shotnumMax };
}
return { ...inputRange, ...response.data };
}
});
};

export const useDateToShotnumConverter = (
fromDate: string | undefined,
toDate: string | undefined
): UseQueryResult<DateRangetoShotnumConverter, AxiosError> => {
const { apiUrl } = useAppSelector(selectUrls);

return useQuery<
DateRangetoShotnumConverter,
AxiosError,
DateRangetoShotnumConverter,
[string, { fromDate: string | undefined; toDate: string | undefined }]
>(
['dateToShotnumConverter', { fromDate, toDate }],
(params) => {
return fetchRangeRecordConverterQuery(
apiUrl,
fromDate,
toDate,
undefined,
undefined
);
},
{
onError: (error) => {
console.log('Got error ' + error.message);
},
}
);
};

export const useShotnumToDateConverter = (
shotnumMin: number | undefined,
shotnumMax: number | undefined
): UseQueryResult<DateRangetoShotnumConverter, AxiosError> => {
const { apiUrl } = useAppSelector(selectUrls);

return useQuery<
DateRangetoShotnumConverter,
AxiosError,
DateRangetoShotnumConverter,
[string, { shotnumMin: number | undefined; shotnumMax: number | undefined }]
>(
['shotnumToDateConverter', { shotnumMin, shotnumMax }],
(params) => {
return fetchRangeRecordConverterQuery(
apiUrl,
undefined,
undefined,
shotnumMin,
shotnumMax
);
},
{
onError: (error) => {
console.log('Got error ' + error.message);
},
}
);
};
export const useRecordsPaginated = (): UseQueryResult<
RecordRow[],
AxiosError
Expand Down
7 changes: 7 additions & 0 deletions src/app.types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ export interface DateRange {
toDate?: string;
}

export interface DateRangetoShotnumConverter {
from?: string;
to?: string;
min?: number;
max?: number;
}

export interface ShotnumRange {
min?: number;
max?: number;
Expand Down
53 changes: 30 additions & 23 deletions src/mocks/experiments.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
[
{
"_id": "18325019-4",
"end_date": "2020-01-06T18:00:00",
"experiment_id": "18325019",
"part": 4,
"start_date": "2020-01-03T10:00:00"
"_id": "19210001-1",
"end_date": "2022-01-03T12:00:00",
"experiment_id": "19210001",
"part": 1,
"start_date": "2021-12-31T13:00:00"
},
{
"_id": "18325019-5",
"end_date": "2019-06-12T17:00:00",
"experiment_id": "18325019",
"part": 5,
"start_date": "2019-06-12T09:00:00"
"_id": "19210001-2",
"end_date": "2022-01-06T12:00:00",
"experiment_id": "19210001",
"part": 2,
"start_date": "2022-01-03T13:00:00"
},
{
"_id": "18325024-1",
"end_date": "2019-03-13T18:00:00",
"experiment_id": "18325024",
"_id": "19210012-1",
"end_date": "2022-01-09T12:00:00",
"experiment_id": "19210012",
"part": 1,
"start_date": "2019-03-13T10:00:00"
"start_date": "2022-01-06T13:00:00"
},
{
"_id": "18325025-1",
"end_date": "2019-12-01T18:00:00",
"experiment_id": "18325025",
"part": 1,
"start_date": "2019-12-01T10:00:00"
"_id": "19210012-2",
"end_date": "2022-01-12T12:00:00",
"experiment_id": "19210012",
"part": 2,
"start_date": "2022-01-09T13:00:00"
},
{
"_id": "19510000-1",
"end_date": "2019-10-01T17:00:00",
"experiment_id": "19510000",
"_id": "22110007-1",
"end_date": "2022-01-15T12:00:00",
"experiment_id": "22110007",
"part": 1,
"start_date": "2019-10-01T09:00:00"
"start_date": "2022-01-12T13:00:00"
},
{
"_id": "22110007-2",
"end_date": "2022-01-18T12:00:00",
"experiment_id": "22110007",
"part": 2,
"start_date": "2022-01-15T13:00:00"
}
]
Loading

0 comments on commit 023a8ae

Please sign in to comment.