Skip to content

Commit

Permalink
Added test for readUnprocessedTrips
Browse files Browse the repository at this point in the history
  • Loading branch information
the-bay-kay committed Nov 23, 2023
1 parent 667fec8 commit 07c5f80
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 21 deletions.
79 changes: 73 additions & 6 deletions www/__mocks__/timelineHelperMocks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { MetaData, ServerData, ServerResponse } from '../js/types/serverData';
import { CompositeTrip, ConfirmedPlace, TripTransition } from '../js/types/diaryTypes';
import {
CompositeTrip,
ConfirmedPlace,
FilteredLocation,
TripTransition,
UnprocessedTrip,
} from '../js/types/diaryTypes';
import { LabelOptions } from '../js/types/labelTypes';

const mockMetaData: MetaData = {
Expand Down Expand Up @@ -56,8 +62,31 @@ const mockConfirmedPlaceData: ConfirmedPlace = {
let tempMetaData = JSON.parse(JSON.stringify(mockMetaData));
tempMetaData.write_ts = 2;
tempMetaData.origin_key = '2';

export const mockMetaDataTwo = tempMetaData;

export const mockUnprocessedTrip: UnprocessedTrip = {
_id: { $oid: 'mockUnprocessedTrip' },
additions: [],
confidence_threshold: 0.0,
distance: 1.0,
duration: 3.0,
end_fmt_time: '',
end_loc: { type: '', coordinates: [] },
end_local_dt: null,
expectation: null,
inferred_labels: [],
key: 'mockUnprocessedTrip',
locations: [],
origin_key: '',
source: '',
start_local_dt: null,
start_ts: 0.1,
start_loc: { type: '', coordinates: [] },
starting_trip: null,
user_input: null,
};

export const mockData: ServerResponse<CompositeTrip> = {
phone_data: [
{
Expand Down Expand Up @@ -150,18 +179,56 @@ export const mockDataTwo = {
phone_data: [mockData.phone_data[0], newPhoneData],
};

export const mockTransition: Array<ServerData<TripTransition>> = [
export const mockTransitions: Array<ServerData<TripTransition>> = [
{
data: {
// mock of a startTransition
currstate: '',
transition: 'T_EXITED_GEOFENCE',
ts: 1,
},
metadata: mockMetaData,
},
{
data: {
currstate: 'STATE_WAITING_FOR_TRIP_TO_START',
transition: 'T_NOP',
ts: 12345.6789,
// mock of an endTransition
currstate: '',
transition: 'T_TRIP_ENDED',
ts: 9999,
},
metadata: mockMetaData,
},
];

export const mockTransitionTwo = mockTransition.push(mockTransition[0]);
const mockFilterLocation: FilteredLocation = {
accuracy: 0.1,
altitude: 100,
elapsedRealtimeNanos: 10000,
filter: 'time',
fmt_time: '',
heading: 1.0,
latitude: 1.0,
loc: null,
local_dt: null,
longitude: -1.0,
sensed_speed: 0,
ts: 100,
};
let mockFilterLocationTwo = JSON.parse(JSON.stringify(mockFilterLocation));
mockFilterLocationTwo.ts = 900;
mockFilterLocationTwo.longitude = 200;
mockFilterLocationTwo.longitude = -200;

export const mockFilterLocations: Array<ServerData<FilteredLocation>> = [
{
data: mockFilterLocation,
metadata: mockMetaData,
},
{
data: mockFilterLocationTwo,
metadata: mockMetaDataTwo,
},
];

// When called by mocks, pair 1 returns 1 value, Pair two 2, pair 3 returns none.
export const fakeStartTsOne = -14576291;
Expand Down
41 changes: 29 additions & 12 deletions www/__tests__/timelineHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,12 @@ describe('compositeTrips2TimelineMap', () => {

it('Works with a list of len >= 1, with flag', () => {
testValue = compositeTrips2TimelineMap(tripListTwo, true);
console.log(`Len: ${testValue.size}`);
expect(testValue.size).toBe(6);
});
});

// updateAllUnprocessedinputs tests
it('can use an appConfig to get labelInputKeys', () => {
// Tests for updateLocalUnprocessedInputs & keysForLabelInputs
describe('The updateUnprocessedInput functions can ', () => {
const mockAppConfigOne = {
survey_info: {
'trip-labels': 'ENKETO',
Expand All @@ -106,9 +105,15 @@ it('can use an appConfig to get labelInputKeys', () => {
mode_studied: 'sample',
},
};
expect(keysForLabelInputs(mockAppConfigOne)).rejects;
expect(keysForLabelInputs(mockAppConfigOne)).toEqual(['manual/trip_user_input']);
expect(keysForLabelInputs(mockAppConfigTwo).length).toEqual(3);
// keysForLabelInputs tests
it('use an appConfig to get labelInputKeys', () => {
expect(keysForLabelInputs(mockAppConfigOne)).rejects;
expect(keysForLabelInputs(mockAppConfigOne)).toEqual(['manual/trip_user_input']);
expect(keysForLabelInputs(mockAppConfigTwo).length).toEqual(3);
});
it('update the unprocessed labels', () => {
// TODO
});
});

// Tests for readAllCompositeTrips
Expand Down Expand Up @@ -162,23 +167,35 @@ it('Works with multiple trips', async () => {
// Tests for `readUnprocessedTrips`
jest.mock('../js/services/unifiedDataLoader', () => ({
getUnifiedDataForInterval: jest.fn((key, tq, combiner) => {
if (tq.startTs === mockTLH.fakeStartTsOne) return Promise.resolve(mockTLH.mockTransition);
if (tq.startTs === mockTLH.fakeStartTsTwo) return Promise.resolve(mockTLH.mockTransitionTwo);
return Promise.resolve([]);
if (key === 'statemachine/transition') {
if (tq.startTs === mockTLH.fakeStartTsOne) return Promise.resolve(mockTLH.mockTransitions);
return Promise.resolve([]);
}
if (key === 'background/filtered_location') {
return Promise.resolve(mockTLH.mockFilterLocations);
}
}),
}));

it('works when there are no unprocessed trips...', async () => {
expect(readUnprocessedTrips(-1, -1, null)).resolves.toEqual([]);
});

// In manual testing, it seems that `trip_gj_list` always returns
// as an empty array - should find data where this is different...
it('works when there are one or more unprocessed trips...', async () => {
const testValueOne = await readUnprocessedTrips(
mockTLH.fakeStartTsOne,
mockTLH.fakeEndTsOne,
null,
);
expect(testValueOne).toEqual([]);
expect(testValueOne.length).toEqual(1);
expect(testValueOne[0]).toEqual(
expect.objectContaining({
origin_key: expect.any(String),
distance: expect.any(Number),
start_loc: expect.objectContaining({
type: expect.any(String),
coordinates: expect.any(Array<Number>),
}),
}),
);
});
5 changes: 3 additions & 2 deletions www/js/diary/timelineHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,9 @@ const transitionTrip2TripObj = function (trip) {
endTs: tripEndTransition.data.ts,
};
logDebug(
'About to pull location data for range' +
'About to pull location data for range ' +
DateTime.fromSeconds(tripStartTransition.data.ts).toLocaleString(DateTime.DATETIME_MED) +
' to ' +
DateTime.fromSeconds(tripEndTransition.data.ts).toLocaleString(DateTime.DATETIME_MED),
);
const getSensorData = window['cordova'].plugins.BEMUserCache.getSensorDataForInterval;
Expand Down Expand Up @@ -544,7 +545,7 @@ export const readUnprocessedTrips = function (startTs, endTs, lastProcessedTrip)
(trip) => trip && (trip.distance >= 100 || trip.duration >= 300),
);
logDebug(
`after filtering undefined and distance < 100, trip_gj_list size = ${raw_trip_gj_list.length}`,
`after filtering undefined and distance < 100, trip_gj_list size = ${trip_gj_list.length}`,
);
// Link 0th trip to first, first to second, ...
for (var i = 0; i < trip_gj_list.length - 1; i++) {
Expand Down
2 changes: 1 addition & 1 deletion www/js/types/diaryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export type FilteredLocation = {
accuracy: number;
altitude: number;
elapsedRealtimeNanos: number;
filter: number;
filter: string;
fmt_time: string;
heading: number;
latitude: number;
Expand Down

0 comments on commit 07c5f80

Please sign in to comment.