github.com/kaydxh/golang@v0.0.131/pkg/database/redis/redis_string_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package redis_test
    23  
    24  import (
    25  	"sync"
    26  	"testing"
    27  	"time"
    28  
    29  	"context"
    30  
    31  	"github.com/go-redis/redis/v8"
    32  	redis_ "github.com/kaydxh/golang/pkg/database/redis"
    33  	viper_ "github.com/kaydxh/golang/pkg/viper"
    34  	"github.com/stretchr/testify/assert"
    35  )
    36  
    37  func TestGetDataBase(t *testing.T) {
    38  	testCases := []struct {
    39  		TestName  string
    40  		Addresses []string
    41  		DB        int
    42  		UserName  string
    43  		Password  string
    44  	}{
    45  		{
    46  			TestName:  "test1",
    47  			Addresses: []string{"9.135.232.102:6380"},
    48  			DB:        0,
    49  			UserName:  "root",
    50  			Password:  "HXufW*3569FShs",
    51  		},
    52  	}
    53  
    54  	for _, testCase := range testCases {
    55  		t.Run(testCase.TestName, func(t *testing.T) {
    56  			db := redis_.NewRedisClient(redis_.DBConfig{
    57  				Addresses: testCase.Addresses,
    58  				UserName:  testCase.UserName,
    59  				Password:  testCase.Password,
    60  				DB:        testCase.DB,
    61  			})
    62  			redisDB, err := db.GetRedis(context.Background())
    63  			if err != nil {
    64  				t.Fatalf("failed to get redis database: %v, got : %s", testCase.Addresses, err)
    65  			}
    66  			assert.NotNil(t, redisDB)
    67  		})
    68  	}
    69  
    70  }
    71  
    72  var (
    73  	once sync.Once
    74  	db   *redis.Client
    75  	err  error
    76  )
    77  
    78  func GetDBOrDie() *redis.Client {
    79  	once.Do(func() {
    80  		cfgFile := "./redis.yaml"
    81  		config := redis_.NewConfig(redis_.WithViper(viper_.GetViper(cfgFile, "database.redis")))
    82  
    83  		db, err = config.Complete().New(context.Background())
    84  		if err != nil {
    85  			panic(err)
    86  		}
    87  		if db == nil {
    88  			panic("db is not enable")
    89  		}
    90  	})
    91  
    92  	return db
    93  
    94  }
    95  
    96  func TestNew(t *testing.T) {
    97  	db := GetDBOrDie()
    98  	//defer db.Close()
    99  
   100  	t.Logf("db: %#v", db)
   101  }
   102  
   103  // set string
   104  // Redis `SET key value [expiration]` command.
   105  //
   106  // Use expiration for `SETEX`-like behavior.
   107  // Zero expiration means the key has no expiration time.
   108  func TestSet(t *testing.T) {
   109  
   110  	db := GetDBOrDie()
   111  	//defer db.Close()
   112  
   113  	testCases := []struct {
   114  		key      string
   115  		value    string
   116  		expire   time.Duration
   117  		expected string
   118  	}{
   119  		{
   120  			key:      "test1",
   121  			value:    "test1-1, test1-2",
   122  			expected: "test1",
   123  		},
   124  		{
   125  			key:      "test2",
   126  			value:    "test2-1, test2-2",
   127  			expected: "test2",
   128  		},
   129  
   130  		{
   131  			key:      "test3-tmp",
   132  			value:    "test3-1, test3-2",
   133  			expire:   time.Minute,
   134  			expected: "test3",
   135  		},
   136  	}
   137  
   138  	ctx := context.Background()
   139  	for _, testCase := range testCases {
   140  
   141  		result, err := db.Set(ctx, testCase.key, testCase.value, testCase.expire).Result()
   142  		if err != nil {
   143  			t.Fatalf("failed to set string, err: %v", err)
   144  		}
   145  
   146  		t.Logf("result of %v: %v", testCase.key, result)
   147  	}
   148  
   149  }
   150  
   151  //get values with keys
   152  func TestGetValues(t *testing.T) {
   153  
   154  	db := GetDBOrDie()
   155  	//	defer db.Close()
   156  
   157  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   158  	defer cancel()
   159  
   160  	keys, err := db.Keys(ctx, "*").Result()
   161  	if err != nil {
   162  		t.Fatalf("failed to get all keys , err: %v", err)
   163  	}
   164  
   165  	values, err := redis_.GetValues(ctx, db, keys...)
   166  	if err != nil {
   167  		t.Fatalf("failed to get values, err: %v", err)
   168  	}
   169  	t.Logf("keys: %v, values: %v", keys, values)
   170  }
   171  
   172  //get range of value with key
   173  func TestGetRange(t *testing.T) {
   174  
   175  	db := GetDBOrDie()
   176  	//	defer db.Close()
   177  
   178  	testCases := []struct {
   179  		key      string
   180  		start    int64
   181  		end      int64
   182  		expected string
   183  	}{
   184  		{
   185  			key:      "test1",
   186  			start:    0,
   187  			end:      -1, // get all range
   188  			expected: "test1-1",
   189  		},
   190  		{
   191  			key:      "test2",
   192  			start:    0,
   193  			end:      int64(len("test1-1")) - 1, //include end
   194  			expected: "test2-1",
   195  		},
   196  	}
   197  
   198  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   199  	defer cancel()
   200  
   201  	for _, testCase := range testCases {
   202  		value, err := db.GetRange(ctx, testCase.key, testCase.start, testCase.end).Result()
   203  		if err != nil {
   204  			t.Fatalf("failed to get range, err: %v", err)
   205  		}
   206  		t.Logf("key: %v, range [%d:%d] value: %v", testCase.key, testCase.start, testCase.end, value)
   207  	}
   208  }