Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Included tests for fxmodule #54

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/mschoch/smat v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
go.uber.org/dig v1.18.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.26.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3k
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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=
Expand Down
44 changes: 44 additions & 0 deletions pkg/storages/fxmodule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package storages

import (
"context"
"testing"

"github.com/bit-bom/minefield/pkg/graph"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
"go.uber.org/fx"
)

func TestNewRedisStorageModule(t *testing.T) {
setupTestRedis()
// Create a test Redis address
addr := "localhost:6379"

// Create a Redis client to check if Redis is available
client := redis.NewClient(&redis.Options{Addr: addr})
defer client.Close()

// Check if Redis is available
_, err := client.Ping(context.Background()).Result()
if err != nil {
t.Skipf("Skipping test: Redis is not available at %s", addr)
}

// Create an fx.App for testing
app := fx.New(
NewRedisStorageModule(addr),
fx.Invoke(func(storage graph.Storage) {
assert.Implements(t, (*graph.Storage)(nil), storage, "RedisStorage should implement graph.Storage")

// Perform additional assertions on the storage instance
redisStorage, ok := storage.(*RedisStorage)
assert.True(t, ok, "Expected storage to be of type *RedisStorage")
assert.NotNil(t, redisStorage.client, "RedisStorage client should not be nil")
}),
)

// Start and stop the fx.App
assert.NoError(t, app.Start(context.Background()))
assert.NoError(t, app.Stop(context.Background()))
Comment on lines +28 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enhance test isolation and cleanup.

Ensure that the test does not affect other tests by isolating its side effects. Consider using a mock or a separate test Redis instance. Additionally, verify that all resources are properly cleaned up after the test.

  • Consider using a mock Redis server or a separate test instance to avoid conflicts with other tests.
  • Ensure that all resources, such as connections, are closed and cleaned up after the test.

}