From 85da64f6949d9fd202907e10ebfedf4aa20f9620 Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Sun, 22 May 2022 01:16:47 +1000 Subject: [PATCH 01/10] more tests --- posts/client_test.go | 93 ++++++++++++++++++++++++++++++++++++++++++++ tgstat.go | 7 ++++ 2 files changed, 100 insertions(+) diff --git a/posts/client_test.go b/posts/client_test.go index 7a00f5b..ec12303 100644 --- a/posts/client_test.go +++ b/posts/client_test.go @@ -3,6 +3,7 @@ package posts import ( "context" "encoding/json" + "fmt" tgstat "github.com/helios-ag/tgstat-go" "github.com/helios-ag/tgstat-go/endpoints" server "github.com/helios-ag/tgstat-go/testing" @@ -17,6 +18,10 @@ func prepareClient(URL string) { tgstat.WithEndpoint(URL) } +var NewRestRequestStub = func(c *tgstat.Client, ctx context.Context, token, method, urlPath string, data map[string]string) (*http.Request, error) { + return nil, fmt.Errorf("error happened") +} + func TestClient_PostsGet(t *testing.T) { RegisterTestingT(t) t.Run("Test host not reachable", func(t *testing.T) { @@ -26,6 +31,28 @@ func TestClient_PostsGet(t *testing.T) { Expect(err.Error()).To(ContainSubstring("dial tcp")) }) + t.Run("Test validation", func(t *testing.T) { + testServer := server.NewServer() + defer testServer.Teardown() + prepareClient(testServer.URL) + + _, _, err := Get(context.Background(), "") + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("postId can not be empty")) + }) + + t.Run("Test newrest request triggers error", func(t *testing.T) { + testServer := server.NewServer() + defer testServer.Teardown() + prepareClient(testServer.URL) + oldNewRequest := tgstat.NewRestRequest + tgstat.NewRestRequest = NewRestRequestStub + _, _, err := Get(context.Background(), "") + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("postId can not be empty")) + tgstat.NewRestRequest = oldNewRequest + }) + t.Run("Test PostsGet response Mapping", func(t *testing.T) { testServer := server.NewServer() defer testServer.Teardown() @@ -74,6 +101,59 @@ func TestClient_PostsStat(t *testing.T) { Expect(err.Error()).To(ContainSubstring("PostId: cannot be blank")) }) + t.Run("Test PostStat group isn't nil and not valid", func(t *testing.T) { + testServer := server.NewServer() + defer testServer.Teardown() + prepareClient(testServer.URL) + + req := PostStatRequest{ + PostId: "t.me/123/123", + Group: tgstat.String("test"), + } + _, _, err := PostStat(context.Background(), req) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("must be a valid value")) + }) + + t.Run("Test PostStat group isn't nil", func(t *testing.T) { + testServer := server.NewServer() + defer testServer.Teardown() + prepareClient(testServer.URL) + + testServer.Mux.HandleFunc(endpoints.PostsStat, func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(tgstat.PostStatResult{ + Status: "ok", + Response: tgstat.PostStatResponse{}, + }) + }) + + req := PostStatRequest{ + PostId: "t.me/123/123", + Group: tgstat.String("day"), + } + _, _, err := PostStat(context.Background(), req) + Expect(err).ToNot(HaveOccurred()) + }) + + t.Run("Test rest request triggers error", func(t *testing.T) { + testServer := server.NewServer() + defer testServer.Teardown() + prepareClient(testServer.URL) + oldNewRequest := tgstat.NewRestRequest + tgstat.NewRestRequest = NewRestRequestStub + req := PostStatRequest{ + PostId: "321", + Group: nil, + } + + _, _, err := PostStat(context.Background(), req) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("error happened")) + tgstat.NewRestRequest = oldNewRequest + }) + t.Run("Test PostsStat response Mapping", func(t *testing.T) { testServer := server.NewServer() defer testServer.Teardown() @@ -113,6 +193,19 @@ func TestClient_PostsSearch(t *testing.T) { Expect(err.Error()).To(ContainSubstring("dial tcp")) }) + t.Run("Test search validation", func(t *testing.T) { + testServer := server.NewServer() + defer testServer.Teardown() + prepareClient(testServer.URL) + + req := PostSearchRequest{ + Q: "", + } + _, _, err := PostSearch(context.Background(), req) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("cannot be blank")) + }) + t.Run("Test PostsSearch response Mapping", func(t *testing.T) { testServer := server.NewServer() defer testServer.Teardown() diff --git a/tgstat.go b/tgstat.go index b6d4bde..ea543b5 100644 --- a/tgstat.go +++ b/tgstat.go @@ -205,3 +205,10 @@ func GetAPI(options ...ClientOption) API { return newAPI(TGStatClient.Url, options...) } + +func String(v string) *string { + if v == "" { + return nil + } + return &v +} From eea1655ace46900177d11d2f82dd9109a0a0e860 Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Sun, 22 May 2022 12:49:46 +1000 Subject: [PATCH 02/10] updated deps --- go.mod | 6 +++--- go.sum | 56 +++++++------------------------------------------------- 2 files changed, 10 insertions(+), 52 deletions(-) diff --git a/go.mod b/go.mod index 7d87bae..5a7cdb6 100644 --- a/go.mod +++ b/go.mod @@ -4,14 +4,14 @@ go 1.18 require ( github.com/go-ozzo/ozzo-validation/v4 v4.3.0 - github.com/onsi/gomega v1.11.0 + github.com/onsi/gomega v1.19.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/stretchr/testify v1.6.1 // indirect - golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb // indirect + golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e // indirect - golang.org/x/text v0.3.3 // indirect + golang.org/x/text v0.3.7 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 4a8f5e1..b67d464 100644 --- a/go.sum +++ b/go.sum @@ -3,68 +3,26 @@ github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:o github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= -github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es= github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= -github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= -github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= -github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= -github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= -github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= -github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= -github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= -github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/ginkgo v1.12.1 h1:mFwc4LvZ0xpSvDZ3E+k8Yte0hLOMxXUlP+yXtJqkYfQ= -github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= -github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= -github.com/onsi/gomega v1.11.0 h1:+CqWgvj0OZycCaqclBD1pxKHAU+tOkHmQIWvDHq2aug= -github.com/onsi/gomega v1.11.0/go.mod h1:azGKhqFUon9Vuj0YmTfLSmx0FUwqXYSTl5re8lQLTUg= +github.com/onsi/ginkgo/v2 v2.1.3 h1:e/3Cwtogj0HA+25nMP1jCMDIf8RtRYbGwGGuBIFztkc= +github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw= +github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro= 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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb h1:eBmm0M9fYhWpKZLjQUUKka/LtIxf46G4fxeEz5KJr9U= -golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc= +golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e h1:w36l2Uw3dRan1K3TyXriXvY+6T56GNmlKGcqiQUJDfM= golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= -google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= -google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= -google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= -google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= -google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 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/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= From 4a140b72104e335d70ce15a090d6e5bb1253ebfd Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Sun, 22 May 2022 12:52:28 +1000 Subject: [PATCH 03/10] added smol test --- tgstat.go | 4 ++++ tgstat_test.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/tgstat.go b/tgstat.go index ea543b5..6d477f8 100644 --- a/tgstat.go +++ b/tgstat.go @@ -212,3 +212,7 @@ func String(v string) *string { } return &v } + +func Bool(b bool) *bool { + return &b +} diff --git a/tgstat_test.go b/tgstat_test.go index e0f3e76..3018128 100644 --- a/tgstat_test.go +++ b/tgstat_test.go @@ -186,3 +186,19 @@ func TestNewRequest(t *testing.T) { Expect(err.Error()).To(ContainSubstring("unable to parse URL")) }) } + +func TestString(t *testing.T) { + RegisterTestingT(t) + t.Run("Test string point", func(t *testing.T) { + val := String("value") + Expect(&val).To(HaveValue(Equal("value"))) + }) +} + +func TestBool(t *testing.T) { + RegisterTestingT(t) + t.Run("Test bool converter point", func(t *testing.T) { + val := Bool(false) + Expect(&val).To(HaveValue(Equal(false))) + }) +} From be1d823818c7a07c65e4031f33447267733fc145 Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Sun, 22 May 2022 15:06:16 +1000 Subject: [PATCH 04/10] test bodyrequest --- posts/client.go | 8 +++++++ posts/client_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/posts/client.go b/posts/client.go index b748dfc..19d2ac6 100644 --- a/posts/client.go +++ b/posts/client.go @@ -238,6 +238,14 @@ func makeRequestBody(request PostSearchRequest) map[string]string { } }() + body["strongSearch"] = func() string { + if nil != request.StrongSearch && *request.StrongSearch { + return "1" + } else { + return "0" + } + }() + return body } diff --git a/posts/client_test.go b/posts/client_test.go index ec12303..a6f42d1 100644 --- a/posts/client_test.go +++ b/posts/client_test.go @@ -193,6 +193,22 @@ func TestClient_PostsSearch(t *testing.T) { Expect(err.Error()).To(ContainSubstring("dial tcp")) }) + t.Run("Test rest triggers error", func(t *testing.T) { + testServer := server.NewServer() + defer testServer.Teardown() + prepareClient(testServer.URL) + oldNewRequest := tgstat.NewRestRequest + tgstat.NewRestRequest = NewRestRequestStub + req := PostSearchRequest{ + Q: "Query", + } + + _, _, err := PostSearch(context.Background(), req) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("error happened")) + tgstat.NewRestRequest = oldNewRequest + }) + t.Run("Test search validation", func(t *testing.T) { testServer := server.NewServer() defer testServer.Teardown() @@ -204,6 +220,23 @@ func TestClient_PostsSearch(t *testing.T) { _, _, err := PostSearch(context.Background(), req) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("cannot be blank")) + + req = PostSearchRequest{ + Q: "val", + Limit: tgstat.Int(100), + } + _, _, err = PostSearch(context.Background(), req) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("Limit: must be no greater than")) + + req = PostSearchRequest{ + Q: "val", + Offset: tgstat.Int(100), + } + _, _, err = PostSearch(context.Background(), req) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("Offset: must be no greater than")) + }) t.Run("Test PostsSearch response Mapping", func(t *testing.T) { @@ -268,3 +301,24 @@ func TestClient_PostsSearchExtended(t *testing.T) { }))) }) } + +func Test_makeRequestBody(t *testing.T) { + RegisterTestingT(t) + t.Run("Test body request", func(t *testing.T) { + req := PostSearchRequest{ + Q: "Test", + Limit: tgstat.Int(30), + Offset: tgstat.Int(20), + PeerType: tgstat.String("1"), + StartDate: tgstat.String("1"), + EndDate: tgstat.String("1"), + HideForwards: tgstat.Bool(true), + HideDeleted: tgstat.Bool(true), + StrongSearch: tgstat.Bool(true), + MinusWords: tgstat.String("words"), + ExtendedSyntax: tgstat.Bool(false), + } + body := makeRequestBody(req) + Expect(body).Should(ContainElements("30", "20", "1", "1", "1", "1", "1", "1", "words", "0")) + }) +} From 127f54f013517a84de311f59c2ab03605cd9743f Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Sun, 22 May 2022 15:08:30 +1000 Subject: [PATCH 05/10] More test cases --- tgstat.go | 4 ++++ tgstat_test.go | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/tgstat.go b/tgstat.go index 6d477f8..14cd6bf 100644 --- a/tgstat.go +++ b/tgstat.go @@ -216,3 +216,7 @@ func String(v string) *string { func Bool(b bool) *bool { return &b } + +func Int(v int) *int { + return &v +} diff --git a/tgstat_test.go b/tgstat_test.go index 3018128..cf4bbdb 100644 --- a/tgstat_test.go +++ b/tgstat_test.go @@ -202,3 +202,11 @@ func TestBool(t *testing.T) { Expect(&val).To(HaveValue(Equal(false))) }) } + +func TestInt(t *testing.T) { + RegisterTestingT(t) + t.Run("Test int converter point", func(t *testing.T) { + val := Int(100) + Expect(&val).To(HaveValue(Equal(100))) + }) +} From 554dfcfbc04574213cfddea0c1235d326c0f64df Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Sun, 22 May 2022 15:51:45 +1000 Subject: [PATCH 06/10] more posts tests --- posts/client_test.go | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/posts/client_test.go b/posts/client_test.go index a6f42d1..ec65fbf 100644 --- a/posts/client_test.go +++ b/posts/client_test.go @@ -41,13 +41,13 @@ func TestClient_PostsGet(t *testing.T) { Expect(err.Error()).To(ContainSubstring("postId can not be empty")) }) - t.Run("Test newrest request triggers error", func(t *testing.T) { + t.Run("Test rest request triggers error", func(t *testing.T) { testServer := server.NewServer() defer testServer.Teardown() prepareClient(testServer.URL) oldNewRequest := tgstat.NewRestRequest tgstat.NewRestRequest = NewRestRequestStub - _, _, err := Get(context.Background(), "") + _, _, err := Get(context.Background(), "t.me/123") Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("postId can not be empty")) tgstat.NewRestRequest = oldNewRequest @@ -277,6 +277,20 @@ func TestClient_PostsSearchExtended(t *testing.T) { Expect(err.Error()).To(ContainSubstring("dial tcp")) }) + t.Run("Test rest triggers error", func(t *testing.T) { + testServer := server.NewServer() + defer testServer.Teardown() + prepareClient(testServer.URL) + oldNewRequest := tgstat.NewRestRequest + tgstat.NewRestRequest = NewRestRequestStub + req := PostSearchRequest{ + Q: "Search", + } + _, _, err := PostSearchExtended(context.Background(), req) + Expect(err.Error()).To(ContainSubstring("error happened")) + tgstat.NewRestRequest = oldNewRequest + }) + t.Run("Test PostsGet response Mapping", func(t *testing.T) { testServer := server.NewServer() defer testServer.Teardown() @@ -321,4 +335,22 @@ func Test_makeRequestBody(t *testing.T) { body := makeRequestBody(req) Expect(body).Should(ContainElements("30", "20", "1", "1", "1", "1", "1", "1", "words", "0")) }) + + t.Run("Test body request with inverted data", func(t *testing.T) { + req := PostSearchRequest{ + Q: "Test", + Limit: tgstat.Int(30), + Offset: tgstat.Int(20), + PeerType: tgstat.String("1"), + StartDate: tgstat.String("1"), + EndDate: tgstat.String("1"), + HideForwards: tgstat.Bool(false), + HideDeleted: tgstat.Bool(false), + StrongSearch: tgstat.Bool(false), + MinusWords: tgstat.String("words, words"), + ExtendedSyntax: tgstat.Bool(true), + } + body := makeRequestBody(req) + Expect(body).Should(ContainElements("30", "20", "1", "1", "1", "0", "0", "0", "words, words", "1")) + }) } From 1a0f1d4bb273235200c2183322ef336dc3c85e21 Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Sun, 22 May 2022 15:51:54 +1000 Subject: [PATCH 07/10] more callback tests --- callback/client.go | 2 +- callback/client_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/callback/client.go b/callback/client.go index 7e05f56..946cdb8 100644 --- a/callback/client.go +++ b/callback/client.go @@ -93,7 +93,7 @@ type SubscribeChannelRequest struct { func (subscribeChannelRequest SubscribeChannelRequest) Validate() error { return validation.ValidateStruct(&subscribeChannelRequest, validation.Field(&subscribeChannelRequest.ChannelId, validation.Required), - validation.Field(&subscribeChannelRequest.EventTypes, validation.Required), + validation.Field(&subscribeChannelRequest.EventTypes, validation.Required, validation.In("new_post", "edit_post", "remove_post")), ) } diff --git a/callback/client_test.go b/callback/client_test.go index ace7998..2ed1a84 100644 --- a/callback/client_test.go +++ b/callback/client_test.go @@ -47,6 +47,24 @@ func TestClient_SetCallback(t *testing.T) { Expect(err.Error()).To(Equal("CallbackUrl must be set")) }) + t.Run("Test validate callback", func(t *testing.T) { + testServer := server.NewServer() + defer testServer.Teardown() + prepareClient(testServer.URL) + testServer.Mux.HandleFunc(endpoints.SetCallbackURL, func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(tgstat.SetCallbackVerificationResult{ + Status: "error", + Error: "wrong verify code", + VerifyCode: "TGSTAT_VERIFY_CODE_123456", + }) + }) + _, _, err := SetCallback(context.Background(), "hi/there?") + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("unable to parse URL")) + }) + t.Run("Test SetCallback response Mapping", func(t *testing.T) { testServer := server.NewServer() defer testServer.Teardown() @@ -149,6 +167,22 @@ func TestClient_SubscribeChannel(t *testing.T) { tgstat.NewRestRequest = oldNewRequest }) + t.Run("Test validation", func(t *testing.T) { + testServer := server.NewServer() + defer testServer.Teardown() + prepareClient(testServer.URL) + + req := SubscribeChannelRequest{ + SubscriptionId: String("blabla"), + ChannelId: "t.me/username", + EventTypes: "new_post1", + } + + _, _, err := SubscribeChannel(context.Background(), req) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("EventTypes: must be a valid value")) + }) + t.Run("Test SubscribeChannel response Mapping", func(t *testing.T) { testServer := server.NewServer() defer testServer.Teardown() From efb6ae1dbcf9270a4f59db4c3850fb30be8e1ea3 Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Sun, 22 May 2022 16:04:07 +1000 Subject: [PATCH 08/10] fixes and tests --- posts/client_test.go | 2 +- usage/client_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/posts/client_test.go b/posts/client_test.go index ec65fbf..f71367c 100644 --- a/posts/client_test.go +++ b/posts/client_test.go @@ -49,7 +49,7 @@ func TestClient_PostsGet(t *testing.T) { tgstat.NewRestRequest = NewRestRequestStub _, _, err := Get(context.Background(), "t.me/123") Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("postId can not be empty")) + Expect(err.Error()).To(ContainSubstring("error happened")) tgstat.NewRestRequest = oldNewRequest }) diff --git a/usage/client_test.go b/usage/client_test.go index 6a0b503..ff16fa2 100644 --- a/usage/client_test.go +++ b/usage/client_test.go @@ -3,6 +3,7 @@ package usage import ( "context" "encoding/json" + "fmt" tgstat "github.com/helios-ag/tgstat-go" "github.com/helios-ag/tgstat-go/endpoints" server "github.com/helios-ag/tgstat-go/testing" @@ -17,6 +18,10 @@ func prepareClient(URL string) { tgstat.WithEndpoint(URL) } +var NewRestRequestStub = func(c *tgstat.Client, ctx context.Context, token, method, urlPath string, data map[string]string) (*http.Request, error) { + return nil, fmt.Errorf("error happened") +} + func TestClient_UsageStat(t *testing.T) { RegisterTestingT(t) t.Run("Test host not reachable", func(t *testing.T) { @@ -48,4 +53,15 @@ func TestClient_UsageStat(t *testing.T) { "Status": ContainSubstring("ok"), }))) }) + + t.Run("Test request trigger error", func(t *testing.T) { + prepareClient("http://local123") + oldNewRequest := tgstat.NewRestRequest + tgstat.NewRestRequest = NewRestRequestStub + + _, _, err := Stat(context.Background()) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("error happened")) + tgstat.NewRestRequest = oldNewRequest + }) } From da45425becca9956ae5621de35c6ed4200b43529 Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Sun, 22 May 2022 16:05:11 +1000 Subject: [PATCH 09/10] raise go version --- .github/workflows/go.yaml | 2 +- .github/workflows/staticcheck.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index 1647291..9d98d60 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -16,7 +16,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.17 + go-version: 1.18 - name: Prepare run: go get -t -v ./... diff --git a/.github/workflows/staticcheck.yml b/.github/workflows/staticcheck.yml index a1cfc55..d640436 100644 --- a/.github/workflows/staticcheck.yml +++ b/.github/workflows/staticcheck.yml @@ -13,7 +13,7 @@ jobs: fail-fast: false matrix: os: ["windows-latest", "ubuntu-latest", "macOS-latest"] - go: ["1.17.x"] + go: ["1.18.x"] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v1 From 1d90e1691ab51f72b77b5e5db30b933d8f8e5988 Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Sun, 22 May 2022 16:11:11 +1000 Subject: [PATCH 10/10] staticheck fix --- .github/workflows/staticcheck.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/staticcheck.yml b/.github/workflows/staticcheck.yml index d640436..9bffa53 100644 --- a/.github/workflows/staticcheck.yml +++ b/.github/workflows/staticcheck.yml @@ -24,8 +24,8 @@ jobs: go-version: ${{ matrix.go }} - run: "go test ./..." - run: "go vet ./..." - - uses: dominikh/staticcheck-action@v1.1.0 + - uses: dominikh/staticcheck-action@v1.2.0 with: - version: "2021.1.1" + version: "2022.1" install-go: false cache-key: ${{ matrix.go }} \ No newline at end of file