/
devstatus_test.go
107 lines (93 loc) · 2.43 KB
/
devstatus_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
//"fmt"
"github.com/coopernurse/gorp"
"github.com/jjhageman/dev-status/db"
"github.com/jjhageman/dev-status/dev"
"github.com/rcrowley/go-tigertonic/mocking"
"log"
"net/http"
"os"
"strconv"
"testing"
)
var dbmap *gorp.DbMap
func newDevOrFatal(t *testing.T, first_name string, last_name string, github_id string, status string) *dev.Dev {
dev, err := dev.NewDev(first_name, last_name, github_id, status)
if err != nil {
t.Fatalf("new dev: %v", err)
}
return dev
}
func initDbMap() *gorp.DbMap {
dbmap = db.InitDb("postgres://jjhageman@localhost:5432/devstatus_test?sslmode=disable")
dbmap.TraceOn("", log.New(os.Stdout, "gorptest: ", log.Lmicroseconds))
dbmap.AddTableWithName(dev.Dev{}, "devs").SetKeys(true, "ID")
err := dbmap.CreateTablesIfNotExists()
checkErr(err, "Create tables failed")
return dbmap
}
func dropAndClose(dbmap *gorp.DbMap) {
dbmap.DropTablesIfExists()
dbmap.Db.Close()
}
func checkErr(err error, msg string) {
if err != nil {
log.Fatalln(msg, err)
}
}
func TestGetUser(t *testing.T) {
dbmap := initDbMap()
defer dropAndClose(dbmap)
dev := newDevOrFatal(t, "Bob", "Jones", "killer_bob", "unavailable")
dev.Save()
code, _, response, err := getUser(
mocking.URL(mux, "GET", "/user/"+strconv.FormatInt(dev.ID, 10)),
mocking.Header(nil),
nil,
)
if err != nil {
t.Fatal(err)
}
if code != http.StatusOK {
t.Fatal(code)
}
if response.FirstName != dev.FirstName {
t.Errorf("expected Bob, got %v", response.FirstName)
t.Fatal(response)
}
if response.LastName != dev.LastName {
t.Errorf("expected %v, got %v", dev.LastName, response.LastName)
t.Fatal(response)
}
if response.GithubID != dev.GithubID {
t.Errorf("expected %v, got %v", dev.GithubID, response.GithubID)
t.Fatal(response)
}
if response.Status != dev.Status {
t.Errorf("expected %v, got %v", dev.Status, response.Status)
t.Fatal(response)
}
}
func TestGetInvalidUser(t *testing.T) {
dbmap := initDbMap()
defer dropAndClose(dbmap)
code, _, response, err := getUser(
mocking.URL(mux, "GET", "/user/99"),
mocking.Header(nil),
nil,
)
log.Println(err)
if err.Error() != "User not found" {
t.Errorf("expected User not found error, got %v", err.Error())
t.Fatal(err)
}
if code != http.StatusNotFound {
t.Errorf("expected StatusNotFound, got %v", code)
t.Fatal(code)
}
if response != nil {
t.Errorf("expected 'Invalid user' status, got %v", response.Status)
t.Fatal(response.Status)
}
}