generated from VLADIMIR/template
add tests
This commit is contained in:
parent
3612805009
commit
795edad998
@ -55,7 +55,7 @@ func main() {
|
|||||||
|
|
||||||
storyFilepath := config.GetStoryFilepath()
|
storyFilepath := config.GetStoryFilepath()
|
||||||
|
|
||||||
storyStorage := story_storage.NewStoryStorage(storyFilepath)
|
storyStorage := story_storage.NewFileStoryStorage(storyFilepath)
|
||||||
|
|
||||||
storyService, err := story_service.NewStoryService(cleaner, formatter, storyStorage)
|
storyService, err := story_service.NewStoryService(cleaner, formatter, storyStorage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
102
internal/services/story_service/service_test.go
Normal file
102
internal/services/story_service/service_test.go
Normal file
@ -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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,17 +9,17 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type service struct {
|
type fileService struct {
|
||||||
filepath string
|
filepath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStoryStorage(filepath string) story_service.IStoryStorage {
|
func NewFileStoryStorage(filepath string) story_service.IStoryStorage {
|
||||||
return &service{
|
return &fileService{
|
||||||
filepath: filepath,
|
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)
|
data, err := os.ReadFile(s.filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("story file %s not found", s.filepath)
|
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
|
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)
|
data, err := json.Marshal(story)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
25
internal/services/story_storage/var_storage_service.go
Normal file
25
internal/services/story_storage/var_storage_service.go
Normal file
@ -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
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user