package helpers import ( "testing" "time" "github.com/brianvoe/gofakeit/v6" "github.com/stretchr/testify/assert" ) func Test_ParseDateTime(t *testing.T) { var ( year = gofakeit.Year() month = gofakeit.Month() day = gofakeit.Day() hour = gofakeit.Hour() minute = gofakeit.Minute() second = gofakeit.Second() correctRes = time.Date(year, time.Month(month), day, hour, minute, second, 0, time.UTC) correctReq = correctRes.Format("2006-01-02 15:04:05") incorrectReq = gofakeit.Word() ) tests := []struct { name string req string res time.Time err error }{ { name: "positive case", req: correctReq, err: nil, res: correctRes, }, { name: "negative case", req: incorrectReq, err: &time.ParseError{Layout: "2006-01-02 15:04:05", Value: incorrectReq, LayoutElem: "2006", ValueElem: incorrectReq, Message: ""}, res: time.Time{}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { res, err := ParseDateTime(tt.req) assert.Equal(t, tt.res, res) assert.Equal(t, tt.err, err) }) } } func Test_FormatDateForm(t *testing.T) { var ( year = gofakeit.Year() month = gofakeit.Month() day = gofakeit.Day() hour = gofakeit.Hour() minute = gofakeit.Minute() second = gofakeit.Second() correctReq = time.Date(year, time.Month(month), day, hour, minute, second, 0, time.UTC) correctRes = correctReq.Format("2006-01-02") ) tests := []struct { name string req time.Time res string }{ { name: "positive case", req: correctReq, res: correctRes, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { res := FormatDateForm(tt.req) assert.Equal(t, tt.res, res) }) } } func Test_FormatDateStr(t *testing.T) { var ( year = gofakeit.Year() month = gofakeit.Month() day = gofakeit.Day() hour = gofakeit.Hour() minute = gofakeit.Minute() second = gofakeit.Second() correctReq = time.Date(year, time.Month(month), day, hour, minute, second, 0, time.UTC) correctRes = correctReq.Format("2") + " " + getMonthStr(correctReq.Format("01")) + " " + correctReq.Format("2006") ) tests := []struct { name string req time.Time res string }{ { name: "positive case", req: correctReq, res: correctRes, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { res := FormatDateStr(tt.req) assert.Equal(t, tt.res, res) }) } } func Test_GetMonthStr(t *testing.T) { tests := []struct { name string req string res string }{ { name: "Jan", req: "01", res: "января", }, { name: "Feb", req: "02", res: "февраля", }, { name: "Mar", req: "03", res: "марта", }, { name: "Apr", req: "04", res: "апреля", }, { name: "May", req: "05", res: "мая", }, { name: "Jun", req: "06", res: "июня", }, { name: "Jul", req: "07", res: "июля", }, { name: "Aug", req: "08", res: "августа", }, { name: "Sep", req: "09", res: "сентября", }, { name: "Oct", req: "10", res: "октября", }, { name: "Nov", req: "11", res: "ноября", }, { name: "Dec", req: "12", res: "декабря", }, { name: "Empty", req: "", res: "", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { res := getMonthStr(tt.req) assert.Equal(t, tt.res, res) }) } }