Current and Longest streak calculation fixed
Minor refactoring and unit tests added After CR: user-stats file moved to util folder, export keywork added to exported functions, new line added at the end of gulp file User-stats-test file moved to replicate user-stats path in test folder
This commit is contained in:
34
test/server/utils/date-utils-test.js
Normal file
34
test/server/utils/date-utils-test.js
Normal file
@@ -0,0 +1,34 @@
|
||||
import moment from 'moment';
|
||||
|
||||
import { dayCount } from '../../../server/utils/date-utils';
|
||||
|
||||
let test = require('tape');
|
||||
|
||||
test('Day count between two epochs (inclusive) calculation', function (t) {
|
||||
t.plan(5);
|
||||
|
||||
t.equal(dayCount([
|
||||
moment("8/3/2015 3:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
|
||||
]), 1, "should return 1 day given epochs of the same day");
|
||||
|
||||
t.equal(dayCount([
|
||||
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
|
||||
]), 1, "should return 1 day given same epochs");
|
||||
|
||||
t.equal(dayCount([
|
||||
moment("8/4/2015 2:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
|
||||
]), 2, "should return 2 days when there is a 24 hours difference between given dates");
|
||||
|
||||
t.equal(dayCount([
|
||||
moment("8/4/2015 1:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("8/3/2015 23:00", "M/D/YYYY H:mm").valueOf()
|
||||
]), 2, "should return 2 days when the diff is less than 24h but days of the month are different");
|
||||
|
||||
t.equal(dayCount([
|
||||
moment("10/27/2015 1:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("5/12/1982 1:00", "M/D/YYYY H:mm").valueOf()
|
||||
]), 12222, "should return correct count when there is very big period");
|
||||
});
|
132
test/server/utils/user-stats-test.js
Normal file
132
test/server/utils/user-stats-test.js
Normal file
@@ -0,0 +1,132 @@
|
||||
import moment from 'moment';
|
||||
|
||||
import { calcCurrentStreak, calcLongestStreak } from '../../../server/utils/user-stats';
|
||||
|
||||
let test = require('tape');
|
||||
|
||||
test('Current streak calculation', function (t) {
|
||||
t.plan(7);
|
||||
|
||||
t.equal(calcCurrentStreak([
|
||||
moment(moment().subtract(1, 'hours')).valueOf()
|
||||
]), 1, "should return 1 day when today one challenge was completed");
|
||||
|
||||
t.equal(calcCurrentStreak([
|
||||
moment(moment().subtract(2, 'hours')).valueOf(),
|
||||
moment(moment().subtract(1, 'hours')).valueOf()
|
||||
]), 1, "should return 1 day when today more than one challenge were completed");
|
||||
|
||||
t.equal(calcCurrentStreak([
|
||||
moment("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf()
|
||||
]), 0, "should return 0 day when today 0 challenges were completed");
|
||||
|
||||
t.equal(calcCurrentStreak([
|
||||
moment(moment().subtract(1, 'days')).valueOf(),
|
||||
moment(moment().subtract(1, 'hours')).valueOf()
|
||||
]), 2, "should return 2 days when today and yesterday challenges were completed");
|
||||
|
||||
t.equal(calcCurrentStreak([
|
||||
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/12/2015 2:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/12/2015 3:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/13/2015 4:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/14/2015 5:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment(moment().subtract(2, 'days')).valueOf(),
|
||||
moment(moment().subtract(1, 'days')).valueOf(),
|
||||
moment(moment().subtract(1, 'hours')).valueOf()
|
||||
]), 3, "should return 3 when today and for two days before user was activity");
|
||||
|
||||
t.equal(calcCurrentStreak([
|
||||
moment(moment().subtract(37, 'hours')).valueOf(),
|
||||
moment(moment().subtract(1, 'hours')).valueOf()
|
||||
]), 1, "should return 1 when between todays challenge completion and yesterdays there is a 1.5 day (36 hours) long break");
|
||||
|
||||
t.equal(calcCurrentStreak([
|
||||
moment(moment().subtract(35, 'hours')).valueOf(),
|
||||
moment(moment().subtract(1, 'hours')).valueOf()
|
||||
]), 2, "should return 2 days when between todays challenge completion and yesterdays there is less than 1.5 day (36 hours) long break");
|
||||
|
||||
});
|
||||
|
||||
test('Longest streak calculation', function (t) {
|
||||
t.plan(9);
|
||||
|
||||
t.equal(calcLongestStreak([
|
||||
moment("9/12/2015 4:00", "M/D/YYYY H:mm").valueOf()
|
||||
]), 1, "should return 1 when there is the only one one-day-long streak available");
|
||||
|
||||
t.equal(calcLongestStreak([
|
||||
moment("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/12/2015 2:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/13/2015 3:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/14/2015 1:00", "M/D/YYYY H:mm").valueOf()
|
||||
]), 4, "should return 4 when there is the only one more-than-one-days-long streak available");
|
||||
|
||||
t.equal(calcLongestStreak([
|
||||
moment(moment().subtract(1, 'hours')).valueOf()
|
||||
]), 1, "should return 1 when there is only one one-day-long streak and it is today");
|
||||
|
||||
t.equal(calcLongestStreak([
|
||||
moment(moment().subtract(1, 'days')).valueOf(),
|
||||
moment(moment().subtract(1, 'hours')).valueOf()
|
||||
]), 2, "should return 2 when yesterday and today makes longest streak");
|
||||
|
||||
t.equal(calcLongestStreak([
|
||||
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/12/2015 2:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("10/4/2015 1:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("10/5/2015 5:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("10/6/2015 4:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("10/7/2015 5:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("11/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
|
||||
]), 4, "should return 4 when there is a month long break");
|
||||
|
||||
t.equal(calcLongestStreak([
|
||||
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment(moment("9/12/2015 1:00", "M/D/YYYY H:mm").add(37, 'hours')).valueOf(),
|
||||
moment("9/14/2015 22:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/15/2015 4:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("10/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
|
||||
]), 3, "should return 3 when there is a more than 1.5 days long break of (36 hours)");
|
||||
|
||||
t.equal(calcLongestStreak([
|
||||
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/12/2015 2:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/12/2015 3:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/13/2015 4:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/14/2015 5:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment(moment().subtract(2, 'days')).valueOf(),
|
||||
moment(moment().subtract(1, 'days')).valueOf(),
|
||||
moment().valueOf()
|
||||
]), 4, "should return 4 when the longest streak consist of several same day timestamps");
|
||||
|
||||
t.equal(calcLongestStreak([
|
||||
moment("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/13/2015 4:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("9/14/2015 5:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("10/11/2015 4:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("10/12/2015 1:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("10/13/2015 4:00", "M/D/YYYY H:mm").valueOf(),
|
||||
moment("10/14/2015 5:00", "M/D/YYYY H:mm").valueOf()
|
||||
]), 4, "should return 4 when there are several longest streaks (same length)");
|
||||
|
||||
let cals = [];
|
||||
const n = 100;
|
||||
for (var i = 0; i < n; i++) {
|
||||
cals.push(moment(moment().subtract(i, 'days')).valueOf());
|
||||
}
|
||||
cals.sort();
|
||||
t.equal(calcLongestStreak(cals), n, "should return correct longest streak when there is a very long period");
|
||||
|
||||
});
|
Reference in New Issue
Block a user