From 8e17da2073d205766cb9ac06ffcab19a594a4fad Mon Sep 17 00:00:00 2001 From: dimkene Date: Wed, 27 Mar 2024 01:57:30 +0500 Subject: [PATCH 1/4] first test --- go.mod | 5 +++++ go.sum | 2 ++ main.go | 45 ++++++++++++++++++++++++++++++++++++++++ main_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++ precode.go | 58 ---------------------------------------------------- 5 files changed, 104 insertions(+), 58 deletions(-) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 main_test.go delete mode 100644 precode.go diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..ce4afa04 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/Yandex-Practicum/go-testify + +go 1.20 + +require github.com/stretchr/testify v1.9.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..f2033831 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +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= diff --git a/main.go b/main.go new file mode 100644 index 00000000..908d1ef8 --- /dev/null +++ b/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "net/http" + "strconv" + "strings" +) + +var cafeList = map[string][]string{ + "moscow": {"Мир кофе", "Сладкоежка", "Кофе и завтраки", "Сытый студент"}, +} + +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/main_test.go b/main_test.go new file mode 100644 index 00000000..6f2a8932 --- /dev/null +++ b/main_test.go @@ -0,0 +1,52 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "strconv" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMainHandlerWhenAllRight(t *testing.T) { + totalCount := 4 + req := httptest.NewRequest("GET", "/cafe?city=moscow&count="+strconv.Itoa(totalCount), nil) // здесь нужно создать запрос к сервису + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + + // здесь нужно добавить необходимые проверки + require.Equal(t, http.StatusOK, responseRecorder.Code) + require.NotEmpty(t, responseRecorder.Body) +} + +func TestMainHandlerWhenCityWrong(t *testing.T) { + totalCount := 4 + req := httptest.NewRequest("GET", "/cafe?city=spb&count="+strconv.Itoa(totalCount), nil) // здесь нужно создать запрос к сервису + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + + // здесь нужно добавить необходимые проверки + require.Equal(t, http.StatusBadRequest, responseRecorder.Code) + assert.Equal(t, "wrong city value", responseRecorder.Body +} + +func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) { + totalCount := 4 + req := httptest.NewRequest("GET", "/cafe?city=moscow&count="+strconv.Itoa(totalCount+1), nil) // здесь нужно создать запрос к сервису + + responseRecorder := httptest.NewRecorder() + handler := http.HandlerFunc(mainHandle) + handler.ServeHTTP(responseRecorder, req) + + // здесь нужно добавить необходимые проверки + require.Equal(t, http.StatusOK, responseRecorder.Code) + require.NotEmpty(t, responseRecorder.Body + assert.Equal(t, strings.Join(cafeList["moscow"][:totalCount], ","), responseRecorder.Body) +} 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) - - // здесь нужно добавить необходимые проверки -} From bd87e963e31d3b1a28b7d2a94224acf01d706871 Mon Sep 17 00:00:00 2001 From: dimkene Date: Tue, 2 Apr 2024 01:06:31 +0500 Subject: [PATCH 2/4] fix missing parenthesis, add main, fix test --- go.mod | 12 +++++++++--- go.sum | 8 ++++++++ main.go | 10 +++++++++- main_test.go | 41 +++++++++++++++++++++-------------------- 4 files changed, 47 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index ce4afa04..89b7bc81 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,11 @@ -module github.com/Yandex-Practicum/go-testify +module main.go -go 1.20 +go 1.21.1 -require github.com/stretchr/testify v1.9.0 // indirect +require github.com/stretchr/testify v1.9.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 +) \ No newline at end of file diff --git a/go.sum b/go.sum index f2033831..774a0411 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +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.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 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= \ No newline at end of file diff --git a/main.go b/main.go index 908d1ef8..b06ea93f 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,7 @@ import ( ) var cafeList = map[string][]string{ - "moscow": {"Мир кофе", "Сладкоежка", "Кофе и завтраки", "Сытый студент"}, + "moscow": []string{"Мир кофе", "Сладкоежка", "Кофе и завтраки", "Сытый студент"}, } func mainHandle(w http.ResponseWriter, req *http.Request) { @@ -43,3 +43,11 @@ func mainHandle(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte(answer)) } + +func main() { + http.HandleFunc(`/cafe`, mainHandle) + err := http.ListenAndServe(":8080", nil) + if err != nil { + panic(err) + } +} diff --git a/main_test.go b/main_test.go index 6f2a8932..8a5fa4fa 100644 --- a/main_test.go +++ b/main_test.go @@ -3,7 +3,6 @@ package main import ( "net/http" "net/http/httptest" - "strconv" "strings" "testing" @@ -12,41 +11,43 @@ import ( ) func TestMainHandlerWhenAllRight(t *testing.T) { - totalCount := 4 - req := httptest.NewRequest("GET", "/cafe?city=moscow&count="+strconv.Itoa(totalCount), nil) // здесь нужно создать запрос к сервису - + req := httptest.NewRequest("GET", "/cafe?count=4&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) -} + require.NotEmpty(t, responseRecorder.Code) -func TestMainHandlerWhenCityWrong(t *testing.T) { - totalCount := 4 - req := httptest.NewRequest("GET", "/cafe?city=spb&count="+strconv.Itoa(totalCount), nil) // здесь нужно создать запрос к сервису + status := responseRecorder.Code + require.Equal(t, status, http.StatusOK) +} +func TestMainHandWrongCity(t *testing.T) { + req := httptest.NewRequest("GET", "/cafe?count=10&city=mocsow", nil) responseRecorder := httptest.NewRecorder() handler := http.HandlerFunc(mainHandle) handler.ServeHTTP(responseRecorder, req) - // здесь нужно добавить необходимые проверки - require.Equal(t, http.StatusBadRequest, responseRecorder.Code) - assert.Equal(t, "wrong city value", responseRecorder.Body + body := responseRecorder.Body.String() + assert.Equal(t, "wrong city value", body) } func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) { totalCount := 4 - req := httptest.NewRequest("GET", "/cafe?city=moscow&count="+strconv.Itoa(totalCount+1), nil) // здесь нужно создать запрос к сервису - + req := httptest.NewRequest("GET", "/cafe?count=10&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 - assert.Equal(t, strings.Join(cafeList["moscow"][:totalCount], ","), responseRecorder.Body) + require.NotEmpty(t, responseRecorder.Code) + + status := responseRecorder.Code + require.Equal(t, status, http.StatusOK) + + body := responseRecorder.Body.String() + assert.NotEqual(t, "wrong city value", body) + + list := strings.Split(body, ",") + assert.Equal(t, len(list), totalCount) + } From 5b4c81526367d9353876dbc640962a74a29dce8a Mon Sep 17 00:00:00 2001 From: dimkene Date: Tue, 2 Apr 2024 17:40:38 +0500 Subject: [PATCH 3/4] fix param order, fix checks --- main_test.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/main_test.go b/main_test.go index 8a5fa4fa..c2da5914 100644 --- a/main_test.go +++ b/main_test.go @@ -16,10 +16,10 @@ func TestMainHandlerWhenAllRight(t *testing.T) { handler := http.HandlerFunc(mainHandle) handler.ServeHTTP(responseRecorder, req) - require.NotEmpty(t, responseRecorder.Code) - status := responseRecorder.Code - require.Equal(t, status, http.StatusOK) + body := responseRecorder.Body + require.Equal(t, http.StatusOK, status) + assert.NotEmpty(t, body) } func TestMainHandWrongCity(t *testing.T) { @@ -28,8 +28,11 @@ func TestMainHandWrongCity(t *testing.T) { handler := http.HandlerFunc(mainHandle) handler.ServeHTTP(responseRecorder, req) + status := responseRecorder.Code body := responseRecorder.Body.String() + require.Equal(t, http.StatusOK, status) assert.Equal(t, "wrong city value", body) + } func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) { @@ -42,12 +45,10 @@ func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) { require.NotEmpty(t, responseRecorder.Code) status := responseRecorder.Code - require.Equal(t, status, http.StatusOK) + require.Equal(t, http.StatusOK, status) body := responseRecorder.Body.String() - assert.NotEqual(t, "wrong city value", body) - list := strings.Split(body, ",") - assert.Equal(t, len(list), totalCount) + assert.Equal(t, totalCount, len(list)) } From bd65143d44bc448da03434fef0215abffee3f0bb Mon Sep 17 00:00:00 2001 From: dimkene Date: Tue, 2 Apr 2024 19:30:31 +0500 Subject: [PATCH 4/4] fix wrongcity test --- go.mod | 2 +- main_test.go | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 89b7bc81..a1a65e80 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module main.go -go 1.21.1 +go 1.20 require github.com/stretchr/testify v1.9.0 diff --git a/main_test.go b/main_test.go index c2da5914..7b34b600 100644 --- a/main_test.go +++ b/main_test.go @@ -23,16 +23,15 @@ func TestMainHandlerWhenAllRight(t *testing.T) { } func TestMainHandWrongCity(t *testing.T) { - req := httptest.NewRequest("GET", "/cafe?count=10&city=mocsow", nil) + req := httptest.NewRequest("GET", "/cafe?count=4&city=mocsow", nil) responseRecorder := httptest.NewRecorder() handler := http.HandlerFunc(mainHandle) handler.ServeHTTP(responseRecorder, req) status := responseRecorder.Code body := responseRecorder.Body.String() - require.Equal(t, http.StatusOK, status) + require.Equal(t, http.StatusBadRequest, status) assert.Equal(t, "wrong city value", body) - } func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) { @@ -50,5 +49,4 @@ func TestMainHandlerWhenCountMoreThanTotal(t *testing.T) { body := responseRecorder.Body.String() list := strings.Split(body, ",") assert.Equal(t, totalCount, len(list)) - }