github.com/angryronald/go-kit@v0.0.0-20240505173814-ff2bd9c79dbf/generic/repository/memcached/memcachedtest/generic.repository_test.go (about)

     1  package memcachedtest
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"reflect"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/google/uuid"
    11  
    12  	"github.com/angryronald/go-kit/cast"
    13  	"github.com/angryronald/go-kit/generic/repository"
    14  	"github.com/angryronald/go-kit/generic/repository/memcached"
    15  )
    16  
    17  type TestModel struct {
    18  	ID   string
    19  	Name string
    20  	Age  int
    21  }
    22  
    23  var sample []*TestModel
    24  
    25  func init() {
    26  	sample = append(sample, &TestModel{
    27  		ID:   "1",
    28  		Name: "Alice",
    29  		Age:  30,
    30  	})
    31  	sample = append(sample, &TestModel{
    32  		ID:   "2",
    33  		Name: "Bob",
    34  		Age:  25,
    35  	})
    36  	sample = append(sample, &TestModel{
    37  		ID:   "3",
    38  		Name: "Charlie",
    39  		Age:  35,
    40  	})
    41  }
    42  
    43  func preSeeding(key string) {
    44  	bytes, _ := cast.ToBytes(sample)
    45  	// Insert test data into Redis
    46  	redisClient.Set(
    47  		context.Background(),
    48  		key,
    49  		bytes,
    50  		15*time.Minute,
    51  	)
    52  
    53  	for _, s := range sample {
    54  		bytes, _ := cast.ToBytes(s)
    55  		// Insert test data into Redis
    56  		redisClient.Set(
    57  			context.Background(),
    58  			fmt.Sprintf("%s%s%s", key, memcached.CONJUNCTION, s.ID),
    59  			bytes,
    60  			15*time.Minute,
    61  		)
    62  	}
    63  }
    64  
    65  func removePreseeding(key string) {
    66  	redisClient.Del(
    67  		context.Background(),
    68  		key,
    69  	)
    70  
    71  	for _, s := range sample {
    72  		redisClient.Del(
    73  			context.Background(),
    74  			fmt.Sprintf("%s%s%s", key, memcached.CONJUNCTION, s.ID),
    75  		)
    76  	}
    77  }
    78  
    79  func TestGenericRepository_FindAll(t *testing.T) {
    80  	// Create a GenericRepository instance
    81  	key := "testKey"
    82  	repo := memcached.NewRepository(redisClient, key, 0, []*memcached.KeyIdentifier{})
    83  
    84  	preSeeding(key)
    85  
    86  	params := map[string]interface{}{"NAME": "Alice"}
    87  	conditionalOperations := []repository.ConditionalOperation{repository.EQUAL_WITH}
    88  	relationalOperations := []repository.RelationalOperation{}
    89  	page := 0
    90  	limit := 10
    91  	result := []*TestModel{}
    92  
    93  	// Test FindAll
    94  	foundData, err := repo.FindAll(context.Background(), params, conditionalOperations, relationalOperations, page, limit, result)
    95  	if err != nil {
    96  		t.Errorf("FindAll failed: %v", err)
    97  	}
    98  
    99  	// Check if the data was found
   100  	if len(foundData.([]interface{})) != 1 {
   101  		t.Errorf("FindAll did not return the expected number of results")
   102  	}
   103  }
   104  
   105  func TestGenericRepository_Insert(t *testing.T) {
   106  	// Create a GenericRepository instance
   107  	key := "testKey"
   108  	repo := memcached.NewRepository(redisClient, key, 0, []*memcached.KeyIdentifier{})
   109  
   110  	preSeeding(key)
   111  
   112  	data := &TestModel{
   113  		ID:   "4",
   114  		Name: "David",
   115  		Age:  28,
   116  	}
   117  
   118  	// Test Insert
   119  	_, err := repo.Insert(context.Background(), data)
   120  	if err != nil {
   121  		t.Errorf("Insert failed: %v", err)
   122  	}
   123  
   124  	// Check if the data was inserted into Redis
   125  	resultRaw, err := redisClient.Get(context.Background(), key).Result()
   126  	if err != nil {
   127  		t.Errorf("Insert did not insert data into Redis")
   128  	}
   129  
   130  	result := []*TestModel{}
   131  	cast.FromBytes([]byte(resultRaw), &result)
   132  
   133  	if !reflect.DeepEqual(data, result[0]) {
   134  		t.Errorf("should got %v, but got %v", data, result[3])
   135  	}
   136  }
   137  
   138  func TestGenericRepository_Update(t *testing.T) {
   139  	key := "testKey"
   140  	// Create a GenericRepository instance
   141  	repo := memcached.NewRepository(redisClient, key, 0, []*memcached.KeyIdentifier{})
   142  
   143  	preSeeding(key)
   144  
   145  	data := &TestModel{
   146  		ID:   "2",
   147  		Name: "Updated Bob",
   148  		Age:  26,
   149  	}
   150  
   151  	// Test Update
   152  	_, err := repo.Update(context.Background(), data)
   153  	if err != nil {
   154  		t.Errorf("Update failed: %v", err)
   155  	}
   156  
   157  	// Check if the data was updated in Redis
   158  	resultRaw, err := redisClient.Get(context.Background(), key).Result()
   159  	if err != nil {
   160  		t.Errorf("update did not update data into Redis")
   161  	}
   162  
   163  	result := []*TestModel{}
   164  	cast.FromBytes([]byte(resultRaw), &result)
   165  
   166  	if !reflect.DeepEqual(data, result[1]) {
   167  		t.Errorf("should got %v, but got %v", data, result)
   168  	}
   169  }
   170  
   171  func TestGenericRepository_Delete(t *testing.T) {
   172  	// Create a GenericRepository instance
   173  	key := "testKey"
   174  	repo := memcached.NewRepository(redisClient, key, 15*time.Minute, []*memcached.KeyIdentifier{})
   175  
   176  	preSeeding(key)
   177  
   178  	data := &TestModel{
   179  		ID:   "2",
   180  		Name: "Bob",
   181  		Age:  25,
   182  	}
   183  
   184  	// Test Delete
   185  	_, err := repo.Delete(context.Background(), data)
   186  	if err != nil {
   187  		t.Errorf("Delete failed: %v", err)
   188  	}
   189  
   190  	// Check if the data was deleted from Redis
   191  	resultRaw, err := redisClient.Get(context.Background(), key).Result()
   192  	if err != nil {
   193  		t.Errorf("update did not update data into Redis")
   194  	}
   195  
   196  	result := []*TestModel{}
   197  	cast.FromBytes([]byte(resultRaw), &result)
   198  
   199  	if memcached.AreStructsEqual(data, result[1]) {
   200  		t.Errorf("should got %v, but got %v", data, result[1])
   201  	}
   202  }
   203  
   204  func TestGenericRepository_Upsert(t *testing.T) {
   205  	// Create a GenericRepository instance
   206  	key := "testKey"
   207  	repo := memcached.NewRepository(redisClient, key, 15*time.Minute, []*memcached.KeyIdentifier{})
   208  
   209  	preSeeding(key)
   210  
   211  	data := &TestModel{
   212  		ID:   "4",
   213  		Name: "David",
   214  		Age:  28,
   215  	}
   216  
   217  	// Test Upsert
   218  	_, err := repo.Upsert(context.Background(), data)
   219  	if err != nil {
   220  		t.Errorf("Upsert failed: %v", err)
   221  	}
   222  
   223  	// Check if the data was upserted in Redis
   224  	resultRaw, err := redisClient.Get(context.Background(), key).Result()
   225  	if err != nil {
   226  		t.Errorf("update did not update data into Redis")
   227  	}
   228  
   229  	result := []*TestModel{}
   230  	cast.FromBytes([]byte(resultRaw), &result)
   231  
   232  	if !memcached.AreStructsEqual(data, result[3]) {
   233  		t.Errorf("should got %v, but got %v", data, result)
   234  	}
   235  }
   236  
   237  func TestGenericRepository_BulkInsert(t *testing.T) {
   238  	// Create a GenericRepository instance
   239  	key := "testKey"
   240  	repo := memcached.NewRepository(redisClient, key, 0, []*memcached.KeyIdentifier{})
   241  
   242  	// Test BulkInsert
   243  	_, err := repo.BulkInsert(context.Background(), sample)
   244  	if err != nil {
   245  		t.Errorf("BulkInsert failed: %v", err)
   246  	}
   247  
   248  	// Check if the data was bulk inserted into Redis
   249  	resultRaw, err := redisClient.Get(context.Background(), key).Result()
   250  	if err != nil {
   251  		t.Errorf("update did not update data into Redis")
   252  	}
   253  
   254  	result := []*TestModel{}
   255  	cast.FromBytes([]byte(resultRaw), &result)
   256  
   257  	if len(sample) != len(result) {
   258  		t.Errorf("should got %v, but got %v", sample, result)
   259  	}
   260  }
   261  
   262  func TestGenericRepository_BulkUpsert(t *testing.T) {
   263  	// Create a GenericRepository instance
   264  	key := "testKey"
   265  	repo := memcached.NewRepository(redisClient, key, 0, []*memcached.KeyIdentifier{})
   266  
   267  	// Test BulkUpsert
   268  	_, err := repo.BulkUpsert(context.Background(), sample)
   269  	if err != nil {
   270  		t.Errorf("BulkUpsert failed: %v", err)
   271  	}
   272  
   273  	// Check if the data was bulk upserted in Redis
   274  	resultRaw, err := redisClient.Get(context.Background(), key).Result()
   275  	if err != nil {
   276  		t.Errorf("update did not update data into Redis")
   277  	}
   278  
   279  	result := []*TestModel{}
   280  	cast.FromBytes([]byte(resultRaw), &result)
   281  
   282  	if len(sample) != len(result) {
   283  		t.Errorf("should got %v, but got %v", sample, result)
   284  	}
   285  }
   286  
   287  func TestGenericRepository_FindOne(t *testing.T) {
   288  	// Create a GenericRepository instance
   289  	key := "testKey"
   290  
   291  	repo := memcached.NewRepository(redisClient, key, 15*time.Minute, []*memcached.KeyIdentifier{})
   292  
   293  	preSeeding(key)
   294  
   295  	keyParam := "NAME"
   296  	value := "Alice"
   297  	result := &TestModel{}
   298  
   299  	// Test FindOne
   300  	foundData, err := repo.FindOne(context.Background(), keyParam, value, &result)
   301  	if err != nil {
   302  		t.Errorf("FindOne failed: %v", err)
   303  	}
   304  
   305  	// Check if the data was found
   306  	if foundData == nil {
   307  		t.Errorf("FindOne did not find the expected data")
   308  	}
   309  
   310  	expected := &TestModel{
   311  		ID:   "1",
   312  		Name: "Alice",
   313  		Age:  30,
   314  	}
   315  	cast.TransformObject(foundData, &result)
   316  	if !memcached.AreStructsEqual(expected, result) {
   317  		t.Errorf("expected %v, got %v", expected, result)
   318  	}
   319  }
   320  
   321  func TestGenericRepository_FindByID(t *testing.T) {
   322  	var TEST_ID = uuid.MustParse("25ebc2e2-7fd2-47a0-bd2f-a8ec6e4d0163")
   323  
   324  	// Create a GenericRepository instance
   325  	key := "testKey"
   326  	repo := memcached.NewRepository(redisClient, key, 15*time.Minute, []*memcached.KeyIdentifier{})
   327  
   328  	preSeeding(key)
   329  
   330  	sample = append(sample, &TestModel{
   331  		ID:   TEST_ID.String(),
   332  		Name: "Test",
   333  		Age:  1,
   334  	})
   335  
   336  	bytes, _ := cast.ToBytes(sample)
   337  	// Insert test data into Redis
   338  	redisClient.Set(
   339  		context.Background(),
   340  		fmt.Sprintf("%s%s%s", key, memcached.CONJUNCTION, TEST_ID.String()),
   341  		bytes,
   342  		15*time.Minute,
   343  	)
   344  
   345  	result := &TestModel{}
   346  
   347  	// Test FindByID
   348  	foundData, err := repo.FindByID(context.Background(), TEST_ID, &result)
   349  	if err != nil {
   350  		t.Errorf("FindByID failed: %v", err)
   351  	}
   352  
   353  	// Check if the data was found
   354  	if foundData == nil {
   355  		t.Errorf("FindByID did not find the expected data")
   356  	}
   357  }
   358  
   359  func TestGenericRepository_Query(t *testing.T) {
   360  	// Create a GenericRepository instance
   361  	key := "testKey"
   362  	repo := memcached.NewRepository(redisClient, key, 0, []*memcached.KeyIdentifier{})
   363  
   364  	query := "SELECT * FROM testKey WHERE Name = ?"
   365  	params := []interface{}{"Alice"}
   366  	result := []struct {
   367  		ID   string
   368  		Name string
   369  		Age  int
   370  	}{}
   371  
   372  	// Test Query
   373  	_, err := repo.Query(context.Background(), query, params, &result)
   374  	if err != repository.ErrNotImplement {
   375  		t.Errorf("Query did not return the expected error")
   376  	}
   377  }
   378  
   379  func TestNewRepository(t *testing.T) {
   380  	key := "testKey"
   381  	expirationDuration := time.Duration(0)
   382  	keyIdentifiers := []*memcached.KeyIdentifier{}
   383  	repo := memcached.NewRepository(redisClient, key, expirationDuration, keyIdentifiers)
   384  
   385  	// Check if the repository was created successfully
   386  	if repo == nil {
   387  		t.Errorf("NewRepository did not create a repository")
   388  	}
   389  }
   390  
   391  func TestNewMutableRepository(t *testing.T) {
   392  	key := "testKey"
   393  	expirationDuration := time.Duration(0)
   394  	keyIdentifiers := []*memcached.KeyIdentifier{}
   395  	repo := memcached.NewMutableRepository(redisClient, key, expirationDuration, keyIdentifiers)
   396  
   397  	// Check if the repository was created successfully
   398  	if repo == nil {
   399  		t.Errorf("NewMutableRepository did not create a repository")
   400  	}
   401  }
   402  
   403  func TestNewImmutableRepository(t *testing.T) {
   404  	key := "testKey"
   405  	expirationDuration := time.Duration(0)
   406  	keyIdentifiers := []*memcached.KeyIdentifier{}
   407  	repo := memcached.NewImmutableRepository(redisClient, key, expirationDuration, keyIdentifiers)
   408  
   409  	// Check if the repository was created successfully
   410  	if repo == nil {
   411  		t.Errorf("NewImmutableRepository did not create a repository")
   412  	}
   413  }