github.com/k0marov/go-socnet@v0.0.0-20220715154813-90d07867c782/features/comments/domain/contexters/contexters_test.go (about)

     1  package contexters_test
     2  
     3  import (
     4  	likeable_contexters "github.com/k0marov/go-socnet/core/abstract/ownable_likeable/contexters"
     5  	"github.com/k0marov/go-socnet/core/general/core_values"
     6  	. "github.com/k0marov/go-socnet/core/helpers/test_helpers"
     7  	"testing"
     8  
     9  	"github.com/k0marov/go-socnet/features/comments/domain/contexters"
    10  	"github.com/k0marov/go-socnet/features/comments/domain/entities"
    11  	profile_entities "github.com/k0marov/go-socnet/features/profiles/domain/entities"
    12  )
    13  
    14  func TestCommentContextAdder(t *testing.T) {
    15  	comment := RandomComment()
    16  	author := RandomContextedProfile()
    17  	caller := RandomId()
    18  	context := likeable_contexters.OwnLikeContext{
    19  		IsLiked: RandomBool(),
    20  		IsMine:  RandomBool(),
    21  	}
    22  
    23  	authorGetter := func(id, callerId core_values.UserId) (profile_entities.ContextedProfile, error) {
    24  		if id == comment.AuthorId && callerId == caller {
    25  			return author, nil
    26  		}
    27  		panic("unexpected args")
    28  	}
    29  	t.Run("error case - getting author throws", func(t *testing.T) {
    30  		authorGetter := func(id, callerId core_values.UserId) (profile_entities.ContextedProfile, error) {
    31  			return profile_entities.ContextedProfile{}, RandomError()
    32  		}
    33  		_, err := contexters.NewCommentContextAdder(authorGetter, nil)(comment, caller)
    34  		AssertSomeError(t, err)
    35  	})
    36  	contextGetter := func(target string, ownerId, callerId core_values.UserId) (likeable_contexters.OwnLikeContext, error) {
    37  		if target == comment.Id && ownerId == author.Id && callerId == caller {
    38  			return context, nil
    39  		}
    40  		panic("unexpected args")
    41  	}
    42  	t.Run("error case - getting likeable context throws", func(t *testing.T) {
    43  		contextGetter := func(target string, ownerId, callerId core_values.UserId) (likeable_contexters.OwnLikeContext, error) {
    44  			return likeable_contexters.OwnLikeContext{}, RandomError()
    45  		}
    46  		_, err := contexters.NewCommentContextAdder(authorGetter, contextGetter)(comment, caller)
    47  		AssertSomeError(t, err)
    48  	})
    49  	t.Run("happy case", func(t *testing.T) {
    50  		contextedComment, err := contexters.NewCommentContextAdder(authorGetter, contextGetter)(comment, caller)
    51  		AssertNoError(t, err)
    52  		wantComment := entities.ContextedComment{
    53  			Comment:        comment,
    54  			OwnLikeContext: context,
    55  			Author:         author,
    56  		}
    57  		Assert(t, contextedComment, wantComment, "returned contexted comment")
    58  	})
    59  }