github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/cache/cache_test.go (about)

     1  package cache_test
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/ngocphuongnb/tetua/app/auth"
     9  	"github.com/ngocphuongnb/tetua/app/cache"
    10  	"github.com/ngocphuongnb/tetua/app/entities"
    11  	"github.com/ngocphuongnb/tetua/app/mock"
    12  	"github.com/ngocphuongnb/tetua/app/repositories"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func init() {
    17  	mock.CreateRepositories()
    18  }
    19  
    20  func TestCache(t *testing.T) {
    21  	repositories.Topic.Create(context.Background(), &entities.Topic{
    22  		ID:   1,
    23  		Name: "Topic 1",
    24  	})
    25  	repositories.Role.Create(context.Background(), auth.ROLE_ADMIN)
    26  	repositories.Role.Create(context.Background(), auth.ROLE_USER)
    27  	repositories.Role.Create(context.Background(), auth.ROLE_GUEST)
    28  
    29  	err := repositories.Role.SetPermissions(context.Background(), auth.ROLE_USER.ID, []*entities.PermissionValue{{
    30  		Action: "post.view",
    31  		Value:  entities.PERM_ALL,
    32  	}})
    33  
    34  	assert.Equal(t, nil, err)
    35  	assert.Equal(t, nil, cache.All())
    36  	assert.Equal(t, 1, len(cache.Topics))
    37  	assert.Equal(t, 3, len(cache.Roles))
    38  	assert.Equal(t, 2, cache.RolesPermissions[1].RoleID)
    39  	assert.Equal(t, []*entities.PermissionValue{{
    40  		Action: "post.view",
    41  		Value:  entities.PERM_ALL,
    42  	}}, cache.RolesPermissions[1].Permissions)
    43  }
    44  
    45  func TestCacheError(t *testing.T) {
    46  	ctx := context.WithValue(context.Background(), "query_error", true)
    47  
    48  	assert.Equal(t, errors.New("Get all topics error"), cache.CacheTopics(ctx))
    49  	assert.Equal(t, errors.New("Get all roles error"), cache.CachePermissions(ctx))
    50  }