generated from VLADIMIR/template
fix
This commit is contained in:
parent
795edad998
commit
83868cc778
@ -45,5 +45,5 @@ func (s *service) ClearCode(code string) string {
|
||||
}
|
||||
|
||||
func (s *service) ClearText(text string) string {
|
||||
return re.ReplaceAllString(text, "")
|
||||
return strings.TrimSpace(re.ReplaceAllString(text, ""))
|
||||
}
|
||||
|
||||
@ -29,10 +29,9 @@ func mapProtoTeamsToTeam(team *proto.Team) *models.Team {
|
||||
}
|
||||
}
|
||||
|
||||
func mapActionToProtoAction(action *models.Action) *proto.Action {
|
||||
func mapPlaceToProtoAction(place *story_service.Place) *proto.Action {
|
||||
return &proto.Action{
|
||||
Id: action.ID,
|
||||
Place: action.Place,
|
||||
Place: place.Code,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -133,10 +133,10 @@ func (s *Services) GetTeam(ctx context.Context, req *proto.GetTeamReq) (*proto.G
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, err.Error())
|
||||
}
|
||||
|
||||
res := make([]*proto.Action, 0, len(actions))
|
||||
for _, action := range actions {
|
||||
newAction := mapActionToProtoAction(action)
|
||||
place := s.storyService.GetPlace(action.Place)
|
||||
for _, place := range s.getPlaces(actions) {
|
||||
newAction := mapPlaceToProtoAction(place)
|
||||
newAction.Text = place.Text
|
||||
newAction.Name = place.Name
|
||||
newAction.Applications = make([]*proto.Application, 0, len(place.Applications))
|
||||
@ -151,6 +151,27 @@ func (s *Services) GetTeam(ctx context.Context, req *proto.GetTeamReq) (*proto.G
|
||||
}, err
|
||||
}
|
||||
|
||||
func (s *Services) getPlaces(actions []*models.Action) []*story_service.Place {
|
||||
places := []*story_service.Place{}
|
||||
m := map[string]any{}
|
||||
for _, action := range actions {
|
||||
place := s.storyService.GetPlace(action.Place)
|
||||
_, ok := m[place.Code]
|
||||
if place.Hidden && !ok {
|
||||
place = &story_service.Place{
|
||||
Code: place.Code,
|
||||
Name: "Не найдено",
|
||||
Text: "Такой точки не существует.",
|
||||
}
|
||||
}
|
||||
places = append(places, place)
|
||||
for _, door := range place.Doors {
|
||||
m[door.Code] = struct{}{}
|
||||
}
|
||||
}
|
||||
return places
|
||||
}
|
||||
|
||||
func (s *Services) GetTeamsCSV(ctx context.Context, req *proto.GetTeamsCSVReq) (*proto.GetTeamsCSVRsp, error) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
155
internal/services/services_test.go
Normal file
155
internal/services/services_test.go
Normal file
@ -0,0 +1,155 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"evening_detective/internal/models"
|
||||
"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 TestServices_getPlaces(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
story *story_service.Story
|
||||
actions []*models.Action
|
||||
want []*story_service.Place
|
||||
}{
|
||||
// {
|
||||
// name: "Нельзя открыть скрытую точку",
|
||||
// story: &story_service.Story{
|
||||
// Places: []*story_service.Place{
|
||||
// {
|
||||
// Code: "Ы",
|
||||
// Name: "Название",
|
||||
// Text: "Текст",
|
||||
// Hidden: true,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// actions: []*models.Action{
|
||||
// {
|
||||
// Place: "Ы",
|
||||
// },
|
||||
// },
|
||||
// want: []*story_service.Place{
|
||||
// {
|
||||
// Code: "Ы",
|
||||
// Name: "Не найдено",
|
||||
// Text: "Такой точки не существует.",
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// {
|
||||
// name: "Нельзя открыть скрытую точку",
|
||||
// story: &story_service.Story{
|
||||
// Places: []*story_service.Place{
|
||||
// {
|
||||
// Code: "Ы-1",
|
||||
// Name: "Название",
|
||||
// Text: "Текст",
|
||||
// },
|
||||
// {
|
||||
// Code: "Ы-2",
|
||||
// Name: "Название",
|
||||
// Text: "Текст",
|
||||
// Hidden: true,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// actions: []*models.Action{
|
||||
// {
|
||||
// Place: "Ы-1",
|
||||
// },
|
||||
// {
|
||||
// Place: "Ы-2",
|
||||
// },
|
||||
// },
|
||||
// want: []*story_service.Place{
|
||||
// {
|
||||
// Code: "Ы-1",
|
||||
// Name: "Название",
|
||||
// Text: "Текст",
|
||||
// Applications: []*story_service.Application{},
|
||||
// },
|
||||
// {
|
||||
// Code: "Ы-2",
|
||||
// Name: "Не найдено",
|
||||
// Text: "Такой точки не существует.",
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
{
|
||||
name: "Нельзя открыть скрытую точку",
|
||||
story: &story_service.Story{
|
||||
Places: []*story_service.Place{
|
||||
{
|
||||
Code: "Ы-1",
|
||||
Name: "Название",
|
||||
Text: "Текст",
|
||||
Applications: []*story_service.Application{},
|
||||
Doors: []*story_service.Door{
|
||||
{
|
||||
Code: "Ы-2",
|
||||
Name: "Название",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Code: "Ы-2",
|
||||
Name: "Название",
|
||||
Text: "Текст",
|
||||
Applications: []*story_service.Application{},
|
||||
Hidden: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
actions: []*models.Action{
|
||||
{
|
||||
Place: "Ы-1",
|
||||
},
|
||||
{
|
||||
Place: "Ы-2",
|
||||
},
|
||||
},
|
||||
want: []*story_service.Place{
|
||||
{
|
||||
Code: "Ы-1",
|
||||
Name: "Название",
|
||||
Text: "Текст",
|
||||
Applications: []*story_service.Application{},
|
||||
Doors: []*story_service.Door{
|
||||
{
|
||||
Code: "Ы-2",
|
||||
Name: "Название",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Code: "Ы-2",
|
||||
Name: "Название",
|
||||
Text: "Текст",
|
||||
Applications: []*story_service.Application{},
|
||||
Hidden: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
storyService, err := story_service.NewStoryService(
|
||||
cleaner.NewCleaner(),
|
||||
formatter.NewFormatter(),
|
||||
story_storage.NewVarStoryStorage(tt.story),
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
s := NewServices(nil, storyService, nil, nil, nil, "")
|
||||
got := s.getPlaces(tt.actions)
|
||||
assert.Equal(t, got, tt.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -9,8 +9,15 @@ type Place struct {
|
||||
Name string `json:"name"`
|
||||
Text string `json:"text"`
|
||||
Applications []*Application `json:"applications,omitempty"`
|
||||
Hidden bool `json:"hidden"`
|
||||
Doors []*Door `json:"doors"`
|
||||
}
|
||||
|
||||
type Application struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Door struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
@ -71,6 +71,8 @@ func (s *StoryService) GetPlace(code string) *Place {
|
||||
Name: place.Name,
|
||||
Text: s.cleaner.ClearText(place.Text),
|
||||
Applications: applications,
|
||||
Hidden: place.Hidden,
|
||||
Doors: place.Doors,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,6 +84,37 @@ func TestStoryService_GetPlace(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "получение точки с проходами",
|
||||
story: &story_service.Story{
|
||||
Places: []*story_service.Place{
|
||||
{
|
||||
Code: "Ы-1",
|
||||
Name: "Название",
|
||||
Text: "Текст",
|
||||
Doors: []*story_service.Door{
|
||||
{
|
||||
Code: "Ы-2",
|
||||
Name: "Приложение",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
code: "Ы-1",
|
||||
want: &story_service.Place{
|
||||
Code: "Ы-1",
|
||||
Name: "Название",
|
||||
Text: "Текст",
|
||||
Applications: []*story_service.Application{},
|
||||
Doors: []*story_service.Door{
|
||||
{
|
||||
Code: "Ы-2",
|
||||
Name: "Приложение",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user