github.com/martinohmann/rfoutlet@v1.2.1-0.20220707195255-8a66aa411105/internal/schedule/interval_test.go (about)

     1  package schedule_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/martinohmann/rfoutlet/internal/schedule"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestIntervalContains(t *testing.T) {
    13  	tests := []struct {
    14  		i        schedule.Interval
    15  		t        time.Time
    16  		expected bool
    17  	}{
    18  		// From > To
    19  		{
    20  			i: schedule.Interval{
    21  				Enabled:  true,
    22  				Weekdays: []time.Weekday{time.Monday},
    23  				From:     schedule.NewDayTime(0, 0),
    24  				To:       schedule.NewDayTime(3, 0),
    25  			},
    26  			t:        time.Date(2018, 11, 5, 0, 0, 0, 0, time.UTC), // it's a monday
    27  			expected: true,
    28  		},
    29  		{
    30  			i: schedule.Interval{
    31  				Enabled:  false,
    32  				Weekdays: []time.Weekday{time.Monday},
    33  				From:     schedule.NewDayTime(0, 0),
    34  				To:       schedule.NewDayTime(3, 0),
    35  			},
    36  			t:        time.Date(2018, 11, 5, 0, 0, 0, 0, time.UTC), // it's a monday
    37  			expected: false,
    38  		},
    39  		{
    40  			i: schedule.Interval{
    41  				Enabled:  false,
    42  				Weekdays: []time.Weekday{time.Monday},
    43  				From:     schedule.NewDayTime(0, 0),
    44  				To:       schedule.NewDayTime(3, 0),
    45  			},
    46  			t:        time.Date(2018, 11, 5, 23, 59, 0, 0, time.UTC), // it's a monday
    47  			expected: false,
    48  		},
    49  		{
    50  			i: schedule.Interval{
    51  				Enabled:  false,
    52  				Weekdays: []time.Weekday{time.Monday},
    53  				From:     schedule.NewDayTime(0, 0),
    54  				To:       schedule.NewDayTime(3, 0),
    55  			},
    56  			t:        time.Date(2018, 11, 5, 1, 30, 0, 0, time.UTC), // it's a monday
    57  			expected: false,
    58  		},
    59  		{
    60  			i: schedule.Interval{
    61  				Enabled:  true,
    62  				Weekdays: []time.Weekday{time.Monday},
    63  				From:     schedule.NewDayTime(0, 0),
    64  				To:       schedule.NewDayTime(3, 0),
    65  			},
    66  			t:        time.Date(2018, 11, 6, 0, 0, 0, 0, time.UTC), // it's a tuesday
    67  			expected: false,
    68  		},
    69  		{
    70  			i: schedule.Interval{
    71  				Enabled:  true,
    72  				Weekdays: []time.Weekday{time.Monday, time.Tuesday},
    73  				From:     schedule.NewDayTime(0, 0),
    74  				To:       schedule.NewDayTime(3, 0),
    75  			},
    76  			t:        time.Date(2018, 11, 6, 0, 0, 0, 0, time.UTC), // it's a tuesday
    77  			expected: true,
    78  		},
    79  		{
    80  			i: schedule.Interval{
    81  				Enabled:  true,
    82  				Weekdays: []time.Weekday{time.Monday, time.Tuesday},
    83  				From:     schedule.NewDayTime(0, 0),
    84  				To:       schedule.NewDayTime(3, 0),
    85  			},
    86  			t:        time.Date(2018, 11, 6, 3, 0, 0, 0, time.UTC), // it's a tuesday
    87  			expected: false,
    88  		},
    89  		{
    90  			i: schedule.Interval{
    91  				Enabled:  true,
    92  				Weekdays: []time.Weekday{time.Monday, time.Tuesday},
    93  				From:     schedule.NewDayTime(0, 0),
    94  				To:       schedule.NewDayTime(3, 0),
    95  			},
    96  			t:        time.Date(2018, 11, 6, 2, 59, 0, 0, time.UTC), // it's a tuesday
    97  			expected: true,
    98  		},
    99  		{
   100  			i: schedule.Interval{
   101  				Enabled:  true,
   102  				Weekdays: []time.Weekday{time.Monday, time.Tuesday},
   103  				From:     schedule.NewDayTime(0, 0),
   104  				To:       schedule.NewDayTime(3, 0),
   105  			},
   106  			t:        time.Date(2018, 11, 6, 3, 1, 0, 0, time.UTC), // it's a tuesday
   107  			expected: false,
   108  		},
   109  		// From < To
   110  		{
   111  			i: schedule.Interval{
   112  				Enabled:  true,
   113  				Weekdays: []time.Weekday{time.Tuesday},
   114  				From:     schedule.NewDayTime(3, 0),
   115  				To:       schedule.NewDayTime(0, 0),
   116  			},
   117  			t:        time.Date(2018, 11, 6, 6, 35, 0, 0, time.UTC), // it's a tuesday
   118  			expected: true,
   119  		},
   120  		{
   121  			i: schedule.Interval{
   122  				Enabled:  true,
   123  				Weekdays: []time.Weekday{time.Tuesday},
   124  				From:     schedule.NewDayTime(3, 0),
   125  				To:       schedule.NewDayTime(0, 0),
   126  			},
   127  			t:        time.Date(2018, 11, 6, 3, 0, 0, 0, time.UTC), // it's a tuesday
   128  			expected: true,
   129  		},
   130  		{
   131  			i: schedule.Interval{
   132  				Enabled:  true,
   133  				Weekdays: []time.Weekday{time.Tuesday},
   134  				From:     schedule.NewDayTime(3, 0),
   135  				To:       schedule.NewDayTime(0, 0),
   136  			},
   137  			t:        time.Date(2018, 11, 6, 0, 0, 0, 0, time.UTC), // it's a tuesday
   138  			expected: false,
   139  		},
   140  		{
   141  			i: schedule.Interval{
   142  				Enabled:  true,
   143  				Weekdays: []time.Weekday{time.Tuesday},
   144  				From:     schedule.NewDayTime(3, 0),
   145  				To:       schedule.NewDayTime(0, 0),
   146  			},
   147  			t:        time.Date(2018, 11, 6, 2, 59, 0, 0, time.UTC), // it's a tuesday
   148  			expected: false,
   149  		},
   150  		{
   151  			i: schedule.Interval{
   152  				Enabled:  true,
   153  				Weekdays: []time.Weekday{time.Tuesday},
   154  				From:     schedule.NewDayTime(3, 0),
   155  				To:       schedule.NewDayTime(0, 0),
   156  			},
   157  			t:        time.Date(2018, 11, 6, 0, 1, 0, 0, time.UTC), // it's a tuesday
   158  			expected: false,
   159  		},
   160  		{
   161  			i: schedule.Interval{
   162  				Enabled:  true,
   163  				Weekdays: []time.Weekday{time.Tuesday},
   164  				From:     schedule.NewDayTime(3, 0),
   165  				To:       schedule.NewDayTime(0, 0),
   166  			},
   167  			t:        time.Date(2018, 11, 6, 23, 59, 0, 0, time.UTC), // it's a tuesday
   168  			expected: true,
   169  		},
   170  		{
   171  			i: schedule.Interval{
   172  				Enabled:  true,
   173  				Weekdays: []time.Weekday{time.Tuesday},
   174  				From:     schedule.NewDayTime(3, 0),
   175  				To:       schedule.NewDayTime(0, 0),
   176  			},
   177  			t:        time.Date(2018, 11, 6, 3, 1, 0, 0, time.UTC), // it's a tuesday
   178  			expected: true,
   179  		},
   180  		// From == To
   181  		{
   182  			i: schedule.Interval{
   183  				Enabled:  true,
   184  				Weekdays: []time.Weekday{time.Tuesday},
   185  				From:     schedule.NewDayTime(0, 0),
   186  				To:       schedule.NewDayTime(0, 0),
   187  			},
   188  			t:        time.Date(2018, 11, 6, 0, 0, 0, 0, time.UTC), // it's a tuesday
   189  			expected: true,
   190  		},
   191  		{
   192  			i: schedule.Interval{
   193  				Enabled:  true,
   194  				Weekdays: []time.Weekday{time.Tuesday},
   195  				From:     schedule.NewDayTime(0, 0),
   196  				To:       schedule.NewDayTime(0, 0),
   197  			},
   198  			t:        time.Date(2018, 11, 6, 0, 1, 0, 0, time.UTC), // it's a tuesday
   199  			expected: false,
   200  		},
   201  		{
   202  			i: schedule.Interval{
   203  				Enabled:  true,
   204  				Weekdays: []time.Weekday{time.Tuesday},
   205  				From:     schedule.NewDayTime(0, 0),
   206  				To:       schedule.NewDayTime(0, 0),
   207  			},
   208  			t:        time.Date(2018, 11, 6, 23, 59, 0, 0, time.UTC), // it's a tuesday
   209  			expected: false,
   210  		},
   211  	}
   212  
   213  	for _, tt := range tests {
   214  		assert.Equal(t, tt.expected, tt.i.Contains(tt.t),
   215  			fmt.Sprintf("i=%v, t=%v", tt.i, tt.t))
   216  	}
   217  }