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

Create framework to automate creation/teardown of test data

This commit is contained in:
Aaron Lindsay 2017-10-08 20:29:51 -04:00
parent 3c6b5528f9
commit bd52df65cd
4 changed files with 142 additions and 108 deletions

View File

@ -35,6 +35,16 @@ func PutForm(client *http.Client, url string, data url.Values) (*http.Response,
return client.Do(request) return client.Do(request)
} }
func RunWith(t *testing.T, d *TestData, fn TestDataFunc) {
testdata, err := d.Initialize()
if err != nil {
t.Fatal("Failed to initialize test data: %s", err)
}
defer testdata.Teardown()
fn(t, testdata)
}
func RunTests(m *testing.M) int { func RunTests(m *testing.M) int {
tmpdir, err := ioutil.TempDir("./", "handlertest") tmpdir, err := ioutil.TempDir("./", "handlertest")
if err != nil { if err != nil {

View File

@ -97,35 +97,16 @@ func sessionExistsOrError(c *http.Client) error {
} }
func TestCreateSession(t *testing.T) { func TestCreateSession(t *testing.T) {
u, err := createUser(&users[0]) RunWith(t, &data[0], func(t *testing.T, d *TestData) {
if err != nil { if err := sessionExistsOrError(d.clients[0]); err != nil {
t.Fatal(err)
}
u.Password = users[0].Password
client, err := newSession(u)
if err != nil {
t.Fatal(err)
}
defer deleteUser(client, u)
if err := sessionExistsOrError(client); err != nil {
t.Fatal(err) t.Fatal(err)
} }
})
} }
func TestGetSession(t *testing.T) { func TestGetSession(t *testing.T) {
u, err := createUser(&users[0]) RunWith(t, &data[0], func(t *testing.T, d *TestData) {
if err != nil { session, err := getSession(d.clients[0])
t.Fatal(err)
}
u.Password = users[0].Password
client, err := newSession(u)
if err != nil {
t.Fatal(err)
}
defer deleteUser(client, u)
session, err := getSession(client)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -134,11 +115,12 @@ func TestGetSession(t *testing.T) {
t.Error("Session.SessionSecret should not be passed back in JSON") t.Error("Session.SessionSecret should not be passed back in JSON")
} }
if session.UserId != u.UserId { if session.UserId != d.users[0].UserId {
t.Errorf("session's UserId (%d) should equal user's UserID (%d)", session.UserId, u.UserId) t.Errorf("session's UserId (%d) should equal user's UserID (%d)", session.UserId, d.users[0].UserId)
} }
if session.SessionId == 0 { if session.SessionId == 0 {
t.Error("session's SessionId should not be 0") t.Error("session's SessionId should not be 0")
} }
})
} }

View File

@ -2,12 +2,14 @@ package handlers_test
import ( import (
"encoding/json" "encoding/json"
"fmt"
"github.com/aclindsa/moneygo/internal/handlers"
"net/http" "net/http"
"strings" "strings"
"testing"
) )
// Needed because handlers.User doesn't allow Password to be written to JSON // Needed because handlers.User doesn't allow Password to be written to JSON
type User struct { type User struct {
UserId int64 UserId int64
DefaultCurrency int64 // SecurityId of default currency, or ISO4217 code for it if creating new user DefaultCurrency int64 // SecurityId of default currency, or ISO4217 code for it if creating new user
@ -28,7 +30,75 @@ func (u *User) Read(json_str string) error {
return dec.Decode(u) return dec.Decode(u)
} }
var users = []User{ // TestData
type TestData struct {
initialized bool
users []User
clients []*http.Client
accounts []handlers.Account
securities []handlers.Security
transactions []handlers.Transaction
prices []handlers.Price
reports []handlers.Report
}
type TestDataFunc func(*testing.T, *TestData)
func (t *TestData) initUser(user *User, userid int) error {
newuser, err := createUser(user)
if err != nil {
return err
}
t.users = append(t.users, *newuser)
// make a copy of the user so we can set the password for creating the
// session without disturbing the original
userWithPassword := *newuser
userWithPassword.Password = user.Password
client, err := newSession(&userWithPassword)
if err != nil {
return err
}
t.clients = append(t.clients, client)
// TODO initialize everything else owned by this user in the TestData struct
return nil
}
// Initialize makes requests to the server to create all of the objects
// represented in it before returning a copy of the data, with all of the *Id
// fields updated to their actual values
func (t *TestData) Initialize() (*TestData, error) {
var t2 TestData
for userid, user := range t.users {
err := t2.initUser(&user, userid)
if err != nil {
return nil, err
}
}
t2.initialized = true
return &t2, nil
}
func (t *TestData) Teardown() error {
if !t.initialized {
return fmt.Errorf("Cannot teardown uninitialized TestData")
}
for userid, user := range t.users {
err := deleteUser(t.clients[userid], &user)
if err != nil {
return err
}
}
return nil
}
var data = []TestData{
{
users: []User{
User{ User{
DefaultCurrency: 840, // USD DefaultCurrency: 840, // USD
Name: "John Smith", Name: "John Smith",
@ -36,4 +106,6 @@ var users = []User{
Password: "hunter2", Password: "hunter2",
Email: "jsmith@example.com", Email: "jsmith@example.com",
}, },
},
},
} }

View File

@ -146,73 +146,43 @@ func getUser(client *http.Client, userid int64) (*User, error) {
} }
func TestCreateUser(t *testing.T) { func TestCreateUser(t *testing.T) {
u, err := createUser(&users[0]) RunWith(t, &data[0], func(t *testing.T, d *TestData) {
if err != nil { if len(d.users[0].Password) != 0 || len(d.users[0].PasswordHash) != 0 {
t.Fatal(err)
}
if len(u.Password) != 0 || len(u.PasswordHash) != 0 {
t.Error("Never send password, only send password hash when necessary") t.Error("Never send password, only send password hash when necessary")
} }
})
u.Password = users[0].Password
client, err := newSession(u)
if err != nil {
t.Fatalf("Error creating new session, user not deleted (may cause errors in other tests): %s", err)
}
defer deleteUser(client, u)
} }
func TestGetUser(t *testing.T) { func TestGetUser(t *testing.T) {
origu, err := createUser(&users[0]) RunWith(t, &data[0], func(t *testing.T, d *TestData) {
if err != nil { u, err := getUser(d.clients[0], d.users[0].UserId)
t.Fatal(err)
}
origu.Password = users[0].Password
client, err := newSession(origu)
if err != nil {
t.Fatalf("Error creating new session, user not deleted (may cause errors in other tests): %s", err)
}
defer deleteUser(client, origu)
u, err := getUser(client, origu.UserId)
if err != nil { if err != nil {
t.Fatalf("Error fetching user: %s\n", err) t.Fatalf("Error fetching user: %s\n", err)
} }
if u.UserId != origu.UserId { if u.UserId != d.users[0].UserId {
t.Errorf("UserId doesn't match") t.Errorf("UserId doesn't match")
} }
})
} }
func TestUpdateUser(t *testing.T) { func TestUpdateUser(t *testing.T) {
origu, err := createUser(&users[0]) RunWith(t, &data[0], func(t *testing.T, d *TestData) {
if err != nil { user := &d.users[0]
t.Fatal(err) user.Name = "Bob"
} user.Email = "bob@example.com"
origu.Password = users[0].Password
client, err := newSession(origu) u, err := updateUser(d.clients[0], user)
if err != nil {
t.Fatalf("Error creating new session, user not deleted (may cause errors in other tests): %s", err)
}
defer deleteUser(client, origu)
origu.Name = "Bob"
origu.Email = "bob@example.com"
u, err := updateUser(client, origu)
if err != nil { if err != nil {
t.Fatalf("Error updating user: %s\n", err) t.Fatalf("Error updating user: %s\n", err)
} }
if u.UserId != origu.UserId { if u.UserId != user.UserId {
t.Errorf("UserId doesn't match") t.Errorf("UserId doesn't match")
} }
if u.Name != origu.Name { if u.Name != user.Name {
t.Errorf("UserId doesn't match") t.Errorf("UserId doesn't match")
} }
if u.Email != origu.Email { if u.Email != user.Email {
t.Errorf("UserId doesn't match") t.Errorf("UserId doesn't match")
} }
})
} }