From 94e92c6d0e088f74a8314cde90c5c07ec57e6082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=83=D1=80=20=D0=9D=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D0=B4=D0=B8=D0=BD=D0=BE=D0=B2?= Date: Tue, 4 Mar 2025 15:37:00 +0300 Subject: [PATCH 1/2] Add tests --- go.mod | 11 +++++++++ go.sum | 10 +++++++++ precode.go | 13 ----------- precode_test.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 13 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..7e079d28 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module testify + +go 1.23.4 + +require github.com/stretchr/testify v1.10.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..713a0b4f --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +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.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +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..018f7894 100644 --- a/precode.go +++ b/precode.go @@ -2,10 +2,8 @@ package main import ( "net/http" - "net/http/httptest" "strconv" "strings" - "testing" ) var cafeList = map[string][]string{ @@ -45,14 +43,3 @@ func mainHandle(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusOK) 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..cbfb80d7 --- /dev/null +++ b/precode_test.go @@ -0,0 +1,59 @@ +package main + +import ( + "net/http" + "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=10&city=moscow", nil) + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + + // необходимые проверки + assert.Equal(t, responseRecorder.Code, 200) + + body := responseRecorder.Body.String() + + require.NotEmpty(t, body) + + list := strings.Split(body, ",") + + assert.Len(t, list, totalCount) +} + +func TestMainHandlerWhenOK(t *testing.T) { + req := httptest.NewRequest("GET", "/cafe?count=2&city=moscow", nil) + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + + // необходимые проверки + assert.Equal(t, responseRecorder.Code, 200) +} + +func TestMainHandlerWhenUnsupportedCity(t *testing.T) { + req := httptest.NewRequest("GET", "/cafe?count=2&city=kazan", nil) + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + + // необходимые проверки + assert.Equal(t, responseRecorder.Code, 400) + + body := responseRecorder.Body.String() + require.NotEmpty(t, body) + + expectedBody := "wrong city value" + assert.Equal(t, body, expectedBody) +} \ No newline at end of file From 4a48e8d0b5249090281e83986fa365d02c044c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=83=D1=80=20=D0=9D=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D0=B4=D0=B8=D0=BD=D0=BE=D0=B2?= Date: Wed, 5 Mar 2025 08:39:08 +0300 Subject: [PATCH 2/2] Change tests --- precode.go | 70 ++++++++++++++++++++++++------------------------- precode_test.go | 44 ++++++++++++++++--------------- 2 files changed, 58 insertions(+), 56 deletions(-) diff --git a/precode.go b/precode.go index 018f7894..b433dd8a 100644 --- a/precode.go +++ b/precode.go @@ -1,45 +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 cbfb80d7..d7d6f88a 100644 --- a/precode_test.go +++ b/precode_test.go @@ -11,15 +11,15 @@ import ( ) func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) { - totalCount := 4 - req := httptest.NewRequest("GET", "/cafe?count=10&city=moscow", nil) + totalCount := 4 + req := httptest.NewRequest("GET", "/cafe?count=10&city=moscow", nil) - responseRecorder := httptest.NewRecorder() - handler := http.HandlerFunc(mainHandle) - handler.ServeHTTP(responseRecorder, req) + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) - // необходимые проверки - assert.Equal(t, responseRecorder.Code, 200) + // необходимые проверки + require.Equal(t, responseRecorder.Code, http.StatusOK) body := responseRecorder.Body.String() @@ -31,29 +31,31 @@ func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) { } func TestMainHandlerWhenOK(t *testing.T) { - req := httptest.NewRequest("GET", "/cafe?count=2&city=moscow", nil) + req := httptest.NewRequest("GET", "/cafe?count=2&city=moscow", nil) - responseRecorder := httptest.NewRecorder() - handler := http.HandlerFunc(mainHandle) - handler.ServeHTTP(responseRecorder, req) + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) - // необходимые проверки - assert.Equal(t, responseRecorder.Code, 200) + // необходимые проверки + require.Equal(t, responseRecorder.Code, http.StatusOK) + + body := responseRecorder.Body.String() + require.NotEmpty(t, body) } func TestMainHandlerWhenUnsupportedCity(t *testing.T) { - req := httptest.NewRequest("GET", "/cafe?count=2&city=kazan", nil) + req := httptest.NewRequest("GET", "/cafe?count=2&city=kazan", nil) - responseRecorder := httptest.NewRecorder() - handler := http.HandlerFunc(mainHandle) - handler.ServeHTTP(responseRecorder, req) + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) - // необходимые проверки - assert.Equal(t, responseRecorder.Code, 400) + // необходимые проверки + require.Equal(t, responseRecorder.Code, http.StatusBadRequest) body := responseRecorder.Body.String() - require.NotEmpty(t, body) expectedBody := "wrong city value" assert.Equal(t, body, expectedBody) -} \ No newline at end of file +}