Skip to content

Commit

Permalink
Merge pull request #12 from justinfarrelldev/lobby-tests
Browse files Browse the repository at this point in the history
Created more tests for lobbies
  • Loading branch information
justinfarrelldev authored Nov 23, 2024
2 parents 701fa66 + 32672b2 commit 1b56206
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ const docTemplate = `{
"properties": {
"id": {
"description": "ID is the unique identifier for the lobby.",
"type": "string"
"type": "integer"
},
"is_closed": {
"description": "IsClosed indicates if the lobby is closed.",
Expand Down
2 changes: 1 addition & 1 deletion docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@
"properties": {
"id": {
"description": "ID is the unique identifier for the lobby.",
"type": "string"
"type": "integer"
},
"is_closed": {
"description": "IsClosed indicates if the lobby is closed.",
Expand Down
2 changes: 1 addition & 1 deletion docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ definitions:
properties:
id:
description: ID is the unique identifier for the lobby.
type: string
type: integer
is_closed:
description: IsClosed indicates if the lobby is closed.
type: boolean
Expand Down
File renamed without changes.
183 changes: 183 additions & 0 deletions internal/lobby/create_lobby_test.go
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)
}
}
165 changes: 165 additions & 0 deletions internal/lobby/get_lobby_test.go
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)
}
}
Loading

0 comments on commit 1b56206

Please sign in to comment.