Skip to content

Commit

Permalink
Split common package more and QA
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasher- committed Jun 7, 2019
1 parent cbd3e7b commit 04c7c48
Show file tree
Hide file tree
Showing 16 changed files with 148 additions and 180 deletions.
3 changes: 2 additions & 1 deletion cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"io/ioutil"
"log"

"github.com/thrasher-/gocryptotrader/common"
Expand Down Expand Up @@ -42,7 +43,7 @@ func main() {
key = string(result)
}

file, err := common.ReadFile(inFile)
file, err := ioutil.ReadFile(inFile)
if err != nil {
log.Fatalf("Unable to read input file %s. Error: %s.", inFile, err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/huobi_auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"log"

"github.com/thrasher-/gocryptotrader/common"
Expand Down Expand Up @@ -59,7 +60,7 @@ func writeFile(file string, data []byte) error {

func main() {
genKeys := false
privKeyData, err := common.ReadFile("privatekey.pem")
privKeyData, err := ioutil.ReadFile("privatekey.pem")
if err != nil {
genKeys = true
}
Expand Down Expand Up @@ -100,7 +101,7 @@ func main() {

} else {
var pubKeyData []byte
pubKeyData, err = common.ReadFile("publickey.pem")
pubKeyData, err = ioutil.ReadFile("publickey.pem")
if err != nil {
log.Fatal(err)
}
Expand Down
50 changes: 4 additions & 46 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const (
)

// GetV4UUID returns a RFC 4122 UUID based on random numbers
func GetV4UUID() uuid.UUID {
func GetV4UUID() (uuid.UUID, error) {
return uuid.NewV4()
}

Expand Down Expand Up @@ -277,7 +277,7 @@ func ExtractPort(host string) int {

// OutputCSV dumps data into a file as comma-separated values
func OutputCSV(filePath string, data [][]string) error {
_, err := ReadFile(filePath)
_, err := ioutil.ReadFile(filePath)
if err != nil {
errTwo := WriteFile(filePath, nil)
if errTwo != nil {
Expand All @@ -289,49 +289,17 @@ func OutputCSV(filePath string, data [][]string) error {
if err != nil {
return err
}
defer file.Close()

writer := csv.NewWriter(file)

err = writer.WriteAll(data)
if err != nil {
return err
}

writer.Flush()
file.Close()
return nil
}

// UnixTimestampToTime returns time.time
func UnixTimestampToTime(timeint64 int64) time.Time {
return time.Unix(timeint64, 0)
}

// UnixTimestampStrToTime returns a time.time and an error
func UnixTimestampStrToTime(timeStr string) (time.Time, error) {
i, err := strconv.ParseInt(timeStr, 10, 64)
if err != nil {
return time.Time{}, err
}

return time.Unix(i, 0), nil
}

// ReadFile reads a file and returns read data as byte array.
func ReadFile(file string) ([]byte, error) {
return ioutil.ReadFile(file)
return writer.WriteAll(data)
}

// WriteFile writes selected data to a file and returns an error
func WriteFile(file string, data []byte) error {
return ioutil.WriteFile(file, data, 0644)
}

// RemoveFile removes a file
func RemoveFile(file string) error {
return os.Remove(file)
}

// GetURIPath returns the path of a URL given a URI
func GetURIPath(uri string) string {
urip, err := url.Parse(uri)
Expand All @@ -353,16 +321,6 @@ func GetExecutablePath() (string, error) {
return filepath.Dir(ex), nil
}

// UnixMillis converts a UnixNano timestamp to milliseconds
func UnixMillis(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
}

// RecvWindow converts a supplied time.Duration to milliseconds
func RecvWindow(d time.Duration) int64 {
return int64(d) / int64(time.Millisecond)
}

// GetDefaultDataDir returns the default data directory
// Windows - C:\Users\%USER%\AppData\Roaming\GoCryptoTrader
// Linux/Unix or OSX - $HOME/.gocryptotrader
Expand Down
115 changes: 7 additions & 108 deletions common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"runtime"
"strings"
"testing"
"time"
)

func TestIsEnabled(t *testing.T) {
Expand All @@ -29,7 +28,6 @@ func TestIsEnabled(t *testing.T) {

func TestIsValidCryptoAddress(t *testing.T) {
t.Parallel()

b, err := IsValidCryptoAddress("1Mz7153HMuxXTuR2R1t78mGSdzaAtNbBWX", "bTC")
if err != nil && !b {
t.Errorf("Test Failed - Common IsValidCryptoAddress error: %s", err)
Expand Down Expand Up @@ -229,6 +227,7 @@ func TestSendHTTPRequest(t *testing.T) {
}

func TestSendHTTPGetRequest(t *testing.T) {
t.Parallel()
type test struct {
Address string `json:"address"`
ETH struct {
Expand Down Expand Up @@ -265,6 +264,7 @@ func TestSendHTTPGetRequest(t *testing.T) {
}

func TestJSONEncode(t *testing.T) {
t.Parallel()
type test struct {
Status int `json:"status"`
Data []struct {
Expand Down Expand Up @@ -320,6 +320,7 @@ func TestJSONDecode(t *testing.T) {
}

func TestEncodeURLValues(t *testing.T) {
t.Parallel()
urlstring := "https://www.test.com"
expectedOutput := `https://www.test.com?env=TEST%2FDATABASE&format=json`
values := url.Values{}
Expand Down Expand Up @@ -385,84 +386,6 @@ func TestOutputCSV(t *testing.T) {
}
}

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(
"Test failed. 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(
"Test failed. Expected '%s'. Actual '%s'.", expectedOutput, actualResult)
}
actualResult, err = UnixTimestampStrToTime(incorrectTime)
if err == nil {
t.Error("Test failed. Common UnixTimestampStrToTime error")
}
}

func TestReadFile(t *testing.T) {
pathCorrect := "../testdata/dump"
pathIncorrect := "testdata/dump"

_, err := ReadFile(pathCorrect)
if err != nil {
t.Errorf("Test failed - Common ReadFile error: %s", err)
}
_, err = ReadFile(pathIncorrect)
if err == nil {
t.Errorf("Test failed - Common ReadFile error")
}
}

func TestWriteFile(t *testing.T) {
path := "../testdata/writefiletest"
err := WriteFile(path, nil)
if err != nil {
t.Errorf("Test failed. Common WriteFile error: %s", err)
}
_, err = ReadFile(path)
if err != nil {
t.Errorf("Test failed. Common WriteFile error: %s", err)
}

err = WriteFile("", nil)
if err == nil {
t.Error("Test failed. Common WriteFile allowed bad path")
}
}

func TestRemoveFile(t *testing.T) {
TestWriteFile(t)
path := "../testdata/writefiletest"
err := RemoveFile(path)
if err != nil {
t.Errorf("Test failed. Common RemoveFile error: %s", err)
}

TestOutputCSV(t)
path = "../testdata/dump"
err = RemoveFile(path)
if err != nil {
t.Errorf("Test failed. Common RemoveFile error: %s", err)
}
}

func TestGetURIPath(t *testing.T) {
t.Parallel()
// mapping of input vs expected result
Expand All @@ -488,30 +411,6 @@ func TestGetExecutablePath(t *testing.T) {
}
}

func TestUnixMillis(t *testing.T) {
t.Parallel()
testTime := time.Date(2014, time.October, 28, 0, 32, 0, 0, time.UTC)
expectedOutput := int64(1414456320000)

actualOutput := UnixMillis(testTime)
if actualOutput != expectedOutput {
t.Errorf("Test failed. Common UnixMillis. Expected '%d'. Actual '%d'.",
expectedOutput, actualOutput)
}
}

func TestRecvWindow(t *testing.T) {
t.Parallel()
testTime := time.Duration(24760000)
expectedOutput := int64(24)

actualOutput := RecvWindow(testTime)
if actualOutput != expectedOutput {
t.Errorf("Test failed. Common RecvWindow. Expected '%d'. Actual '%d'",
expectedOutput, actualOutput)
}
}

func TestGetDefaultDataDir(t *testing.T) {
switch runtime.GOOS {
case "windows":
Expand Down Expand Up @@ -625,9 +524,9 @@ func TestChangePerm(t *testing.T) {
if err != nil {
t.Fatalf("os.Stat failed. Err: %v", err)
}
err = RemoveFile(testDir)
err = os.Remove(testDir)
if err != nil {
t.Fatalf("RemoveFile failed. Err: %v", err)
t.Fatalf("os.Remove failed. Err: %v", err)
}
default:
err := ChangePerm("")
Expand All @@ -650,9 +549,9 @@ func TestChangePerm(t *testing.T) {
if a.Mode().Perm() != 0770 {
t.Fatalf("expected file permissions differ. expecting 0770 got %#o", a.Mode().Perm())
}
err = RemoveFile(testDir)
err = os.Remove(testDir)
if err != nil {
t.Fatalf("RemoveFile failed. Err: %v", err)
t.Fatalf("os.Remove failed. Err: %v", err)
}
}
}
24 changes: 24 additions & 0 deletions common/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,27 @@ func TimeFromUnixTimestampFloat(raw interface{}) (time.Time, error) {
}
return time.Unix(0, int64(ts)*int64(time.Millisecond)), nil
}

// UnixTimestampToTime returns time.time
func UnixTimestampToTime(timeint64 int64) time.Time {
return time.Unix(timeint64, 0)
}

// UnixTimestampStrToTime returns a time.time and an error
func UnixTimestampStrToTime(timeStr string) (time.Time, error) {
i, err := strconv.ParseInt(timeStr, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(i, 0), nil
}

// UnixMillis converts a UnixNano timestamp to milliseconds
func UnixMillis(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
}

// RecvWindow converts a supplied time.Duration to milliseconds
func RecvWindow(d time.Duration) int64 {
return int64(d) / int64(time.Millisecond)
}
55 changes: 55 additions & 0 deletions common/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,58 @@ func TestTimeFromUnixTimestampFloat(t *testing.T) {
t.Error("Test failed. Common TimeFromUnixTimestampFloat. Converted invalid syntax.")
}
}

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(
"Test failed. 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(
"Test failed. Expected '%s'. Actual '%s'.", expectedOutput, actualResult)
}
actualResult, err = UnixTimestampStrToTime(incorrectTime)
if err == nil {
t.Error("Test failed. Common UnixTimestampStrToTime error")
}
}

func TestUnixMillis(t *testing.T) {
t.Parallel()
testTime := time.Date(2014, time.October, 28, 0, 32, 0, 0, time.UTC)
expectedOutput := int64(1414456320000)

actualOutput := UnixMillis(testTime)
if actualOutput != expectedOutput {
t.Errorf("Test failed. Common UnixMillis. Expected '%d'. Actual '%d'.",
expectedOutput, actualOutput)
}
}

func TestRecvWindow(t *testing.T) {
t.Parallel()
testTime := time.Duration(24760000)
expectedOutput := int64(24)

actualOutput := RecvWindow(testTime)
if actualOutput != expectedOutput {
t.Errorf("Test failed. Common RecvWindow. Expected '%d'. Actual '%d'",
expectedOutput, actualOutput)
}
}
Loading

0 comments on commit 04c7c48

Please sign in to comment.