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

testing: Add GET, DELETE, PUT helper functions too

This commit is contained in:
Aaron Lindsay 2017-10-09 06:00:51 -04:00
parent 2decf765ac
commit 32ac18647b
3 changed files with 108 additions and 107 deletions

View File

@ -41,12 +41,12 @@ type TransactType interface {
Read(string) error Read(string) error
} }
func create(c *http.Client, input TransactType, output TransactType, urlsuffix, key string) error { func create(client *http.Client, input, output TransactType, urlsuffix, key string) error {
bytes, err := json.Marshal(input) bytes, err := json.Marshal(input)
if err != nil { if err != nil {
return err return err
} }
response, err := c.PostForm(server.URL+urlsuffix, url.Values{key: {string(bytes)}}) response, err := client.PostForm(server.URL+urlsuffix, url.Values{key: {string(bytes)}})
if err != nil { if err != nil {
return err return err
} }
@ -63,7 +63,7 @@ func create(c *http.Client, input TransactType, output TransactType, urlsuffix,
return err return err
} }
if e.ErrorId != 0 || len(e.ErrorString) != 0 { if e.ErrorId != 0 || len(e.ErrorString) != 0 {
return fmt.Errorf("Error when creating %s: %+v", key, e) return fmt.Errorf("Error when creating %s: %+v", urlsuffix, e)
} }
err = output.Read(string(body)) err = output.Read(string(body))
@ -74,6 +74,92 @@ func create(c *http.Client, input TransactType, output TransactType, urlsuffix,
return nil return nil
} }
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 {
return fmt.Errorf("Error when updating %s: %+v", urlsuffix, e)
}
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 {
return fmt.Errorf("Error when updating %s: %+v", urlsuffix, e)
}
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 {
return fmt.Errorf("Error when removing %s: %+v", urlsuffix, e)
}
return nil
}
func RunWith(t *testing.T, d *TestData, fn TestDataFunc) { func RunWith(t *testing.T, d *TestData, fn TestDataFunc) {
testdata, err := d.Initialize() testdata, err := d.Initialize()
if err != nil { if err != nil {

View File

@ -3,7 +3,6 @@ package handlers_test
import ( import (
"fmt" "fmt"
"github.com/aclindsa/moneygo/internal/handlers" "github.com/aclindsa/moneygo/internal/handlers"
"io/ioutil"
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
"net/url" "net/url"
@ -28,22 +27,7 @@ func newSession(user *User) (*http.Client, error) {
func getSession(client *http.Client) (*handlers.Session, error) { func getSession(client *http.Client) (*handlers.Session, error) {
var s handlers.Session var s handlers.Session
response, err := client.Get(server.URL + "/session/") read(client, &s, "/session/", "session")
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return nil, err
}
err = (&s).Read(string(body))
if err != nil {
return nil, err
}
return &s, nil return &s, nil
} }

View File

@ -1,12 +1,7 @@
package handlers_test package handlers_test
import ( import (
"encoding/json"
"fmt"
"github.com/aclindsa/moneygo/internal/handlers"
"io/ioutil"
"net/http" "net/http"
"net/url"
"strconv" "strconv"
"testing" "testing"
) )
@ -17,102 +12,32 @@ func createUser(user *User) (*User, error) {
return &u, err return &u, err
} }
func updateUser(client *http.Client, user *User) (*User, error) { func getUser(client *http.Client, userid int64) (*User, error) {
bytes, err := json.Marshal(user)
if err != nil {
return nil, err
}
response, err := PutForm(client, server.URL+"/user/"+strconv.FormatInt(user.UserId, 10), url.Values{"user": {string(bytes)}})
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return nil, err
}
var e handlers.Error
err = (&e).Read(string(body))
if err != nil {
return nil, err
}
if e.ErrorId != 0 || len(e.ErrorString) != 0 {
return nil, fmt.Errorf("Error when updating user %+v", e)
}
var u User var u User
err = (&u).Read(string(body)) err := read(client, &u, "/user/"+strconv.FormatInt(userid, 10), "user")
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &u, nil
if u.UserId == 0 || len(u.Username) == 0 {
return nil, fmt.Errorf("Unable to update user: %+v", user)
} }
func updateUser(client *http.Client, user *User) (*User, error) {
var u User
err := update(client, user, &u, "/user/"+strconv.FormatInt(user.UserId, 10), "user")
if err != nil {
return nil, err
}
return &u, nil return &u, nil
} }
func deleteUser(client *http.Client, u *User) error { func deleteUser(client *http.Client, u *User) error {
response, err := Delete(client, server.URL+"/user/"+strconv.FormatInt(u.UserId, 10)) err := remove(client, "/user/"+strconv.FormatInt(u.UserId, 10), "user")
if err != nil { if err != nil {
return err 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 {
return fmt.Errorf("Error when deleting user %+v", e)
}
return nil return nil
} }
func getUser(client *http.Client, userid int64) (*User, error) {
response, err := client.Get(server.URL + "/user/" + strconv.FormatInt(userid, 10))
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return nil, err
}
var e handlers.Error
err = (&e).Read(string(body))
if err != nil {
return nil, err
}
if e.ErrorId != 0 || len(e.ErrorString) != 0 {
return nil, fmt.Errorf("Error when get user %+v", e)
}
var u User
err = (&u).Read(string(body))
if err != nil {
return nil, err
}
if u.UserId == 0 || len(u.Username) == 0 {
return nil, fmt.Errorf("Unable to get userid: %d", userid)
}
return &u, nil
}
func TestCreateUser(t *testing.T) { func TestCreateUser(t *testing.T) {
RunWith(t, &data[0], func(t *testing.T, d *TestData) { RunWith(t, &data[0], func(t *testing.T, d *TestData) {
if d.users[0].UserId == 0 || len(d.users[0].Username) == 0 { if d.users[0].UserId == 0 || len(d.users[0].Username) == 0 {
@ -134,6 +59,9 @@ func TestGetUser(t *testing.T) {
if u.UserId != d.users[0].UserId { if u.UserId != d.users[0].UserId {
t.Errorf("UserId doesn't match") t.Errorf("UserId doesn't match")
} }
if len(u.Username) == 0 {
t.Fatalf("Empty username for: %d", d.users[0].UserId)
}
}) })
} }
@ -150,11 +78,14 @@ func TestUpdateUser(t *testing.T) {
if u.UserId != user.UserId { if u.UserId != user.UserId {
t.Errorf("UserId doesn't match") t.Errorf("UserId doesn't match")
} }
if u.Username != u.Username {
t.Errorf("Username doesn't match")
}
if u.Name != user.Name { if u.Name != user.Name {
t.Errorf("UserId doesn't match") t.Errorf("Name doesn't match")
} }
if u.Email != user.Email { if u.Email != user.Email {
t.Errorf("UserId doesn't match") t.Errorf("Email doesn't match")
} }
}) })
} }