github.com/kaydxh/golang@v0.0.131/pkg/database/redis/redis_list_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  
    30  //push value to list key
    31  func TestRPush(t *testing.T) {
    32  	db := GetDBOrDie()
    33  
    34  	testCases := []struct {
    35  		Key   string
    36  		Value string
    37  	}{
    38  		{
    39  			Key:   "list-test-1",
    40  			Value: "value-list-test-1",
    41  		},
    42  		{
    43  			Key:   "list-test-2",
    44  			Value: "value-list-test-2",
    45  		},
    46  	}
    47  
    48  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    49  	defer cancel()
    50  
    51  	for _, testCase := range testCases {
    52  		val, err := db.RPush(ctx, testCase.Key, testCase.Value).Result()
    53  		if err != nil {
    54  			t.Fatalf("failed to RPush, err: %v", err)
    55  		}
    56  		t.Logf("key: %v, val: %v", testCase.Key, val)
    57  
    58  	}
    59  
    60  }
    61  
    62  //push value to list key which is existed
    63  func TestRPushX(t *testing.T) {
    64  	db := GetDBOrDie()
    65  
    66  	testCases := []struct {
    67  		Key   string
    68  		Value string
    69  	}{
    70  		{
    71  			Key:   "list-test-3",
    72  			Value: "value-list-test-1",
    73  		},
    74  		{
    75  			Key:   "list-test-4",
    76  			Value: "value-list-test-2",
    77  		},
    78  	}
    79  
    80  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    81  	defer cancel()
    82  
    83  	for _, testCase := range testCases {
    84  		val, err := db.RPushX(ctx, testCase.Key, testCase.Value).Result()
    85  		if err != nil {
    86  			t.Fatalf("failed to RPushX, err: %v", err)
    87  		}
    88  		t.Logf("key: %v, val: %v", testCase.Key, val)
    89  
    90  	}
    91  }
    92  
    93  //get length for list key
    94  func TestLLen(t *testing.T) {
    95  	db := GetDBOrDie()
    96  
    97  	testCases := []struct {
    98  		Key string
    99  	}{
   100  		{
   101  			Key: "list-test-3",
   102  		},
   103  		{
   104  			Key: "list-test-4",
   105  		},
   106  	}
   107  
   108  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   109  	defer cancel()
   110  
   111  	for _, testCase := range testCases {
   112  		length, err := db.LLen(ctx, testCase.Key).Result()
   113  		if err != nil {
   114  			t.Fatalf("failed to LLen, err: %v", err)
   115  		}
   116  		t.Logf("list: %v, val: %v", testCase.Key, length)
   117  
   118  	}
   119  }
   120  
   121  //push value to list key
   122  //Start: 0 the first element ,End
   123  //End: -1 the last element, -2, next to last element
   124  func TestLRange(t *testing.T) {
   125  	db := GetDBOrDie()
   126  
   127  	testCases := []struct {
   128  		Key   string
   129  		Start int64
   130  		End   int64
   131  	}{
   132  		{
   133  			Key:   "list-test-1",
   134  			Start: 0,
   135  			End:   -1,
   136  		},
   137  		{
   138  			Key:   "list-test-2",
   139  			Start: 0,
   140  			End:   -1,
   141  		},
   142  	}
   143  
   144  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   145  	defer cancel()
   146  
   147  	for _, testCase := range testCases {
   148  		vals, err := db.LRange(ctx, testCase.Key, testCase.Start, testCase.End).Result()
   149  		if err != nil {
   150  			t.Fatalf("failed to LRange, err: %v", err)
   151  		}
   152  		t.Logf("list: %v, vals: %v", testCase.Key, vals)
   153  
   154  	}
   155  }
   156  
   157  //can use negative indexes
   158  //-1 the last element, -2, next to last element
   159  func TestLIndex(t *testing.T) {
   160  	db := GetDBOrDie()
   161  
   162  	testCases := []struct {
   163  		Key   string
   164  		Index int64
   165  	}{
   166  		{
   167  			Key:   "list-test-1",
   168  			Index: 0,
   169  		},
   170  		{
   171  			Key:   "list-test-2",
   172  			Index: -1,
   173  		},
   174  	}
   175  
   176  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   177  	defer cancel()
   178  
   179  	for _, testCase := range testCases {
   180  		val, err := db.LIndex(ctx, testCase.Key, testCase.Index).Result()
   181  		if err != nil {
   182  			t.Fatalf("failed to LRange, err: %v", err)
   183  		}
   184  		t.Logf("list: %v, index: %v, val: %v", testCase.Key, testCase.Index, val)
   185  
   186  	}
   187  }
   188  
   189  //insert value before the pivot
   190  func TestLInsertBefore(t *testing.T) {
   191  	db := GetDBOrDie()
   192  
   193  	testCases := []struct {
   194  		Key   string
   195  		Pivot string
   196  		Value string
   197  	}{
   198  		{
   199  			Key:   "list-test-1",
   200  			Pivot: "value-list-test-1",
   201  			Value: "value-list-test-2",
   202  		},
   203  		{
   204  			Key:   "list-test-2",
   205  			Pivot: "value-list-test-2",
   206  			Value: "value-list-test-3",
   207  		},
   208  	}
   209  
   210  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   211  	defer cancel()
   212  
   213  	for _, testCase := range testCases {
   214  		val, err := db.LInsertBefore(ctx, testCase.Key, testCase.Pivot, testCase.Value).Result()
   215  		if err != nil {
   216  			t.Fatalf("failed to LInsertBefore, err: %v", err)
   217  		}
   218  		t.Logf("list: %v, val: %v", testCase.Key, val)
   219  
   220  	}
   221  }
   222  
   223  //insert value to list head
   224  func TestLPush(t *testing.T) {
   225  	db := GetDBOrDie()
   226  
   227  	testCases := []struct {
   228  		Key    string
   229  		Values []string
   230  	}{
   231  		{
   232  			Key:    "list-test-1",
   233  			Values: []string{"value-list-1-test-10", "value-list-1-test-20"},
   234  		},
   235  		{
   236  			Key:    "list-test-2",
   237  			Values: []string{"value-list-2-test-10", "value-list-2-test-20"},
   238  		},
   239  	}
   240  
   241  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   242  	defer cancel()
   243  
   244  	for _, testCase := range testCases {
   245  		val, err := db.LPush(ctx, testCase.Key, testCase.Values).Result()
   246  		if err != nil {
   247  			t.Fatalf("failed to LInsertBefore, err: %v", err)
   248  		}
   249  		t.Logf("list: %v, val: %v", testCase.Key, val)
   250  
   251  	}
   252  }
   253  
   254  //keep elements in range, the others will deleted
   255  func TestLTrim(t *testing.T) {
   256  	db := GetDBOrDie()
   257  
   258  	testCases := []struct {
   259  		Key   string
   260  		Start int64
   261  		End   int64
   262  	}{
   263  		{
   264  			Key:   "list-test-1",
   265  			Start: 1,
   266  			End:   3,
   267  		},
   268  		{
   269  			Key:   "list-test-2",
   270  			Start: 0,
   271  			End:   -1,
   272  		},
   273  	}
   274  
   275  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   276  	defer cancel()
   277  
   278  	for _, testCase := range testCases {
   279  		val, err := db.LTrim(ctx, testCase.Key, testCase.Start, testCase.End).Result()
   280  		if err != nil {
   281  			t.Fatalf("failed to LInsertBefore, err: %v", err)
   282  		}
   283  		t.Logf("list: %v, val: %v", testCase.Key, val)
   284  
   285  	}
   286  }
   287  
   288  //delete element from list head, if the list is empty, will block times to find elements that can be ejected or wait timout
   289  func TestBLPop(t *testing.T) {
   290  	db := GetDBOrDie()
   291  
   292  	testCases := []struct {
   293  		Key string
   294  	}{
   295  		{
   296  			Key: "list-test-1",
   297  		},
   298  		{
   299  			Key: "list-test-20",
   300  		},
   301  	}
   302  
   303  	timout := 5 * time.Second
   304  
   305  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   306  	defer cancel()
   307  
   308  	for _, testCase := range testCases {
   309  		val, err := db.BLPop(ctx, timout, testCase.Key).Result()
   310  		if err != nil {
   311  			t.Fatalf("failed to BLPop, err: %v", err)
   312  		}
   313  		t.Logf("list: %v, val: %v", testCase.Key, val)
   314  
   315  	}
   316  }