Files
gocryptotrader/common/convert/convert_test.go
Scott 6eaa2e4073 Backtester: USD tracking (#818)
* Initial concept for creating price tracking pairs

* Completes coverage, even with a slow test

* I dont know what point to hook this stuff up

* Bit of a broken way of handling tracking pairs

* Correctly calculates USD rates against all currencies

* Removes dependency on GCT config

* Failed currency statistics redesign

* initial Update chart to use highcharts

* Minor changes to stats

* Creats funding stats to handle the stat calculations. Needs more work

* tracks USD snapshots and BREAKS THINGS FURTHER

* Fixed!

* Adds ratio calculations and such, but its WRONG. do it at totals level dummy

* End of day basic lint

* Remaining lints

* USD totals statistics

* Minor panic fixes

* Printing of funding stats, but its bad

* Properly calculates overall benchmark, moves funding stat output

* Adds some template charge, removes duplicate fields

* New charts!

* Darkcharts. funding protection when disabled

* Now works with usd tracking/funding disabled!

* Attempting to only show working stats based on settings.

* Spruces up the goose/reporting

* Completes report HTML rendering

* lint and test fixes

* funding statistics testing

* slightly more test coverage

* Test coverage

* Initial documentation

* Fixes tests

* Database testing and rendering improvements and breakages

* report and cmd rendering, linting. fix comma output. rm gct cfg

* PR mode 🎉 Path field, config builder support,testing,linting,docs

* minor calculation improvement

* Secret lint that did not show up locally

* Disable USD tracking for example configs

* ShazNitNoScope

* Forgotten errors

* ""

* literally Logarithmically logically renders the date 👀

* Fixes typos, fixes parallel test, fixes chart gui and exporting
2021-11-08 12:10:15 +11:00

285 lines
7.1 KiB
Go

package convert
import (
"strings"
"testing"
"time"
"github.com/shopspring/decimal"
)
func TestFloatFromString(t *testing.T) {
t.Parallel()
testString := "1.41421356237"
expectedOutput := float64(1.41421356237)
actualOutput, err := FloatFromString(testString)
if actualOutput != expectedOutput || err != nil {
t.Errorf("Common FloatFromString. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
var testByte []byte
_, err = FloatFromString(testByte)
if err == nil {
t.Error("Common FloatFromString. Converted non-string.")
}
testString = " something unconvertible "
_, err = FloatFromString(testString)
if err == nil {
t.Error("Common FloatFromString. Converted invalid syntax.")
}
}
func TestIntFromString(t *testing.T) {
t.Parallel()
testString := "1337"
actualOutput, err := IntFromString(testString)
if expectedOutput := 1337; actualOutput != expectedOutput || err != nil {
t.Errorf("Common IntFromString. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
var testByte []byte
_, err = IntFromString(testByte)
if err == nil {
t.Error("Common IntFromString. Converted non-string.")
}
testString = "1.41421356237"
_, err = IntFromString(testString)
if err == nil {
t.Error("Common IntFromString. Converted invalid syntax.")
}
}
func TestInt64FromString(t *testing.T) {
t.Parallel()
testString := "4398046511104"
expectedOutput := int64(1 << 42)
actualOutput, err := Int64FromString(testString)
if actualOutput != expectedOutput || err != nil {
t.Errorf("Common Int64FromString. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
var testByte []byte
_, err = Int64FromString(testByte)
if err == nil {
t.Error("Common Int64FromString. Converted non-string.")
}
testString = "1.41421356237"
_, err = Int64FromString(testString)
if err == nil {
t.Error("Common Int64FromString. Converted invalid syntax.")
}
}
func TestTimeFromUnixTimestampFloat(t *testing.T) {
t.Parallel()
testTimestamp := float64(1414456320000)
expectedOutput := time.Date(2014, time.October, 28, 0, 32, 0, 0, time.UTC)
actualOutput, err := TimeFromUnixTimestampFloat(testTimestamp)
if actualOutput.UTC().String() != expectedOutput.UTC().String() || err != nil {
t.Errorf("Common TimeFromUnixTimestampFloat. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
testString := "Time"
_, err = TimeFromUnixTimestampFloat(testString)
if err == nil {
t.Error("Common TimeFromUnixTimestampFloat. Converted invalid syntax.")
}
}
func TestTimeFromUnixTimestampDecimal(t *testing.T) {
r := TimeFromUnixTimestampDecimal(1590633982.5714)
if r.Year() != 2020 ||
r.Month().String() != "May" ||
r.Day() != 28 {
t.Error("unexpected result")
}
r = TimeFromUnixTimestampDecimal(1560516023.070651)
if r.Year() != 2019 ||
r.Month().String() != "June" ||
r.Day() != 14 {
t.Error("unexpected result")
}
}
func TestUnixTimestampToTime(t *testing.T) {
t.Parallel()
testTime := int64(1489439831)
tm := time.Unix(testTime, 0)
expectedOutput := "2017-03-13 21:17:11 +0000 UTC"
actualResult := UnixTimestampToTime(testTime)
if tm.String() != actualResult.String() {
t.Errorf(
"Expected '%s'. Actual '%s'.", expectedOutput, actualResult)
}
}
func TestUnixTimestampStrToTime(t *testing.T) {
t.Parallel()
testTime := "1489439831"
incorrectTime := "DINGDONG"
expectedOutput := "2017-03-13 21:17:11 +0000 UTC"
actualResult, err := UnixTimestampStrToTime(testTime)
if err != nil {
t.Error(err)
}
if actualResult.UTC().String() != expectedOutput {
t.Errorf(
"Expected '%s'. Actual '%s'.", expectedOutput, actualResult)
}
_, err = UnixTimestampStrToTime(incorrectTime)
if err == nil {
t.Error("should throw an error")
}
}
func TestBoolPtr(t *testing.T) {
y := BoolPtr(true)
if !*y {
t.Fatal("true expected received false")
}
z := BoolPtr(false)
if *z {
t.Fatal("false expected received true")
}
}
func TestFloatToHumanFriendlyString(t *testing.T) {
t.Parallel()
test := FloatToHumanFriendlyString(0, 3, ".", ",")
if strings.Contains(test, ",") {
t.Error("unexpected ','")
}
test = FloatToHumanFriendlyString(100, 3, ".", ",")
if strings.Contains(test, ",") {
t.Error("unexpected ','")
}
test = FloatToHumanFriendlyString(1000, 3, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = FloatToHumanFriendlyString(-1000, 3, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = FloatToHumanFriendlyString(-1000, 10, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = FloatToHumanFriendlyString(1000.1337, 1, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
dec := strings.Split(test, ".")
if len(dec) == 1 {
t.Error("expected decimal place")
}
if dec[1] != "1" {
t.Error("expected decimal place")
}
}
func TestDecimalToHumanFriendlyString(t *testing.T) {
t.Parallel()
test := DecimalToHumanFriendlyString(decimal.Zero, 0, ".", ",")
if strings.Contains(test, ",") {
t.Log(test)
t.Error("unexpected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromInt(100), 0, ".", ",")
if strings.Contains(test, ",") {
t.Log(test)
t.Error("unexpected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromInt(1000), 0, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromFloat(1000.1337), 1, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
dec := strings.Split(test, ".")
if len(dec) == 1 {
t.Error("expected decimal place")
}
if dec[1] != "1" {
t.Error("expected decimal place")
}
test = DecimalToHumanFriendlyString(decimal.NewFromFloat(-1000.1337), 1, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromFloat(-1000.1337), 100000, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromFloat(1000.1), 10, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
dec = strings.Split(test, ".")
if len(dec) == 1 {
t.Error("expected decimal place")
}
if dec[1] != "1" {
t.Error("expected decimal place")
}
}
func TestIntToHumanFriendlyString(t *testing.T) {
t.Parallel()
test := IntToHumanFriendlyString(0, ",")
if strings.Contains(test, ",") {
t.Log(test)
t.Error("unexpected ','")
}
test = IntToHumanFriendlyString(100, ",")
if strings.Contains(test, ",") {
t.Log(test)
t.Error("unexpected ','")
}
test = IntToHumanFriendlyString(1000, ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = IntToHumanFriendlyString(-1000, ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = IntToHumanFriendlyString(1000000, ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
dec := strings.Split(test, ",")
if len(dec) <= 2 {
t.Error("expected two commas place")
}
}
func TestNumberToHumanFriendlyString(t *testing.T) {
resp := numberToHumanFriendlyString("1", 1337, ".", ",", false)
if strings.Contains(resp, ".") {
t.Error("expected no comma")
}
}