From 2e5140b250b7c489951e0a83dde353069a173331 Mon Sep 17 00:00:00 2001 From: Yousuf Jawwad Date: Wed, 14 Aug 2024 22:39:20 +0500 Subject: [PATCH] chore(queue): some tests --- .github/workflows/test.yml | 5 +-- .gitignore | 1 + README.md | 1 + go.mod | 2 +- queues/queues_test.go | 89 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 queues/queues_test.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b7b488c..958e6aa 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,9 +1,6 @@ -name: Test +name: test on: - push: - branches: - - main pull_request: branches: - main diff --git a/.gitignore b/.gitignore index d20262f..c2ec9d0 100644 --- a/.gitignore +++ b/.gitignore @@ -228,3 +228,4 @@ $RECYCLE.BIN/ # End of https://www.toptal.com/developers/gitignore/api/go,osx,linux,visualstudiocode,goland,windows dist/ +ctrf-report.json diff --git a/README.md b/README.md index 937436f..f6444b5 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ ![GitHub release (with filter)](https://img.shields.io/github/v/release/breuHQ/go-temporal-tools) ![GitHub go.mod Go version (subdirectory of monorepo)](https://img.shields.io/github/go-mod/go-version/breuHQ/go-temporal-tools) +![Tests](https://img.shields.io/github/actions/workflow/status/breuHQ/go-temporal-tools/test.yml?label=test) [![License](https://img.shields.io/github/license/breuHQ/go-temporal-tools)](./LICENSE) ![GitHub contributors](https://img.shields.io/github/contributors/breuHQ/go-temporal-tools) diff --git a/go.mod b/go.mod index 0ea3e40..6f16ad8 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21.0 require ( github.com/gobeam/stringy v0.0.7 + github.com/google/uuid v1.6.0 github.com/stretchr/testify v1.9.0 go.temporal.io/api v1.32.0 go.temporal.io/sdk v1.26.1 @@ -14,7 +15,6 @@ require ( github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/mock v1.6.0 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect github.com/pborman/uuid v1.2.1 // indirect diff --git a/queues/queues_test.go b/queues/queues_test.go new file mode 100644 index 0000000..f5d7dd5 --- /dev/null +++ b/queues/queues_test.go @@ -0,0 +1,89 @@ +package queues_test + +import ( + "context" + "testing" + + "github.com/google/uuid" + "github.com/stretchr/testify/suite" + "go.temporal.io/sdk/testsuite" + "go.temporal.io/sdk/workflow" + + "go.breu.io/temporal-tools/queues" + "go.breu.io/temporal-tools/workflows" +) + +type ( + QueueTestSuite struct { + suite.Suite + testsuite.WorkflowTestSuite + + env *testsuite.TestWorkflowEnvironment + queue queues.Queue + } +) + +func (s *QueueTestSuite) SetupTest() { + s.env = s.NewTestWorkflowEnvironment() + c := &MockClient{env: s.env} + + s.queue = queues.New( + queues.WithName("test"), + queues.WithClient(c), + ) +} + +func (s *QueueTestSuite) AfterTest(suiteName, testName string) { + s.env.AssertExpectations(s.T()) +} + +func (s *QueueTestSuite) TestName() { + expected := "test" + + s.Equal(expected, s.queue.Name().String()) +} + +func (s *QueueTestSuite) TestPrefix() { + expected := "io.breu.test" + + s.Equal(expected, s.queue.Prefix()) +} + +func (s *QueueTestSuite) TestWorkflowID() { + id := uuid.New() + opts, _ := workflows.NewOptions( + workflows.WithBlock("suite"), + workflows.WithBlockID(id.String()), + ) + expected := "io.breu.test.suite." + id.String() + actual := s.queue.WorkflowID(opts) + + s.Equal(expected, actual) +} + +func (s *QueueTestSuite) TestExecuteWorkflow() { + ctx := context.Background() + id := uuid.New() + opts, _ := workflows.NewOptions( + workflows.WithBlock("test"), + workflows.WithBlockID(id.String()), + ) + + fn := func(ctx workflow.Context, in string) (string, error) { + return in + " world", nil + } + + // Execute the workflow + _, _ = s.queue.ExecuteWorkflow(ctx, opts, fn, "hello") + + expected := "hello world" + result := "" + + _ = s.env.GetWorkflowResult(&result) + + s.Equal(expected, result) +} + +func TestQueueTestSuite(t *testing.T) { + suite.Run(t, new(QueueTestSuite)) +}