-
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 #13 from justinfarrelldev/test-coverage
Added test coverage checking and brought coverage up to the required amounts.
- Loading branch information
Showing
17 changed files
with
1,382 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# example 2: on merge to master from pull request (recommended) | ||
name: Check test coverage | ||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
coverage: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Setup Go 1.23.x | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: "1.23.0" | ||
- name: Install dependencies | ||
run: go install | ||
- name: Install bc | ||
run: sudo apt-get install -y bc | ||
- name: Check code coverage | ||
run: | | ||
echo "Checking that code coverage is above 80% within the internal package..." | ||
go test ./internal/... -coverprofile=coverage.out | ||
total_coverage=$(go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+') | ||
echo "Total coverage: $total_coverage" | ||
if (( $(echo "$total_coverage > 80.0" | bc -l) )); then | ||
echo "Code coverage is above 80%!" | ||
else | ||
echo "Code coverage is below 80%!" | ||
exit 1 | ||
fi |
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
{ | ||
"recommendations": [ | ||
"golang.go", | ||
"streetsidesoftware.code-spell-checker", | ||
"sankethdev.vscode-proto", | ||
"wayou.vscode-todo-highlight" | ||
] | ||
} | ||
"recommendations": [ | ||
"golang.go", | ||
"streetsidesoftware.code-spell-checker", | ||
"sankethdev.vscode-proto", | ||
"wayou.vscode-todo-highlight", | ||
"soren.go-coverage-viewer" | ||
] | ||
} |
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
"Hasher", | ||
"healthcheck", | ||
"healthgrpc", | ||
"jmoiron", | ||
"Ninjaboy", | ||
"proto", | ||
"Protobuf", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package account | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
// func TestCreateAccountHandler_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() | ||
|
||
// account := Account{ | ||
// Name: "Test User", | ||
// Info: "Some info", | ||
// Location: "Some location", | ||
// Email: "[email protected]", | ||
// ExperienceLevel: Beginner, | ||
// } | ||
|
||
// mock.ExpectQuery("INSERT INTO account \\(name, info, location, email, experience_level\\) VALUES \\(\\$1, \\$2, \\$3, \\$4, \\$5\\)"). | ||
// WithArgs(account.Name, account.Info, account.Location, account.Email, account.ExperienceLevel). | ||
// WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1)) | ||
|
||
// req, err := http.NewRequest("POST", "/account/create_account", strings.NewReader(`{"name": "Test User", "info": "Some info", "location": "Some location", "email": "[email protected]", "experience_level": 0}`)) | ||
// if err != nil { | ||
// t.Fatal(err) | ||
// } | ||
|
||
// rr := httptest.NewRecorder() | ||
// sqlxDB := sqlx.NewDb(db, "sqlmock") | ||
// handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// CreateAccountHandler(w, r, sqlxDB) | ||
// }) | ||
|
||
// 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 TestCreateAccountHandler_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", "/account/create_account", strings.NewReader(`{"name": "Test User", "info": "Some info", "location": "Some location", "email": "[email protected]", "experience_level": 0}`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
sqlxDB := sqlx.NewDb(db, "sqlmock") | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
CreateAccountHandler(w, r, sqlxDB) | ||
}) | ||
|
||
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 := "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 TestCreateAccountHandler_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", "/account/create_account", strings.NewReader(`{ "account": {"name": "Test User", "info": "Some info", "location": "Some location", "email": "[email protected]", "experience_level": "invalid"}, "password": "fake" }`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
sqlxDB := sqlx.NewDb(db, "sqlmock") | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
CreateAccountHandler(w, r, sqlxDB) | ||
}) | ||
|
||
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 Account.account.experience_level of type account.ExperienceLevel" | ||
if strings.TrimSpace(rr.Body.String()) != expectedError { | ||
t.Errorf("handler returned unexpected body: got %v want %v", strings.TrimSpace(rr.Body.String()), expectedError) | ||
} | ||
} | ||
|
||
func TestCreateAccountHandler_EmailRequired(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", "/account/create_account", strings.NewReader(`{"account": {"name": "Test User", "info": "Some info", "location": "Some location", "experience_level": 0}, "password": "test password" }`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
sqlxDB := sqlx.NewDb(db, "sqlmock") | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
CreateAccountHandler(w, r, sqlxDB) | ||
}) | ||
|
||
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 := "an error occurred while checking whether the email for the account is valid: mail: no address" | ||
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
Oops, something went wrong.