Split common package more and QA

This commit is contained in:
Adrian Gallagher
2019-06-07 20:52:44 +10:00
parent cbd3e7bacd
commit 04c7c4895f
16 changed files with 148 additions and 180 deletions

View File

@@ -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()
}
@@ -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 {
@@ -289,37 +289,10 @@ 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
@@ -327,11 +300,6 @@ 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)
@@ -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

View File

@@ -9,7 +9,6 @@ import (
"runtime"
"strings"
"testing"
"time"
)
func TestIsEnabled(t *testing.T) {
@@ -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)
@@ -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 {
@@ -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 {
@@ -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{}
@@ -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
@@ -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":
@@ -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("")
@@ -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)
}
}
}

View File

@@ -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)
}

View File

@@ -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)
}
}