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

     1  package schedule
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestScheduleEnabled(t *testing.T) {
    12  	tests := []struct {
    13  		is       []Interval
    14  		expected bool
    15  	}{
    16  		{
    17  			is: []Interval{
    18  				{
    19  					Enabled: true,
    20  				},
    21  				{
    22  					Enabled: true,
    23  				},
    24  			},
    25  			expected: true,
    26  		},
    27  		{
    28  			is: []Interval{
    29  				{
    30  					Enabled: true,
    31  				},
    32  				{
    33  					Enabled: false,
    34  				},
    35  			},
    36  			expected: true,
    37  		},
    38  		{
    39  			is: []Interval{
    40  				{
    41  					Enabled: false,
    42  				},
    43  				{
    44  					Enabled: true,
    45  				},
    46  			},
    47  			expected: true,
    48  		},
    49  		{
    50  			is: []Interval{
    51  				{
    52  					Enabled: false,
    53  				},
    54  				{
    55  					Enabled: false,
    56  				},
    57  			},
    58  			expected: false,
    59  		},
    60  	}
    61  
    62  	for _, tt := range tests {
    63  		s := NewWithIntervals(tt.is)
    64  		assert.Equal(t, tt.expected, s.Enabled(), fmt.Sprintf("is=%v", tt.is))
    65  	}
    66  }
    67  
    68  func TestScheduleContains(t *testing.T) {
    69  	tests := []struct {
    70  		is       []Interval
    71  		t        time.Time
    72  		expected bool
    73  	}{
    74  		{
    75  			is: []Interval{
    76  				{
    77  					Enabled:  true,
    78  					Weekdays: []time.Weekday{time.Monday},
    79  					From:     NewDayTime(0, 0),
    80  					To:       NewDayTime(3, 0),
    81  				},
    82  			},
    83  			t:        time.Date(2018, 11, 5, 0, 0, 0, 0, time.UTC), // it's a monday
    84  			expected: true,
    85  		},
    86  		{
    87  			is: []Interval{
    88  				{
    89  					Enabled:  true,
    90  					Weekdays: []time.Weekday{time.Tuesday},
    91  					From:     NewDayTime(0, 0),
    92  					To:       NewDayTime(3, 0),
    93  				},
    94  			},
    95  			t:        time.Date(2018, 11, 5, 0, 0, 0, 0, time.UTC), // it's a monday
    96  			expected: false,
    97  		},
    98  		{
    99  			is: []Interval{
   100  				{
   101  					Enabled:  false,
   102  					Weekdays: []time.Weekday{time.Monday},
   103  					From:     NewDayTime(0, 0),
   104  					To:       NewDayTime(3, 0),
   105  				},
   106  			},
   107  			t:        time.Date(2018, 11, 5, 0, 0, 0, 0, time.UTC), // it's a monday
   108  			expected: false,
   109  		},
   110  	}
   111  
   112  	for _, tt := range tests {
   113  		s := NewWithIntervals(tt.is)
   114  		assert.Equal(t, tt.expected, s.Contains(tt.t), fmt.Sprintf("is=%v, t=%v", tt.is, tt.t))
   115  	}
   116  }
   117  
   118  func TestAddInterval(t *testing.T) {
   119  	s := New()
   120  	i := Interval{}
   121  
   122  	err := s.AddInterval(i)
   123  
   124  	assert.NoError(t, err)
   125  	assert.Len(t, s.intervals, 1)
   126  }
   127  
   128  func TestDeleteInterval(t *testing.T) {
   129  	i := Interval{ID: "foo"}
   130  	s := NewWithIntervals([]Interval{i})
   131  
   132  	err := s.DeleteInterval(i)
   133  
   134  	assert.NoError(t, err)
   135  	assert.Len(t, s.intervals, 0)
   136  }
   137  
   138  func TestUpdateInterval(t *testing.T) {
   139  	i := Interval{ID: "foo", Enabled: false}
   140  	s := NewWithIntervals([]Interval{i})
   141  
   142  	i2 := Interval{ID: "foo", Enabled: true}
   143  
   144  	err := s.UpdateInterval(i2)
   145  
   146  	assert.NoError(t, err)
   147  	assert.Equal(t, true, s.intervals[0].Enabled)
   148  }