Dima 2 роки тому
батько
коміт
df3e33bf9c

+ 199 - 0
internal/helpers/datetime_test.go

@@ -0,0 +1,199 @@
+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)
+		})
+	}
+}

+ 41 - 0
internal/helpers/trans_test.go

@@ -0,0 +1,41 @@
+package helpers
+
+import (
+	"testing"
+
+	"github.com/go-playground/locales/ru"
+	ut "github.com/go-playground/universal-translator"
+	"github.com/go-playground/validator/v10"
+	"github.com/stretchr/testify/assert"
+)
+
+func Test_GetDefaultTranslator(t *testing.T) {
+	var (
+		validate      = validator.New()
+		localeRu      = ru.New()
+		uniTrans      = ut.New(localeRu, localeRu)
+		translator, _ = uniTrans.GetTranslator(localeRu.Locale())
+	)
+
+	tests := []struct {
+		name string
+		req  *validator.Validate
+		res  ut.Translator
+		err  error
+	}{
+		{
+			name: "positive case",
+			req:  validate,
+			res:  translator,
+			err:  nil,
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			res, err := GetDefaultTranslator(tt.req)
+			assert.IsType(t, tt.res, res)
+			assert.Equal(t, tt.err, err)
+		})
+	}
+}

+ 57 - 0
internal/helpers/validate_test.go

@@ -0,0 +1,57 @@
+package helpers
+
+import (
+	"testing"
+
+	"github.com/dmitriygnatenko/internal/models"
+	ut "github.com/go-playground/universal-translator"
+	"github.com/go-playground/validator/v10"
+	"github.com/stretchr/testify/assert"
+)
+
+func Test_FormatValidateErrors(t *testing.T) {
+	type req struct {
+		errors error
+		trans  ut.Translator
+	}
+
+	var (
+		form     = models.TagForm{}
+		validate = validator.New()
+
+		trans, _      = GetDefaultTranslator(validate)
+		correctErrors = validate.Struct(form)
+
+		correctRes = map[string]string{
+			"TagForm.Tag": "Тег обязательное поле",
+			"TagForm.URL": "URL обязательное поле",
+		}
+	)
+
+	tests := []struct {
+		name string
+		req  req
+		res  map[string]string
+	}{
+		{
+			name: "empty request",
+			req:  req{},
+			res:  map[string]string{},
+		},
+		{
+			name: "positive case",
+			req: req{
+				errors: correctErrors,
+				trans:  trans,
+			},
+			res: correctRes,
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			res := FormatValidateErrors(tt.req.errors, tt.req.trans)
+			assert.Equal(t, tt.res, res)
+		})
+	}
+}