github.com/k0marov/go-socnet@v0.0.0-20220715154813-90d07867c782/features/comments/store/store_test.go (about)

     1  package store_test
     2  
     3  import (
     4  	"github.com/k0marov/go-socnet/core/general/core_values"
     5  	. "github.com/k0marov/go-socnet/core/helpers/test_helpers"
     6  	"testing"
     7  
     8  	"github.com/k0marov/go-socnet/features/comments/domain/entities"
     9  	comment_models "github.com/k0marov/go-socnet/features/comments/domain/models"
    10  	"github.com/k0marov/go-socnet/features/comments/store"
    11  )
    12  
    13  func TestCommentsGetter(t *testing.T) {
    14  	commentModels := []comment_models.CommentModel{RandomCommentModel()}
    15  	likes := RandomInt()
    16  	author := RandomId()
    17  
    18  	commentsGetter := func(authorId core_values.UserId) ([]comment_models.CommentModel, error) {
    19  		if authorId == author {
    20  			return commentModels, nil
    21  		}
    22  		panic("unexpected args")
    23  	}
    24  	t.Run("error case - getting comments from db throws", func(t *testing.T) {
    25  		commentsGetter := func(id core_values.UserId) ([]comment_models.CommentModel, error) {
    26  			return nil, RandomError()
    27  		}
    28  		_, err := store.NewCommentsGetter(commentsGetter, nil)(author)
    29  		AssertSomeError(t, err)
    30  	})
    31  	likesGetter := func(targetId string) (int, error) {
    32  		if targetId == commentModels[0].Id {
    33  			return likes, nil
    34  		}
    35  		panic("unexpected args")
    36  	}
    37  	t.Run("error case - getting likes throws", func(t *testing.T) {
    38  		likesGetter := func(string) (int, error) {
    39  			return 0, RandomError()
    40  		}
    41  		_, err := store.NewCommentsGetter(commentsGetter, likesGetter)(author)
    42  		AssertSomeError(t, err)
    43  	})
    44  	gotComments, err := store.NewCommentsGetter(commentsGetter, likesGetter)(author)
    45  	AssertNoError(t, err)
    46  	wantComments := []entities.Comment{
    47  		{
    48  			CommentModel: commentModels[0],
    49  			Likes:        likes,
    50  		},
    51  	}
    52  	Assert(t, gotComments, wantComments, "returned comments")
    53  }