Browse Source

Tests for old segments deleting #209 (#220)

pull/1/head
Evgeny Smyshlyaev 6 years ago committed by rozetko
parent
commit
fa7a7b86c5
  1. 7
      server/spec/config.jest.ts
  2. 66
      server/spec/segments.jest.ts
  3. 11
      server/src/controllers/analytics_controller.ts

7
server/spec/config.jest.ts

@ -1,7 +0,0 @@
import { HASTIC_PORT } from '../src/config';
describe("When importing from .ts files", function() {
it("config should be correct", function() {
expect(HASTIC_PORT).toBe(8000);
})
})

66
server/spec/segments.jest.ts

@ -0,0 +1,66 @@
import { deleteNonpredictedSegments } from '../src/controllers/analytics_controller';
import * as AnalyticUnit from '../src/models/analytic_unit_model';
import * as Segment from '../src/models/segment_model';
import * as _ from 'lodash';
let id: AnalyticUnit.AnalyticUnitId = 'testid';
let baseSegments = segmentBuilder([[0,1], [2,3], [4,5]]);
beforeAll(async () => {
clearDB();
});
beforeEach(async ()=> {
await Segment.insertSegments(baseSegments);
});
afterEach(async () => {
clearDB();
});
describe('Check deleted segments', function() {
let payload = {
lastPredictionTime: 0,
segments: [],
cache: null
};
it('previous segments not found', async function() {
payload.segments = segmentBuilder([[0,1], [4,5]]);
expect(await getDeletedSegments(id, payload)).toEqual(segmentBuilder([[2,3]]));
});
it('all previous segments found', async function() {
payload.segments = segmentBuilder([[0,1], [2,3], [4,5]]);
expect(await getDeletedSegments(id, payload)).toEqual([]);
});
});
async function getDeletedSegments(id, payload): Promise<Segment.Segment[]> {
let preSegments = await Segment.findMany(id, {labeled: false, deleted:false});
await deleteNonpredictedSegments(id, payload);
let postSegments = await Segment.findMany(id, {labeled: false, deleted:false});
let deleted = setDifference(preSegments, postSegments);
deleted = deleted.map(s => {
s.id = undefined;
return s;
});
return deleted;
}
function setDifference(a, b: Segment.Segment[]): Segment.Segment[] {
return _.differenceWith(a, b, (x, y: Segment.Segment) => x.equals(y));
}
function segmentBuilder(times) {
return times.map(t => {
return new Segment.Segment(id, t[0], t[1], false, false, undefined);
});
}
async function clearDB() {
let segments = await Segment.findMany(id, {labeled: false, deleted: false});
await Segment.removeSegments(segments.map(s => s.id));
}

11
server/src/controllers/analytics_controller.ts

@ -195,9 +195,7 @@ export async function runPredict(id: AnalyticUnit.AnalyticUnitId) {
// }
// }
let lastPredictedSegments = await Segment.findMany(id, { labeled: false, deleted: false });
let segmentsToRemove = lastPredictedSegments.filter(s => payload.segments.indexOf(s) === -1);
Segment.removeSegments(segmentsToRemove.map(s => s.id));
await deleteNonpredictedSegments(id, payload);
Segment.insertSegments(payload.segments);
AnalyticUnitCache.setData(id, payload.cache);
@ -212,6 +210,13 @@ export async function runPredict(id: AnalyticUnit.AnalyticUnitId) {
}
}
export async function deleteNonpredictedSegments(id, payload) {
let lastPredictedSegments = await Segment.findMany(id, { labeled: false, deleted: false });
let segmentsToRemove: Segment.Segment[];
segmentsToRemove = _.differenceWith(lastPredictedSegments, payload.segments, (a, b: Segment.Segment) => a.equals(b));
Segment.removeSegments(segmentsToRemove.map(s => s.id));
}
function processPredictionResult(analyticUnitId: AnalyticUnit.AnalyticUnitId, taskResult: any): {
lastPredictionTime: number,
segments: Segment.Segment[],

Loading…
Cancel
Save