summaryrefslogtreecommitdiff
path: root/test/logger/test_logperiod.rb
blob: 5c5318ac955d961f7515c0379ebe22bf32a6715d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# coding: US-ASCII
# frozen_string_literal: false
require_relative 'helper'
require 'time'

class TestLogPeriod < Test::Unit::TestCase
  def test_next_rotate_time
    time = Time.parse("2019-07-18 13:52:02")

    daily_result = Logger::Period.next_rotate_time(time, 'daily')
    next_day = Time.parse("2019-07-19 00:00:00")
    assert_equal(next_day, daily_result)

    weekly_result = Logger::Period.next_rotate_time(time, 'weekly')
    next_week = Time.parse("2019-07-21 00:00:00")
    assert_equal(next_week, weekly_result)

    monthly_result = Logger::Period.next_rotate_time(time, 'monthly')
    next_month = Time.parse("2019-08-1 00:00:00")
    assert_equal(next_month, monthly_result)

    result = Logger::Period.next_rotate_time(time, 'invalid')
    assert_equal(time, result)
  end

  def test_next_rotate_time_extreme_cases
    # First day of Month and Saturday
    time = Time.parse("2018-07-01 00:00:00")

    daily_result = Logger::Period.next_rotate_time(time, 'daily')
    next_day = Time.parse("2018-07-02 00:00:00")
    assert_equal(next_day, daily_result)

    weekly_result = Logger::Period.next_rotate_time(time, 'weekly')
    next_week = Time.parse("2018-07-08 00:00:00")
    assert_equal(next_week, weekly_result)

    monthly_result = Logger::Period.next_rotate_time(time, 'monthly')
    next_month = Time.parse("2018-08-1 00:00:00")
    assert_equal(next_month, monthly_result)

    result = Logger::Period.next_rotate_time(time, 'invalid')
    assert_equal(time, result)
  end

  def test_previous_period_end
    time = Time.parse("2019-07-18 13:52:02")

    daily_result = Logger::Period.previous_period_end(time, 'daily')
    day_ago = Time.parse("2019-07-17 23:59:59")
    assert_equal(day_ago, daily_result)

    weekly_result = Logger::Period.previous_period_end(time, 'weekly')
    week_ago = Time.parse("2019-07-13 23:59:59")
    assert_equal(week_ago, weekly_result)

    monthly_result = Logger::Period.previous_period_end(time, 'monthly')
    month_ago = Time.parse("2019-06-30 23:59:59")
    assert_equal(month_ago, monthly_result)

    result = Logger::Period.previous_period_end(time, 'invalid')
    assert_equal(time, result)
  end

  def test_previous_period_end_extreme_cases
    # First day of Month and Saturday
    time = Time.parse("2018-07-01 00:00:00")

    daily_result = Logger::Period.previous_period_end(time, 'daily')
    day_ago = Time.parse("2018-06-30 23:59:59")
    assert_equal(day_ago, daily_result)

    weekly_result = Logger::Period.previous_period_end(time, 'weekly')
    week_ago = Time.parse("2018-06-30 23:59:59")
    assert_equal(week_ago, weekly_result)

    monthly_result = Logger::Period.previous_period_end(time, 'monthly')
    month_ago = Time.parse("2018-06-30 23:59:59")
    assert_equal(month_ago, monthly_result)

    result = Logger::Period.previous_period_end(time, 'invalid')
    assert_equal(time, result)
  end
end