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,8 +7,17 @@ import (
"card-to-iban/internal/card" "card-to-iban/internal/card"
) )
type ConversionResult struct {
IBAN string
UpstreamHTTPStatus int
RawResponse []byte
}
type CardToIBANClient interface { type CardToIBANClient interface {
Convert(ctx context.Context, cardNumber string) (string, error) Convert(
ctx context.Context,
cardNumber string,
) (ConversionResult, error)
} }
type CardService struct { type CardService struct {
@@ -24,15 +33,15 @@ func NewCardService(client CardToIBANClient) *CardService {
func (s *CardService) ConvertCardToIBAN( func (s *CardService) ConvertCardToIBAN(
ctx context.Context, ctx context.Context,
cardNumber string, cardNumber string,
) (string, error) { ) (ConversionResult, error) {
if err := card.Validate(cardNumber); err != nil { if err := card.Validate(cardNumber); err != nil {
return "", err return ConversionResult{}, err
} }
iban, err := s.client.Convert(ctx, cardNumber) result, err := s.client.Convert(ctx, cardNumber)
if err != nil { if err != nil {
return "", fmt.Errorf("convert card to iban: %w", err) return result, fmt.Errorf("convert card to iban: %w", err)
} }
return iban, nil return result, nil
} }

View File

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