Merge pull request #6479 from HabitRPG/new-history-preening
New history preening
This commit is contained in:
@@ -990,124 +990,6 @@ describe('Cron', () => {
|
||||
expect(beforeTasks).to.eql(afterTasks);
|
||||
});
|
||||
|
||||
describe('preening', () => {
|
||||
beforeEach(function () {
|
||||
this.clock = sinon.useFakeTimers(Date.parse('2013-11-20'), 'Date');
|
||||
});
|
||||
afterEach(function () {
|
||||
return this.clock.restore();
|
||||
});
|
||||
|
||||
it('should preen user history', function () {
|
||||
let ref = beforeAfter({
|
||||
daysAgo: 1,
|
||||
});
|
||||
let after = ref.after;
|
||||
|
||||
let history = [
|
||||
{
|
||||
date: '09/01/2012',
|
||||
value: 0,
|
||||
}, {
|
||||
date: '10/01/2012',
|
||||
value: 0,
|
||||
}, {
|
||||
date: '11/01/2012',
|
||||
value: 2,
|
||||
}, {
|
||||
date: '12/01/2012',
|
||||
value: 2,
|
||||
}, {
|
||||
date: '01/01/2013',
|
||||
value: 1,
|
||||
}, {
|
||||
date: '01/15/2013',
|
||||
value: 3,
|
||||
}, {
|
||||
date: '02/01/2013',
|
||||
value: 2,
|
||||
}, {
|
||||
date: '02/15/2013',
|
||||
value: 4,
|
||||
}, {
|
||||
date: '03/01/2013',
|
||||
value: 3,
|
||||
}, {
|
||||
date: '03/15/2013',
|
||||
value: 5,
|
||||
}, {
|
||||
date: '04/01/2013',
|
||||
value: 4,
|
||||
}, {
|
||||
date: '04/15/2013',
|
||||
value: 6,
|
||||
}, {
|
||||
date: '05/01/2013',
|
||||
value: 5,
|
||||
}, {
|
||||
date: '05/15/2013',
|
||||
value: 7,
|
||||
}, {
|
||||
date: '06/01/2013',
|
||||
value: 6,
|
||||
}, {
|
||||
date: '06/15/2013',
|
||||
value: 8,
|
||||
}, {
|
||||
date: '07/01/2013',
|
||||
value: 7,
|
||||
}, {
|
||||
date: '07/15/2013',
|
||||
value: 9,
|
||||
}, {
|
||||
date: '08/01/2013',
|
||||
value: 8,
|
||||
}, {
|
||||
date: '08/15/2013',
|
||||
value: 10,
|
||||
}, {
|
||||
date: '09/01/2013',
|
||||
value: 9,
|
||||
}, {
|
||||
date: '09/15/2013',
|
||||
value: 11,
|
||||
}, {
|
||||
date: '010/01/2013',
|
||||
value: 10,
|
||||
}, {
|
||||
date: '010/15/2013',
|
||||
value: 12,
|
||||
}, {
|
||||
date: '011/01/2013',
|
||||
value: 12,
|
||||
}, {
|
||||
date: '011/02/2013',
|
||||
value: 13,
|
||||
}, {
|
||||
date: '011/03/2013',
|
||||
value: 14,
|
||||
}, {
|
||||
date: '011/04/2013',
|
||||
value: 15,
|
||||
},
|
||||
];
|
||||
|
||||
after.history = {
|
||||
exp: _.cloneDeep(history),
|
||||
todos: _.cloneDeep(history),
|
||||
};
|
||||
after.habits[0].history = _.cloneDeep(history);
|
||||
after.fns.cron();
|
||||
after.history.exp.pop();
|
||||
after.history.todos.pop();
|
||||
_.each([after.history.exp, after.history.todos, after.habits[0].history], function (arr) {
|
||||
expect(_.map(arr, (x) => {
|
||||
return x.value;
|
||||
})).to.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Todos', () => {
|
||||
it('1 day missed', () => {
|
||||
let ref = beforeAfter({
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import { preenHistory } from '../../common/script/preenUserHistory';
|
||||
import moment from 'moment';
|
||||
import sinon from 'sinon'; // eslint-disable-line no-shadow
|
||||
|
||||
function generateHistory (days) {
|
||||
let history = [];
|
||||
let now = Number(moment().toDate());
|
||||
|
||||
while (days > 0) {
|
||||
history.push({
|
||||
value: days,
|
||||
date: Number(moment(now).subtract(days, 'days').toDate()),
|
||||
});
|
||||
days--;
|
||||
}
|
||||
|
||||
return history;
|
||||
}
|
||||
|
||||
describe('preenHistory', () => {
|
||||
let clock;
|
||||
|
||||
beforeEach(() => {
|
||||
// Replace system clocks so we can get predictable results
|
||||
clock = sinon.useFakeTimers(Number(moment('2013-10-20').zone(0).startOf('day').toDate()), 'Date');
|
||||
});
|
||||
afterEach(() => {
|
||||
return clock.restore();
|
||||
});
|
||||
|
||||
it('does not modify history if all entries are more recent than cutoff (free users)', () => {
|
||||
let h = generateHistory(60);
|
||||
expect(preenHistory(_.cloneDeep(h), false, 0)).to.eql(h);
|
||||
});
|
||||
|
||||
it('does not modify history if all entries are more recent than cutoff (subscribers)', () => {
|
||||
let h = generateHistory(365);
|
||||
expect(preenHistory(_.cloneDeep(h), true, 0)).to.eql(h);
|
||||
});
|
||||
|
||||
it('does aggregate data in monthly entries before cutoff (free users)', () => {
|
||||
let h = generateHistory(81); // Jumps to July
|
||||
let preened = preenHistory(_.cloneDeep(h), false, 0);
|
||||
expect(preened.length).to.eql(62); // Keeps 60 days + 2 entries per august and july
|
||||
});
|
||||
|
||||
it('does aggregate data in monthly entries before cutoff (subscribers)', () => {
|
||||
let h = generateHistory(396); // Jumps to September 2012
|
||||
let preened = preenHistory(_.cloneDeep(h), true, 0);
|
||||
expect(preened.length).to.eql(367); // Keeps 365 days + 2 entries per october and september
|
||||
});
|
||||
|
||||
it('does aggregate data in monthly and yearly entries before cutoff (free users)', () => {
|
||||
let h = generateHistory(731); // Jumps to October 21 2012
|
||||
let preened = preenHistory(_.cloneDeep(h), false, 0);
|
||||
expect(preened.length).to.eql(73); // Keeps 60 days + 11 montly entries and 2 yearly entry for 2011 and 2012
|
||||
});
|
||||
|
||||
it('does aggregate data in monthly and yearly entries before cutoff (subscribers)', () => {
|
||||
let h = generateHistory(1031); // Jumps to October 21 2012
|
||||
let preened = preenHistory(_.cloneDeep(h), true, 0);
|
||||
expect(preened.length).to.eql(380); // Keeps 365 days + 13 montly entries and 2 yearly entries for 2011 and 2010
|
||||
});
|
||||
|
||||
it('correctly aggregates values', () => {
|
||||
let h = generateHistory(63); // Compress last 3 days
|
||||
let preened = preenHistory(_.cloneDeep(h), false, 0);
|
||||
expect(preened[0].value).to.eql((61 + 62 + 63) / 3);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user