Skip to content

Commit

Permalink
refactor: extract testDefaultClient method (openGemini#22)
Browse files Browse the repository at this point in the history
Signed-off-by: PennyYoon <525296438@qq.com>
  • Loading branch information
Chenxulin97 authored Dec 1, 2023
1 parent 05b7b3a commit 91a8d88
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 90 deletions.
56 changes: 8 additions & 48 deletions opengemini/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ import (
)

func TestClientCreateDatabaseSuccess(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)
databaseName := randomDatabaseName()
err := c.CreateDatabase(databaseName)
require.Nil(t, err)
Expand All @@ -20,23 +15,13 @@ func TestClientCreateDatabaseSuccess(t *testing.T) {
}

func TestClientCreateDatabaseEmptyDatabase(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)
err := c.CreateDatabase("")
require.NotNil(t, err)
}

func TestClientCreateDatabaseWithRpSuccess(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)
databaseName := randomDatabaseName()
err := c.CreateDatabaseWithRp(databaseName, RpConfig{Name: "test4", Duration: "1d", ShardGroupDuration: "1h", IndexDuration: "7h"})
require.Nil(t, err)
Expand All @@ -45,58 +30,33 @@ func TestClientCreateDatabaseWithRpSuccess(t *testing.T) {
}

func TestClientCreateDatabaseWithRpInvalid(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)
databaseName := randomDatabaseName()
err := c.CreateDatabaseWithRp(databaseName, RpConfig{Name: "test4", Duration: "1", ShardGroupDuration: "1h", IndexDuration: "7h"})
require.NotNil(t, err)
}

func TestClientCreateDatabaseWithRpEmptyDatabase(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)
err := c.CreateDatabaseWithRp("", RpConfig{Name: "test4", Duration: "1h", ShardGroupDuration: "1h", IndexDuration: "7h"})
require.NotNil(t, err)
}

func TestClientShowDatabase(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)
_, err := c.ShowDatabase()
require.Nil(t, err)
}

func TestClientDropDatabase(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)
databaseName := randomDatabaseName()
err := c.DropDatabase(databaseName)
require.Nil(t, err)
}

func TestClientDropDatabaseEmptyDatabase(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)
err := c.DropDatabase("")
require.NotNil(t, err)
}
14 changes: 2 additions & 12 deletions opengemini/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ import (
)

func TestPingSuccess(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)

err := c.Ping(0)
require.Nil(t, err)
Expand All @@ -33,12 +28,7 @@ func TestPingFailForInaccessibleAddress(t *testing.T) {
}

func TestPingFailForOutOfRangeIndex(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)

err := c.Ping(1)
require.NotNil(t, err)
Expand Down
35 changes: 5 additions & 30 deletions opengemini/retention_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ import (
)

func TestClientCreateRetentionPolicy(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)
databaseName := randomDatabaseName()
err := c.CreateDatabase(databaseName)
require.Nil(t, err)
Expand All @@ -36,12 +31,7 @@ func TestClientCreateRetentionPolicy(t *testing.T) {
}

func TestClientCreateRetentionPolicyNotExistDatabase(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)
databaseName := randomDatabaseName()
err := c.CreateRetentionPolicy(databaseName, RpConfig{Name: "test_rp1", Duration: "3d"}, false)
require.NotNil(t, err)
Expand All @@ -50,34 +40,19 @@ func TestClientCreateRetentionPolicyNotExistDatabase(t *testing.T) {
}

func TestClientCreateRetentionPolicyEmptyDatabase(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)
err := c.CreateRetentionPolicy("", RpConfig{Name: "test_rp1", Duration: "3d"}, false)
require.NotNil(t, err)
}

func TestClientDropRetentionPolicy(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)
err := c.DropRetentionPolicy("test_rp1", "test_database")
require.Nil(t, err)
}

func TestClientShowRetentionPolicy(t *testing.T) {
c := testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
c := testDefaultClient(t)
databaseName := randomDatabaseName()
err := c.CreateDatabase(databaseName)
require.Nil(t, err)
Expand Down
9 changes: 9 additions & 0 deletions opengemini/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import (
"testing"
)

func testDefaultClient(t *testing.T) Client {
return testNewClient(t, &Config{
Addresses: []*Address{{
Host: "localhost",
Port: 8086,
}},
})
}

func testNewClient(t *testing.T, config *Config) Client {
client, err := newClient(config)
require.Nil(t, err)
Expand Down

0 comments on commit 91a8d88

Please sign in to comment.