refactor(service): enrich conversion client result

This commit is contained in:
2026-09-21 07:56:41 +03:30
parent c6ae711c81
commit 18081c8efe
2 changed files with 50 additions and 16 deletions

View File

@@ -7,7 +7,7 @@ import (
)
type fakeCardToIBANClient struct {
iban string
result ConversionResult
err error
called bool
receivedCard string
@@ -17,24 +17,28 @@ type fakeCardToIBANClient struct {
func (f *fakeCardToIBANClient) Convert(
ctx context.Context,
cardNumber string,
) (string, error) {
) (ConversionResult, error) {
f.called = true
f.receivedCard = cardNumber
f.receivedContext = ctx
return f.iban, f.err
return f.result, f.err
}
func TestCardServiceConvertCardToIBANSuccess(t *testing.T) {
client := &fakeCardToIBANClient{
iban: "IR123456789012345678901234",
result: ConversionResult{
IBAN: "IR123456789012345678901234",
UpstreamHTTPStatus: 200,
RawResponse: []byte(`{"success":true}`),
},
}
service := NewCardService(client)
cardNumber := "6037991234567890"
iban, err := service.ConvertCardToIBAN(
result, err := service.ConvertCardToIBAN(
context.Background(),
cardNumber,
)
@@ -43,11 +47,19 @@ func TestCardServiceConvertCardToIBANSuccess(t *testing.T) {
t.Fatalf("ConvertCardToIBAN() unexpected error: %v", err)
}
if iban != client.iban {
if result.IBAN != client.result.IBAN {
t.Errorf(
"ConvertCardToIBAN() iban = %q, want %q",
iban,
client.iban,
result.IBAN,
client.result.IBAN,
)
}
if result.UpstreamHTTPStatus != client.result.UpstreamHTTPStatus {
t.Errorf(
"ConvertCardToIBAN() status = %d, want %d",
result.UpstreamHTTPStatus,
client.result.UpstreamHTTPStatus,
)
}
@@ -87,12 +99,16 @@ func TestCardServiceReturnsClientError(t *testing.T) {
clientErr := errors.New("upstream unavailable")
client := &fakeCardToIBANClient{
result: ConversionResult{
UpstreamHTTPStatus: 503,
RawResponse: []byte(`{"error":"unavailable"}`),
},
err: clientErr,
}
service := NewCardService(client)
_, err := service.ConvertCardToIBAN(
result, err := service.ConvertCardToIBAN(
context.Background(),
"6037991234567890",
)
@@ -108,11 +124,20 @@ func TestCardServiceReturnsClientError(t *testing.T) {
clientErr,
)
}
if result.UpstreamHTTPStatus != 503 {
t.Errorf(
"ConvertCardToIBAN() status = %d, want 503",
result.UpstreamHTTPStatus,
)
}
}
func TestCardServicePassesContextToClient(t *testing.T) {
client := &fakeCardToIBANClient{
iban: "IR123456789012345678901234",
result: ConversionResult{
IBAN: "IR123456789012345678901234",
},
}
service := NewCardService(client)