From 795edad998ee458a54becebea9a658b679285db5 Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Sat, 7 Mar 2026 06:07:15 +0700 Subject: [PATCH] add tests --- cmd/evening_detective/main.go | 2 +- .../services/story_service/service_test.go | 102 ++++++++++++++++++ .../{service.go => file_storage_service.go} | 10 +- .../story_storage/var_storage_service.go | 25 +++++ 4 files changed, 133 insertions(+), 6 deletions(-) create mode 100644 internal/services/story_service/service_test.go rename internal/services/story_storage/{service.go => file_storage_service.go} (70%) create mode 100644 internal/services/story_storage/var_storage_service.go diff --git a/cmd/evening_detective/main.go b/cmd/evening_detective/main.go index 0333245..63594b9 100644 --- a/cmd/evening_detective/main.go +++ b/cmd/evening_detective/main.go @@ -55,7 +55,7 @@ func main() { storyFilepath := config.GetStoryFilepath() - storyStorage := story_storage.NewStoryStorage(storyFilepath) + storyStorage := story_storage.NewFileStoryStorage(storyFilepath) storyService, err := story_service.NewStoryService(cleaner, formatter, storyStorage) if err != nil { diff --git a/internal/services/story_service/service_test.go b/internal/services/story_service/service_test.go new file mode 100644 index 0000000..6798a12 --- /dev/null +++ b/internal/services/story_service/service_test.go @@ -0,0 +1,102 @@ +package story_service_test + +import ( + "evening_detective/internal/modules/cleaner" + "evening_detective/internal/modules/formatter" + "evening_detective/internal/services/story_service" + "evening_detective/internal/services/story_storage" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestStoryService_GetPlace(t *testing.T) { + tests := []struct { + name string + story *story_service.Story + code string + want *story_service.Place + }{ + { + name: "не корректный ввода", + story: &story_service.Story{}, + code: "[Ы]", + want: &story_service.Place{ + Code: "[Ы]", + Name: "Не найдено", + Text: "Уважаемые детективы внимательно прочитайте правила.", + }, + }, + { + name: "точка не найдена", + story: &story_service.Story{}, + code: "Ы", + want: &story_service.Place{ + Code: "Ы", + Name: "Не найдено", + Text: "Такой точки не существует.", + }, + }, + { + name: "получение точки", + story: &story_service.Story{ + Places: []*story_service.Place{ + { + Code: "Ы", + Name: "Название", + Text: "Текст", + }, + }, + }, + code: "Ы", + want: &story_service.Place{ + Code: "Ы", + Name: "Название", + Text: "Текст", + Applications: []*story_service.Application{}, + }, + }, + { + name: "получение точки с приложением", + story: &story_service.Story{ + Places: []*story_service.Place{ + { + Code: "Ы", + Name: "Название", + Text: "Текст", + Applications: []*story_service.Application{ + { + Name: "Приложение", + }, + }, + }, + }, + }, + code: "Ы", + want: &story_service.Place{ + Code: "Ы", + Name: "Название", + Text: "Текст", + Applications: []*story_service.Application{ + { + Name: "Приложение", + }, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s, err := story_service.NewStoryService( + cleaner.NewCleaner(), + formatter.NewFormatter(), + story_storage.NewVarStoryStorage(tt.story), + ) + if err != nil { + t.Fatalf("could not construct receiver type: %v", err) + } + got := s.GetPlace(tt.code) + assert.Equal(t, got, tt.want) + }) + } +} diff --git a/internal/services/story_storage/service.go b/internal/services/story_storage/file_storage_service.go similarity index 70% rename from internal/services/story_storage/service.go rename to internal/services/story_storage/file_storage_service.go index 41d604f..1b929b3 100644 --- a/internal/services/story_storage/service.go +++ b/internal/services/story_storage/file_storage_service.go @@ -9,17 +9,17 @@ import ( "os" ) -type service struct { +type fileService struct { filepath string } -func NewStoryStorage(filepath string) story_service.IStoryStorage { - return &service{ +func NewFileStoryStorage(filepath string) story_service.IStoryStorage { + return &fileService{ filepath: filepath, } } -func (s *service) Load(ctx context.Context) (*story_service.Story, error) { +func (s *fileService) Load(ctx context.Context) (*story_service.Story, error) { data, err := os.ReadFile(s.filepath) if err != nil { return nil, fmt.Errorf("story file %s not found", s.filepath) @@ -32,7 +32,7 @@ func (s *service) Load(ctx context.Context) (*story_service.Story, error) { return story, nil } -func (s *service) Save(ctx context.Context, story *story_service.Story) error { +func (s *fileService) Save(ctx context.Context, story *story_service.Story) error { data, err := json.Marshal(story) if err != nil { return err diff --git a/internal/services/story_storage/var_storage_service.go b/internal/services/story_storage/var_storage_service.go new file mode 100644 index 0000000..81ccce4 --- /dev/null +++ b/internal/services/story_storage/var_storage_service.go @@ -0,0 +1,25 @@ +package story_storage + +import ( + "context" + "evening_detective/internal/services/story_service" +) + +type varService struct { + story *story_service.Story +} + +func NewVarStoryStorage(story *story_service.Story) story_service.IStoryStorage { + return &varService{ + story: story, + } +} + +func (s *varService) Load(ctx context.Context) (*story_service.Story, error) { + return s.story, nil +} + +func (s *varService) Save(ctx context.Context, story *story_service.Story) error { + s.story = story + return nil +}