123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- 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)
- })
- }
- }
|