mirror of
https://github.com/wahyd4/cash-code-test.git
synced 2026-08-09 05:16:05 +10:00
186 lines
3.9 KiB
Go
186 lines
3.9 KiB
Go
package workflow
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/wahyd4/cash/commands"
|
|
"github.com/wahyd4/cash/domains"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
// simpleCommand a dump implementation for test
|
|
type simpleCommand struct {
|
|
shouldErr bool
|
|
}
|
|
|
|
func (command *simpleCommand) Run(bank *domains.Bank) error {
|
|
if command.shouldErr {
|
|
return errors.New("some error")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestAPP_Run(t *testing.T) {
|
|
type fields struct {
|
|
Bank *domains.Bank
|
|
}
|
|
type args struct {
|
|
commands []commands.ICommand
|
|
}
|
|
|
|
bank := domains.InitBank()
|
|
bank.OpenAccount("Dan")
|
|
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
wantErr bool
|
|
totalBalanceInFloat float64
|
|
}{
|
|
{
|
|
"can finish all commands successfully",
|
|
fields{Bank: bank},
|
|
args{[]commands.ICommand{&simpleCommand{false}}},
|
|
false,
|
|
0.00,
|
|
},
|
|
{
|
|
"fail to run some commands",
|
|
fields{Bank: bank},
|
|
args{[]commands.ICommand{&simpleCommand{true}}},
|
|
true,
|
|
0.00,
|
|
},
|
|
{
|
|
"run real commands",
|
|
fields{Bank: bank},
|
|
args{[]commands.ICommand{
|
|
&commands.OpenAccountCommand{Username: "Tom"},
|
|
&commands.DepositCommand{
|
|
Account: "Tom",
|
|
Money: 520.30,
|
|
},
|
|
&commands.UserBalanceCommand{Account: "Tom"},
|
|
&commands.WithdrawCommand{
|
|
Account: "Tom",
|
|
Money: 340.00,
|
|
},
|
|
&commands.UserBalanceCommand{Account: "Tom"},
|
|
&commands.TotalBalanceCommand{},
|
|
},},
|
|
false,
|
|
180.30,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
app := &APP{
|
|
Bank: tt.fields.Bank,
|
|
}
|
|
err := app.Run(tt.args.commands)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
|
|
if err == nil {
|
|
assert.Equal(t, tt.totalBalanceInFloat, bank.TotalBalance())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAPP_Load(t *testing.T) {
|
|
bank := domains.InitBank()
|
|
|
|
type fields struct {
|
|
Bank *domains.Bank
|
|
}
|
|
type args struct {
|
|
filePath string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
want []commands.ICommand
|
|
wantErr bool
|
|
}{
|
|
{
|
|
"build commands from file input",
|
|
fields{Bank: bank},
|
|
args{filePath: "test_input.txt"},
|
|
[]commands.ICommand{
|
|
&commands.OpenAccountCommand{Username: "Tom"},
|
|
&commands.DepositCommand{
|
|
Account: "Tom",
|
|
Money: 520.30,
|
|
},
|
|
&commands.UserBalanceCommand{Account: "Tom"},
|
|
&commands.WithdrawCommand{
|
|
Account: "Tom",
|
|
Money: 340.00,
|
|
},
|
|
&commands.UserBalanceCommand{Account: "Tom"},
|
|
&commands.TotalBalanceCommand{},
|
|
},
|
|
false,
|
|
},
|
|
{
|
|
"build commands from file input for multiple users",
|
|
fields{Bank: bank},
|
|
args{filePath: "test_input_multiple_users.txt"},
|
|
[]commands.ICommand{
|
|
&commands.OpenAccountCommand{Username: "Tom"},
|
|
&commands.DepositCommand{
|
|
Account: "Tom",
|
|
Money: 520.30,
|
|
},
|
|
&commands.UserBalanceCommand{Account: "Tom"},
|
|
&commands.WithdrawCommand{
|
|
Account: "Tom",
|
|
Money: 340.00,
|
|
},
|
|
&commands.UserBalanceCommand{Account: "Tom"},
|
|
&commands.OpenAccountCommand{Username: "Lily"},
|
|
&commands.DepositCommand{
|
|
Account: "Lily",
|
|
Money: 1980.55,
|
|
},
|
|
&commands.UserBalanceCommand{Account: "Lily"},
|
|
&commands.WithdrawCommand{
|
|
Account: "Lily",
|
|
Money: 550.33,
|
|
},
|
|
&commands.UserBalanceCommand{Account: "Lily"},
|
|
&commands.TotalBalanceCommand{},
|
|
},
|
|
false,
|
|
},
|
|
{
|
|
"fail to load inputs",
|
|
fields{Bank: bank},
|
|
args{filePath: "doesnt_exist.txt"},
|
|
[]commands.ICommand{},
|
|
true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
app := &APP{
|
|
Bank: tt.fields.Bank,
|
|
}
|
|
got, err := app.Load(tt.args.filePath)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("Load() got = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|