diff --git a/opengemini/database_test.go b/opengemini/database_test.go index a6fc7a8..841d4b2 100644 --- a/opengemini/database_test.go +++ b/opengemini/database_test.go @@ -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) @@ -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) @@ -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) } diff --git a/opengemini/ping_test.go b/opengemini/ping_test.go index 127873d..c0a70f1 100644 --- a/opengemini/ping_test.go +++ b/opengemini/ping_test.go @@ -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) @@ -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) diff --git a/opengemini/retention_policy_test.go b/opengemini/retention_policy_test.go index 490ba41..f5da828 100644 --- a/opengemini/retention_policy_test.go +++ b/opengemini/retention_policy_test.go @@ -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) @@ -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) @@ -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) diff --git a/opengemini/test_util.go b/opengemini/test_util.go index eda2b83..90f58ce 100644 --- a/opengemini/test_util.go +++ b/opengemini/test_util.go @@ -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)