mirror of
https://github.com/aclindsa/moneygo.git
synced 2025-06-13 13:39:23 -04:00
Add first test
This tests only querying security templates, and not very exhaustively, but it's a test!
This commit is contained in:
@ -56,6 +56,11 @@ func (s *Security) Write(w http.ResponseWriter) error {
|
||||
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 {
|
||||
enc := json.NewEncoder(w)
|
||||
return enc.Encode(sl)
|
||||
@ -415,7 +420,16 @@ func SecurityTemplateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var limit int64 = -1
|
||||
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")
|
||||
if limitstring != "" {
|
||||
|
81
internal/handlers/security_template_test.go
Normal file
81
internal/handlers/security_template_test.go
Normal 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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user