Skip to content

Commit

Permalink
Use env variable for test redis is possible
Browse files Browse the repository at this point in the history
Signed-off-by: neilnaveen <42328488+neilnaveen@users.noreply.github.com>
  • Loading branch information
neilnaveen committed Nov 19, 2024
1 parent 45c8dcc commit a615cb2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Test_E2E(t *testing.T) {
storage graph.Storage
cleanup func()
} {
redis, err := storages.SetupRedisTestDB(context.Background(), "localhost:6379")
redis, err := storages.SetupRedisTestDB(context.Background())
if err != nil {
t.Fatal(err)
}
Expand Down
27 changes: 14 additions & 13 deletions pkg/storages/redis_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import (
)

func TestGenerateID(t *testing.T) {
r, err := SetupRedisTestDB(context.Background(), "localhost:6379")

r, err := SetupRedisTestDB(context.Background())
assert.NoError(t, err)
id, err := r.GenerateID()
assert.NoError(t, err)
assert.NotEqual(t, 0, id)
}

func TestSaveNode(t *testing.T) {
r, err := SetupRedisTestDB(context.Background(), "localhost:6379")
r, err := SetupRedisTestDB(context.Background())
assert.NoError(t, err)
node := &graph.Node{ID: 1, Name: "test_node", Children: roaring.New(), Parents: roaring.New()}
err = r.SaveNode(node)
Expand All @@ -33,7 +34,7 @@ func TestSaveNode(t *testing.T) {
}

func TestNameToID(t *testing.T) {
r, err := SetupRedisTestDB(context.Background(), "localhost:6379")
r, err := SetupRedisTestDB(context.Background())
assert.NoError(t, err)
node := &graph.Node{ID: 1, Name: "test_node", Children: roaring.New(), Parents: roaring.New()}
err = r.SaveNode(node)
Expand All @@ -45,7 +46,7 @@ func TestNameToID(t *testing.T) {
}

func TestGetAllKeys(t *testing.T) {
r, err := SetupRedisTestDB(context.Background(), "localhost:6379")
r, err := SetupRedisTestDB(context.Background())
assert.NoError(t, err)
node1 := &graph.Node{ID: 1, Name: "node1", Children: roaring.New(), Parents: roaring.New()}
node2 := &graph.Node{ID: 2, Name: "node2", Children: roaring.New(), Parents: roaring.New()}
Expand All @@ -61,7 +62,7 @@ func TestGetAllKeys(t *testing.T) {
}

func TestSaveCache(t *testing.T) {
r, err := SetupRedisTestDB(context.Background(), "localhost:6379")
r, err := SetupRedisTestDB(context.Background())
assert.NoError(t, err)
cache := &graph.NodeCache{ID: 1, AllParents: roaring.New(), AllChildren: roaring.New()}
err = r.SaveCache(cache)
Expand All @@ -73,7 +74,7 @@ func TestSaveCache(t *testing.T) {
}

func TestToBeCached(t *testing.T) {
r, err := SetupRedisTestDB(context.Background(), "localhost:6379")
r, err := SetupRedisTestDB(context.Background())
assert.NoError(t, err)
nodeID := uint32(1)
err = r.AddNodeToCachedStack(nodeID)
Expand All @@ -85,7 +86,7 @@ func TestToBeCached(t *testing.T) {
}

func TestClearCacheStack(t *testing.T) {
r, err := SetupRedisTestDB(context.Background(), "localhost:6379")
r, err := SetupRedisTestDB(context.Background())
assert.NoError(t, err)
nodeID := uint32(1)
err = r.AddNodeToCachedStack(nodeID)
Expand All @@ -100,7 +101,7 @@ func TestClearCacheStack(t *testing.T) {
}

func TestGetNodes(t *testing.T) {
r, err := SetupRedisTestDB(context.Background(), "localhost:6379")
r, err := SetupRedisTestDB(context.Background())
assert.NoError(t, err)
// Add test data
node1 := &graph.Node{ID: 1, Name: "test_node1", Children: roaring.New(), Parents: roaring.New()}
Expand All @@ -120,7 +121,7 @@ func TestGetNodes(t *testing.T) {
}

func TestSaveCaches(t *testing.T) {
r, err := SetupRedisTestDB(context.Background(), "localhost:6379")
r, err := SetupRedisTestDB(context.Background())
assert.NoError(t, err)
cache1 := &graph.NodeCache{ID: 1, AllParents: roaring.New(), AllChildren: roaring.New()}
cache2 := &graph.NodeCache{ID: 2, AllParents: roaring.New(), AllChildren: roaring.New()}
Expand All @@ -137,7 +138,7 @@ func TestSaveCaches(t *testing.T) {
}

func TestGetCaches(t *testing.T) {
r, err := SetupRedisTestDB(context.Background(), "localhost:6379")
r, err := SetupRedisTestDB(context.Background())
assert.NoError(t, err)
cache1 := &graph.NodeCache{ID: 1, AllParents: roaring.New(), AllChildren: roaring.New()}
cache2 := &graph.NodeCache{ID: 2, AllParents: roaring.New(), AllChildren: roaring.New()}
Expand All @@ -154,7 +155,7 @@ func TestGetCaches(t *testing.T) {
}

func TestRemoveAllCaches(t *testing.T) {
r, err := SetupRedisTestDB(context.Background(), "localhost:6379")
r, err := SetupRedisTestDB(context.Background())
assert.NoError(t, err)
cache1 := &graph.NodeCache{ID: 1, AllParents: roaring.New(), AllChildren: roaring.New()}
cache2 := &graph.NodeCache{ID: 2, AllParents: roaring.New(), AllChildren: roaring.New()}
Expand All @@ -173,7 +174,7 @@ func TestRemoveAllCaches(t *testing.T) {
}

func TestAddAndGetDataToDB(t *testing.T) {
r, err := SetupRedisTestDB(context.Background(), "localhost:6379")
r, err := SetupRedisTestDB(context.Background())
assert.NoError(t, err)
err = r.AddOrUpdateCustomData("test_tag", "test_key1", "test_data1", []byte("test_data1"))
assert.NoError(t, err)
Expand All @@ -194,7 +195,7 @@ func TestAddAndGetDataToDB(t *testing.T) {
}

func TestGetNodesByGlob(t *testing.T) {
r, err := SetupRedisTestDB(context.Background(), "localhost:6379")
r, err := SetupRedisTestDB(context.Background())
assert.NoError(t, err)
// Add test nodes
node1 := &graph.Node{ID: 1, Name: "test_node1", Children: roaring.New(), Parents: roaring.New()}
Expand Down
11 changes: 9 additions & 2 deletions pkg/storages/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package storages
import (
"context"
"fmt"
"os"

"github.com/go-redis/redis/v8"
)
Expand All @@ -25,9 +26,15 @@ func SetupSQLTestDB(dsn string) (*SQLStorage, error) {
}

// SetupRedisTestDB initializes a new RedisStorage with the given address.
func SetupRedisTestDB(ctx context.Context, addr string) (*RedisStorage, error) {
func SetupRedisTestDB(ctx context.Context) (*RedisStorage, error) {

redisURL := os.Getenv("TEST_REDIS_URL")
if redisURL == "" {
redisURL = "localhost:6379"
}

rdb := redis.NewClient(&redis.Options{
Addr: addr,
Addr: redisURL,
})

// Verify connection
Expand Down

0 comments on commit a615cb2

Please sign in to comment.