2017-10-07 06:21:05 -04:00
|
|
|
package handlers_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2017-10-08 21:18:30 -04:00
|
|
|
"encoding/json"
|
2017-10-07 06:21:05 -04:00
|
|
|
"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"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var server *httptest.Server
|
|
|
|
|
|
|
|
func Delete(client *http.Client, url string) (*http.Response, error) {
|
|
|
|
request, err := http.NewRequest(http.MethodDelete, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client.Do(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PutForm(client *http.Client, url string, data url.Values) (*http.Response, error) {
|
|
|
|
request, err := http.NewRequest(http.MethodPut, url, strings.NewReader(data.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
return client.Do(request)
|
|
|
|
}
|
|
|
|
|
2017-10-08 21:18:30 -04:00
|
|
|
type TransactType interface {
|
|
|
|
Read(string) error
|
|
|
|
}
|
|
|
|
|
2017-10-09 06:00:51 -04:00
|
|
|
func create(client *http.Client, input, output TransactType, urlsuffix, key string) error {
|
2017-10-08 21:18:30 -04:00
|
|
|
bytes, err := json.Marshal(input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-09 06:00:51 -04:00
|
|
|
response, err := client.PostForm(server.URL+urlsuffix, url.Values{key: {string(bytes)}})
|
2017-10-08 21:18:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
response.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var e handlers.Error
|
|
|
|
err = (&e).Read(string(body))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if e.ErrorId != 0 || len(e.ErrorString) != 0 {
|
2017-10-09 21:10:59 -04:00
|
|
|
return &e
|
2017-10-08 21:18:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = output.Read(string(body))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-09 06:00:51 -04:00
|
|
|
func read(client *http.Client, output TransactType, urlsuffix, key string) error {
|
|
|
|
response, err := client.Get(server.URL + urlsuffix)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
response.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var e handlers.Error
|
|
|
|
err = (&e).Read(string(body))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if e.ErrorId != 0 || len(e.ErrorString) != 0 {
|
2017-10-09 21:10:59 -04:00
|
|
|
return &e
|
2017-10-09 06:00:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = output.Read(string(body))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func update(client *http.Client, input, output TransactType, urlsuffix, key string) error {
|
|
|
|
bytes, err := json.Marshal(input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
response, err := PutForm(client, server.URL+urlsuffix, url.Values{key: {string(bytes)}})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
response.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var e handlers.Error
|
|
|
|
err = (&e).Read(string(body))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if e.ErrorId != 0 || len(e.ErrorString) != 0 {
|
2017-10-09 21:10:59 -04:00
|
|
|
return &e
|
2017-10-09 06:00:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = output.Read(string(body))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func remove(client *http.Client, urlsuffix, key string) error {
|
|
|
|
response, err := Delete(client, server.URL+urlsuffix)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
response.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var e handlers.Error
|
|
|
|
err = (&e).Read(string(body))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if e.ErrorId != 0 || len(e.ErrorString) != 0 {
|
2017-10-09 21:10:59 -04:00
|
|
|
return &e
|
2017-10-09 06:00:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-08 20:29:51 -04:00
|
|
|
func RunWith(t *testing.T, d *TestData, fn TestDataFunc) {
|
|
|
|
testdata, err := d.Initialize()
|
|
|
|
if err != nil {
|
2017-10-14 20:38:40 -04:00
|
|
|
t.Fatalf("Failed to initialize test data: %s", err)
|
2017-10-08 20:29:51 -04:00
|
|
|
}
|
2017-10-11 05:48:31 -04:00
|
|
|
defer func() {
|
|
|
|
err := testdata.Teardown()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2017-10-08 20:29:51 -04:00
|
|
|
|
|
|
|
fn(t, testdata)
|
|
|
|
}
|
|
|
|
|
2017-10-07 06:21:05 -04:00
|
|
|
func RunTests(m *testing.M) int {
|
2017-11-01 21:09:55 -04:00
|
|
|
dsn := db.GetDSN(config.SQLite, ":memory:")
|
2017-10-24 20:57:55 -04:00
|
|
|
database, err := sql.Open("sqlite3", dsn)
|
2017-10-07 06:21:05 -04:00
|
|
|
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.NewTLSServer(servemux)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
return m.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2017-10-31 21:04:03 -04:00
|
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
2017-10-07 06:21:05 -04:00
|
|
|
os.Exit(RunTests(m))
|
|
|
|
}
|