github.com/kaydxh/golang@v0.0.131/pkg/database/redis/redis_zset_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  	"context"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/go-redis/redis/v8"
    30  )
    31  
    32  func TestZAdd(t *testing.T) {
    33  	db := GetDBOrDie()
    34  
    35  	testCases := []struct {
    36  		Key   string
    37  		Value string
    38  		Score float64
    39  	}{
    40  		{
    41  			Key:   "zset-test-1",
    42  			Value: "values-zset-test-1-1",
    43  			Score: 0.5,
    44  		},
    45  		{
    46  			Key:   "zset-test-1",
    47  			Value: "values-zset-test-1-2",
    48  			Score: 50,
    49  		},
    50  		{
    51  			Key:   "zset-test-1",
    52  			Value: "values-zset-test-1-3",
    53  			Score: 500,
    54  		},
    55  		{
    56  			Key:   "zset-test-1",
    57  			Value: "values-zset-test-1-4",
    58  			Score: 100,
    59  		},
    60  	}
    61  
    62  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    63  	defer cancel()
    64  
    65  	for _, testCase := range testCases {
    66  		val, err := db.ZAdd(ctx, testCase.Key, &redis.Z{
    67  			Score:  testCase.Score,
    68  			Member: testCase.Value,
    69  		}).Result()
    70  		if err != nil {
    71  			t.Fatalf("failed to ZAdd, err: %v", err)
    72  		}
    73  		t.Logf("set: %v, val: %v", testCase.Key, val)
    74  	}
    75  }
    76  
    77  func TestZCard(t *testing.T) {
    78  	db := GetDBOrDie()
    79  
    80  	testCases := []struct {
    81  		Key string
    82  	}{
    83  		{
    84  			Key: "zset-test-1",
    85  		},
    86  		{
    87  			Key: "zset-test-20",
    88  		},
    89  	}
    90  
    91  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    92  	defer cancel()
    93  
    94  	for _, testCase := range testCases {
    95  		count, err := db.ZCard(ctx, testCase.Key).Result()
    96  		if err != nil {
    97  			t.Fatalf("failed to ZCard, err: %v", err)
    98  		}
    99  		t.Logf("set: %v, member count: %v", testCase.Key, count)
   100  	}
   101  }
   102  
   103  // return values sorted by score
   104  func TestZRange(t *testing.T) {
   105  	db := GetDBOrDie()
   106  
   107  	testCases := []struct {
   108  		Key   string
   109  		Start int64
   110  		Stop  int64
   111  	}{
   112  		{
   113  			Key:   "zset-test-1",
   114  			Start: 0,
   115  			Stop:  -1,
   116  		},
   117  		{
   118  			Key:   "zset-test-20",
   119  			Start: 0,
   120  			Stop:  -1,
   121  		},
   122  	}
   123  
   124  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   125  	defer cancel()
   126  
   127  	for _, testCase := range testCases {
   128  		vals, err := db.ZRange(ctx, testCase.Key, testCase.Start, testCase.Stop).Result()
   129  		if err != nil {
   130  			t.Fatalf("failed to ZRange, err: %v", err)
   131  		}
   132  		t.Logf("set: %v, members: %v", testCase.Key, vals)
   133  	}
   134  }
   135  
   136  func TestZRangeByScore(t *testing.T) {
   137  	db := GetDBOrDie()
   138  
   139  	testCases := []struct {
   140  		Key string
   141  		Min string
   142  		Max string
   143  	}{
   144  		{
   145  			Key: "zset-test-1",
   146  			Min: "50",
   147  			Max: "100",
   148  		},
   149  		{
   150  			Key: "zset-test-1",
   151  			Min: "-inf",
   152  			Max: "+inf",
   153  		},
   154  		{
   155  			Key: "zset-test-1",
   156  			Min: "-1",
   157  			Max: "100",
   158  		},
   159  	}
   160  
   161  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   162  	defer cancel()
   163  
   164  	for _, testCase := range testCases {
   165  		vals, err := db.ZRangeByScore(ctx, testCase.Key, &redis.ZRangeBy{
   166  			Min: testCase.Min,
   167  			Max: testCase.Max,
   168  		}).Result()
   169  		if err != nil {
   170  			t.Fatalf("failed to ZRangeByScore, err: %v", err)
   171  		}
   172  		t.Logf("set: %v, ZRangeByScore: %v", testCase.Key, vals)
   173  	}
   174  }