对接
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package gift
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type sendRepoStub struct {
|
||||
Repo
|
||||
called bool
|
||||
}
|
||||
|
||||
func (r *sendRepoStub) Send(_ context.Context, userID, roomID, giftID uint, quantity int) (*Send, error) {
|
||||
r.called = true
|
||||
return &Send{UserID: userID, RoomID: &roomID, GiftID: &giftID, Quantity: quantity}, nil
|
||||
}
|
||||
|
||||
type availabilityStub struct {
|
||||
live bool
|
||||
err error
|
||||
}
|
||||
|
||||
func (s availabilityStub) IsLive(context.Context, uint) (bool, error) { return s.live, s.err }
|
||||
|
||||
func TestSendRejectsOfflineRoomBeforeWalletMutation(t *testing.T) {
|
||||
repo := &sendRepoStub{}
|
||||
uc := newUsecase(repo, nil, availabilityStub{live: false})
|
||||
|
||||
if _, err := uc.Send(context.Background(), 1, 7, 3, 2); !errors.Is(err, ErrSendNotAllowed) {
|
||||
t.Fatalf("offline send error = %v", err)
|
||||
}
|
||||
if repo.called {
|
||||
t.Fatal("offline room reached gift repository")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendAcceptsCustomQuantityForLiveRoom(t *testing.T) {
|
||||
repo := &sendRepoStub{}
|
||||
uc := newUsecase(repo, nil, availabilityStub{live: true})
|
||||
|
||||
item, err := uc.Send(context.Background(), 1, 7, 3, 37)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !repo.called || item.Quantity != 37 {
|
||||
t.Fatalf("custom quantity result = %+v called=%v", item, repo.called)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user