-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove database interface (#220)
- Loading branch information
Showing
41 changed files
with
311 additions
and
749 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,70 @@ | ||
package voucher | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/pagu-project/pagu/internal/engine/command" | ||
"github.com/pagu-project/pagu/internal/entity" | ||
"github.com/pagu-project/pagu/pkg/amount" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestClaimNormal(t *testing.T) { | ||
voucher, db, client, wallet := setup(t) | ||
func TestClaim(t *testing.T) { | ||
td := setup(t) | ||
|
||
validatorAddr := "pc1p123" | ||
t.Run("normal", func(t *testing.T) { | ||
amt, _ := amount.NewAmount(100) | ||
db.EXPECT().GetVoucherByCode("12345678").Return( | ||
entity.Voucher{ | ||
ValidMonths: 1, | ||
Amount: amt, | ||
ID: 1, | ||
}, nil, | ||
).AnyTimes() | ||
voucherCode := "12345678" | ||
caller := &entity.User{DBModel: entity.DBModel{ID: 1}} | ||
cmd := &command.Command{} | ||
|
||
t.Run("Invalid Voucher Code", func(t *testing.T) { | ||
args := make(map[string]string) | ||
args["code"] = "0" | ||
args["address"] = "pc1z" | ||
result := td.voucherCmd.claimHandler(caller, cmd, args) | ||
assert.False(t, result.Successful) | ||
assert.Equal(t, result.Message, "An error occurred: voucher code is not valid, length must be 8") | ||
}) | ||
|
||
client.EXPECT().GetValidatorInfo(validatorAddr).Return( | ||
t.Run("Voucher Code Not Issued Yet", func(t *testing.T) { | ||
args := make(map[string]string) | ||
args["code"] = voucherCode | ||
args["address"] = "pc1z" | ||
result := td.voucherCmd.claimHandler(caller, cmd, args) | ||
assert.False(t, result.Successful) | ||
assert.Equal(t, result.Message, "An error occurred: voucher code is not valid, no voucher found") | ||
}) | ||
|
||
t.Run("Claim a Voucher", func(t *testing.T) { | ||
testVoucher := td.createTestVoucher(t, WithCode(voucherCode)) | ||
validatorAddr := "pc1p123" | ||
|
||
td.clientManager.EXPECT().GetValidatorInfo(validatorAddr).Return( | ||
nil, nil, | ||
).AnyTimes() | ||
|
||
client.EXPECT().FindPublicKey(validatorAddr, false).Return( | ||
td.clientManager.EXPECT().FindPublicKey(validatorAddr, false).Return( | ||
validatorAddr, nil, | ||
).AnyTimes() | ||
|
||
wallet.EXPECT().BondTransaction(validatorAddr, validatorAddr, "voucher 12345678 claimed by Pagu", amt).Return( | ||
td.wallet.EXPECT().BondTransaction(gomock.Any(), validatorAddr, | ||
"voucher 12345678 claimed by Pagu", testVoucher.Amount).Return( | ||
"0x1", nil, | ||
).AnyTimes() | ||
|
||
db.EXPECT().ClaimVoucher(uint(1), "0x1", uint(1)).Return( | ||
nil, | ||
).AnyTimes() | ||
|
||
cmd := &command.Command{} | ||
caller := &entity.User{ID: 1} | ||
|
||
args := make(map[string]string) | ||
args["code"] = "12345678" | ||
args["code"] = testVoucher.Code | ||
args["address"] = validatorAddr | ||
result := voucher.claimHandler(caller, cmd, args) | ||
result := td.voucherCmd.claimHandler(caller, cmd, args) | ||
assert.True(t, result.Successful) | ||
assert.Equal(t, result.Message, "Voucher claimed successfully!\n\n https://pacviewer.com/transaction/0x1") | ||
}) | ||
|
||
t.Run("wrong code", func(t *testing.T) { | ||
cmd := &command.Command{} | ||
caller := &entity.User{ID: 1} | ||
|
||
t.Run("Claim again", func(t *testing.T) { | ||
args := make(map[string]string) | ||
args["code"] = "0" | ||
args["code"] = voucherCode | ||
args["address"] = "pc1z" | ||
result := voucher.claimHandler(caller, cmd, args) | ||
result := td.voucherCmd.claimHandler(caller, cmd, args) | ||
assert.False(t, result.Successful) | ||
assert.Equal(t, result.Message, "An error occurred: voucher code is not valid, length must be 8") | ||
assert.Equal(t, result.Message, "An error occurred: voucher code claimed before") | ||
}) | ||
} | ||
|
||
func TestClaimNotFound(t *testing.T) { | ||
voucher, db, _, _ := setup(t) | ||
|
||
db.EXPECT().GetVoucherByCode("12345678").Return( | ||
entity.Voucher{}, errors.New(""), | ||
).AnyTimes() | ||
|
||
cmd := &command.Command{} | ||
caller := &entity.User{ID: 1} | ||
|
||
args := make(map[string]string) | ||
args["code"] = "12345678" | ||
args["address"] = "pc1z" | ||
result := voucher.claimHandler(caller, cmd, args) | ||
assert.False(t, result.Successful) | ||
assert.Equal(t, result.Message, "An error occurred: voucher code is not valid, no voucher found") | ||
} | ||
|
||
func TestClaimAlreadyClaimed(t *testing.T) { | ||
voucher, db, _, _ := setup(t) | ||
|
||
db.EXPECT().GetVoucherByCode("12345678").Return( | ||
entity.Voucher{ | ||
TxHash: "123456789", | ||
}, nil, | ||
).AnyTimes() | ||
|
||
cmd := &command.Command{} | ||
caller := &entity.User{ID: 1} | ||
|
||
args := make(map[string]string) | ||
args["code"] = "12345678" | ||
args["address"] = "pc1z" | ||
result := voucher.claimHandler(caller, cmd, args) | ||
assert.False(t, result.Successful) | ||
assert.Equal(t, result.Message, "An error occurred: voucher code claimed before") | ||
} |
Oops, something went wrong.