Skip to content

Commit

Permalink
Finished character service coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Wil Simpson committed Mar 8, 2024
1 parent f9bddf9 commit a5bb8e3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/service/character_s.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (s characterService) AddPlayTime(ctx context.Context, characterId uint, amo
}

character.PlayTime += amount
_, err = s.repo.Save(ctx, character)
character, err = s.repo.Save(ctx, character)

return character.PlayTime, err
}
53 changes: 53 additions & 0 deletions pkg/service/character_s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,57 @@ var _ = Describe("Character service", func() {
})
})
})

Describe("FindAll", func() {
It("should call repo directly", func() {
characters := []*model.Character{character}
mockRepository.EXPECT().FindAll(ctx).Return(characters, fakeError)
out, err := charService.FindAll(ctx)
Expect(err).To(MatchError(fakeError))
Expect(out).To(ContainElements(characters))
})
})

Describe("FindAllByOwner", func() {
It("should call repo directly", func() {
characters := []*model.Character{character}
mockRepository.EXPECT().FindAllByOwner(ctx, character.OwnerId).Return(characters, fakeError)
out, err := charService.FindAllByOwner(ctx, character.OwnerId)
Expect(err).To(MatchError(fakeError))
Expect(out).To(ContainElements(characters))
})
})

Describe("AddPlayTime", func() {
var amount uint64

BeforeEach(func() {
nums, err := faker.RandomInt(1, 1e5, 1)
Expect(err).NotTo(HaveOccurred())
amount = uint64(nums[0])
})

When("given valid input", func() {
It("should try to update playtime", func() {
mockRepository.EXPECT().FindById(ctx, character.ID).Return(character, nil)
charOut := new(model.Character)
*charOut = *character
charOut.PlayTime += amount
mockRepository.EXPECT().Save(ctx, gomock.Any()).Return(charOut, fakeError)
out, err := charService.AddPlayTime(ctx, character.ID, amount)
Expect(err).To(MatchError(fakeError))
Expect(out).To(BeEquivalentTo(charOut.PlayTime))
})
})

When("given invalid input", func() {
It("should error on find error", func() {
mockRepository.EXPECT().FindById(ctx, character.ID).Return(nil, fakeError)
out, err := charService.AddPlayTime(ctx, character.ID, amount)
Expect(err).To(MatchError(fakeError))
Expect(out).To(BeEquivalentTo(0))
})
})
})

})

0 comments on commit a5bb8e3

Please sign in to comment.