diff --git a/go.mod b/go.mod index 798468f..11136be 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21.0 require ( github.com/docker/docker v20.10.17+incompatible + github.com/jaswdr/faker/v2 v2.1.0 github.com/joho/godotenv v1.4.0 go.uber.org/mock v0.3.0 gopkg.in/telebot.v3 v3.0.0 @@ -12,9 +13,6 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/k0kubun/pp v3.0.1+incompatible // indirect - github.com/mattn/go-colorable v0.1.12 // indirect - github.com/mattn/go-isatty v0.0.14 // indirect github.com/moby/term v0.5.0 // indirect github.com/morikuni/aec v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/go.sum b/go.sum index b3b7db7..387ea61 100644 --- a/go.sum +++ b/go.sum @@ -16,8 +16,6 @@ github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= -github.com/go-faker/faker/v4 v4.2.0 h1:dGebOupKwssrODV51E0zbMrv5e2gO9VWSLNC1WDCpWg= -github.com/go-faker/faker/v4 v4.2.0/go.mod h1:F/bBy8GH9NxOxMInug5Gx4WYeG6fHJZ8Ol/dhcpRub4= github.com/go-faker/faker/v4 v4.4.1 h1:LY1jDgjVkBZWIhATCt+gkl0x9i/7wC61gZx73GTFb+Q= github.com/go-faker/faker/v4 v4.4.1/go.mod h1:HRLrjis+tYsbFtIHufEPTAIzcZiRu0rS9EYl2Ccwme4= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= @@ -31,19 +29,17 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/jaswdr/faker/v2 v2.1.0 h1:WH3gmTasNM2UctjTFAGpItyLkzei+Z48hG0VIKL1sAw= +github.com/jaswdr/faker/v2 v2.1.0/go.mod h1:ROK8xwQV0hYOLDUtxCQgHGcl10jbVzIvqHxcIDdwY2Q= github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40= -github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/moby/moby v20.10.18+incompatible h1:aAQ5lDb+SDrhVDnoMbR3kSzswd+41X34pex8VRJXvHg= github.com/moby/moby v20.10.18+incompatible/go.mod h1:fDXVQ6+S340veQPv35CzDahGBmHsiclFwfEygB/TWMc= @@ -110,16 +106,12 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= -golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.4.0 h1:Z81tqI5ddIoXDPvVQ7/7CC9TnLM7ubaFG2qXYd5BbYY= diff --git a/telegram/handlers/containers.go b/telegram/handlers/containers.go index be5fc5e..dc1ef4f 100644 --- a/telegram/handlers/containers.go +++ b/telegram/handlers/containers.go @@ -44,8 +44,7 @@ func (h *handler) ContainersNavBtn(ctx telebot.Context) error { index = tools.Indexer(index, len(containers)) current := containers[index] - err = ctx.Respond() - if err != nil { + if err := ctx.Respond(); err != nil { log.Gl.Error(err.Error()) } return ctx.Edit( diff --git a/telegram/handlers/containers_test.go b/telegram/handlers/containers_test.go index 0cb79d2..0af72d2 100644 --- a/telegram/handlers/containers_test.go +++ b/telegram/handlers/containers_test.go @@ -2,6 +2,7 @@ package handlers_test import ( "context" + "fmt" "testing" "github.com/arshamalh/dockeroller/entities" @@ -9,7 +10,7 @@ import ( "github.com/arshamalh/dockeroller/telegram/handlers" "github.com/arshamalh/dockeroller/telegram/msgs" "github.com/docker/docker/api/types/filters" - "github.com/go-faker/faker/v4" + "github.com/jaswdr/faker/v2" "github.com/stretchr/testify/assert" "go.uber.org/mock/gomock" "gopkg.in/telebot.v3" @@ -27,7 +28,7 @@ func TestContainersList(t *testing.T) { ctx := NewMockContext(ctrl) ssn := session.New() - containersList := fakeContainers(t, 3) + containersList := fakeContainers(3) // pp.Print(containersList) offBot, err := telebot.NewBot(telebot.Settings{Offline: true}) @@ -63,14 +64,91 @@ func TestContainersList(t *testing.T) { t.Run("filling the session", func(t *testing.T) {}) } -func fakeContainers(t *testing.T, howMany int) []*entities.Container { +func TestContainersNav(t *testing.T) { + assert := assert.New(t) + ctrl := gomock.NewController(t) + + docker := NewMockDocker(ctrl) + ctx := NewMockContext(ctrl) + ssn := session.New() + + const NumberOfContainers = 3 + containersList := fakeContainers(NumberOfContainers) + // pp.Print(containersList) + + offBot, err := telebot.NewBot(telebot.Settings{Offline: true}) + assert.Nil(err, "can't make offline bot") + + handler := handlers.NewHandler(offBot, docker, ssn) + + t.Run("next container when there is a next", func(t *testing.T) { + ctx.EXPECT().Respond().Return(nil) + ctx.EXPECT().Chat().Return(&telebot.Chat{ID: 467}) + ctx.EXPECT().Data().Return("1") + + docker.EXPECT().ContainersList(context.TODO(), filters.Args{}).Return(containersList) + + editMockedFunc := func(receivedContainer interface{}, opts ...interface{}) error { + formattedContainer := msgs.FmtContainer(containersList[1]) + assert.Equal(formattedContainer, receivedContainer) + return nil + } + + ctx.EXPECT().Edit(gomock.Any(), gomock.Any()).DoAndReturn(editMockedFunc) + + err := handler.ContainersNavBtn(ctx) + + assert.Nil(err) + }) + + t.Run("previous container of the first, look at last", func(t *testing.T) { + ctx.EXPECT().Respond().Return(nil) + ctx.EXPECT().Chat().Return(&telebot.Chat{ID: 467}) + ctx.EXPECT().Data().Return("-1") + + docker.EXPECT().ContainersList(context.TODO(), filters.Args{}).Return(containersList) + + editMockedFunc := func(receivedContainer interface{}, opts ...interface{}) error { + formattedContainer := msgs.FmtContainer(containersList[NumberOfContainers-1]) + assert.Equal(formattedContainer, receivedContainer) + return nil + } + + ctx.EXPECT().Edit(gomock.Any(), gomock.Any()).DoAndReturn(editMockedFunc) + + err := handler.ContainersNavBtn(ctx) + + assert.Nil(err) + }) + + t.Run("filling the session", func(t *testing.T) {}) +} + +func fakeContainers(howMany int) []*entities.Container { + faker := faker.New() + allContainerStates := []entities.ContainerState{ + entities.ContainerStateCreated, + entities.ContainerStateRunning, + entities.ContainerStateDead, + entities.ContainerStateExited, + entities.ContainerStatePaused, + } + containersList := make([]*entities.Container, howMany) - for i, _ := range containersList { - container := new(entities.Container) - if err := faker.FakeData(container); err != nil { - t.Error("can't make stubs", err) + + for i := range containersList { + ranInt := faker.IntBetween(0, len(allContainerStates)-1) + containersList[i] = &entities.Container{ + ID: faker.UUID().V4(), + Status: faker.Lorem().Sentence(10), + Name: faker.Person().FirstName(), + Image: fmt.Sprintf( + "%s:%f-%s%f", + faker.Address().City(), faker.Float32(2, 10, 99), + faker.Blood().Name(), faker.Float32(2, 10, 99), + ), + State: allContainerStates[ranInt], } - containersList[i] = container } return containersList }