Initial integration tests scaffold
parent
af01b63554
commit
1208c705f8
1
go.mod
1
go.mod
|
@ -2,6 +2,7 @@ module backend/core
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/golang/protobuf v1.1.0 // indirect
|
github.com/golang/protobuf v1.1.0 // indirect
|
||||||
|
github.com/google/go-cmp v0.3.1
|
||||||
github.com/joho/godotenv v1.3.0
|
github.com/joho/godotenv v1.3.0
|
||||||
github.com/julienschmidt/httprouter v0.0.0-20180715161854-348b672cd90d
|
github.com/julienschmidt/httprouter v0.0.0-20180715161854-348b672cd90d
|
||||||
github.com/lib/pq v0.0.0-20180523175426-90697d60dd84
|
github.com/lib/pq v0.0.0-20180523175426-90697d60dd84
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,5 +1,7 @@
|
||||||
github.com/golang/protobuf v1.1.0 h1:0iH4Ffd/meGoXqF2lSAhZHt8X+cPgkfn/cb6Cce5Vpc=
|
github.com/golang/protobuf v1.1.0 h1:0iH4Ffd/meGoXqF2lSAhZHt8X+cPgkfn/cb6Cce5Vpc=
|
||||||
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
|
||||||
|
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||||
github.com/julienschmidt/httprouter v0.0.0-20180715161854-348b672cd90d h1:of6+TpypLAaiv4JxgH5aplBZnt0b65B4v4c8q5oy+Sk=
|
github.com/julienschmidt/httprouter v0.0.0-20180715161854-348b672cd90d h1:of6+TpypLAaiv4JxgH5aplBZnt0b65B4v4c8q5oy+Sk=
|
||||||
|
|
26
main.go
26
main.go
|
@ -19,7 +19,20 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error loading .env file")
|
log.Fatal("Error loading .env file")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Database
|
||||||
|
db := connect()
|
||||||
|
// Handler
|
||||||
|
h := NewHandler(db)
|
||||||
|
// Routes
|
||||||
|
router := NewRouter(h)
|
||||||
|
|
||||||
listen = os.Getenv("LISTEN")
|
listen = os.Getenv("LISTEN")
|
||||||
|
log.Printf("starting server on %s", listen)
|
||||||
|
log.Fatal(http.ListenAndServe(listen, router))
|
||||||
|
}
|
||||||
|
|
||||||
|
func connect() *sql.DB {
|
||||||
postgres = os.Getenv("POSTGRES")
|
postgres = os.Getenv("POSTGRES")
|
||||||
|
|
||||||
// Open postgres
|
// Open postgres
|
||||||
|
@ -28,13 +41,10 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
defer db.Close()
|
err = db.Ping()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
// Handler
|
return db
|
||||||
h := NewHandler(db)
|
|
||||||
// Routes
|
|
||||||
router := NewRouter(h)
|
|
||||||
|
|
||||||
log.Printf("starting server on %s", listen)
|
|
||||||
log.Fatal(http.ListenAndServe(listen, router))
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func assertCode(t *testing.T, w *httptest.ResponseRecorder, code int) {
|
||||||
|
if got, want := w.Code, code; want != got {
|
||||||
|
t.Errorf("Want response code %d, got %d", want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertDB(t *testing.T, db *sql.DB, query string) {
|
||||||
|
rows, err := db.Query(query)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error during query %s: %s", query, err)
|
||||||
|
}
|
||||||
|
if rows.Next() != true {
|
||||||
|
t.Errorf("Want one result, found none for query %s", query)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
// +build integration
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
//"github.com/google/go-cmp/cmp"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUser(t *testing.T) {
|
||||||
|
db := connect()
|
||||||
|
defer db.Close()
|
||||||
|
h := NewHandler(db)
|
||||||
|
r := NewRouter(h)
|
||||||
|
|
||||||
|
t.Run("Create", testCreateUser(db, r))
|
||||||
|
}
|
||||||
|
|
||||||
|
func testCreateUser(db *sql.DB, router http.Handler) func(t *testing.T) {
|
||||||
|
return func(t *testing.T) {
|
||||||
|
mockUser := &User{
|
||||||
|
PhoneNumber: "+65 99999999",
|
||||||
|
}
|
||||||
|
b, _ := json.Marshal(mockUser)
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
r := httptest.NewRequest("POST", "/", bytes.NewBuffer(b))
|
||||||
|
|
||||||
|
router.ServeHTTP(w, r)
|
||||||
|
assertCode(t, w, 200)
|
||||||
|
|
||||||
|
assertDB(t, db, `SELECT * FROM "user" WHERE phone_number = "+65 97663827`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
got, want := new(User), created
|
||||||
|
json.NewDecoder(w.Body).Decode(got)
|
||||||
|
if diff := cmp.Diff(got, want); len(diff) != 0 {
|
||||||
|
t.Error(diff)
|
||||||
|
}
|
||||||
|
*/
|
Loading…
Reference in New Issue