Skip to content

Commit cdf322f

Browse files
committed
write some tests
1 parent 7ab90cd commit cdf322f

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

models/streaks.moon

+10-3
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class Streaks extends Model
313313
else
314314
error "don't know how to format date for rate"
315315

316-
-- move UTC date to closest unit start in UTC
316+
-- return new date to closest unit start in UTC, returns in UTC time
317317
truncate_date: (d) =>
318318
start = @start_datetime!
319319

@@ -351,6 +351,7 @@ class Streaks extends Model
351351
@increment_date_by_unit @current_unit!
352352

353353
-- UTC date to unit number
354+
-- NOTE: this does not verify that d is in valid date in streak
354355
unit_number_for_date: (d) =>
355356
@unit_span @start_datetime!, d
356357

@@ -579,21 +580,27 @@ class Streaks extends Model
579580

580581
-- each unit of the streak in UTC between (inclusive) the specified range
581582
each_unit_in_range: (range_left, range_right) =>
583+
range_right = date range_right
582584
current = @truncate_date range_left
583585
-- NOTE: truncate shifts before, so we increment once if the truncated
584586
-- date falls before our desired range
585587

586-
if current < range_left
588+
if current < date(range_left)
587589
current = @increment_date_by_unit current
588590

591+
-- don't show dates before the streak
592+
streak_start = @start_datetime!
593+
if current < streak_start
594+
current = streak_start
595+
589596
stop = @end_datetime!
590597

591598
limit = 1000
592599

593600
coroutine.wrap ->
594601
while true
595602
break if current > range_right
596-
break if stop and current > stop
603+
break if stop and current >= stop
597604

598605
limit -= 1
599606
if limit == 0

spec/models/streaks_spec.moon

+64-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ describe "models.streaks", ->
221221
{k, true for k in pairs user2\get_completed_units!}
222222

223223
describe "time functions", ->
224-
describe "daily", ->
224+
describe "daily unending", ->
225225
local streak
226226

227227
before_each ->
@@ -241,6 +241,69 @@ describe "models.streaks", ->
241241
some_date = streak\start_datetime!\addhours 2922
242242
assert.same 1, streak\unit_span some_date, some_date
243243

244+
describe "daily ending", ->
245+
local streak
246+
247+
before_each ->
248+
db = require "lapis.db"
249+
streak = factory.Streaks {
250+
start_date: "2019-1-10"
251+
end_date: "2019-1-20"
252+
hour_offset: -8
253+
rate: "daily"
254+
}
255+
256+
it "each_unit", ->
257+
units = [unit\fmt "%Y-%m-%d %H:%M:%S" for unit in streak\each_unit!]
258+
259+
assert.same {
260+
"2019-01-10 08:00:00",
261+
"2019-01-11 08:00:00",
262+
"2019-01-12 08:00:00",
263+
"2019-01-13 08:00:00",
264+
"2019-01-14 08:00:00",
265+
"2019-01-15 08:00:00",
266+
"2019-01-16 08:00:00",
267+
"2019-01-17 08:00:00",
268+
"2019-01-18 08:00:00",
269+
"2019-01-19 08:00:00"
270+
}, units
271+
272+
it "each_unit_in_range", ->
273+
do -- range before start and before end
274+
units = [unit\fmt "%Y-%m-%d %H:%M:%S" for unit in streak\each_unit_in_range "2019-1-1", "2019-1-15"]
275+
assert.same {
276+
"2019-01-10 08:00:00"
277+
"2019-01-11 08:00:00"
278+
"2019-01-12 08:00:00"
279+
"2019-01-13 08:00:00"
280+
"2019-01-14 08:00:00"
281+
}, units
282+
283+
do -- exact range should return all units
284+
units = [unit\fmt "%Y-%m-%d %H:%M:%S" for unit in streak\each_unit_in_range "2019-01-10 08:00:00", "2019-01-19 08:00:00"]
285+
assert.same {
286+
"2019-01-10 08:00:00",
287+
"2019-01-11 08:00:00",
288+
"2019-01-12 08:00:00",
289+
"2019-01-13 08:00:00",
290+
"2019-01-14 08:00:00",
291+
"2019-01-15 08:00:00",
292+
"2019-01-16 08:00:00",
293+
"2019-01-17 08:00:00",
294+
"2019-01-18 08:00:00",
295+
"2019-01-19 08:00:00"
296+
}, units
297+
298+
do -- range extends past end of streak
299+
units = [unit\fmt "%Y-%m-%d %H:%M:%S" for unit in streak\each_unit_in_range "2019-01-16", "2019-01-30"]
300+
assert.same {
301+
"2019-01-16 08:00:00",
302+
"2019-01-17 08:00:00",
303+
"2019-01-18 08:00:00",
304+
"2019-01-19 08:00:00"
305+
}, units
306+
244307
describe "weekly", ->
245308
local streak
246309

0 commit comments

Comments
 (0)