github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekatime/calendar_test.go (about)

     1  // Copyright © 2021. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekatime_test
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/qioalice/ekago/v3/ekatime"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestCalendar2015(t *testing.T) {
    18  
    19  	const YEAR = ekatime.Year(2015)
    20  
    21  	cal := ekatime.NewCalendar(YEAR, true, false)
    22  
    23  	type (
    24  		T1 struct {
    25  			d        ekatime.Day
    26  			isDayOff bool
    27  		}
    28  		T0 struct {
    29  			m  ekatime.Month
    30  			ds []T1
    31  		}
    32  	)
    33  
    34  	for _, r := range []T0{
    35  		{ekatime.MONTH_JANUARY, []T1{{1, true}, {2, true}, {5, true}, {6, true}, {7, true}, {8, true}, {9, true}}},
    36  		{ekatime.MONTH_FEBRUARY, []T1{{23, true}}},
    37  		{ekatime.MONTH_MARCH, []T1{{9, true}}},
    38  		{ekatime.MONTH_MAY, []T1{{1, true}, {4, true}, {11, true}}},
    39  		{ekatime.MONTH_JUNE, []T1{{12, true}}},
    40  		{ekatime.MONTH_NOVEMBER, []T1{{4, true}}},
    41  	} {
    42  		for _, d := range r.ds {
    43  			dd := ekatime.NewDate(YEAR, r.m, d.d)
    44  			cal.OverrideDate(dd, d.isDayOff)
    45  
    46  			assert.True(t, cal.IsDayOff(dd) == d.isDayOff,
    47  				"DD: %s, DayOff: %t", dd.String(), d.isDayOff)
    48  		}
    49  	}
    50  
    51  	workDays := []ekatime.Days{15, 19, 21, 22, 18, 21, 23, 21, 22, 22, 20, 23}
    52  	for i, workDays := range workDays {
    53  		m := ekatime.Month(i + 1)
    54  		assert.Equal(t, workDays, cal.WorkDaysCount(m),
    55  			"M: %s, WorkDays: %v", m.String(), cal.WorkDays(m))
    56  	}
    57  
    58  	daysOff := []ekatime.Days{16, 9, 10, 8, 13, 9, 8, 10, 8, 9, 10, 8}
    59  	for i, daysOff := range daysOff {
    60  		m := ekatime.Month(i + 1)
    61  		assert.Equal(t, daysOff, cal.DaysOffCount(m),
    62  			"M: %s, DaysOff: %v", m.String(), cal.DaysOff(m))
    63  	}
    64  }
    65  
    66  func TestCalendar2020(t *testing.T) {
    67  
    68  	const YEAR = ekatime.Year(2020)
    69  
    70  	cal := ekatime.NewCalendar(YEAR, true, false)
    71  
    72  	type (
    73  		T1 struct {
    74  			d        ekatime.Day
    75  			isDayOff bool
    76  		}
    77  		T0 struct {
    78  			m  ekatime.Month
    79  			ds []T1
    80  		}
    81  	)
    82  
    83  	for _, r := range []T0{
    84  		{ekatime.MONTH_JANUARY, []T1{{1, true}, {2, true}, {3, true}, {6, true}, {7, true}, {8, true}}},
    85  		{ekatime.MONTH_FEBRUARY, []T1{{24, true}}},
    86  		{ekatime.MONTH_MARCH, []T1{{9, true}, {30, true}, {31, true}}},
    87  		{ekatime.MONTH_MAY, []T1{{1, true}, {4, true}, {5, true}, {6, true}, {7, true}, {8, true}, {11, true}}},
    88  		{ekatime.MONTH_JUNE, []T1{{12, true}, {24, true}}},
    89  		{ekatime.MONTH_JULY, []T1{{1, true}}},
    90  		{ekatime.MONTH_NOVEMBER, []T1{{4, true}}},
    91  	} {
    92  		for _, d := range r.ds {
    93  			dd := ekatime.NewDate(YEAR, r.m, d.d)
    94  			cal.OverrideDate(dd, d.isDayOff)
    95  
    96  			assert.True(t, cal.IsDayOff(dd) == d.isDayOff,
    97  				"DD: %s, DayOff: %t", dd.String(), d.isDayOff)
    98  		}
    99  	}
   100  
   101  	for d, n := ekatime.Day(1), ekatime.Day(30); d <= n; d++ {
   102  		dd := ekatime.NewDate(YEAR, ekatime.MONTH_APRIL, d)
   103  		cal.OverrideDate(dd, true)
   104  
   105  		assert.True(t, cal.IsDayOff(dd),
   106  			"DD: %s, DayOff: %t", dd.String(), true)
   107  	}
   108  
   109  	workDays := []ekatime.Days{17, 19, 19, 0, 14, 20, 22, 21, 22, 22, 20, 23}
   110  	for i, workDays := range workDays {
   111  		m := ekatime.Month(i + 1)
   112  		assert.Equal(t, workDays, cal.WorkDaysCount(m),
   113  			"M: %s, WorkDays: %v", m.String(), cal.WorkDays(m))
   114  	}
   115  
   116  	daysOff := []ekatime.Days{14, 10, 12, 30, 17, 10, 9, 10, 8, 9, 10, 8}
   117  	for i, daysOff := range daysOff {
   118  		m := ekatime.Month(i + 1)
   119  		assert.Equal(t, daysOff, cal.DaysOffCount(m),
   120  			"M: %s, DaysOff: %v", m.String(), cal.DaysOff(m))
   121  	}
   122  }
   123  
   124  func TestCalendar2021(t *testing.T) {
   125  
   126  	const YEAR = ekatime.Year(2021)
   127  
   128  	cal := ekatime.NewCalendar(YEAR, true, false)
   129  
   130  	type (
   131  		T1 struct {
   132  			d        ekatime.Day
   133  			isDayOff bool
   134  		}
   135  		T0 struct {
   136  			m  ekatime.Month
   137  			ds []T1
   138  		}
   139  	)
   140  
   141  	for _, r := range []T0{
   142  		{ekatime.MONTH_JANUARY, []T1{{1, true}, {4, true}, {5, true}, {6, true}, {7, true}, {8, true}}},
   143  		{ekatime.MONTH_FEBRUARY, []T1{{20, false}, {22, true}, {23, true}}},
   144  		{ekatime.MONTH_MARCH, []T1{{8, true}}},
   145  		{ekatime.MONTH_MAY, []T1{{3, true}, {4, true}, {5, true}, {6, true}, {7, true}, {10, true}}},
   146  		{ekatime.MONTH_JUNE, []T1{{14, true}}},
   147  		{ekatime.MONTH_NOVEMBER, []T1{{4, true}, {5, true}}},
   148  		{ekatime.MONTH_DECEMBER, []T1{{31, true}}},
   149  	} {
   150  		for _, d := range r.ds {
   151  			dd := ekatime.NewDate(YEAR, r.m, d.d)
   152  			cal.OverrideDate(dd, d.isDayOff)
   153  
   154  			assert.True(t, cal.IsDayOff(dd) == d.isDayOff,
   155  				"DD: %s, DayOff: %t", dd.String(), d.isDayOff)
   156  		}
   157  	}
   158  
   159  	encodedBinary, err := cal.MarshalBinary()
   160  	require.NoError(t, err)
   161  
   162  	cal = ekatime.NewCalendar(1991, true, false)
   163  	err = cal.UnmarshalBinary(encodedBinary)
   164  	require.NoError(t, err)
   165  
   166  	workDays := []ekatime.Days{15, 19, 22, 22, 15, 21, 22, 22, 22, 21, 20, 22}
   167  	for i, workDays := range workDays {
   168  		m := ekatime.Month(i + 1)
   169  		assert.Equal(t, workDays, cal.WorkDaysCount(m),
   170  			"M: %s, WorkDays: %v", m.String(), cal.WorkDays(m))
   171  	}
   172  
   173  	encodedText, err := cal.MarshalText()
   174  	require.NoError(t, err)
   175  
   176  	cal = ekatime.NewCalendar(2031, true, false)
   177  	err = cal.UnmarshalText(encodedText)
   178  	require.NoError(t, err)
   179  
   180  	daysOff := []ekatime.Days{16, 9, 9, 8, 16, 9, 9, 9, 8, 10, 10, 9}
   181  	for i, daysOff := range daysOff {
   182  		m := ekatime.Month(i + 1)
   183  		assert.Equal(t, daysOff, cal.DaysOffCount(m),
   184  			"M: %s, DaysOff: %v", m.String(), cal.DaysOff(m))
   185  	}
   186  }
   187  
   188  func TestCalendar_EventDescription(t *testing.T) {
   189  
   190  	//goland:noinspection GoSnakeCaseUsage
   191  	const (
   192  		YEAR              = 2021
   193  		DAY_OF_YEAR       = 145
   194  		EVENT_ID          = 42
   195  		EVENT_DESCRIPTION = "some event"
   196  	)
   197  
   198  	cal := ekatime.NewCalendar(YEAR, true, true)
   199  
   200  	dd := ekatime.NewDateFromDayOfYear(YEAR, DAY_OF_YEAR)
   201  	ev := ekatime.NewEvent(dd, EVENT_ID, true)
   202  
   203  	cal.AddEvent(ev)
   204  	cal.AddEventDescription(ev.ID(), EVENT_DESCRIPTION)
   205  
   206  	require.True(t, cal.EventOfDate(dd).IsValid())
   207  	require.Equal(t, ev.ID(), cal.EventOfDate(dd).ID())
   208  	require.Equal(t, EVENT_DESCRIPTION, cal.DescriptionOfEvent(ev.ID()))
   209  
   210  	dd = ekatime.NewDateFromDayOfYear(YEAR, DAY_OF_YEAR-1)
   211  	require.False(t, cal.EventOfDate(dd).IsValid())
   212  
   213  	dd = ekatime.NewDateFromDayOfYear(YEAR, DAY_OF_YEAR+1)
   214  	require.False(t, cal.EventOfDate(dd).IsValid())
   215  
   216  	require.Empty(t, cal.DescriptionOfEvent(EVENT_ID-1))
   217  	require.Empty(t, cal.DescriptionOfEvent(EVENT_ID+1))
   218  }