Modify runCron tests to use hoursAgo instead of daysAgo.

This commit also sets hoursAgo for many of the cases and restructures
how cronMatrix is parsed so that more specific settings may override
defaults higher up on the tree.
This commit is contained in:
Cole Gleason 2014-01-08 23:42:31 -06:00
parent 3bd81abda3
commit 393bbb1885

View file

@ -46,7 +46,7 @@ rewrapUser = (user)->
expectStrings = (obj, paths) ->
_.each paths, (path) -> expect(obj[path]).to.be.ok()
# options.daysAgo: days ago when the last cron was executed
# options.hoursAgo: hours since the last cron was executed
beforeAfter = (options={}) ->
user = newUser()
[before, after] = [user, _.cloneDeep(user)]
@ -57,9 +57,9 @@ beforeAfter = (options={}) ->
if options.limitOne
before["#{options.limitOne}s"] = [before["#{options.limitOne}s"][0]]
after["#{options.limitOne}s"] = [after["#{options.limitOne}s"][0]]
lastCron = +(moment(options.now || +new Date).subtract('days', options.daysAgo)) if options.daysAgo
lastCron = +(moment(options.now || +new Date).subtract('hours', options.hoursAgo)) unless options.hoursAgo == undefined
_.each [before,after], (obj) ->
obj.lastCron = lastCron if options.daysAgo
obj.lastCron = lastCron unless options.hoursAgo == undefined
{before:before, after:after}
#TODO calculate actual poins
@ -287,7 +287,7 @@ describe 'Cron', ->
# expect(paths.lastCron).to.be true # busted cron (was set to after today's date)
it 'only dailies & todos are effected', ->
{before,after} = beforeAfter({daysAgo:1})
{before,after} = beforeAfter({hoursAgo:24})
before.dailys = before.todos = after.dailys = after.todos = []
after.fns.cron()
expect(after.lastCron).to.not.be before.lastCron # make sure cron was run
@ -304,7 +304,7 @@ describe 'Cron', ->
@clock.restore()
it 'should preen user history', ->
{before,after} = beforeAfter({daysAgo:1})
{before,after} = beforeAfter({hoursAgo:24})
history = [
# Last year should be condensed to one entry, avg: 1
{date:'09/01/2012', value: 0}
@ -362,7 +362,7 @@ describe 'Cron', ->
describe 'Todos', ->
it '1 day missed', ->
{before,after} = beforeAfter({daysAgo:1})
{before,after} = beforeAfter({hoursAgo:24})
before.dailys = after.dailys = []
after.fns.cron()
@ -388,7 +388,7 @@ describe 'Cron', ->
runCron = (options) ->
_.each [480, 240, 0, -120], (timezoneOffset) -> # test different timezones
now = shared.startOfWeek({timezoneOffset}).add('hours', options.currentHour||0)
{before,after} = beforeAfter({now, timezoneOffset, daysAgo:1, dayStart:options.dayStart||0, limitOne:'daily'})
{before,after} = beforeAfter({now, timezoneOffset, hoursAgo:options.hoursAgo||0, dayStart:options.dayStart||0, limitOne:'daily'})
before.dailys[0].repeat = after.dailys[0].repeat = options.repeat if options.repeat
before.dailys[0].streak = after.dailys[0].streak = 10
before.dailys[0].completed = after.dailys[0].completed = true if options.checked
@ -405,7 +405,7 @@ describe 'Cron', ->
steps:
'due yesterday':
defaults: {daysAgo:1, limitOne: 'daily'}
defaults: {hoursAgo:24, limitOne: 'daily'}
steps:
'(simple)': {expect:'losePoints'}
@ -415,7 +415,7 @@ describe 'Cron', ->
defaults: {repeat:{su:1,m:true,t:1,w:1,th:1,f:1,s:1}}
steps:
'pre-dayStart':
defaults: {currentHour:3, dayStart:4, shouldDo:true}
defaults: {hoursAgo:2,currentHour:3, dayStart:4, shouldDo:true}
steps:
'checked': {checked: true, expect:'noChange'}
'un-checked': {checked: false, expect:'noChange'}
@ -429,7 +429,7 @@ describe 'Cron', ->
defaults: {repeat:{su:1,m:false,t:1,w:1,th:1,f:1,s:1}}
steps:
'pre-dayStart':
defaults: {currentHour:3, dayStart:4, shouldDo:true}
defaults: {hoursAgo:23, currentHour:3, dayStart:4, shouldDo:true}
steps:
'checked': {checked: true, expect:'noChange'}
'un-checked': {checked: false, expect:'noChange'}
@ -442,18 +442,18 @@ describe 'Cron', ->
'not due yesterday':
defaults: repeatWithoutLastWeekday()
steps:
'(simple)': {expect:'noDamage'}
'post-dayStart': {currentHour:5,dayStart:4, expect:'noDamage'}
'pre-dayStart': {currentHour:3, dayStart:4, expect:'noChange'}
'(simple)': {hoursAgo:24, expect:'noDamage'}
'post-dayStart': {hoursAgo:23,currentHour:5,dayStart:4, expect:'noDamage'}
'pre-dayStart': {hoursAgo:21, currentHour:3, dayStart:4, expect:'noChange'}
recurseCronMatrix = (obj, options={}) ->
if obj.steps
_.each obj.steps, (step, text) ->
o = _.cloneDeep options
o.text ?= ''; o.text += " #{text} "
recurseCronMatrix step, _.defaults(o,obj.defaults)
recurseCronMatrix step, _.assign(o, obj.defaults)
else
it "#{options.text}", -> runCron(_.defaults(obj,options))
it "#{options.text}", -> runCron(_.assign(options, obj))
recurseCronMatrix(cronMatrix)
it 'calculates day differences with dayStart properly', ->
@ -501,4 +501,4 @@ describe 'Helper', ->
pets = { "Wolf-Base": 2, "Wolf-Veteran": 1, "Wolf-Cerberus": 1, "Dragon-Hydra": 1}
expect(shared.countPets(null, pets)).to.eql 1
expect(shared.countPets(_.size(pets), pets)).to.eql 1
expect(shared.countPets(_.size(pets), pets)).to.eql 1