Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
more verbose to handle tests error
Browse files Browse the repository at this point in the history
  • Loading branch information
dimboknv committed Sep 20, 2022
1 parent 2c0d0a3 commit c1524af
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 173 deletions.
27 changes: 18 additions & 9 deletions balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,34 @@ func Test_CardBalance(t *testing.T) {
cases := []struct {
data []byte
expected CardBalance
withErr bool
errMsg string
}{
{
[]byte(`<cardbalance><bal_date>02.09.13 21:34</bal_date><bal_dyn>dyn</bal_dyn><card><account>acc</account><card_number>num</card_number><acc_name>name</acc_name><acc_type>acctype</acc_type><currency>UAH</currency><card_type>type</card_type><main_card_number>main</main_card_number><card_stat>Status</card_stat><src>src</src></card><av_balance>1.23</av_balance><balance>3.21</balance><fin_limit>0.10</fin_limit><trade_limit>0.01</trade_limit></cardbalance>`),
CardBalance{Date: time.Date(2013, 9, 2, 21, 34, 0, 0, kievLocation), Dyn: "dyn", Card: Card{Account: "acc", Number: "num", AccName: "name", AccType: "acctype", Currency: "UAH", Type: "type", MainCard: "main", Status: "Status", Src: "src"}, Available: 123, Balance: 321, FinLimit: 10, TradeLimit: 1},
false,
"",
},
{
[]byte(`<cardbalance><bal_date>02.09.13 21:34</bal_date><bal_dyn>dyn</bal_dyn><card><acc`),
CardBalance{},
true,
"unexpected EOF",
},
{
[]byte(`<cardbalance><bal_date>02-09-13 21:34</bal_date></cardbalance>`),
CardBalance{},
true,
"parsing time",
},
}
for i, c := range cases {
c := c
t.Run(strconv.Itoa(i), func(t *testing.T) {
var actual CardBalance
err := xml.Unmarshal(c.data, &actual)
require.True(t, c.withErr == (err != nil), err)
if err := xml.Unmarshal(c.data, &actual); err != nil {
require.NotEmpty(t, c.errMsg)
require.ErrorContains(t, err, c.errMsg)
return
}
require.Empty(t, c.errMsg)
require.Equal(t, c.expected, actual)
})
}
Expand All @@ -75,7 +79,7 @@ func TestClient_GetCardBalance(t *testing.T) {
reqBody []byte
respBody []byte
expected CardBalance
withErr bool
errMsg string
}{
{
opts: BalanceOpts{
Expand All @@ -100,7 +104,7 @@ func TestClient_GetCardBalance(t *testing.T) {
CardNumber: "sdalkfj",
Country: "USA",
},
withErr: true,
errMsg: "invalid card number",
},
}
for i, c := range cases {
Expand All @@ -118,7 +122,12 @@ func TestClient_GetCardBalance(t *testing.T) {

cli := Client{do, nil, m}
actual, err := cli.GetCardBalance(context.Background(), c.opts)
require.True(t, c.withErr == (err != nil), err)
if err != nil {
require.NotEmpty(t, c.errMsg)
require.ErrorContains(t, err, c.errMsg)
return
}
require.Empty(t, c.errMsg)
require.Equal(t, c.expected, actual)
})
}
Expand Down
76 changes: 32 additions & 44 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestClient_DoContext(t *testing.T) {
m Merchant
body []byte
code int
withErr bool
errMsg string
}{
{ // ok
expected: Response{
Expand All @@ -35,7 +35,7 @@ func TestClient_DoContext(t *testing.T) {
m: Merchant{"id", "pass"},
code: 200,
},
{ // can`t unmarshal xml response
{
expected: Response{
XMLName: xml.Name{Local: "response"},
MerchantSign: MerchantSign{"id", "ad67cf1c11e0f87bedac2c9bb260e3abf54e9862"},
Expand All @@ -46,12 +46,12 @@ func TestClient_DoContext(t *testing.T) {
}{Test: "test"},
},
},
body: []byte(xml.Header + `<response><data><info><test>test</test></info><oper>cmt</oper></data><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
m: Merchant{"id", "pass"},
code: 200,
withErr: true,
body: []byte(xml.Header + `<response><data><info><test>test</test></info><oper>cmt</oper></data><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
m: Merchant{"id", "pass"},
code: 200,
errMsg: "can`t unmarshal xml response",
},
{ // unexpected xml response content
{
expected: Response{
XMLName: xml.Name{Local: "response"},
MerchantSign: MerchantSign{"id", "ad67cf1c11e0f87bedac2c9bb260e3abf54e9862"},
Expand All @@ -62,12 +62,12 @@ func TestClient_DoContext(t *testing.T) {
}{Test: "test"},
},
},
body: []byte(xml.Header + `<response><data><info><test>test</test></info><oper>cmt</oper><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
m: Merchant{"id", "pass"},
code: 200,
withErr: true,
body: []byte(xml.Header + `<response><data><info><test>test</test></info><oper>cmt</oper><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
m: Merchant{"id", "pass"},
code: 200,
errMsg: "unexpected xml response content",
},
{ // can`t unmarshal data
{
expected: Response{
XMLName: xml.Name{Local: "response"},
MerchantSign: MerchantSign{"id", "ad67cf1c11e0f87bedac2c9bb260e3abf54e9862"},
Expand All @@ -78,12 +78,12 @@ func TestClient_DoContext(t *testing.T) {
}{Test: "test"},
},
},
body: []byte(xml.Header + `<response><data><info><test1>test1</test1></info><oper>cmt</oper></data><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
m: Merchant{"id", "pass"},
withErr: true,
code: 200,
body: []byte(xml.Header + `<response><data><info><test>test</test></info><oper>cmt</oper></data><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
m: Merchant{"id", "other pass"},
errMsg: "invalid signature",
code: 200,
},
{ // invalid signature
{
expected: Response{
XMLName: xml.Name{Local: "response"},
MerchantSign: MerchantSign{"id", "ad67cf1c11e0f87bedac2c9bb260e3abf54e9862"},
Expand All @@ -94,12 +94,12 @@ func TestClient_DoContext(t *testing.T) {
}{Test: "test"},
},
},
body: []byte(xml.Header + `<response><data><info><test>test</test></info><oper>cmt</oper></data><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
m: Merchant{"id", "other pass"},
withErr: true,
code: 200,
body: []byte(xml.Header + `<response><data><info>some error</info><oper>cmt</oper></data><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
m: Merchant{"id", "pass"},
errMsg: "xml response with err",
code: 200,
},
{ // xml response with err
{
expected: Response{
XMLName: xml.Name{Local: "response"},
MerchantSign: MerchantSign{"id", "ad67cf1c11e0f87bedac2c9bb260e3abf54e9862"},
Expand All @@ -110,26 +110,10 @@ func TestClient_DoContext(t *testing.T) {
}{Test: "test"},
},
},
body: []byte(xml.Header + `<response><data><info>some error</info><oper>cmt</oper></data><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
m: Merchant{"id", "pass"},
withErr: true,
code: 200,
},
{ // invalid http code
expected: Response{
XMLName: xml.Name{Local: "response"},
MerchantSign: MerchantSign{"id", "ad67cf1c11e0f87bedac2c9bb260e3abf54e9862"},
Data: ResponseData{
Oper: defaultOper,
Info: struct {
Test string `xml:"test"`
}{Test: "test"},
},
},
body: []byte(xml.Header + `<response><data><info><test>test</test></info><oper>cmt</oper></data><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
m: Merchant{"id", "pass"},
withErr: true,
code: 400,
body: []byte(xml.Header + `<response><data><info><test>test</test></info><oper>cmt</oper></data><merchant><id>id</id><signature>ad67cf1c11e0f87bedac2c9bb260e3abf54e9862</signature></merchant></response>`),
m: Merchant{"id", "pass"},
errMsg: "unexpected http status code",
code: 400,
},
}
url, method, req := "http://localhost", "POST", Request{}
Expand All @@ -155,8 +139,12 @@ func TestClient_DoContext(t *testing.T) {

cli := Client{do, nil, c.m}
actual := c.expected
err := cli.DoContext(context.Background(), url, method, req, &actual)
require.True(t, c.withErr == (err != nil), err)
if err := cli.DoContext(context.Background(), url, method, req, &actual); err != nil {
require.NotEmpty(t, c.errMsg)
require.ErrorContains(t, err, c.errMsg)
return
}
require.Empty(t, c.errMsg)
require.Equal(t, c.expected, actual)
})
}
Expand Down
127 changes: 67 additions & 60 deletions funds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,41 @@ func Test_Amount(t *testing.T) {
cases := []struct {
text []byte
expected Amount
withErr bool
errMsg string
}{
{[]byte("0"), 0, false},

{[]byte("0.01"), 1, false},
{[]byte("0.20"), 20, false},
{[]byte("21"), 2100, false},
{[]byte("100.09"), 10009, false},
{[]byte("100.90"), 10090, false},
{[]byte("100.89"), 10089, false},

{[]byte("-0.01"), -1, false},
{[]byte("-0.20"), -20, false},
{[]byte("-21"), -2100, false},
{[]byte("-100.09"), -10009, false},
{[]byte("-100.90"), -10090, false},
{[]byte("-100.89"), -10089, false},
{[]byte("1.15"), 115, false},

{[]byte(""), 0, true},
{[]byte("31.as"), 0, true},
{[]byte("as.31"), 0, true},
{[]byte("hello"), 0, true},
{[]byte("-32.-89"), 0, true},
{[]byte("0"), 0, ""},

{[]byte("0.01"), 1, ""},
{[]byte("0.20"), 20, ""},
{[]byte("21"), 2100, ""},
{[]byte("100.09"), 10009, ""},
{[]byte("100.90"), 10090, ""},
{[]byte("100.89"), 10089, ""},

{[]byte("-0.01"), -1, ""},
{[]byte("-0.20"), -20, ""},
{[]byte("-21"), -2100, ""},
{[]byte("-100.09"), -10009, ""},
{[]byte("-100.90"), -10090, ""},
{[]byte("-100.89"), -10089, ""},
{[]byte("1.15"), 115, ""},

{[]byte(""), 0, "parsing"},
{[]byte("31.as"), 0, "parsing"},
{[]byte("as.31"), 0, "parsing"},
{[]byte("hello"), 0, "parsing"},
{[]byte("-32.-89"), 0, "parsing"},
}
for i, c := range cases {
c := c
t.Run(strconv.Itoa(i), func(t *testing.T) {
var actual Amount
err := actual.UnmarshalText(c.text)
require.True(t, c.withErr == (err != nil), err)
if err := actual.UnmarshalText(c.text); err != nil {
require.NotEmpty(t, c.errMsg, err)
require.ErrorContains(t, err, c.errMsg)
return
}
require.Empty(t, c.errMsg)
require.Equal(t, c.expected, actual)
})
}
Expand All @@ -85,60 +89,63 @@ func Test_Funds(t *testing.T) {
cases := []struct {
expected []byte
f Funds
withErr bool
}{
{[]byte("0 UAH"), Funds{"UAH", 0}, false},
{[]byte("0 "), Funds{"", 0}, false},

{[]byte("21 UAH"), Funds{"UAH", 2100}, false},
{[]byte("100.09 USD"), Funds{"USD", 10009}, false},
{[]byte("100.90 USD"), Funds{"USD", 10090}, false},
{[]byte("100.89 BTC"), Funds{"BTC", 10089}, false},

{[]byte("-21 EUR"), Funds{"EUR", -2100}, false},
{[]byte("-100.09 ETH"), Funds{"ETH", -10009}, false},
{[]byte("-100.90 ETH"), Funds{"ETH", -10090}, false},
{[]byte("-100.89 RUB"), Funds{"RUB", -10089}, false},
{[]byte("0 UAH"), Funds{"UAH", 0}},
{[]byte("0 "), Funds{"", 0}},

{[]byte("21 UAH"), Funds{"UAH", 2100}},
{[]byte("100.09 USD"), Funds{"USD", 10009}},
{[]byte("100.90 USD"), Funds{"USD", 10090}},
{[]byte("100.89 BTC"), Funds{"BTC", 10089}},

{[]byte("-21 EUR"), Funds{"EUR", -2100}},
{[]byte("-100.09 ETH"), Funds{"ETH", -10009}},
{[]byte("-100.90 ETH"), Funds{"ETH", -10090}},
{[]byte("-100.89 RUB"), Funds{"RUB", -10089}},
}
for i, c := range cases {
c := c
t.Run(strconv.Itoa(i), func(t *testing.T) {
actual, err := c.f.MarshalText()
require.True(t, c.withErr == (err != nil), err)
actual, _ := c.f.MarshalText()
require.Equal(t, string(c.expected), string(actual))
})
}
})

t.Run("UnmarshalText", func(t *testing.T) {
cases := []struct {
text []byte
expected Funds
withErr bool
errMsg string
}{
{[]byte("0 UAH"), Funds{"UAH", 0}, false},

{[]byte("21 UAH"), Funds{"UAH", 2100}, false},
{[]byte("100.09 USD"), Funds{"USD", 10009}, false},
{[]byte("100.90 USD"), Funds{"USD", 10090}, false},
{[]byte("100.89 BTC"), Funds{"BTC", 10089}, false},

{[]byte("-21 EUR"), Funds{"EUR", -2100}, false},
{[]byte("-100.09 ETH"), Funds{"ETH", -10009}, false},
{[]byte("-100.90 ETH"), Funds{"ETH", -10090}, false},
{[]byte("-100.89 RUB"), Funds{"RUB", -10089}, false},

{[]byte(""), Funds{}, true},
{[]byte("31.as USD"), Funds{}, true},
{[]byte("as.31 USD"), Funds{}, true},
{[]byte("hello USD"), Funds{}, true},
{[]byte("-32.-89 USD"), Funds{}, true},
{[]byte("0 UAH"), Funds{"UAH", 0}, ""},

{[]byte("21 UAH"), Funds{"UAH", 2100}, ""},
{[]byte("100.09 USD"), Funds{"USD", 10009}, ""},
{[]byte("100.90 USD"), Funds{"USD", 10090}, ""},
{[]byte("100.89 BTC"), Funds{"BTC", 10089}, ""},

{[]byte("-21 EUR"), Funds{"EUR", -2100}, ""},
{[]byte("-100.09 ETH"), Funds{"ETH", -10009}, ""},
{[]byte("-100.90 ETH"), Funds{"ETH", -10090}, ""},
{[]byte("-100.89 RUB"), Funds{"RUB", -10089}, ""},

{[]byte(""), Funds{}, "parsing"},
{[]byte("31.as USD"), Funds{}, "parsing"},
{[]byte("as.31 USD"), Funds{}, "parsing"},
{[]byte("hello USD"), Funds{}, "parsing"},
{[]byte("-32.-89 USD"), Funds{}, "parsing"},
}
for i, c := range cases {
c := c
t.Run(strconv.Itoa(i), func(t *testing.T) {
var actual Funds
err := actual.UnmarshalText(c.text)
require.True(t, c.withErr == (err != nil), err)
if err := actual.UnmarshalText(c.text); err != nil {
require.NotEmpty(t, c.errMsg)
require.ErrorContains(t, err, c.errMsg)
return
}
require.Empty(t, c.errMsg)
require.Equal(t, c.expected, actual)
})
}
Expand Down
Loading

0 comments on commit c1524af

Please sign in to comment.