mirror of
https://github.com/wahyd4/cash-code-test.git
synced 2026-08-10 22:06:21 +10:00
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package commands
|
|
|
|
import (
|
|
"github.com/wahyd4/cash/domains"
|
|
"testing"
|
|
)
|
|
|
|
func TestWithdrawCommand_Run(t *testing.T) {
|
|
bank := domains.InitBank()
|
|
bank.OpenAccount("Tom")
|
|
account, _ := bank.AccountFromName("Tom")
|
|
bank.DepositFor(account, 30)
|
|
|
|
type fields struct {
|
|
Account string
|
|
Money float64
|
|
}
|
|
type args struct {
|
|
bank *domains.Bank
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
wantErr bool
|
|
}{
|
|
{
|
|
"can withdraw some money",
|
|
fields{
|
|
Account: "Tom",
|
|
Money: 20.10,
|
|
},
|
|
args{bank: bank},
|
|
false,
|
|
},
|
|
{
|
|
"cannot withdraw money due to does not have enough money",
|
|
fields{
|
|
Account: "Tom",
|
|
Money: 50.0, //we only have 9.9 left
|
|
},
|
|
args{bank: bank},
|
|
true,
|
|
},
|
|
{
|
|
"cannot withdraw due to the user can't found",
|
|
fields{
|
|
Account: "NotExist",
|
|
Money: 0.00,
|
|
},
|
|
args{bank: bank},
|
|
true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
command := &WithdrawCommand{
|
|
Account: tt.fields.Account,
|
|
Money: tt.fields.Money,
|
|
}
|
|
if err := command.Run(tt.args.bank); (err != nil) != tt.wantErr {
|
|
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|