validate_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package helpers
  2. import (
  3. "testing"
  4. "github.com/dmitriygnatenko/internal/models"
  5. ut "github.com/go-playground/universal-translator"
  6. "github.com/go-playground/validator/v10"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func Test_FormatValidateErrors(t *testing.T) {
  10. type req struct {
  11. errors error
  12. trans ut.Translator
  13. }
  14. var (
  15. form = models.TagForm{}
  16. validate = validator.New()
  17. trans, _ = GetDefaultTranslator(validate)
  18. correctErrors = validate.Struct(form)
  19. correctRes = map[string]string{
  20. "TagForm.Tag": "Тег обязательное поле",
  21. "TagForm.URL": "URL обязательное поле",
  22. }
  23. )
  24. tests := []struct {
  25. name string
  26. req req
  27. res map[string]string
  28. }{
  29. {
  30. name: "empty request",
  31. req: req{},
  32. res: map[string]string{},
  33. },
  34. {
  35. name: "positive case",
  36. req: req{
  37. errors: correctErrors,
  38. trans: trans,
  39. },
  40. res: correctRes,
  41. },
  42. }
  43. for _, tt := range tests {
  44. t.Run(tt.name, func(t *testing.T) {
  45. res := FormatValidateErrors(tt.req.errors, tt.req.trans)
  46. assert.Equal(t, tt.res, res)
  47. })
  48. }
  49. }