Skip to content

Migrate to classic school of unit testing #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Docker
.dockerignore
Dockerfile
docker-compose.yml

# Jetbrains IDEA
.idea
Expand All @@ -11,4 +14,6 @@ README.md

# Project
docs
tests
Makefile
.golangci.yml
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19-alpine as builder
FROM golang:1.21-alpine as builder

WORKDIR /build

Expand All @@ -16,4 +16,3 @@ FROM gcr.io/distroless/base:latest
COPY --from=builder /build/binary /app

CMD ["/app"]

55 changes: 18 additions & 37 deletions app/aggregate/bitcoin/entity/btc.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,38 @@
package bitcoinentity

import (
"errors"
"fmt"
"math/big"
"time"

"github.com/F0rzend/simple-go-webserver/app/common"

"github.com/shopspring/decimal"
)

const (
SatoshiInBitcoin = 100_000_000

BTCSuffix = "BTC"
BTCSuffix = "BTC"
)

type BTC struct {
amount decimal.Decimal
}

func NewBTC(amount float64) (BTC, error) {
if amount < 0 {
return BTC{}, ErrNegativeCurrency
}
return BTC{decimal.NewFromFloat(amount)}, nil
}

func MustNewBTC(amount float64) BTC {
btc, err := NewBTC(amount)
if err != nil {
panic(err)
}
return btc
func NewBTC(amount float64) BTC {
return BTC{decimal.NewFromFloat(amount)}
}

func (btc BTC) ToFloat() *big.Float {
return btc.amount.BigFloat()
}

func (btc BTC) ToFloat64() float64 {
amount, _ := btc.amount.Float64()
return amount
}

func (btc BTC) ToUSD(price BTCPrice) USD {
return USD{btc.amount.Mul(price.GetPrice().amount)}
}

func (btc BTC) IsZero() bool {
return btc.amount.IsZero()
}

func (btc BTC) String() string {
if btc.IsZero() {
if btc.amount.IsZero() {
return fmt.Sprintf("0 %s", BTCSuffix)
}
if btc.amount.IsInteger() {
Expand All @@ -61,21 +41,15 @@ func (btc BTC) String() string {

precision := MustCountPrecision(SatoshiInBitcoin)
format := fmt.Sprintf("%%.%df %s", precision, BTCSuffix)
return fmt.Sprintf(format, btc.ToFloat())
return fmt.Sprintf(format, btc.amount.BigFloat())
}

func (btc BTC) Add(toAdd BTC) BTC {
return BTC{btc.amount.Add(toAdd.amount)}
}

// TODO remove sub error
var ErrSubtractMoreBTCThanHave = errors.New("can't subtract more btc than available")

func (btc BTC) Sub(toSubtract BTC) (BTC, error) {
if toSubtract.amount.GreaterThan(btc.amount) {
return BTC{}, ErrSubtractMoreBTCThanHave
}
return BTC{btc.amount.Sub(toSubtract.amount)}, nil
func (btc BTC) Sub(toSubtract BTC) BTC {
return BTC{btc.amount.Sub(toSubtract.amount)}
}

func (btc BTC) LessThan(other BTC) bool {
Expand All @@ -91,11 +65,18 @@ type BTCPrice struct {
updatedAt time.Time
}

func NewBTCPrice(price USD, updatedAt time.Time) BTCPrice {
func NewBTCPrice(price USD, updatedAt time.Time) (BTCPrice, error) {
if price.LessThan(NewUSD(0)) {
return BTCPrice{}, common.NewFlaggedError(
"the price cannot be negative. Please pass a number greater than 0",
common.FlagInvalidArgument,
)
}

return BTCPrice{
price: price,
updatedAt: updatedAt,
}
}, nil
}

func (btcPrice BTCPrice) GetPrice() USD {
Expand Down
Loading