4
1
Fork 0

Increase integration test coverage to 30%
continuous-integration/drone/push Build is passing Details

pull/24/head
Ambrose Chua 2019-09-29 23:53:18 +08:00
parent 208f0576e5
commit 0d7a4be43b
Signed by: ambrose
GPG Key ID: BC367D33F140B5C2
5 changed files with 109 additions and 29 deletions

View File

@ -26,6 +26,8 @@ test_integration: test_integration_prepare
test_integration_prepare: test_integration_prepare:
$(GORUN) scripts/testutils.go isrunning || $(DOCKERCOMPOSE) -f $(DOCKERCOMPOSE_INTEGRATION_CONFIG) up -d $(GORUN) scripts/testutils.go isrunning || $(DOCKERCOMPOSE) -f $(DOCKERCOMPOSE_INTEGRATION_CONFIG) up -d
$(GORUN) scripts/testutils.go wait $(GORUN) scripts/testutils.go wait
test_integration_sql_shell:
$(DOCKERCOMPOSE) -f $(DOCKERCOMPOSE_INTEGRATION_CONFIG) exec pg psql -d core
test_integration_cleanup: test_integration_cleanup:
$(DOCKERCOMPOSE) -f $(DOCKERCOMPOSE_INTEGRATION_CONFIG) down $(DOCKERCOMPOSE) -f $(DOCKERCOMPOSE_INTEGRATION_CONFIG) down

View File

@ -11,17 +11,17 @@ import (
func (h *Handler) CreateContact(w http.ResponseWriter, r *http.Request, p httprouter.Params) { func (h *Handler) CreateContact(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// Parse // Parse
userID := r.Context().Value("user").(string) userID := r.Context().Value("user").(string)
contact := PhoneNumber{} contactPhone := PhoneNumber{}
decoder := json.NewDecoder(r.Body) decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&contact) err := decoder.Decode(&contactPhone)
if err != nil { if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return return
} }
// Validate // Validate
phone, err := ParsePhone(contact.PhoneNumber) phone, err := ParsePhone(contactPhone.PhoneNumber)
if err != nil || len(contact.PhoneNumber) < 1 { if err != nil || len(contactPhone.PhoneNumber) < 1 {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return return
} }
@ -30,14 +30,14 @@ func (h *Handler) CreateContact(w http.ResponseWriter, r *http.Request, p httpro
id := "u-" + RandomHex() id := "u-" + RandomHex()
// Create contact if not exists, returning the id regardless // Create contact if not exists, returning the id regardless
var contactId string contact := User{}
err = h.db.QueryRow(` err = h.db.QueryRow(`
INSERT INTO "user" (id, username, bio, profile_pic, first_name, last_name, phone_number) INSERT INTO "user" (id, username, bio, profile_pic, first_name, last_name, phone_number)
VALUES ($1, '', '', '', '', '', $2) VALUES ($1, '', '', '', '', '', $2)
ON CONFLICT(phone_number) ON CONFLICT(phone_number)
DO UPDATE SET phone_number=EXCLUDED.phone_number DO UPDATE SET phone_number=EXCLUDED.phone_number
RETURNING id RETURNING id, username, bio, profile_pic, first_name, last_name, phone_number
`, id, phone).Scan(&contactId) `, id, phone).Scan(&contact.ID, &contact.Username, &contact.Bio, &contact.ProfilePic, &contact.FirstName, &contact.LastName, &contact.PhoneNumber)
if err != nil { if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err) log.Print(err)
@ -47,7 +47,7 @@ func (h *Handler) CreateContact(w http.ResponseWriter, r *http.Request, p httpro
// Insert // Insert
_, err = h.db.Exec(` _, err = h.db.Exec(`
INSERT INTO contact ("user", contact) VALUES ($1, $2) INSERT INTO contact ("user", contact) VALUES ($1, $2)
`, userID, contactId) `, userID, contact.ID)
if err != nil { if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err) log.Print(err)
@ -56,8 +56,8 @@ func (h *Handler) CreateContact(w http.ResponseWriter, r *http.Request, p httpro
// Respond // Respond
w.WriteHeader(200) w.WriteHeader(200)
//w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
//json.NewEncoder(w).Encode(contact) json.NewEncoder(w).Encode(contact)
} }
func (h *Handler) GetContacts(w http.ResponseWriter, r *http.Request, p httprouter.Params) { func (h *Handler) GetContacts(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
@ -82,13 +82,13 @@ func (h *Handler) GetContacts(w http.ResponseWriter, r *http.Request, p httprout
// Scan // Scan
for rows.Next() { for rows.Next() {
var id, username, bio, profilePic, firstName, lastName, phone string contact := User{}
if err := rows.Scan(&id, &username, &bio, &profilePic, &firstName, &lastName, &phone); err != nil { if err := rows.Scan(&contact.ID, &contact.Username, &contact.Bio, &contact.ProfilePic, &contact.FirstName, &contact.LastName, &contact.PhoneNumber); err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Print(err) log.Print(err)
return return
} }
contacts = append(contacts, User{id, &username, bio, profilePic, firstName, lastName, phone}) contacts = append(contacts, contact)
} }
// Respond // Respond

View File

@ -9,7 +9,8 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
// "github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp"
) )
func TestContact(t *testing.T) { func TestContact(t *testing.T) {
@ -21,9 +22,10 @@ func TestContact(t *testing.T) {
users := setupUsers(t, db, r) users := setupUsers(t, db, r)
t.Run("Create", testCreateContact(db, r, users)) t.Run("Create", testCreateContact(db, r, users))
t.Run("Get", testGetContacts(db, r, users))
} }
func setupUsers(t *testing.T, db *sql.DB, router http.Handler) { func setupUsers(t *testing.T, db *sql.DB, router http.Handler) []User {
users := []User{ users := []User{
User{ User{
@ -53,8 +55,8 @@ func setupUsers(t *testing.T, db *sql.DB, router http.Handler) {
router.ServeHTTP(w, r) router.ServeHTTP(w, r)
assertCode(t, w, 200) assertCode(t, w, 200)
got := new(User) got := User{}
json.NewDecoder(w.Body).Decode(got) json.NewDecoder(w.Body).Decode(&got)
resultUsers = append(resultUsers, got) resultUsers = append(resultUsers, got)
} }
@ -63,7 +65,7 @@ func setupUsers(t *testing.T, db *sql.DB, router http.Handler) {
} }
func testCreateContact(db *sql.DB, router http.Handler) func(t *testing.T) { func testCreateContact(db *sql.DB, router http.Handler, users []User) func(t *testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
// Setup // Setup
@ -72,24 +74,98 @@ func testCreateContact(db *sql.DB, router http.Handler) func(t *testing.T) {
FirstName: "ContactOwner", FirstName: "ContactOwner",
LastName: "User", LastName: "User",
} }
b, _ := json.Marshal(mockUser) bs, _ := json.Marshal(mockUser)
ws := httptest.NewRecorder()
rs := httptest.NewRequest("POST", "/user", bytes.NewBuffer(bs))
router.ServeHTTP(ws, rs)
createdUser := new(User)
json.NewDecoder(ws.Body).Decode(createdUser)
// Test // Test
b := []byte(`{"phone_number": "` + users[0].PhoneNumber + `"}`)
w := httptest.NewRecorder() w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/user", bytes.NewBuffer(b)) r := httptest.NewRequest("POST", "/user/contact", bytes.NewBuffer(b))
claim, _ := json.Marshal(&RawClient{UserId: createdUser.ID, ClientId: "test"})
r.Header.Add("X-User-Claim", string(claim))
router.ServeHTTP(w, r) router.ServeHTTP(w, r)
assertCode(t, w, 200) assertCode(t, w, 200)
// Assert // Assert
got, want := new(User), mockUser got, want := User{}, users[0]
wantPhone, _ := ParsePhone(want.PhoneNumber) json.NewDecoder(w.Body).Decode(&got)
json.NewDecoder(w.Body).Decode(got) if diff := cmp.Diff(got, want); len(diff) != 0 {
if got.FirstName != want.FirstName || got.LastName != want.LastName || got.PhoneNumber != wantPhone { t.Error(diff)
t.Error("Wanted a User with same FirstName, LastName, PhoneNumber. Got something else")
} }
assertDB(t, db, `SELECT * FROM "user" WHERE phone_number = '+65 9999 9999' AND first_name = 'Test' AND last_name = 'User 1'`) assertDB(t, db, `SELECT * FROM contact WHERE "user" = $1 AND contact = $2`, createdUser.ID, users[0].ID)
}
}
func testGetContacts(db *sql.DB, router http.Handler, users []User) func(t *testing.T) {
return func(t *testing.T) {
// Setup
mockUser := &User{
PhoneNumber: "+65 9999 1002",
FirstName: "ContactOwner",
LastName: "User",
}
bs, _ := json.Marshal(mockUser)
ws := httptest.NewRecorder()
rs := httptest.NewRequest("POST", "/user", bytes.NewBuffer(bs))
router.ServeHTTP(ws, rs)
createdUser := new(User)
json.NewDecoder(ws.Body).Decode(createdUser)
b := []byte(`{"phone_number": "` + users[0].PhoneNumber + `"}`)
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/user/contact", bytes.NewBuffer(b))
claim, _ := json.Marshal(&RawClient{UserId: createdUser.ID, ClientId: "test"})
r.Header.Add("X-User-Claim", string(claim))
router.ServeHTTP(w, r)
assertCode(t, w, 200)
b = []byte(`{"phone_number": "` + users[1].PhoneNumber + `"}`)
w = httptest.NewRecorder()
r = httptest.NewRequest("POST", "/user/contact", bytes.NewBuffer(b))
r.Header.Add("X-User-Claim", string(claim))
router.ServeHTTP(w, r)
assertCode(t, w, 200)
b = []byte(`{"phone_number": "` + users[2].PhoneNumber + `"}`)
w = httptest.NewRecorder()
r = httptest.NewRequest("POST", "/user/contact", bytes.NewBuffer(b))
r.Header.Add("X-User-Claim", string(claim))
router.ServeHTTP(w, r)
assertCode(t, w, 200)
// Test
w = httptest.NewRecorder()
r = httptest.NewRequest("GET", "/user/contact", nil)
r.Header.Add("X-User-Claim", string(claim))
router.ServeHTTP(w, r)
assertCode(t, w, 200)
// Assert
got, want := []User{}, users
json.NewDecoder(w.Body).Decode(&got)
if diff := cmp.Diff(got, want); len(diff) != 0 {
t.Error(diff)
}
} }
} }

View File

@ -12,10 +12,11 @@ func assertCode(t *testing.T, w *httptest.ResponseRecorder, code int) {
} }
} }
func assertDB(t *testing.T, db *sql.DB, query string) { func assertDB(t *testing.T, db *sql.DB, query string, args ...interface{}) {
rows, err := db.Query(query) rows, err := db.Query(query, args...)
if err != nil { if err != nil {
t.Errorf("Error during query %s: %s", query, err) t.Errorf("Error during query %s: %s", query, err)
return
} }
defer rows.Close() defer rows.Close()
if rows.Next() != true { if rows.Next() != true {

View File

@ -6,10 +6,11 @@ import (
"bytes" "bytes"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"github.com/google/go-cmp/cmp"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"github.com/google/go-cmp/cmp"
) )
func TestUser(t *testing.T) { func TestUser(t *testing.T) {