1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-12-26 23:42:29 -05:00

Merge pull request #22 from aclindsa/first_test

First test
This commit is contained in:
Aaron Lindsay 2017-10-05 08:22:16 -04:00 committed by GitHub
commit e239d62034
5 changed files with 120 additions and 7 deletions

18
.travis.yml Normal file
View File

@ -0,0 +1,18 @@
language: go
os:
- linux
go:
- 1.9.x
- master
script:
- go get golang.org/x/tools/cmd/cover
- go get github.com/mattn/goveralls
- go install github.com/mattn/goveralls
- go get -d github.com/aclindsa/moneygo
- touch $GOPATH/src/github.com/aclindsa/moneygo/internal/handlers/cusip_list.csv
- go generate -v github.com/aclindsa/moneygo/internal/handlers
- go test -v -covermode=count -coverprofile=coverage.out github.com/aclindsa/moneygo/internal/handlers
- $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN

View File

@ -11,19 +11,19 @@ import (
"gopkg.in/gorp.v1" "gopkg.in/gorp.v1"
) )
func GetDbMap(db *sql.DB, cfg *config.Config) (*gorp.DbMap, error) { func GetDbMap(db *sql.DB, dbtype config.DbType) (*gorp.DbMap, error) {
var dialect gorp.Dialect var dialect gorp.Dialect
if cfg.MoneyGo.DBType == config.SQLite { if dbtype == config.SQLite {
dialect = gorp.SqliteDialect{} dialect = gorp.SqliteDialect{}
} else if cfg.MoneyGo.DBType == config.MySQL { } else if dbtype == config.MySQL {
dialect = gorp.MySQLDialect{ dialect = gorp.MySQLDialect{
Engine: "InnoDB", Engine: "InnoDB",
Encoding: "UTF8", Encoding: "UTF8",
} }
} else if cfg.MoneyGo.DBType == config.Postgres { } else if dbtype == config.Postgres {
dialect = gorp.PostgresDialect{} dialect = gorp.PostgresDialect{}
} else { } else {
return nil, fmt.Errorf("Don't know gorp dialect to go with '%s' DB type", cfg.MoneyGo.DBType.String()) return nil, fmt.Errorf("Don't know gorp dialect to go with '%s' DB type", dbtype.String())
} }
dbmap := &gorp.DbMap{Db: db, Dialect: dialect} dbmap := &gorp.DbMap{Db: db, Dialect: dialect}

View File

@ -56,6 +56,11 @@ func (s *Security) Write(w http.ResponseWriter) error {
return enc.Encode(s) return enc.Encode(s)
} }
func (sl *SecurityList) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(sl)
}
func (sl *SecurityList) Write(w http.ResponseWriter) error { func (sl *SecurityList) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w) enc := json.NewEncoder(w)
return enc.Encode(sl) return enc.Encode(sl)
@ -415,7 +420,16 @@ func SecurityTemplateHandler(w http.ResponseWriter, r *http.Request) {
var limit int64 = -1 var limit int64 = -1
search := query.Get("search") search := query.Get("search")
_type := GetSecurityType(query.Get("type"))
var _type int64 = 0
typestring := query.Get("type")
if len(typestring) > 0 {
_type = GetSecurityType(typestring)
if _type == 0 {
WriteError(w, 3 /*Invalid Request*/)
return
}
}
limitstring := query.Get("limit") limitstring := query.Get("limit")
if limitstring != "" { if limitstring != "" {

View File

@ -0,0 +1,81 @@
package handlers_test
import (
"database/sql"
"github.com/aclindsa/moneygo/internal/config"
"github.com/aclindsa/moneygo/internal/db"
"github.com/aclindsa/moneygo/internal/handlers"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"path"
"testing"
)
var server *httptest.Server
func RunTests(m *testing.M) int {
tmpdir, err := ioutil.TempDir("./", "handlertest")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpdir)
dbpath := path.Join(tmpdir, "moneygo.sqlite")
database, err := sql.Open("sqlite3", "file:"+dbpath+"?cache=shared&mode=rwc")
if err != nil {
log.Fatal(err)
}
defer database.Close()
dbmap, err := db.GetDbMap(database, config.SQLite)
if err != nil {
log.Fatal(err)
}
servemux := handlers.GetHandler(dbmap)
server = httptest.NewServer(servemux)
defer server.Close()
return m.Run()
}
func TestMain(m *testing.M) {
os.Exit(RunTests(m))
}
func TestSecurityTemplates(t *testing.T) {
var sl handlers.SecurityList
response, err := http.Get(server.URL + "/securitytemplate/?search=USD&type=currency")
if err != nil {
t.Error(err)
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
t.Error(err)
}
err = (&sl).Read(string(body))
if err != nil {
t.Error(err)
}
num_usd := 0
for _, s := range *sl.Securities {
if s.Type != handlers.Currency {
t.Fatalf("Requested Currency-only security templates, received a non-Currency template for %s", s.Name)
}
if s.Name == "USD" && s.AlternateId == "840" {
num_usd++
}
}
if num_usd != 1 {
t.Fatalf("Expected one USD security template, found %d\n", num_usd)
}
}

View File

@ -72,7 +72,7 @@ func main() {
} }
defer database.Close() defer database.Close()
dbmap, err := db.GetDbMap(database, cfg) dbmap, err := db.GetDbMap(database, cfg.MoneyGo.DBType)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }