github.com/kaydxh/golang@v0.0.131/pkg/database/redis/redis_hset_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  	redis_ "github.com/kaydxh/golang/pkg/database/redis"
    30  )
    31  
    32  func TestHSetStruct(t *testing.T) {
    33  
    34  	db := GetDBOrDie()
    35  
    36  	// only get export Fields from testCases
    37  	testCases := []struct {
    38  		Key string `redis:"reids_key"`
    39  		ID  int64  `redis:"redis_id"`
    40  	}{
    41  		{
    42  			Key: "hset-test-1",
    43  			ID:  1,
    44  		},
    45  		{
    46  			Key: "hset-test-2",
    47  			ID:  2,
    48  		},
    49  	}
    50  
    51  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    52  	defer cancel()
    53  
    54  	for _, testCase := range testCases {
    55  		err := redis_.HSetStruct(ctx, db, testCase.Key, testCase)
    56  		if err != nil {
    57  			t.Fatalf("failed to hset, err: %v", err)
    58  		}
    59  		t.Logf("key: %v,  value: %v", testCase.Key, testCase)
    60  	}
    61  }
    62  
    63  //get all fields by hash key
    64  func TestHKeys(t *testing.T) {
    65  
    66  	db := GetDBOrDie()
    67  
    68  	testCases := []struct {
    69  		Key string
    70  	}{
    71  		{
    72  			Key: "hset-test-1",
    73  		},
    74  		{
    75  			Key: "hset-test-2",
    76  		},
    77  	}
    78  
    79  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    80  	defer cancel()
    81  
    82  	for _, testCase := range testCases {
    83  		result, err := db.HKeys(ctx, testCase.Key).Result()
    84  		if err != nil {
    85  			t.Fatalf("failed to HKeys, err: %v", err)
    86  		}
    87  		t.Logf("key: %v,  result: %v", testCase.Key, result)
    88  
    89  	}
    90  }
    91  
    92  //get all field-value by hash key
    93  func TestHGetAll(t *testing.T) {
    94  
    95  	db := GetDBOrDie()
    96  
    97  	testCases := []struct {
    98  		Key string
    99  	}{
   100  		{
   101  			Key: "hset-test-1",
   102  		},
   103  		{
   104  			Key: "hset-test-2",
   105  		},
   106  	}
   107  
   108  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   109  	defer cancel()
   110  
   111  	for _, testCase := range testCases {
   112  		result, err := db.HGetAll(ctx, testCase.Key).Result()
   113  		if err != nil {
   114  			t.Fatalf("failed to hGetAll, err: %v", err)
   115  		}
   116  		t.Logf("key: %v,  result: %v", testCase.Key, result)
   117  
   118  	}
   119  }
   120  
   121  //get value by hash key and field
   122  func TestHGet(t *testing.T) {
   123  
   124  	db := GetDBOrDie()
   125  
   126  	testCases := []struct {
   127  		Key   string
   128  		Field string
   129  	}{
   130  		{
   131  			Key:   "hset-test-1",
   132  			Field: "redis_id",
   133  		},
   134  		{
   135  			Key:   "hset-test-2",
   136  			Field: "redis_id",
   137  		},
   138  	}
   139  
   140  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   141  	defer cancel()
   142  
   143  	for _, testCase := range testCases {
   144  		fieldValues, err := db.HGet(ctx, testCase.Key, testCase.Field).Result()
   145  		if err != nil {
   146  			t.Fatalf("failed to hGet, err: %v", err)
   147  		}
   148  		t.Logf("key: %v, field: %v,  result: %v", testCase.Key, testCase.Field, fieldValues)
   149  
   150  	}
   151  }
   152  
   153  //exist hash key and field
   154  func TestHExists(t *testing.T) {
   155  
   156  	db := GetDBOrDie()
   157  
   158  	testCases := []struct {
   159  		Key   string
   160  		Field string
   161  	}{
   162  		{
   163  			Key:   "hset-test-1",
   164  			Field: "redis_id",
   165  		},
   166  		{
   167  			Key:   "hset-test-2",
   168  			Field: "redis_id",
   169  		},
   170  		{
   171  			Key:   "hset-test-no-exist",
   172  			Field: "redis_id",
   173  		},
   174  	}
   175  
   176  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   177  	defer cancel()
   178  
   179  	for _, testCase := range testCases {
   180  		ok, err := db.HExists(ctx, testCase.Key, testCase.Field).Result()
   181  		if err != nil {
   182  			t.Fatalf("failed to hGet, err: %v", err)
   183  		}
   184  		t.Logf("key: %v, field: %v,  result: %v", testCase.Key, testCase.Field, ok)
   185  
   186  	}
   187  }
   188  
   189  // increment by hash key and field
   190  func TestHIncrBy(t *testing.T) {
   191  
   192  	db := GetDBOrDie()
   193  
   194  	testCases := []struct {
   195  		Key   string
   196  		Field string
   197  		Incr  int64
   198  	}{
   199  		{
   200  			Key:   "hset-test-1",
   201  			Field: "redis_id",
   202  			Incr:  1,
   203  		},
   204  		{
   205  			Key:   "hset-test-2",
   206  			Field: "redis_id",
   207  			Incr:  2,
   208  		},
   209  	}
   210  
   211  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   212  	defer cancel()
   213  
   214  	for _, testCase := range testCases {
   215  		// val is the result after incr
   216  		val, err := db.HIncrBy(ctx, testCase.Key, testCase.Field, testCase.Incr).Result()
   217  		if err != nil {
   218  			t.Fatalf("failed to HIncrBy, err: %v", err)
   219  		}
   220  		t.Logf("key: %v, field: %v, val: %v", testCase.Key, testCase.Field, val)
   221  
   222  	}
   223  }
   224  
   225  // get fields num by hash key
   226  func TestHLen(t *testing.T) {
   227  
   228  	db := GetDBOrDie()
   229  
   230  	testCases := []struct {
   231  		Key string
   232  	}{
   233  		{
   234  			Key: "hset-test-1",
   235  		},
   236  		{
   237  			Key: "hset-test-2",
   238  		},
   239  		{
   240  			Key: "hset-test-no-exist",
   241  		},
   242  	}
   243  
   244  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   245  	defer cancel()
   246  
   247  	for _, testCase := range testCases {
   248  		// count is the number of field by hash key
   249  		count, err := db.HLen(ctx, testCase.Key).Result()
   250  		if err != nil {
   251  			t.Fatalf("failed to HLen, err: %v", err)
   252  		}
   253  		t.Logf("key: %v, field count: %v", testCase.Key, count)
   254  
   255  	}
   256  }
   257  
   258  // get values by fields and  hash key
   259  func TestHMGet(t *testing.T) {
   260  
   261  	db := GetDBOrDie()
   262  
   263  	testCases := []struct {
   264  		Key   string
   265  		Field string
   266  	}{
   267  		{
   268  			Key:   "hset-test-1",
   269  			Field: "redis_id",
   270  		},
   271  		{
   272  			Key:   "hset-test-2",
   273  			Field: "redis_id",
   274  		},
   275  		{
   276  			Key:   "hset-test-no-exist",
   277  			Field: "redis_id",
   278  		},
   279  	}
   280  
   281  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   282  	defer cancel()
   283  
   284  	for _, testCase := range testCases {
   285  		// vals is value of field by hash key
   286  		vals, err := db.HMGet(ctx, testCase.Key, testCase.Field).Result()
   287  		if err != nil {
   288  			t.Fatalf("failed to HMGet, err: %v", err)
   289  		}
   290  		t.Logf("key: %v, field: %v, vals: %v", testCase.Key, testCase.Field, vals)
   291  
   292  	}
   293  }
   294  
   295  // set values by fields and  hash key
   296  func TestHMSet(t *testing.T) {
   297  
   298  	db := GetDBOrDie()
   299  
   300  	testCases := []struct {
   301  		Key   string
   302  		Field string
   303  		Value interface{}
   304  	}{
   305  		{
   306  			Key:   "hset-test-1",
   307  			Field: "redis_id",
   308  			Value: 10,
   309  		},
   310  		{
   311  			Key:   "hset-test-2",
   312  			Field: "redis_id",
   313  			Value: 20,
   314  		},
   315  		{
   316  			Key:   "hset-test-2",
   317  			Field: "redis_id_new",
   318  			Value: "new field",
   319  		},
   320  		{
   321  			Key:   "hset-test-no-exist",
   322  			Field: "redis_id",
   323  			Value: 30,
   324  		},
   325  	}
   326  
   327  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   328  	defer cancel()
   329  
   330  	for _, testCase := range testCases {
   331  		// count is the number of field by hash key
   332  		ok, err := db.HMSet(ctx, testCase.Key, testCase.Field, testCase.Value).Result()
   333  		if err != nil {
   334  			t.Fatalf("failed to HMSet, err: %v", err)
   335  		}
   336  		t.Logf("key: %v, field: %v, ok: %v", testCase.Key, testCase.Field, ok)
   337  
   338  	}
   339  }
   340  
   341  // set values by fields and  hash key when field is not exist
   342  func TestHSetNX(t *testing.T) {
   343  
   344  	db := GetDBOrDie()
   345  
   346  	testCases := []struct {
   347  		Key   string
   348  		Field string
   349  		Value interface{}
   350  	}{
   351  		{
   352  			Key:   "hset-test-1",
   353  			Field: "redis_id",
   354  			Value: 10,
   355  		},
   356  		{
   357  			Key:   "hset-test-2",
   358  			Field: "redis_id",
   359  			Value: 20,
   360  		},
   361  		{
   362  			Key:   "hset-test-2",
   363  			Field: "redis_id_new_1",
   364  			Value: "new field_1",
   365  		},
   366  		{
   367  			Key:   "hset-test-no-exist-1",
   368  			Field: "redis_id",
   369  			Value: 30,
   370  		},
   371  	}
   372  
   373  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   374  	defer cancel()
   375  
   376  	for _, testCase := range testCases {
   377  		// count is the number of field by hash key
   378  		ok, err := db.HSetNX(ctx, testCase.Key, testCase.Field, testCase.Value).Result()
   379  		if err != nil {
   380  			t.Fatalf("failed to HSetNX, err: %v", err)
   381  		}
   382  		t.Logf("key: %v, field: %v, ok: %v", testCase.Key, testCase.Field, ok)
   383  
   384  	}
   385  }
   386  
   387  // set values by fields and  hash key when field is not exist
   388  /*
   389     redis_hset_test.go:397: key: hset-test-1, values: [10 hset-test-1]
   390     redis_hset_test.go:397: key: hset-test-2, values: [hset-test-2 20 new field new field_1]
   391     redis_hset_test.go:397: key: hset-test-no-exist-1, values: [30]
   392  */
   393  func TestHVals(t *testing.T) {
   394  
   395  	db := GetDBOrDie()
   396  
   397  	testCases := []struct {
   398  		Key string
   399  	}{
   400  		{
   401  			Key: "hset-test-1",
   402  		},
   403  		{
   404  			Key: "hset-test-2",
   405  		},
   406  		{
   407  			Key: "hset-test-no-exist-1",
   408  		},
   409  	}
   410  
   411  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   412  	defer cancel()
   413  
   414  	for _, testCase := range testCases {
   415  		// count is the number of field by hash key
   416  		vals, err := db.HVals(ctx, testCase.Key).Result()
   417  		if err != nil {
   418  			t.Fatalf("failed to HVals, err: %v", err)
   419  		}
   420  		t.Logf("key: %v, values: %v", testCase.Key, vals)
   421  
   422  	}
   423  }