Dima 1 سال پیش
والد
کامیت
a197634a6e

+ 0 - 199
internal/helpers/datetime_test.go

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

+ 0 - 41
internal/helpers/trans_test.go

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

+ 0 - 57
internal/helpers/validate_test.go

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

+ 8 - 8
internal/mapper/article.go

@@ -8,7 +8,7 @@ import (
 	"github.com/dmitriygnatenko/internal/models"
 )
 
-func ConvertArticleModelsToDTO(m []models.Article) ([]dto.ArticleDTO, error) {
+func ConvertArticleModelsToDTO(m []models.Article) []dto.ArticleDTO {
 	res := make([]dto.ArticleDTO, 0, len(m))
 
 	for i := range m {
@@ -24,10 +24,10 @@ func ConvertArticleModelsToDTO(m []models.Article) ([]dto.ArticleDTO, error) {
 		})
 	}
 
-	return res, nil
+	return res
 }
 
-func ConvertArticleModelToDTO(m models.Article) (*dto.ArticleDTO, error) {
+func ConvertArticleModelToDTO(m models.Article) *dto.ArticleDTO {
 	return &dto.ArticleDTO{
 		ID:              m.ID,
 		URL:             m.URL,
@@ -37,10 +37,10 @@ func ConvertArticleModelToDTO(m models.Article) (*dto.ArticleDTO, error) {
 		MetaKeywords:    m.MetaKeywords.String,
 		MetaDescription: m.MetaDescription.String,
 		PublishTime:     helpers.FormatDateStr(m.PublishTime),
-	}, nil
+	}
 }
 
-func ConvertArticlePreviewModelsToDTO(m []models.ArticlePreview) ([]dto.ArticlePreviewDTO, error) {
+func ConvertArticlePreviewModelsToDTO(m []models.ArticlePreview) []dto.ArticlePreviewDTO {
 	res := make([]dto.ArticlePreviewDTO, 0, len(m))
 
 	for i := range m {
@@ -54,10 +54,10 @@ func ConvertArticlePreviewModelsToDTO(m []models.ArticlePreview) ([]dto.ArticleP
 		})
 	}
 
-	return res, nil
+	return res
 }
 
-func ConvertArticleModelToForm(a models.Article, tags []models.Tag) (*models.ArticleForm, error) {
+func ConvertArticleModelToForm(a models.Article, tags []models.Tag) *models.ArticleForm {
 	tagMap := make(map[int]bool, len(tags))
 	for i := range tags {
 		tagMap[tags[i].ID] = true
@@ -75,7 +75,7 @@ func ConvertArticleModelToForm(a models.Article, tags []models.Tag) (*models.Art
 		MetaDescription: a.MetaDescription.String,
 		PublishTime:     a.PublishTime,
 		ActiveTags:      tagMap,
-	}, nil
+	}
 }
 
 func ConvertArticleFormToModel(f models.ArticleForm) models.Article {

+ 2 - 10
internal/services/handler/admin/article.go

@@ -20,13 +20,8 @@ func ArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
 			return err
 		}
 
-		articlesDTO, err := mapper.ConvertArticleModelsToDTO(articles)
-		if err != nil {
-			return err
-		}
-
 		return ctx.Render("admin/article", fiber.Map{
-			"articles": articlesDTO,
+			"articles": mapper.ConvertArticleModelsToDTO(articles),
 			"section":  "article",
 		}, "admin/_layout")
 	}
@@ -148,10 +143,7 @@ func EditArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
 
 		var form *models.ArticleForm
 		if ctx.Method() == fiber.MethodGet {
-			form, err = mapper.ConvertArticleModelToForm(*article, articleTags)
-			if err != nil {
-				return err
-			}
+			form = mapper.ConvertArticleModelToForm(*article, articleTags)
 		} else if ctx.Method() == fiber.MethodPost {
 			form = &models.ArticleForm{
 				ID:         ID,

+ 2 - 10
internal/services/handler/article.go

@@ -33,10 +33,7 @@ func ArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
 				return fiber.ErrNotFound
 			}
 
-			articleDTO, err := mapper.ConvertArticleModelToDTO(*article)
-			if err != nil {
-				return err
-			}
+			articleDTO := mapper.ConvertArticleModelToDTO(*article)
 
 			// All used tags
 			tags, err := sp.GetTagRepository().GetAllUsed(ctx.Context())
@@ -55,18 +52,13 @@ func ArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
 				articles = articles[:maxArticlesCount]
 			}
 
-			articlesDTO, err := mapper.ConvertArticlePreviewModelsToDTO(articles)
-			if err != nil {
-				return err
-			}
-
 			renderData = fiber.Map{
 				"headTitle":       "От слона к суслику - статьи про PHP, Go, алгоритмы",
 				"headDescription": articleDTO.MetaDescription,
 				"headKeywords":    articleDTO.MetaKeywords,
 				"pageTitle":       "Статья<br>" + articleDTO.Title,
 				"article":         articleDTO,
-				"sidebarArticles": articlesDTO,
+				"sidebarArticles": mapper.ConvertArticlePreviewModelsToDTO(articles),
 				"sidebarTags":     tagsDTO,
 			}
 

+ 42 - 103
internal/services/handler/article_test.go

@@ -9,21 +9,21 @@ import (
 	"testing"
 
 	"github.com/brianvoe/gofakeit/v6"
+	"github.com/dmitriygnatenko/internal/helpers"
 	"github.com/dmitriygnatenko/internal/interfaces"
 	"github.com/dmitriygnatenko/internal/models"
 	repositoryMocks "github.com/dmitriygnatenko/internal/repositories/mocks"
 	sp "github.com/dmitriygnatenko/internal/service_provider"
 	cacheMocks "github.com/dmitriygnatenko/internal/services/cache/mocks"
-	"github.com/dmitriygnatenko/internal/services/handler/test"
 	"github.com/gofiber/fiber/v2"
 	"github.com/gojuno/minimock/v3"
 	"github.com/stretchr/testify/assert"
 )
 
 func Test_ArticleHandler(t *testing.T) {
-	type cacheMockFunc func(mc *minimock.Controller) interfaces.ICache
-	type tagMockFunc func(mc *minimock.Controller) interfaces.ITagRepository
-	type articleMockFunc func(mc *minimock.Controller) interfaces.IArticleRepository
+	type cacheMockFunc func(mc *minimock.Controller) interfaces.Cache
+	type tagMockFunc func(mc *minimock.Controller) interfaces.TagRepository
+	type articleMockFunc func(mc *minimock.Controller) interfaces.ArticleRepository
 
 	type req struct {
 		method string
@@ -33,8 +33,7 @@ func Test_ArticleHandler(t *testing.T) {
 	var (
 		mc          = minimock.NewController(t)
 		articleID   = gofakeit.Number(1, 100)
-		date        = gofakeit.Date()
-		publishTime = date.Format("2006-01-02 15:04:05")
+		publishTime = gofakeit.Date()
 		internalErr = errors.New(gofakeit.Phrase())
 
 		article = models.Article{
@@ -124,21 +123,21 @@ func Test_ArticleHandler(t *testing.T) {
 			},
 			res: fiber.StatusOK,
 			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
+			cacheMock: func(mc *minimock.Controller) interfaces.Cache {
+				mock := cacheMocks.NewCacheMock(mc)
 				mock.GetMock.Return(nil, false)
 				mock.SetMock.Return()
 
 				return mock
 			},
-			tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
-				mock := repositoryMocks.NewITagRepositoryMock(mc)
+			tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
+				mock := repositoryMocks.NewTagRepositoryMock(mc)
 				mock.GetAllUsedMock.Return(tags, nil)
 
 				return mock
 			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				mock := repositoryMocks.NewIArticleRepositoryMock(mc)
+			articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
+				mock := repositoryMocks.NewArticleRepositoryMock(mc)
 
 				mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
 					assert.Equal(mc, strconv.Itoa(articleID), url)
@@ -157,19 +156,19 @@ func Test_ArticleHandler(t *testing.T) {
 			},
 			res: fiber.StatusNotFound,
 			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
+			cacheMock: func(mc *minimock.Controller) interfaces.Cache {
+				mock := cacheMocks.NewCacheMock(mc)
 				mock.GetMock.Return(nil, false)
 
 				return mock
 			},
-			tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
-				mock := repositoryMocks.NewITagRepositoryMock(mc)
+			tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
+				mock := repositoryMocks.NewTagRepositoryMock(mc)
 
 				return mock
 			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				mock := repositoryMocks.NewIArticleRepositoryMock(mc)
+			articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
+				mock := repositoryMocks.NewArticleRepositoryMock(mc)
 				mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
 					assert.Equal(mc, strconv.Itoa(articleID), url)
 				}).Return(nil, sql.ErrNoRows)
@@ -185,19 +184,19 @@ func Test_ArticleHandler(t *testing.T) {
 			},
 			res: fiber.StatusInternalServerError,
 			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
+			cacheMock: func(mc *minimock.Controller) interfaces.Cache {
+				mock := cacheMocks.NewCacheMock(mc)
 				mock.GetMock.Return(nil, false)
 
 				return mock
 			},
-			tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
-				mock := repositoryMocks.NewITagRepositoryMock(mc)
+			tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
+				mock := repositoryMocks.NewTagRepositoryMock(mc)
 
 				return mock
 			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				mock := repositoryMocks.NewIArticleRepositoryMock(mc)
+			articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
+				mock := repositoryMocks.NewArticleRepositoryMock(mc)
 				mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
 					assert.Equal(mc, strconv.Itoa(articleID), url)
 				}).Return(nil, internalErr)
@@ -213,19 +212,19 @@ func Test_ArticleHandler(t *testing.T) {
 			},
 			res: fiber.StatusNotFound,
 			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
+			cacheMock: func(mc *minimock.Controller) interfaces.Cache {
+				mock := cacheMocks.NewCacheMock(mc)
 				mock.GetMock.Return(nil, false)
 
 				return mock
 			},
-			tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
-				mock := repositoryMocks.NewITagRepositoryMock(mc)
+			tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
+				mock := repositoryMocks.NewTagRepositoryMock(mc)
 
 				return mock
 			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				mock := repositoryMocks.NewIArticleRepositoryMock(mc)
+			articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
+				mock := repositoryMocks.NewArticleRepositoryMock(mc)
 				mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
 					assert.Equal(mc, strconv.Itoa(articleID), url)
 				}).Return(&notActiveArticle, nil)
@@ -233,34 +232,6 @@ func Test_ArticleHandler(t *testing.T) {
 				return mock
 			},
 		},
-		{
-			name: "negative case - article mapper error",
-			req: req{
-				method: fiber.MethodGet,
-				route:  "/article/" + strconv.Itoa(articleID),
-			},
-			res: fiber.StatusInternalServerError,
-			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
-				mock.GetMock.Return(nil, false)
-
-				return mock
-			},
-			tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
-				mock := repositoryMocks.NewITagRepositoryMock(mc)
-
-				return mock
-			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				mock := repositoryMocks.NewIArticleRepositoryMock(mc)
-				mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
-					assert.Equal(mc, strconv.Itoa(articleID), url)
-				}).Return(&models.Article{IsActive: true}, nil)
-
-				return mock
-			},
-		},
 		{
 			name: "negative case - tags repository error",
 			req: req{
@@ -269,20 +240,20 @@ func Test_ArticleHandler(t *testing.T) {
 			},
 			res: fiber.StatusInternalServerError,
 			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
+			cacheMock: func(mc *minimock.Controller) interfaces.Cache {
+				mock := cacheMocks.NewCacheMock(mc)
 				mock.GetMock.Return(nil, false)
 
 				return mock
 			},
-			tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
-				mock := repositoryMocks.NewITagRepositoryMock(mc)
+			tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
+				mock := repositoryMocks.NewTagRepositoryMock(mc)
 				mock.GetAllUsedMock.Return(nil, internalErr)
 
 				return mock
 			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				mock := repositoryMocks.NewIArticleRepositoryMock(mc)
+			articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
+				mock := repositoryMocks.NewArticleRepositoryMock(mc)
 
 				mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
 					assert.Equal(mc, strconv.Itoa(articleID), url)
@@ -299,20 +270,20 @@ func Test_ArticleHandler(t *testing.T) {
 			},
 			res: fiber.StatusInternalServerError,
 			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
+			cacheMock: func(mc *minimock.Controller) interfaces.Cache {
+				mock := cacheMocks.NewCacheMock(mc)
 				mock.GetMock.Return(nil, false)
 
 				return mock
 			},
-			tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
-				mock := repositoryMocks.NewITagRepositoryMock(mc)
+			tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
+				mock := repositoryMocks.NewTagRepositoryMock(mc)
 				mock.GetAllUsedMock.Return(tags, nil)
 
 				return mock
 			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				mock := repositoryMocks.NewIArticleRepositoryMock(mc)
+			articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
+				mock := repositoryMocks.NewArticleRepositoryMock(mc)
 
 				mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
 					assert.Equal(mc, strconv.Itoa(articleID), url)
@@ -320,38 +291,6 @@ func Test_ArticleHandler(t *testing.T) {
 
 				mock.GetAllPreviewMock.Return(nil, internalErr)
 
-				return mock
-			},
-		},
-		{
-			name: "negative case - articles mapper error",
-			req: req{
-				method: fiber.MethodGet,
-				route:  "/article/" + strconv.Itoa(articleID),
-			},
-			res: fiber.StatusInternalServerError,
-			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
-				mock.GetMock.Return(nil, false)
-
-				return mock
-			},
-			tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
-				mock := repositoryMocks.NewITagRepositoryMock(mc)
-				mock.GetAllUsedMock.Return(tags, nil)
-
-				return mock
-			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				mock := repositoryMocks.NewIArticleRepositoryMock(mc)
-
-				mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
-					assert.Equal(mc, strconv.Itoa(articleID), url)
-				}).Return(&article, nil)
-
-				mock.GetAllPreviewMock.Return([]models.ArticlePreview{{}}, nil)
-
 				return mock
 			},
 		},
@@ -359,7 +298,7 @@ func Test_ArticleHandler(t *testing.T) {
 
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			fiberApp := fiber.New(test.GetFiberConfig())
+			fiberApp := fiber.New(helpers.GetFiberTestConfig())
 			fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, nil)
 			serviceProvider := sp.InitMock(tt.cacheMock(mc), tt.tagMock(mc), tt.articleMock(mc))
 

+ 1 - 6
internal/services/handler/main_page.go

@@ -18,17 +18,12 @@ func MainPageHandler(sp interfaces.ServiceProvider) fiber.Handler {
 				return err
 			}
 
-			articlesDTO, err := mapper.ConvertArticlePreviewModelsToDTO(articles)
-			if err != nil {
-				return err
-			}
-
 			renderData = fiber.Map{
 				"headTitle":       "От слона к суслику - статьи про PHP, Go, алгоритмы",
 				"headDescription": "список статей",
 				"headKeywords":    "Дмитрий Гнатенко, программист, PHP, Go, Golang, программирование, статьи, блог",
 				"pageTitle":       "Список статей",
-				"articles":        articlesDTO,
+				"articles":        mapper.ConvertArticlePreviewModelsToDTO(articles),
 			}
 
 			sp.GetCacheService().Set(allPreviewArticlesCacheKey, renderData)

+ 13 - 35
internal/services/handler/main_page_test.go

@@ -7,20 +7,20 @@ import (
 	"testing"
 
 	"github.com/brianvoe/gofakeit/v6"
+	"github.com/dmitriygnatenko/internal/helpers"
 	"github.com/dmitriygnatenko/internal/interfaces"
 	"github.com/dmitriygnatenko/internal/models"
 	repositoryMocks "github.com/dmitriygnatenko/internal/repositories/mocks"
 	sp "github.com/dmitriygnatenko/internal/service_provider"
 	cacheMocks "github.com/dmitriygnatenko/internal/services/cache/mocks"
-	"github.com/dmitriygnatenko/internal/services/handler/test"
 	"github.com/gofiber/fiber/v2"
 	"github.com/gojuno/minimock/v3"
 	"github.com/stretchr/testify/assert"
 )
 
 func Test_MainPageHandler(t *testing.T) {
-	type cacheMockFunc func(mc *minimock.Controller) interfaces.ICache
-	type articleMockFunc func(mc *minimock.Controller) interfaces.IArticleRepository
+	type cacheMockFunc func(mc *minimock.Controller) interfaces.Cache
+	type articleMockFunc func(mc *minimock.Controller) interfaces.ArticleRepository
 
 	type req struct {
 		method string
@@ -29,8 +29,7 @@ func Test_MainPageHandler(t *testing.T) {
 
 	var (
 		mc          = minimock.NewController(t)
-		date        = gofakeit.Date()
-		publishTime = date.Format("2006-01-02 15:04:05")
+		publishTime = gofakeit.Date()
 		internalErr = errors.New(gofakeit.Phrase())
 
 		previewArticles = []models.ArticlePreview{
@@ -77,15 +76,15 @@ func Test_MainPageHandler(t *testing.T) {
 			},
 			res: fiber.StatusOK,
 			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
+			cacheMock: func(mc *minimock.Controller) interfaces.Cache {
+				mock := cacheMocks.NewCacheMock(mc)
 				mock.GetMock.Return(nil, false)
 				mock.SetMock.Return()
 
 				return mock
 			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				mock := repositoryMocks.NewIArticleRepositoryMock(mc)
+			articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
+				mock := repositoryMocks.NewArticleRepositoryMock(mc)
 				mock.GetAllPreviewMock.Return(previewArticles, nil)
 
 				return mock
@@ -99,37 +98,16 @@ func Test_MainPageHandler(t *testing.T) {
 			},
 			res: fiber.StatusInternalServerError,
 			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
+			cacheMock: func(mc *minimock.Controller) interfaces.Cache {
+				mock := cacheMocks.NewCacheMock(mc)
 				mock.GetMock.Return(nil, false)
 
 				return mock
 			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				mock := repositoryMocks.NewIArticleRepositoryMock(mc)
+			articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
+				mock := repositoryMocks.NewArticleRepositoryMock(mc)
 				mock.GetAllPreviewMock.Return(nil, internalErr)
 
-				return mock
-			},
-		},
-		{
-			name: "negative case - articles mapper error",
-			req: req{
-				method: fiber.MethodGet,
-				route:  "/",
-			},
-			res: fiber.StatusInternalServerError,
-			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
-				mock.GetMock.Return(nil, false)
-
-				return mock
-			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				mock := repositoryMocks.NewIArticleRepositoryMock(mc)
-				mock.GetAllPreviewMock.Return([]models.ArticlePreview{{}}, nil)
-
 				return mock
 			},
 		},
@@ -137,7 +115,7 @@ func Test_MainPageHandler(t *testing.T) {
 
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			fiberApp := fiber.New(test.GetFiberConfig())
+			fiberApp := fiber.New(helpers.GetFiberTestConfig())
 			fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, nil)
 			serviceProvider := sp.InitMock(tt.cacheMock(mc), tt.articleMock(mc))
 

+ 1 - 6
internal/services/handler/tag.go

@@ -31,17 +31,12 @@ func TagHandler(sp interfaces.ServiceProvider) fiber.Handler {
 				return err
 			}
 
-			articlesDTO, err := mapper.ConvertArticlePreviewModelsToDTO(articles)
-			if err != nil {
-				return err
-			}
-
 			renderData = fiber.Map{
 				"headTitle":       "От слона к суслику - статьи про PHP, Go, алгоритмы",
 				"headDescription": "статьи с тегом " + tag.Tag,
 				"headKeywords":    "программирование, статьи, блог, " + tag.Tag,
 				"pageTitle":       "Тег<br>" + tag.Tag,
-				"articles":        articlesDTO,
+				"articles":        mapper.ConvertArticlePreviewModelsToDTO(articles),
 			}
 
 			sp.GetCacheService().Set(tagCacheKey+tagReq, renderData)

+ 30 - 63
internal/services/handler/tag_test.go

@@ -9,21 +9,21 @@ import (
 	"testing"
 
 	"github.com/brianvoe/gofakeit/v6"
+	"github.com/dmitriygnatenko/internal/helpers"
 	"github.com/dmitriygnatenko/internal/interfaces"
 	"github.com/dmitriygnatenko/internal/models"
 	repositoryMocks "github.com/dmitriygnatenko/internal/repositories/mocks"
 	sp "github.com/dmitriygnatenko/internal/service_provider"
 	cacheMocks "github.com/dmitriygnatenko/internal/services/cache/mocks"
-	"github.com/dmitriygnatenko/internal/services/handler/test"
 	"github.com/gofiber/fiber/v2"
 	"github.com/gojuno/minimock/v3"
 	"github.com/stretchr/testify/assert"
 )
 
 func Test_TagHandler(t *testing.T) {
-	type cacheMockFunc func(mc *minimock.Controller) interfaces.ICache
-	type tagMockFunc func(mc *minimock.Controller) interfaces.ITagRepository
-	type articleMockFunc func(mc *minimock.Controller) interfaces.IArticleRepository
+	type cacheMockFunc func(mc *minimock.Controller) interfaces.Cache
+	type tagMockFunc func(mc *minimock.Controller) interfaces.TagRepository
+	type articleMockFunc func(mc *minimock.Controller) interfaces.ArticleRepository
 
 	type req struct {
 		method string
@@ -33,8 +33,7 @@ func Test_TagHandler(t *testing.T) {
 	var (
 		mc          = minimock.NewController(t)
 		tagID       = gofakeit.Number(1, 100)
-		date        = gofakeit.Date()
-		publishTime = date.Format("2006-01-02 15:04:05")
+		publishTime = gofakeit.Date()
 		internalErr = errors.New(gofakeit.Phrase())
 
 		tag = models.Tag{
@@ -68,15 +67,15 @@ func Test_TagHandler(t *testing.T) {
 			},
 			res: fiber.StatusOK,
 			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
+			cacheMock: func(mc *minimock.Controller) interfaces.Cache {
+				mock := cacheMocks.NewCacheMock(mc)
 				mock.GetMock.Return(nil, false)
 				mock.SetMock.Return()
 
 				return mock
 			},
-			tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
-				mock := repositoryMocks.NewITagRepositoryMock(mc)
+			tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
+				mock := repositoryMocks.NewTagRepositoryMock(mc)
 				mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
 					assert.Equal(mc, strconv.Itoa(tagID), url)
 				}).Return(&tag, nil)
@@ -84,8 +83,8 @@ func Test_TagHandler(t *testing.T) {
 				return mock
 
 			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				mock := repositoryMocks.NewIArticleRepositoryMock(mc)
+			articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
+				mock := repositoryMocks.NewArticleRepositoryMock(mc)
 				mock.GetPreviewByTagIDMock.Inspect(func(ctx context.Context, id int) {
 					assert.Equal(mc, tagID, id)
 				}).Return([]models.ArticlePreview{article}, nil)
@@ -101,14 +100,14 @@ func Test_TagHandler(t *testing.T) {
 			},
 			res: fiber.StatusNotFound,
 			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
+			cacheMock: func(mc *minimock.Controller) interfaces.Cache {
+				mock := cacheMocks.NewCacheMock(mc)
 				mock.GetMock.Return(nil, false)
 
 				return mock
 			},
-			tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
-				mock := repositoryMocks.NewITagRepositoryMock(mc)
+			tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
+				mock := repositoryMocks.NewTagRepositoryMock(mc)
 				mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
 					assert.Equal(mc, strconv.Itoa(tagID), url)
 				}).Return(nil, sql.ErrNoRows)
@@ -116,8 +115,8 @@ func Test_TagHandler(t *testing.T) {
 				return mock
 
 			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				return repositoryMocks.NewIArticleRepositoryMock(mc)
+			articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
+				return repositoryMocks.NewArticleRepositoryMock(mc)
 			},
 		},
 		{
@@ -128,22 +127,22 @@ func Test_TagHandler(t *testing.T) {
 			},
 			res: fiber.StatusInternalServerError,
 			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
+			cacheMock: func(mc *minimock.Controller) interfaces.Cache {
+				mock := cacheMocks.NewCacheMock(mc)
 				mock.GetMock.Return(nil, false)
 
 				return mock
 			},
-			tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
-				mock := repositoryMocks.NewITagRepositoryMock(mc)
+			tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
+				mock := repositoryMocks.NewTagRepositoryMock(mc)
 				mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
 					assert.Equal(mc, strconv.Itoa(tagID), url)
 				}).Return(nil, internalErr)
 
 				return mock
 			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				return repositoryMocks.NewIArticleRepositoryMock(mc)
+			articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
+				return repositoryMocks.NewArticleRepositoryMock(mc)
 			},
 		},
 		{
@@ -154,14 +153,14 @@ func Test_TagHandler(t *testing.T) {
 			},
 			res: fiber.StatusInternalServerError,
 			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
+			cacheMock: func(mc *minimock.Controller) interfaces.Cache {
+				mock := cacheMocks.NewCacheMock(mc)
 				mock.GetMock.Return(nil, false)
 
 				return mock
 			},
-			tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
-				mock := repositoryMocks.NewITagRepositoryMock(mc)
+			tagMock: func(mc *minimock.Controller) interfaces.TagRepository {
+				mock := repositoryMocks.NewTagRepositoryMock(mc)
 				mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
 					assert.Equal(mc, strconv.Itoa(tagID), url)
 				}).Return(&tag, nil)
@@ -169,44 +168,12 @@ func Test_TagHandler(t *testing.T) {
 				return mock
 
 			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				mock := repositoryMocks.NewIArticleRepositoryMock(mc)
+			articleMock: func(mc *minimock.Controller) interfaces.ArticleRepository {
+				mock := repositoryMocks.NewArticleRepositoryMock(mc)
 				mock.GetPreviewByTagIDMock.Inspect(func(ctx context.Context, id int) {
 					assert.Equal(mc, tagID, id)
 				}).Return(nil, internalErr)
 
-				return mock
-			},
-		},
-		{
-			name: "negative case - article mapper error",
-			req: req{
-				method: fiber.MethodGet,
-				route:  "/tag/" + strconv.Itoa(tagID),
-			},
-			res: fiber.StatusInternalServerError,
-			err: nil,
-			cacheMock: func(mc *minimock.Controller) interfaces.ICache {
-				mock := cacheMocks.NewICacheMock(mc)
-				mock.GetMock.Return(nil, false)
-
-				return mock
-			},
-			tagMock: func(mc *minimock.Controller) interfaces.ITagRepository {
-				mock := repositoryMocks.NewITagRepositoryMock(mc)
-				mock.GetByURLMock.Inspect(func(ctx context.Context, url string) {
-					assert.Equal(mc, strconv.Itoa(tagID), url)
-				}).Return(&tag, nil)
-
-				return mock
-
-			},
-			articleMock: func(mc *minimock.Controller) interfaces.IArticleRepository {
-				mock := repositoryMocks.NewIArticleRepositoryMock(mc)
-				mock.GetPreviewByTagIDMock.Inspect(func(ctx context.Context, id int) {
-					assert.Equal(mc, tagID, id)
-				}).Return([]models.ArticlePreview{{}}, nil)
-
 				return mock
 			},
 		},
@@ -214,7 +181,7 @@ func Test_TagHandler(t *testing.T) {
 
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			fiberApp := fiber.New(test.GetFiberConfig())
+			fiberApp := fiber.New(helpers.GetFiberTestConfig())
 			fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, nil)
 			serviceProvider := sp.InitMock(tt.cacheMock(mc), tt.tagMock(mc), tt.articleMock(mc))