Skip to content

Commit

Permalink
feat: Add rates to convert currency response (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
obalunenko authored Sep 8, 2023
1 parent a248812 commit 2d3a270
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 37 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
# Dependency directories (remove the comment below to include it)
# vendor/

# IDEs and editors
.idea
.vscode

bin/
*.tape
dist/
Expand Down
11 changes: 9 additions & 2 deletions internal/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func NewConverter(client nbggovge.Client) Converter {
// Response of conversion.
type Response struct {
models.Money
Rate float64
}

// Convert converts amount from currency to with rates according to passed date.
Expand Down Expand Up @@ -65,13 +66,19 @@ func (c converter) Convert(ctx context.Context, m models.Money, to string, date

tosum := convert(fromingel, 1/toCurrency.Rate, 1/float64(toCurrency.Quantity))

const places int32 = 2
rate := moneyutils.Div(tosum, m.Amount)

const (
amountPlaces int32 = 2
ratePlaces int32 = 4
)

return Response{
Money: models.Money{
Amount: round(tosum, places),
Amount: round(tosum, amountPlaces),
Currency: to,
},
Rate: round(rate, ratePlaces),
}, nil
}

Expand Down
83 changes: 57 additions & 26 deletions internal/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,18 @@ func TestConverter_Convert(t *testing.T) {
args: args{
ctx: ctx,
m: models.Money{
Amount: 2678.27,
Amount: 2_678.27,
Currency: currencies.EUR,
},
to: currencies.GEL,
date: time.Now(),
},
want: Response{
models.Money{
Amount: 7557.54,
Money: models.Money{
Amount: 7_557.54,
Currency: currencies.GEL,
},
Rate: 2.8218,
},
wantErr: assert.NoError,
},
Expand All @@ -90,17 +91,18 @@ func TestConverter_Convert(t *testing.T) {
args: args{
ctx: ctx,
m: models.Money{
Amount: 2678.27,
Amount: 2_678.27,
Currency: currencies.EUR,
},
to: currencies.EUR,
date: time.Now(),
},
want: Response{
models.Money{
Amount: 2678.27,
Money: models.Money{
Amount: 2_678.27,
Currency: currencies.EUR,
},
Rate: 1,
},
wantErr: assert.NoError,
},
Expand All @@ -112,17 +114,18 @@ func TestConverter_Convert(t *testing.T) {
args: args{
ctx: ctx,
m: models.Money{
Amount: 2678.27,
Amount: 2_678.27,
Currency: currencies.EUR,
},
to: currencies.GBP,
date: time.Now(),
},
want: Response{
models.Money{
Amount: 2299.50,
Money: models.Money{
Amount: 2_299.50,
Currency: currencies.GBP,
},
Rate: 0.8586,
},
wantErr: assert.NoError,
},
Expand All @@ -134,7 +137,7 @@ func TestConverter_Convert(t *testing.T) {
args: args{
ctx: ctx,
m: models.Money{
Amount: 2678.27,
Amount: 2_678.27,
Currency: "",
},
to: currencies.GBP,
Expand All @@ -151,7 +154,7 @@ func TestConverter_Convert(t *testing.T) {
args: args{
ctx: ctx,
m: models.Money{
Amount: 2678.27,
Amount: 2_678.27,
Currency: currencies.EUR,
},
to: "",
Expand All @@ -168,17 +171,18 @@ func TestConverter_Convert(t *testing.T) {
args: args{
ctx: ctx,
m: models.Money{
Amount: 2678.27,
Amount: 2_678.27,
Currency: currencies.GEL,
},
to: currencies.GEL,
date: time.Now(),
},
want: Response{
models.Money{
Amount: 2678.27,
Money: models.Money{
Amount: 2_678.27,
Currency: currencies.GEL,
},
Rate: 1,
},
wantErr: assert.NoError,
},
Expand All @@ -190,17 +194,18 @@ func TestConverter_Convert(t *testing.T) {
args: args{
ctx: ctx,
m: models.Money{
Amount: 2678.27,
Amount: 2_678.27,
Currency: currencies.PLN,
},
to: currencies.GEL,
date: time.Now(),
},
want: Response{
models.Money{
Amount: 1607.93,
Money: models.Money{
Amount: 1_607.93,
Currency: currencies.GEL,
},
Rate: 0.6004,
},
wantErr: assert.NoError,
},
Expand All @@ -212,17 +217,18 @@ func TestConverter_Convert(t *testing.T) {
args: args{
ctx: ctx,
m: models.Money{
Amount: 2678.27,
Amount: 2_678.27,
Currency: currencies.BYN,
},
to: currencies.GEL,
date: time.Now(),
},
want: Response{
models.Money{
Amount: 2883.16,
Money: models.Money{
Amount: 2_883.16,
Currency: currencies.GEL,
},
Rate: 1.0765,
},
wantErr: assert.NoError,
},
Expand All @@ -234,17 +240,18 @@ func TestConverter_Convert(t *testing.T) {
args: args{
ctx: ctx,
m: models.Money{
Amount: 2678.27,
Amount: 2_678.27,
Currency: currencies.BYN,
},
to: currencies.PLN,
date: time.Now(),
},
want: Response{
models.Money{
Amount: 4802.38,
Money: models.Money{
Amount: 4_802.38,
Currency: currencies.PLN,
},
Rate: 1.7931,
},
wantErr: assert.NoError,
},
Expand All @@ -256,17 +263,41 @@ func TestConverter_Convert(t *testing.T) {
args: args{
ctx: ctx,
m: models.Money{
Amount: 2678.27,
Amount: 2_678.27,
Currency: currencies.PLN,
},
to: currencies.BYN,
date: time.Now(),
},
want: Response{
models.Money{
Amount: 1493.66,
Money: models.Money{
Amount: 1_493.66,
Currency: currencies.BYN,
},
Rate: 0.5577,
},
wantErr: assert.NoError,
},
{
name: "RUB - EUR",
fields: fields{
client: newMockRatesClient(t),
},
args: args{
ctx: ctx,
m: models.Money{
Amount: 500_000,
Currency: currencies.RUB,
},
to: currencies.EUR,
date: time.Now(),
},
want: Response{
Money: models.Money{
Amount: 7_950.78,
Currency: currencies.EUR,
},
Rate: 0.0159,
},
wantErr: assert.NoError,
},
Expand Down
8 changes: 7 additions & 1 deletion internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models

import (
"fmt"
"strconv"
)

// Money model.
Expand All @@ -20,5 +21,10 @@ func NewMoney(amount float64, currency string) Money {
}

func (r Money) String() string {
return fmt.Sprintf("%.2f %s", r.Amount, r.Currency)
a := strconv.FormatFloat(r.Amount, 'f', -1, 64)
if r.Currency == "" {
return a
}

return fmt.Sprintf("%s %s", a, r.Currency)
}
14 changes: 11 additions & 3 deletions internal/models/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,31 @@ func TestMoney_String(t *testing.T) {
Amount: 25.26789,
Currency: currencies.GEL,
},
want: "25.27 GEL",
want: "25.26789 GEL",
},
{
name: "",
fields: fields{
Amount: 25.21289,
Currency: currencies.GEL,
},
want: "25.21 GEL",
want: "25.21289 GEL",
},
{
name: "",
fields: fields{
Amount: 25.21489,
Currency: currencies.GEL,
},
want: "25.21 GEL",
want: "25.21489 GEL",
},
{
name: "",
fields: fields{
Amount: 25.21489,
Currency: "",
},
want: "25.21489",
},
}

Expand Down
7 changes: 6 additions & 1 deletion internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ type ConvertResponse struct {
Date time.Time
Amount models.Money
Converted models.Money
Rate models.Money
}

func (c ConvertResponse) String() string {
var resp string

resp += fmt.Sprintf("Date: %s\n", c.Date.Format(layout))
resp += fmt.Sprintf("Amount: %s\n", c.Amount.String())
resp += fmt.Sprintf("Converted: %s", c.Converted.String())
resp += fmt.Sprintf("Converted: %s\n", c.Converted.String())
resp += fmt.Sprintf("Rate: %s", c.Rate.String())

return resp
}
Expand Down Expand Up @@ -160,10 +162,13 @@ func (s service) Convert(ctx context.Context, p ConvertRequest) (*ConvertRespons
return nil, fmt.Errorf("failed to convert: %w", err)
}

rate := models.NewMoney(converted.Rate, "")

return &ConvertResponse{
Date: date,
Amount: amount,
Converted: converted.Money,
Rate: rate,
}, nil
}

Expand Down
10 changes: 8 additions & 2 deletions internal/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (m mockConverter) Convert(_ context.Context, money models.Money, toCurrency
Amount: money.Amount,
Currency: toCurrency,
},
Rate: 1,
}, nil
}

Expand Down Expand Up @@ -78,6 +79,7 @@ func Test_service_Convert(t *testing.T) {
Amount: 568,
Currency: currencies.EUR,
},
Rate: models.NewMoney(1, ""),
},
wantErr: assert.NoError,
},
Expand Down Expand Up @@ -369,7 +371,7 @@ func TestCalculateResponse_String(t *testing.T) {
},
},
want: "Tax Rate: Employment 20 %\n" +
"Year Income: 0.00 GEL\n" +
"Year Income: 0 GEL\n" +
"Converted: 789.99 EUR\n" +
"Taxes: 99.02 AMD",
},
Expand All @@ -394,6 +396,7 @@ func TestConvertResponse_String(t *testing.T) {
Date time.Time
Amount models.Money
Converted models.Money
Rate models.Money
}

tests := []struct {
Expand All @@ -413,10 +416,12 @@ func TestConvertResponse_String(t *testing.T) {
Amount: 789.99,
Currency: currencies.EUR,
},
Rate: models.NewMoney(1.39, ""),
},
want: "Date: 2022-12-08\n" +
"Amount: 568.99 AED\n" +
"Converted: 789.99 EUR",
"Converted: 789.99 EUR\n" +
"Rate: 1.39",
},
}

Expand All @@ -426,6 +431,7 @@ func TestConvertResponse_String(t *testing.T) {
Date: tt.fields.Date,
Amount: tt.fields.Amount,
Converted: tt.fields.Converted,
Rate: tt.fields.Rate,
}

assert.Equalf(t, tt.want, c.String(), "String()")
Expand Down
Loading

0 comments on commit 2d3a270

Please sign in to comment.