From fd615d3fa872917a4260aaadf8f8f314b30ea3dc Mon Sep 17 00:00:00 2001 From: Viacheslav Dmitriev Date: Sat, 8 Mar 2025 11:54:29 +0300 Subject: [PATCH 1/3] Tests are boring --- go.mod | 11 +++++++ go.sum | 10 +++++++ precode.go | 58 ----------------------------------- t_test.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 58 deletions(-) create mode 100644 go.mod create mode 100644 go.sum delete mode 100644 precode.go create mode 100644 t_test.go diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..5c9d0ad1 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module v1 + +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 deleted file mode 100644 index 5139755e..00000000 --- a/precode.go +++ /dev/null @@ -1,58 +0,0 @@ -package main - -import ( - "net/http" - "net/http/httptest" - "strconv" - "strings" - "testing" -) - -var cafeList = map[string][]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)) -} - -func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) { - totalCount := 4 - req := ... // здесь нужно создать запрос к сервису - - responseRecorder := httptest.NewRecorder() - handler := http.HandlerFunc(mainHandle) - handler.ServeHTTP(responseRecorder, req) - - // здесь нужно добавить необходимые проверки -} diff --git a/t_test.go b/t_test.go new file mode 100644 index 00000000..f1d85872 --- /dev/null +++ b/t_test.go @@ -0,0 +1,88 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "strconv" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +var cafeList = map[string][]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)) +} + +func TestMainHandlerWhenOk(t *testing.T) { + req := httptest.NewRequest("GET", "/cafe?count=4&city=moscow", nil) + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + // проверка на пустоту + require.NotEmpty(t, responseRecorder.Body.String()) + // проверка на 200 OK + require.Equal(t, responseRecorder.Code, http.StatusOK) +} + +func TestMainHandlerWhenWrongCity(t *testing.T) { + req := httptest.NewRequest("GET", "/cafe?count=2&city=somecity", nil) + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + + bodyEcpected := "wrong city value" + bodyActual := responseRecorder.Body.String() + + assert.Equal(t, http.StatusBadRequest, responseRecorder.Code) + assert.Equal(t, bodyEcpected, bodyActual) +} + +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.Len(t, strings.Split(responseRecorder.Body.String(), ","), totalCount) +} From c56714133ec742cad9098a36d0d0ee0d19612657 Mon Sep 17 00:00:00 2001 From: Viacheslav Dmitriev Date: Mon, 10 Mar 2025 20:08:32 +0300 Subject: [PATCH 2/3] OK code :) --- precode.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ t_test.go | 49 ++++++------------------------------------------- 2 files changed, 51 insertions(+), 43 deletions(-) create mode 100644 precode.go diff --git a/precode.go b/precode.go new file mode 100644 index 00000000..6c864278 --- /dev/null +++ b/precode.go @@ -0,0 +1,45 @@ +package main + +import ( + "net/http" + "strconv" + "strings" +) + +var cafeList = map[string][]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)) +} diff --git a/t_test.go b/t_test.go index f1d85872..052b5cc8 100644 --- a/t_test.go +++ b/t_test.go @@ -3,7 +3,6 @@ package main import ( "net/http" "net/http/httptest" - "strconv" "strings" "testing" @@ -11,54 +10,16 @@ import ( "github.com/stretchr/testify/require" ) -var cafeList = map[string][]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)) -} - func TestMainHandlerWhenOk(t *testing.T) { req := httptest.NewRequest("GET", "/cafe?count=4&city=moscow", nil) responseRecorder := httptest.NewRecorder() handler := http.HandlerFunc(mainHandle) handler.ServeHTTP(responseRecorder, req) - // проверка на пустоту - require.NotEmpty(t, responseRecorder.Body.String()) - // проверка на 200 OK + // проверяем код ответа require.Equal(t, responseRecorder.Code, http.StatusOK) + // проверяем не пустое ли тело + assert.NotEmpty(t, responseRecorder.Body) } func TestMainHandlerWhenWrongCity(t *testing.T) { @@ -71,7 +32,7 @@ func TestMainHandlerWhenWrongCity(t *testing.T) { bodyEcpected := "wrong city value" bodyActual := responseRecorder.Body.String() - assert.Equal(t, http.StatusBadRequest, responseRecorder.Code) + require.Equal(t, http.StatusBadRequest, responseRecorder.Code) assert.Equal(t, bodyEcpected, bodyActual) } @@ -83,6 +44,8 @@ func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) { handler := http.HandlerFunc(mainHandle) handler.ServeHTTP(responseRecorder, req) + require.Equal(t, http.StatusOK, responseRecorder.Code) + // здесь нужно добавить необходимые проверки assert.Len(t, strings.Split(responseRecorder.Body.String(), ","), totalCount) } From 3e076c4240772969b6d091f024b88aa1310e8121 Mon Sep 17 00:00:00 2001 From: Viacheslav Dmitriev Date: Tue, 11 Mar 2025 07:29:01 +0300 Subject: [PATCH 3/3] OK-ok test --- t_test.go => precode_test.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename t_test.go => precode_test.go (100%) diff --git a/t_test.go b/precode_test.go similarity index 100% rename from t_test.go rename to precode_test.go