From 38269d0ce3d7d58db0af125eadbad18e2aeb1948 Mon Sep 17 00:00:00 2001 From: Denis Strelnikov Date: Mon, 8 Jul 2024 19:46:08 +0300 Subject: [PATCH 1/2] check --- go.mod | 10 ++++++++++ go.sum | 9 +++++++++ precode.go | 13 +----------- precode_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 precode_test.go diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..b72b2acc --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module sprint7 + +go 1.22.3 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.9.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..e20fa14b --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/precode.go b/precode.go index 5139755e..d5e4d49a 100644 --- a/precode.go +++ b/precode.go @@ -2,14 +2,12 @@ package main import ( "net/http" - "net/http/httptest" "strconv" "strings" - "testing" ) var cafeList = map[string][]string{ - "moscow": []string{"Мир кофе", "Сладкоежка", "Кофе и завтраки", "Сытый студент"}, + "moscow": []string{"Мир кофе", "Сладкоежка", "Кофе и завтраки", "Сытый студент", "тест"}, } func mainHandle(w http.ResponseWriter, req *http.Request) { @@ -46,13 +44,4 @@ func mainHandle(w http.ResponseWriter, req *http.Request) { w.Write([]byte(answer)) } -func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) { - totalCount := 4 - req := ... // здесь нужно создать запрос к сервису - responseRecorder := httptest.NewRecorder() - handler := http.HandlerFunc(mainHandle) - handler.ServeHTTP(responseRecorder, req) - - // здесь нужно добавить необходимые проверки -} diff --git a/precode_test.go b/precode_test.go new file mode 100644 index 00000000..265300bf --- /dev/null +++ b/precode_test.go @@ -0,0 +1,53 @@ +package main + +import( + "net/http" + "strings" + "net/http/httptest" + "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + + +func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) { + totalCount := 4 + req := httptest.NewRequest("GET", "/cafe?count=3&city=moscow", nil) + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + + require.Equal(t, http.StatusOK, responseRecorder.Code) + require.NotEmpty(t, responseRecorder.Body) + + body := responseRecorder.Body.String() + arr := strings.Split(body, ",") + + assert.Equal(t, totalCount, len(arr)) +} + +func TestMainHadlerWhenOk(t *testing.T){ + req := httptest.NewRequest("GET", "/cafe?count=3&city=moscow", nil) + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + + require.Equal(t, http.StatusOK, responseRecorder.Code) + require.NotNil(t, responseRecorder.Body) + +} + + +func TestMainHandlerNotOk(t *testing.T){ + req := httptest.NewRequest("GET", "/cafe?count=3&city=moscow", nil) + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + + require.Equal(t, http.StatusBadRequest, responseRecorder.Code) + expected := "wrong city value" + require.Equal(t, expected, responseRecorder.Body.String()) +} From c707e1a9ea7d781a915ba8d3e4f714d23e754133 Mon Sep 17 00:00:00 2001 From: Denis Strelnikov Date: Tue, 9 Jul 2024 10:12:30 +0300 Subject: [PATCH 2/2] check2 --- precode.go | 72 ++++++++++++++++++++++++------------------------- precode_test.go | 41 +++++++++++++--------------- 2 files changed, 54 insertions(+), 59 deletions(-) diff --git a/precode.go b/precode.go index d5e4d49a..b433dd8a 100644 --- a/precode.go +++ b/precode.go @@ -1,47 +1,45 @@ package main import ( - "net/http" - "strconv" - "strings" + "net/http" + "strconv" + "strings" ) var cafeList = map[string][]string{ - "moscow": []string{"Мир кофе", "Сладкоежка", "Кофе и завтраки", "Сытый студент", "тест"}, + "moscow": []string{"Мир кофе", "Сладкоежка", "Кофе и завтраки", "Сытый студент"}, } func mainHandle(w http.ResponseWriter, req *http.Request) { - countStr := req.URL.Query().Get("count") - if countStr == "" { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("count missing")) - return - } - - count, err := strconv.Atoi(countStr) - if err != nil { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("wrong count value")) - return - } - - city := req.URL.Query().Get("city") - - cafe, ok := cafeList[city] - if !ok { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("wrong city value")) - return - } - - if count > len(cafe) { - count = len(cafe) - } - - answer := strings.Join(cafe[:count], ",") - - w.WriteHeader(http.StatusOK) - w.Write([]byte(answer)) + countStr := req.URL.Query().Get("count") + if countStr == "" { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("count missing")) + return + } + + count, err := strconv.Atoi(countStr) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("wrong count value")) + return + } + + city := req.URL.Query().Get("city") + + cafe, ok := cafeList[city] + if !ok { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("wrong city value")) + return + } + + if count > len(cafe) { + count = len(cafe) + } + + answer := strings.Join(cafe[:count], ",") + + w.WriteHeader(http.StatusOK) + w.Write([]byte(answer)) } - - diff --git a/precode_test.go b/precode_test.go index 265300bf..13825f44 100644 --- a/precode_test.go +++ b/precode_test.go @@ -1,33 +1,31 @@ package main -import( +import ( + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "net/http" - "strings" "net/http/httptest" + "strings" "testing" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) - func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) { - totalCount := 4 - req := httptest.NewRequest("GET", "/cafe?count=3&city=moscow", nil) - - responseRecorder := httptest.NewRecorder() - handler := http.HandlerFunc(mainHandle) - handler.ServeHTTP(responseRecorder, req) - - require.Equal(t, http.StatusOK, responseRecorder.Code) - require.NotEmpty(t, responseRecorder.Body) - - body := responseRecorder.Body.String() - arr := strings.Split(body, ",") - - assert.Equal(t, totalCount, len(arr)) + totalCount := 4 + req := httptest.NewRequest("GET", "/cafe?count=3&city=moscow", nil) + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + + require.Equal(t, http.StatusOK, responseRecorder.Code) + + body := responseRecorder.Body.String() + arr := strings.Split(body, ",") + + assert.Len(t, arr, totalCount) } -func TestMainHadlerWhenOk(t *testing.T){ +func TestMainHadlerWhenOk(t *testing.T) { req := httptest.NewRequest("GET", "/cafe?count=3&city=moscow", nil) responseRecorder := httptest.NewRecorder() @@ -39,8 +37,7 @@ func TestMainHadlerWhenOk(t *testing.T){ } - -func TestMainHandlerNotOk(t *testing.T){ +func TestMainHandlerNotOk(t *testing.T) { req := httptest.NewRequest("GET", "/cafe?count=3&city=moscow", nil) responseRecorder := httptest.NewRecorder()