diff --git a/opengemini/client.go b/opengemini/client.go index f312d0a..b0e8418 100644 --- a/opengemini/client.go +++ b/opengemini/client.go @@ -29,7 +29,7 @@ type Client interface { // isDefault can set the new retention policy as the default retention policy for the database CreateRetentionPolicy(database string, rpConfig RpConfig, isDefault bool) error ShowRetentionPolicy(database string) ([]RetentionPolicy, error) - DropRetentionPolicy(retentionPolicy string, database string) error + DropRetentionPolicy(database, retentionPolicy string) error } // Config is used to construct a openGemini Client instance. diff --git a/opengemini/database_test.go b/opengemini/database_test.go index 5c9779a..a6fc7a8 100644 --- a/opengemini/database_test.go +++ b/opengemini/database_test.go @@ -5,14 +5,17 @@ import ( "testing" ) -func TestClientCreateDatabase(t *testing.T) { +func TestClientCreateDatabaseSuccess(t *testing.T) { c := testNewClient(t, &Config{ Addresses: []*Address{{ Host: "localhost", Port: 8086, }}, }) - err := c.CreateDatabase("test4_database") + databaseName := randomDatabaseName() + err := c.CreateDatabase(databaseName) + require.Nil(t, err) + err = c.DropDatabase(databaseName) require.Nil(t, err) } @@ -27,14 +30,17 @@ func TestClientCreateDatabaseEmptyDatabase(t *testing.T) { require.NotNil(t, err) } -func TestClientCreateDatabaseWithRp(t *testing.T) { +func TestClientCreateDatabaseWithRpSuccess(t *testing.T) { c := testNewClient(t, &Config{ Addresses: []*Address{{ Host: "localhost", Port: 8086, }}, }) - err := c.CreateDatabaseWithRp("test4_database", RpConfig{Name: "test4", Duration: "1d", ShardGroupDuration: "1h", IndexDuration: "7h"}) + databaseName := randomDatabaseName() + err := c.CreateDatabaseWithRp(databaseName, RpConfig{Name: "test4", Duration: "1d", ShardGroupDuration: "1h", IndexDuration: "7h"}) + require.Nil(t, err) + err = c.DropDatabase(databaseName) require.Nil(t, err) } @@ -45,7 +51,8 @@ func TestClientCreateDatabaseWithRpInvalid(t *testing.T) { Port: 8086, }}, }) - err := c.CreateDatabaseWithRp("test4_database", RpConfig{Name: "test4", Duration: "1", ShardGroupDuration: "1h", IndexDuration: "7h"}) + databaseName := randomDatabaseName() + err := c.CreateDatabaseWithRp(databaseName, RpConfig{Name: "test4", Duration: "1", ShardGroupDuration: "1h", IndexDuration: "7h"}) require.NotNil(t, err) } @@ -78,7 +85,8 @@ func TestClientDropDatabase(t *testing.T) { Port: 8086, }}, }) - err := c.DropDatabase("vvv_database") + databaseName := randomDatabaseName() + err := c.DropDatabase(databaseName) require.Nil(t, err) } diff --git a/opengemini/random_util.go b/opengemini/random_util.go new file mode 100644 index 0000000..40f705a --- /dev/null +++ b/opengemini/random_util.go @@ -0,0 +1,28 @@ +package opengemini + +import ( + "math/rand" + "time" +) + +func init() { + rand.New(rand.NewSource(time.Now().UnixNano())) +} + +const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + +func RandBytes(n int64) []byte { + b := make([]byte, n) + for i := range b { + b[i] = letters[rand.Int63()%int64(len(letters))] + } + return b +} + +func RandStr(n int64) string { + b := make([]byte, n) + for i := range b { + b[i] = letters[rand.Int63()%int64(len(letters))] + } + return string(b) +} diff --git a/opengemini/retention_policy.go b/opengemini/retention_policy.go index 5cee2fd..a1f29eb 100644 --- a/opengemini/retention_policy.go +++ b/opengemini/retention_policy.go @@ -128,15 +128,15 @@ func convertRetentionPolicy(queryResult *QueryResult) []RetentionPolicy { } // DropRetentionPolicy Drop retention policy -func (c *client) DropRetentionPolicy(rp string, database string) error { - if len(rp) == 0 { +func (c *client) DropRetentionPolicy(database, retentionPolicy string) error { + if len(retentionPolicy) == 0 { return errors.New("empty retention policy") } if len(database) == 0 { return errors.New("empty database name") } - cmd := fmt.Sprintf("DROP RETENTION POLICY %s ON %s", rp, database) + cmd := fmt.Sprintf("DROP RETENTION POLICY %s ON %s", retentionPolicy, database) queryResult, err := c.queryPost(Query{Command: cmd}) if err != nil { return err diff --git a/opengemini/retention_policy_test.go b/opengemini/retention_policy_test.go index b158d93..490ba41 100644 --- a/opengemini/retention_policy_test.go +++ b/opengemini/retention_policy_test.go @@ -12,25 +12,26 @@ func TestClientCreateRetentionPolicy(t *testing.T) { Port: 8086, }}, }) - err := c.CreateDatabase("test_database") + databaseName := randomDatabaseName() + err := c.CreateDatabase(databaseName) require.Nil(t, err) - err = c.CreateRetentionPolicy("test_database", RpConfig{Name: "test_rp1", Duration: "3d"}, false) + err = c.CreateRetentionPolicy(databaseName, RpConfig{Name: "test_rp1", Duration: "3d"}, false) require.Nil(t, err) - err = c.CreateRetentionPolicy("test_database", RpConfig{Name: "test_rp2", Duration: "3d", ShardGroupDuration: "1h"}, false) + err = c.CreateRetentionPolicy(databaseName, RpConfig{Name: "test_rp2", Duration: "3d", ShardGroupDuration: "1h"}, false) require.Nil(t, err) - err = c.CreateRetentionPolicy("test_database", RpConfig{Name: "test_rp3", Duration: "3d", ShardGroupDuration: "1h", IndexDuration: "7h"}, false) + err = c.CreateRetentionPolicy(databaseName, RpConfig{Name: "test_rp3", Duration: "3d", ShardGroupDuration: "1h", IndexDuration: "7h"}, false) require.Nil(t, err) - err = c.CreateRetentionPolicy("test_database", RpConfig{Name: "test_rp4", Duration: "3d"}, true) + err = c.CreateRetentionPolicy(databaseName, RpConfig{Name: "test_rp4", Duration: "3d"}, true) require.Nil(t, err) - err = c.DropRetentionPolicy("test_rp4", "test_database") + err = c.DropRetentionPolicy(databaseName, "test_rp4") require.Nil(t, err) - err = c.DropRetentionPolicy("test_rp3", "test_database") + err = c.DropRetentionPolicy(databaseName, "test_rp3") require.Nil(t, err) - err = c.DropRetentionPolicy("test_rp2", "test_database") + err = c.DropRetentionPolicy(databaseName, "test_rp2") require.Nil(t, err) - err = c.DropRetentionPolicy("test_rp1", "test_database") + err = c.DropRetentionPolicy(databaseName, "test_rp1") require.Nil(t, err) - err = c.DropDatabase("test_database") + err = c.DropDatabase(databaseName) require.Nil(t, err) } @@ -41,8 +42,11 @@ func TestClientCreateRetentionPolicyNotExistDatabase(t *testing.T) { Port: 8086, }}, }) - err := c.CreateRetentionPolicy("test_db", RpConfig{Name: "test_rp1", Duration: "3d"}, false) + databaseName := randomDatabaseName() + err := c.CreateRetentionPolicy(databaseName, RpConfig{Name: "test_rp1", Duration: "3d"}, false) require.NotNil(t, err) + err = c.DropDatabase(databaseName) + require.Nil(t, err) } func TestClientCreateRetentionPolicyEmptyDatabase(t *testing.T) { @@ -74,7 +78,12 @@ func TestClientShowRetentionPolicy(t *testing.T) { Port: 8086, }}, }) - rpResult, err := c.ShowRetentionPolicy("test_database") + databaseName := randomDatabaseName() + err := c.CreateDatabase(databaseName) + require.Nil(t, err) + rpResult, err := c.ShowRetentionPolicy(databaseName) require.Nil(t, err) require.NotEqual(t, len(rpResult), 0) + err = c.DropDatabase(databaseName) + require.Nil(t, err) } diff --git a/opengemini/test_util.go b/opengemini/test_util.go index 65751a4..eda2b83 100644 --- a/opengemini/test_util.go +++ b/opengemini/test_util.go @@ -11,3 +11,7 @@ func testNewClient(t *testing.T, config *Config) Client { require.NotNil(t, client) return client } + +func randomDatabaseName() string { + return RandStr(8) +}