Skip to content

Commit

Permalink
Add tests for symbols and utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
ackleymi committed Mar 2, 2017
1 parent c1d22dd commit c6efd47
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 69 deletions.
2 changes: 1 addition & 1 deletion fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
secondRegex = regexp.MustCompile(`(\w+):`)
)

const (
var (
// OptionsURL option chains
OptionsURL = "http://www.google.com/finance/option_chain?"
// HistoryURL quote history
Expand Down
1 change: 0 additions & 1 deletion symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ func GetUSEquitySymbols() (symbols []string, err error) {
symbols = append(symbols, r[0])
}
}

return
}
21 changes: 21 additions & 0 deletions symbols_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
package finance

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_GetUSEquitySymbols(t *testing.T) {

s := startTestServer("symbols_fixture.csv")
defer s.Close()
SymbolsURL = s.URL

symbols, err := GetUSEquitySymbols()
assert.Nil(t, err)

// second symbol should be Alcoa.
assert.Equal(t, "AA", symbols[1])
// slice should contain AMD.
assert.Contains(t, symbols, "AMD")
}
25 changes: 15 additions & 10 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ func toInt(value string) int {
}

// toDecimal converts a string to a decimal value.
func toDecimal(value string) decimal.Decimal {
func toDecimal(value string) (d decimal.Decimal) {

value = strings.Replace(value, "%", "", -1)
dec, err := decimal.NewFromString(value)
if err != nil {
return decimal.NewFromFloat(0.0)
}
return dec
d, _ = decimal.NewFromString(value)
return
}

func toEventValue(value string) Value {
Expand All @@ -38,12 +35,16 @@ func toEventValue(value string) Value {
}

// parseDashedDate converts a string to a proper date and sets time to market close.
func parseDashedDate(dString string) (d time.Time, err error) {
func parseDashedDate(s string) (d time.Time, err error) {

d, err = time.Parse("2006-01-02", dString)
if !strings.ContainsAny(s, "0123456789") {
return
}

d, err = time.Parse("2006-01-02", s)
if err != nil {
dString = parseMalformedDate(dString)
d, err = time.Parse("2006-01-02", dString)
s = parseMalformedDate(s)
d, err = time.Parse("2006-01-02", s)
if err != nil {
return time.Time{}, err
}
Expand All @@ -53,6 +54,10 @@ func parseDashedDate(dString string) (d time.Time, err error) {

func parseMalformedDate(s string) string {

if len(s) < 7 {
return s
}

chars := strings.Split(s, "")
chars = chars[1:]
chars = insert(chars, 4, "-")
Expand Down
101 changes: 44 additions & 57 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,79 +10,66 @@ import (

func Test_ToInt(t *testing.T) {

// Given that we have a string of a integer.
intString := "34"
// When we convert it to a proper int,
properInt := toInt(intString)
// Then it should equal its integer value.
assert.Equal(t, 34, properInt)

// Given that we have a string of a non-integer.
notIntString := "-"
// When we convert it to a proper int,
zero := toInt(notIntString)
// Then it be equal to 0.
assert.Equal(t, 0, zero)

// result should be 34.
assert.Equal(t, 34, toInt("34"))
// result should be 0.
assert.Equal(t, 0, toInt("-"))
}

func Test_ToDecimal(t *testing.T) {

// Given that we have a string of a decimal.
decString := "34.4"
// When we convert it to a proper decimal,
properDec := toDecimal(decString)
// Then it should equal its decimal value.
assert.Equal(t, decimal.NewFromFloat(34.4), properDec)

// Given that we have a string of a non-decimal.
notDecString := "-"
// When we convert it to a proper decimal,
zeroDec := toDecimal(notDecString)
// Then it should equal its decimal value.
assert.Equal(t, decimal.NewFromFloat(0.0), zeroDec)

// Given that we have a string of a decimal percent.
percentString := "0.34%"
// When we convert it to a proper decimal,
percent := toDecimal(percentString)
// Then it should equal its decimal value.
assert.Equal(t, decimal.NewFromFloat(0.34), percent)
// result should be a decimal of 34.4.
assert.Equal(t, decimal.NewFromFloat(34.4), toDecimal("34.4"))
// result should be the Decimal zero-value.
assert.Equal(t, decimal.Decimal{}, toDecimal("-"))
// result should be a decimal of 0.34.
assert.Equal(t, decimal.NewFromFloat(0.34), toDecimal("0.34%"))
}

func Test_ToEventValue(t *testing.T) {

// event from split Ratio.
split := toEventValue("1:5")
// split should have a ratio of 1 to 5.
assert.Equal(t, "1:5", split.Ratio)
// split should not have a dividend amt.
assert.Equal(t, decimal.Decimal{}, split.Dividend)

// event from dividend value.
div := toEventValue("3.02")
// dividend should have a dividend amt of 3.02.
assert.Equal(t, decimal.NewFromFloat(3.02), div.Dividend)
// dividend should not have a split ratio.
assert.Equal(t, "", div.Ratio)
}

func Test_ParseDashedDate(t *testing.T) {

// Given that we have a string of a date.
dateString := "2016-04-01"
// When we convert it to a proper date,
properDate, err := parseDashedDate(dateString)
pd, err := parseDashedDate("2016-04-01")
assert.Nil(t, err)

loc, _ := time.LoadLocation("America/New_York")
date := time.Date(2016, 4, 1, 0, 0, 0, 0, loc)
// Then it should equal the date April 1, 2016.
assert.Equal(t, date.Year(), properDate.Year())
assert.Equal(t, date.Month(), properDate.Month())
assert.Equal(t, date.Day(), properDate.Day())

// Given that we have a string of a non-date.
nonDateString := "N/A"
// When we convert it to a proper date,
badDate, err := parseDashedDate(nonDateString)
d := time.Date(2016, 4, 1, 0, 0, 0, 0, loc)

// result should be April 1, 2016.
assert.Equal(t, d.Year(), pd.Year())
assert.Equal(t, d.Month(), pd.Month())
assert.Equal(t, d.Day(), pd.Day())

bd, err := parseDashedDate("N/A")
assert.Nil(t, err)

// Then it should equal the date default.
assert.Equal(t, time.Time{}, badDate)
// result should be the time zero-value.
assert.Equal(t, time.Time{}, bd)

bd, err = parseDashedDate("5434")

// result should be the time zero-value.
assert.Equal(t, time.Time{}, bd)
}

func Test_ParseMalformedDate(t *testing.T) {

// Given we have a conjoined date string.
badString := "020110506"
// When we convert it to a valid date string,
validString := parseMalformedDate(badString)
// Then it should equal a valid date string.
assert.Equal(t, "2011-05-06", validString)

// result should be a valid date string.
assert.Equal(t, "2011-05-06", parseMalformedDate("020110506"))
}

0 comments on commit c6efd47

Please sign in to comment.