//go:build unittest
package server
import (
"html/template"
"reflect"
"strings"
"testing"
"time"
)
func Test_formatInt64(t *testing.T) {
tests := []struct {
name string
n int64
want template.HTML
}{
{"1", 1, "1"},
{"13", 13, "13"},
{"123", 123, "123"},
{"1234", 1234, `1234`},
{"91234", 91234, `91234`},
{"891234", 891234, `891234`},
{"7891234", 7891234, `7891234`},
{"67891234", 67891234, `67891234`},
{"567891234", 567891234, `567891234`},
{"4567891234", 4567891234, `4567891234`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := formatInt64(tt.n); !reflect.DeepEqual(got, tt.want) {
t.Errorf("formatInt64() = %v, want %v", got, tt.want)
}
})
}
}
func Test_formatTime(t *testing.T) {
timeNow = fixedTimeNow
tests := []struct {
name string
want template.HTML
}{
{
name: "2020-12-23 15:16:17",
want: `630 days 21 hours ago`,
},
{
name: "2022-08-23 11:12:13",
want: `23 days 1 hour ago`,
},
{
name: "2022-09-14 11:12:13",
want: `1 day 1 hour ago`,
},
{
name: "2022-09-14 14:12:13",
want: `22 hours 31 mins ago`,
},
{
name: "2022-09-15 09:33:26",
want: `3 hours 10 mins ago`,
},
{
name: "2022-09-15 12:23:56",
want: `20 mins ago`,
},
{
name: "2022-09-15 12:24:07",
want: `19 mins ago`,
},
{
name: "2022-09-15 12:43:21",
want: `35 secs ago`,
},
{
name: "2022-09-15 12:43:56",
want: `0 secs ago`,
},
{
name: "2022-09-16 12:43:56",
want: `2022-09-16 12:43:56`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tm, _ := time.Parse("2006-01-02 15:04:05", tt.name)
if got := timeSpan(&tm); !reflect.DeepEqual(got, tt.want) {
t.Errorf("formatTime() = %v, want %v", got, tt.want)
}
})
}
}
func Test_appendAmountSpan(t *testing.T) {
tests := []struct {
name string
class string
amount string
shortcut string
txDate string
want string
}{
{
name: "prim-amt 1.23456789 BTC",
class: "prim-amt",
amount: "1.23456789",
shortcut: "BTC",
want: `1.23456789 BTC`,
},
{
name: "prim-amt 1432134.23456 BTC",
class: "prim-amt",
amount: "1432134.23456",
shortcut: "BTC",
want: `1432134.23456 BTC`,
},
{
name: "sec-amt 1 EUR",
class: "sec-amt",
amount: "1",
shortcut: "EUR",
want: `1 EUR`,
},
{
name: "sec-amt -1 EUR",
class: "sec-amt",
amount: "-1",
shortcut: "EUR",
want: `-1 EUR`,
},
{
name: "sec-amt 432109.23 EUR",
class: "sec-amt",
amount: "432109.23",
shortcut: "EUR",
want: `432109.23 EUR`,
},
{
name: "sec-amt -432109.23 EUR",
class: "sec-amt",
amount: "-432109.23",
shortcut: "EUR",
want: `-432109.23 EUR`,
},
{
name: "sec-amt 43141.29 EUR",
class: "sec-amt",
amount: "43141.29",
shortcut: "EUR",
txDate: "2022-03-14",
want: `43141.29 EUR`,
},
{
name: "sec-amt -43141.29 EUR",
class: "sec-amt",
amount: "-43141.29",
shortcut: "EUR",
txDate: "2022-03-14",
want: `-43141.29 EUR`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var rv strings.Builder
appendAmountSpan(&rv, tt.class, tt.amount, tt.shortcut, tt.txDate)
if got := rv.String(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendAmountSpan() = %v, want %v", got, tt.want)
}
})
}
}
func Test_appendAmountSpanBitcoinType(t *testing.T) {
tests := []struct {
name string
class string
amount string
shortcut string
txDate string
want string
}{
{
name: "prim-amt 1.23456789 BTC",
class: "prim-amt",
amount: "1.23456789",
shortcut: "BTC",
want: `1.23456789 BTC`,
},
{
name: "prim-amt 1432134.23456 BTC",
class: "prim-amt",
amount: "1432134.23456",
shortcut: "BTC",
want: `1432134.23456000 BTC`,
},
{
name: "prim-amt 1 BTC",
class: "prim-amt",
amount: "1",
shortcut: "BTC",
want: `1.00000000 BTC`,
},
{
name: "prim-amt 0 BTC",
class: "prim-amt",
amount: "0",
shortcut: "BTC",
want: `0 BTC`,
},
{
name: "prim-amt 34.2 BTC",
class: "prim-amt",
amount: "34.2",
shortcut: "BTC",
want: `34.20000000 BTC`,
},
{
name: "prim-amt -34.2345678 BTC",
class: "prim-amt",
amount: "-34.2345678",
shortcut: "BTC",
want: `-34.23456780 BTC`,
},
{
name: "prim-amt -1234.2345 BTC",
class: "prim-amt",
amount: "-1234.2345",
shortcut: "BTC",
want: `-1234.23450000 BTC`,
},
{
name: "prim-amt -123.23 BTC",
class: "prim-amt",
amount: "-123.23",
shortcut: "BTC",
want: `-123.23000000 BTC`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var rv strings.Builder
appendAmountSpanBitcoinType(&rv, tt.class, tt.amount, tt.shortcut, tt.txDate)
if got := rv.String(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendAmountSpanBitcoinType() = %v, want %v", got, tt.want)
}
})
}
}