Files
worker/app/models/notification_test.go
Gleb Tv 2c7a0236da feat: publish standalone worker
Separate worker packaging and service lifecycle from the control plane.
2026-07-13 17:55:14 +03:00

129 строки
4.3 KiB
Go

package models
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
mondayFriday int
allDays int
)
var time1, time3, time8, time9, time10, time23 time.Time
func init() {
// database.ConfigFile = "." + database.ConfigFile
// database.Init()
// Migrate()
// Drop()
mondayFriday = 0
mondayFriday = SetBit(mondayFriday, 0)
mondayFriday = SetBit(mondayFriday, 1)
mondayFriday = SetBit(mondayFriday, 2)
mondayFriday = SetBit(mondayFriday, 3)
mondayFriday = SetBit(mondayFriday, 4)
allDays = 0
allDays = SetBit(allDays, 0)
allDays = SetBit(allDays, 1)
allDays = SetBit(allDays, 2)
allDays = SetBit(allDays, 3)
allDays = SetBit(allDays, 4)
allDays = SetBit(allDays, 5)
allDays = SetBit(allDays, 6)
time1 = time.Date(2019, time.May, 26, 1, 0, 0, 0, time.Local)
time3 = time.Date(2019, time.May, 26, 3, 0, 0, 0, time.Local)
time8 = time.Date(2019, time.May, 26, 8, 0, 0, 0, time.Local)
time9 = time.Date(2019, time.May, 26, 9, 0, 0, 0, time.Local)
time10 = time.Date(2019, time.May, 26, 10, 0, 0, 0, time.Local)
time23 = time.Date(2019, time.May, 26, 23, 0, 0, 0, time.Local)
}
func TestNotificationEnabledNowHolidays(t *testing.T) {
n := &Notification{}
n.NotifyDays = &allDays
n.ID = 1
n.NotifyHolidays = true
holiday := time.Date(2019, time.January, 1, 0, 0, 0, 0, time.Local)
assert.Equal(t, true, n.EnabledNow(&holiday), "notification by default should be enabled on holidays")
n.ID = 2
n.NotifyHolidays = false
assert.Equal(t, false, n.EnabledNow(&holiday), "notification with NotifyHolidays=fasle should not be enabled on holiday")
}
func TestNotificationEnabledNowMondayFriday(t *testing.T) {
n := &Notification{}
n.NotifyHolidays = true
weekend := time.Date(2019, time.May, 26, 0, 0, 0, 0, time.Local)
weekday := time.Date(2019, time.May, 27, 0, 0, 0, 0, time.Local)
n.ID = 3
n.NotifyDays = &mondayFriday
assert.Equal(t, false, n.EnabledNow(&weekend), "notification mon-fri should not be enabled on sunday")
assert.Equal(t, true, n.EnabledNow(&weekday), "notification mon-fri should be enabled on monday")
}
func TestNotificationEnabledNowAllDays(t *testing.T) {
n := &Notification{}
n.NotifyHolidays = true
weekend := time.Date(2019, time.May, 26, 0, 0, 0, 0, time.Local)
weekday := time.Date(2019, time.May, 27, 0, 0, 0, 0, time.Local)
n.ID = 4
n.NotifyDays = &allDays
assert.Equal(t, true, n.EnabledNow(&weekend), "notification mon-sat should be enabled on sunday")
assert.Equal(t, true, n.EnabledNow(&weekday), "notification mon-sat should be enabled on monday")
}
func TestNotificationEnabledNowNormal(t *testing.T) {
n := &Notification{}
n.NotifyDays = &allDays
n.NotifyHolidays = true
// 9am - 18pm
n.ID = 5
start := 9 * 3600
end := 18 * 3600
n.NotifyDayStart = &start
n.NotifyDayEnd = &end
assert.Equal(t, false, n.EnabledNow(&time1), "notification should not be enabled outside day")
assert.Equal(t, false, n.EnabledNow(&time3), "notification should not be enabled outside day")
assert.Equal(t, false, n.EnabledNow(&time8), "notification should not be enabled outside day")
assert.Equal(t, true, n.EnabledNow(&time9), "notification should be enabled inside day")
assert.Equal(t, true, n.EnabledNow(&time10), "notification should be enabled inside day")
assert.Equal(t, false, n.EnabledNow(&time23), "notification should not be enabled outside day")
}
// TestNotificationEnabledNowRollover tests time range that crosses midnight
//
//nolint:dupl // Test structure similar to TestNotificationEnabledNowNormal but tests different behavior (rollover vs normal time range)
func TestNotificationEnabledNowRollover(t *testing.T) {
n := &Notification{}
n.NotifyDays = &allDays
n.NotifyHolidays = true
// 9am - 2am
n.ID = 6
start := 9 * 3600
end := 2 * 3600
n.NotifyDayStart = &start
n.NotifyDayEnd = &end
assert.Equal(t, true, n.EnabledNow(&time1), "notification should be enabled inside day")
assert.Equal(t, false, n.EnabledNow(&time3), "notification should not be enabled outside day")
assert.Equal(t, false, n.EnabledNow(&time8), "notification should not be enabled outside day")
assert.Equal(t, true, n.EnabledNow(&time9), "notification should be enabled inside day")
assert.Equal(t, true, n.EnabledNow(&time10), "notification should be enabled inside day")
assert.Equal(t, true, n.EnabledNow(&time23), "notification should be enabled inside day")
}