Skip to content

Commit

Permalink
test: random database name in tests (openGemini#21)
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <shoothzj@gmail.com>
  • Loading branch information
hezhangjian authored Nov 28, 2023
1 parent f5ede1e commit 05b7b3a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 22 deletions.
2 changes: 1 addition & 1 deletion opengemini/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 14 additions & 6 deletions opengemini/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
28 changes: 28 additions & 0 deletions opengemini/random_util.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 3 additions & 3 deletions opengemini/retention_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 21 additions & 12 deletions opengemini/retention_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions opengemini/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ func testNewClient(t *testing.T, config *Config) Client {
require.NotNil(t, client)
return client
}

func randomDatabaseName() string {
return RandStr(8)
}

0 comments on commit 05b7b3a

Please sign in to comment.