github.com/k0marov/go-socnet@v0.0.0-20220715154813-90d07867c782/features/posts/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/posts/domain/contexters" 10 "github.com/k0marov/go-socnet/features/posts/domain/entities" 11 profile_entities "github.com/k0marov/go-socnet/features/profiles/domain/entities" 12 ) 13 14 func TestPostContextAdder(t *testing.T) { 15 post := RandomPost() 16 caller := RandomId() 17 author := RandomContextedProfile() 18 ctx := RandomLikeableContext() 19 20 getProfile := func(id, callerId core_values.UserId) (profile_entities.ContextedProfile, error) { 21 if id == post.PostModel.AuthorId && callerId == caller { 22 return author, nil 23 } 24 panic("unexpected args") 25 } 26 t.Run("error case - getting author throws", func(t *testing.T) { 27 getProfile := func(id, callerId core_values.UserId) (profile_entities.ContextedProfile, error) { 28 return profile_entities.ContextedProfile{}, RandomError() 29 } 30 _, err := contexters.NewPostContextAdder(getProfile, nil)(post, caller) 31 AssertSomeError(t, err) 32 }) 33 getContext := func(target string, owner, callerId core_values.UserId) (likeable_contexters.OwnLikeContext, error) { 34 if target == post.Id && owner == author.Id && callerId == caller { 35 return ctx, nil 36 } 37 panic("unexpected args") 38 } 39 t.Run("error case - getting context throws", func(t *testing.T) { 40 getContext := func(string, core_values.UserId, core_values.UserId) (likeable_contexters.OwnLikeContext, error) { 41 return likeable_contexters.OwnLikeContext{}, RandomError() 42 } 43 _, err := contexters.NewPostContextAdder(getProfile, getContext)(post, caller) 44 AssertSomeError(t, err) 45 }) 46 t.Run("happy case", func(t *testing.T) { 47 wantPost := entities.ContextedPost{ 48 Post: post, 49 Author: author, 50 OwnLikeContext: ctx, 51 } 52 gotPost, err := contexters.NewPostContextAdder(getProfile, getContext)(post, caller) 53 AssertNoError(t, err) 54 Assert(t, gotPost, wantPost, "returned post") 55 }) 56 }