go-broker-test/cmd/server/validator/validator_test.go

160 lines
3.3 KiB
Go
Raw Normal View History

2025-05-07 00:25:34 +03:00
package validator
import (
"testing"
"github.com/stretchr/testify/assert"
"gitlab.com/digineat/go-broker-test/internal/model"
)
func TestValidateTrade(t *testing.T) {
tests := []struct {
name string
trade *model.Trade
wantError bool
}{
{
name: "Valid trade",
trade: &model.Trade{
Account: "acc123",
Symbol: "EURUSD",
Volume: 1.0,
OpenPrice: 1.1234,
ClosePrice: 1.1250,
Side: "buy",
},
wantError: false,
},
{
name: "Missing required field - Account",
trade: &model.Trade{
Symbol: "EURUSD",
Volume: 1.0,
OpenPrice: 1.1234,
ClosePrice: 1.1250,
Side: "buy",
},
wantError: true,
},
{
name: "Invalid Volume (gt=0)",
trade: &model.Trade{
Account: "acc123",
Symbol: "EURUSD",
Volume: 0,
OpenPrice: 1.1234,
ClosePrice: 1.1250,
Side: "buy",
},
wantError: true,
},
{
name: "Invalid Side (oneof=buy sell)",
trade: &model.Trade{
Account: "acc123",
Symbol: "EURUSD",
Volume: 1.0,
OpenPrice: 1.1234,
ClosePrice: 1.1250,
Side: "hold",
},
wantError: true,
},
{
name: "Custom validation fail - Symbol too short",
trade: &model.Trade{
Account: "acc123",
Symbol: "EURUS",
Volume: 1.0,
OpenPrice: 1.1234,
ClosePrice: 1.1250,
Side: "buy",
},
wantError: true,
},
{
name: "Custom validation fail - Symbol too long",
trade: &model.Trade{
Account: "acc123",
Symbol: "EURUSDT",
Volume: 1.0,
OpenPrice: 1.1234,
ClosePrice: 1.1250,
Side: "buy",
},
wantError: true,
},
{
name: "Custom validation fail - Symbol lowercase",
trade: &model.Trade{
Account: "acc123",
Symbol: "eurusd",
Volume: 1.0,
OpenPrice: 1.1234,
ClosePrice: 1.1250,
Side: "buy",
},
wantError: true,
},
{
name: "Custom validation fail - Symbol with numbers",
trade: &model.Trade{
Account: "acc123",
Symbol: "EURUS1",
Volume: 1.0,
OpenPrice: 1.1234,
ClosePrice: 1.1250,
Side: "buy",
},
wantError: true,
},
{
name: "Symbol with exactly 6 uppercase letters (valid by custom)",
trade: &model.Trade{
Account: "validAcc",
Symbol: "AUDCAD",
Volume: 0.5,
OpenPrice: 0.9000,
ClosePrice: 0.9050,
Side: "sell",
},
wantError: false,
},
{
name: "Trade model with regex tag for symbol (if you switched from custom tag)",
trade: &model.Trade{
Account: "accRegex",
Symbol: "GBPJPY",
Volume: 10,
OpenPrice: 150.0,
ClosePrice: 150.5,
Side: "buy",
},
wantError: false,
},
{
name: "Trade model with regex tag for symbol - invalid",
trade: &model.Trade{
Account: "accRegexFail",
Symbol: "USDCAD!",
Volume: 1,
OpenPrice: 1.2,
ClosePrice: 1.3,
Side: "sell",
},
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateTrade(tt.trade)
if tt.wantError {
assert.Error(t, err, "Expected an error for test case: %s", tt.name)
} else {
assert.NoError(t, err, "Expected no error for test case: %s", tt.name)
}
})
}
}