-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from justinfarrelldev/lobby-tests
Created more tests for lobbies
- Loading branch information
Showing
7 changed files
with
363 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package lobby | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
func TestCreateLobby_Success(t *testing.T) { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
lobby := Lobby{ | ||
Name: "Test Lobby", | ||
OwnerName: "Owner", | ||
IsClosed: false, | ||
IsMuted: false, | ||
IsPublic: true, | ||
} | ||
|
||
mock.ExpectQuery("INSERT INTO lobby \\(name, owner_name, is_closed, is_muted, is_public\\) VALUES \\(\\$1, \\$2, \\$3, \\$4, \\$5\\)"). | ||
WithArgs(lobby.Name, lobby.OwnerName, lobby.IsClosed, lobby.IsMuted, lobby.IsPublic). | ||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1)) | ||
|
||
req, err := http.NewRequest("POST", "/lobby/create_lobby", strings.NewReader(`{"lobby": {"name": "Test Lobby", "owner_name": "Owner", "is_closed": false, "is_muted": false, "is_public": true}, "password": "password123"}`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
sqlxDB := sqlx.NewDb(db, "sqlmock") | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
err := CreateLobby(w, r, sqlxDB) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
}) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusCreated { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusCreated) | ||
} | ||
} | ||
|
||
func TestCreateLobby_InvalidMethod(t *testing.T) { | ||
db, _, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
req, err := http.NewRequest("GET", "/lobby/create_lobby", strings.NewReader(`{"lobby": {"name": "Test Lobby", "owner_name": "Owner", "is_closed": false, "is_muted": false, "is_public": true}, "password": "password123"}`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
sqlxDB := sqlx.NewDb(db, "sqlmock") | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
err := CreateLobby(w, r, sqlxDB) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
}) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusInternalServerError { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusInternalServerError) | ||
} | ||
|
||
expectedError := "invalid request; request must be a POST request" | ||
if strings.TrimSpace(rr.Body.String()) != expectedError { | ||
t.Errorf("handler returned unexpected body: got %v want %v", strings.TrimSpace(rr.Body.String()), expectedError) | ||
} | ||
} | ||
|
||
func TestCreateLobby_DecodeError(t *testing.T) { | ||
db, _, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
req, err := http.NewRequest("POST", "/lobby/create_lobby", strings.NewReader(`{"lobby": {"name": "Test Lobby", "owner_name": "Owner", "is_closed": false, "is_muted": false, "is_public": true}, "password": 123}`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
sqlxDB := sqlx.NewDb(db, "sqlmock") | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
err := CreateLobby(w, r, sqlxDB) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
}) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusInternalServerError { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusInternalServerError) | ||
} | ||
|
||
expectedError := "an error occurred while decoding the request body:json: cannot unmarshal number into Go struct field CreateLobbyArgs.password of type string" | ||
if strings.TrimSpace(rr.Body.String()) != expectedError { | ||
t.Errorf("handler returned unexpected body: got %v want %v", strings.TrimSpace(rr.Body.String()), expectedError) | ||
} | ||
} | ||
|
||
func TestCreateLobby_PasswordTooShort(t *testing.T) { | ||
db, _, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
req, err := http.NewRequest("POST", "/lobby/create_lobby", strings.NewReader(`{"lobby": {"name": "Test Lobby", "owner_name": "Owner", "is_closed": false, "is_muted": false, "is_public": true}, "password": "123"}`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
sqlxDB := sqlx.NewDb(db, "sqlmock") | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
err := CreateLobby(w, r, sqlxDB) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
}) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusBadRequest { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest) | ||
} | ||
|
||
expectedError := ERROR_PASSWORD_TOO_SHORT | ||
if strings.TrimSpace(rr.Body.String()) != expectedError { | ||
t.Errorf("handler returned unexpected body: got %v want %v", strings.TrimSpace(rr.Body.String()), expectedError) | ||
} | ||
} | ||
|
||
func TestCreateLobby_PasswordRequired(t *testing.T) { | ||
db, _, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
req, err := http.NewRequest("POST", "/lobby/create_lobby", strings.NewReader(`{"lobby": {"name": "Test Lobby", "owner_name": "Owner", "is_closed": false, "is_muted": false, "is_public": true}}`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
sqlxDB := sqlx.NewDb(db, "sqlmock") | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
err := CreateLobby(w, r, sqlxDB) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
} | ||
}) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusBadRequest { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusBadRequest) | ||
} | ||
|
||
expectedError := ERROR_PASSWORD_REQUIRED_BUT_NO_PASSWORD | ||
if strings.TrimSpace(rr.Body.String()) != expectedError { | ||
t.Errorf("handler returned unexpected body: got %v want %v", strings.TrimSpace(rr.Body.String()), expectedError) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package lobby | ||
|
||
import ( | ||
"database/sql" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
func TestGetLobby_Success(t *testing.T) { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
lobbyID := int64(1) | ||
expectedLobby := Lobby{ | ||
ID: lobbyID, | ||
Name: "Test Lobby", | ||
OwnerName: "Owner", | ||
IsClosed: false, | ||
IsMuted: false, | ||
IsPublic: true, | ||
} | ||
|
||
mock.ExpectQuery("SELECT id, name, owner_name, is_closed, is_muted, is_public FROM lobby WHERE id = \\$1"). | ||
WithArgs(lobbyID). | ||
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "owner_name", "is_closed", "is_muted", "is_public"}). | ||
AddRow(expectedLobby.ID, expectedLobby.Name, expectedLobby.OwnerName, expectedLobby.IsClosed, expectedLobby.IsMuted, expectedLobby.IsPublic)) | ||
|
||
req, err := http.NewRequest("GET", "/lobby/get_lobby", strings.NewReader(`{"lobby_id": 1}`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
sqlxDB := sqlx.NewDb(db, "sqlmock") | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
err := GetLobby(w, r, sqlxDB) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
}) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusOK { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) | ||
} | ||
|
||
expectedResponse := `{"id":1,"name":"Test Lobby","owner_name":"Owner","is_closed":false,"is_muted":false,"is_public":true}` | ||
if strings.TrimSpace(rr.Body.String()) != expectedResponse { | ||
t.Errorf("handler returned unexpected body: got %v want %v", strings.TrimSpace(rr.Body.String()), expectedResponse) | ||
} | ||
} | ||
|
||
func TestGetLobby_NotFound(t *testing.T) { | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
lobbyID := int8(1) | ||
|
||
mock.ExpectQuery("SELECT id, name, owner_name, is_closed, is_muted, is_public FROM lobby WHERE id = \\$1"). | ||
WithArgs(lobbyID). | ||
WillReturnError(sql.ErrNoRows) | ||
|
||
req, err := http.NewRequest("GET", "/lobby/get_lobby", strings.NewReader(`{"lobby_id": 1}`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
sqlxDB := sqlx.NewDb(db, "sqlmock") | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
err := GetLobby(w, r, sqlxDB) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
}) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusInternalServerError { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusInternalServerError) | ||
} | ||
|
||
expectedError := "no lobby exists with the ID 1" | ||
if strings.TrimSpace(rr.Body.String()) != expectedError { | ||
t.Errorf("handler returned unexpected body: got %v want %v", strings.TrimSpace(rr.Body.String()), expectedError) | ||
} | ||
} | ||
|
||
func TestGetLobby_InvalidMethod(t *testing.T) { | ||
db, _, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
req, err := http.NewRequest("POST", "/lobby/get_lobby", strings.NewReader(`{"lobby_id": 1}`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
sqlxDB := sqlx.NewDb(db, "sqlmock") | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
err := GetLobby(w, r, sqlxDB) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
}) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusInternalServerError { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusInternalServerError) | ||
} | ||
|
||
expectedError := "invalid request; request must be a GET request" | ||
if strings.TrimSpace(rr.Body.String()) != expectedError { | ||
t.Errorf("handler returned unexpected body: got %v want %v", strings.TrimSpace(rr.Body.String()), expectedError) | ||
} | ||
} | ||
|
||
func TestGetLobby_DecodeError(t *testing.T) { | ||
db, _, err := sqlmock.New() | ||
if err != nil { | ||
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
defer db.Close() | ||
|
||
req, err := http.NewRequest("GET", "/lobby/get_lobby", strings.NewReader(`{"lobby_id": "invalid"}`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
sqlxDB := sqlx.NewDb(db, "sqlmock") | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
err := GetLobby(w, r, sqlxDB) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
}) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != http.StatusInternalServerError { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusInternalServerError) | ||
} | ||
|
||
expectedError := "an error occurred while decoding the request body: json: cannot unmarshal string into Go struct field GetLobbyArgs.lobby_id of type int8" | ||
if strings.TrimSpace(rr.Body.String()) != expectedError { | ||
t.Errorf("handler returned unexpected body: got %v want %v", strings.TrimSpace(rr.Body.String()), expectedError) | ||
} | ||
} |
Oops, something went wrong.