-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: WIP - added router.go ginkgo unit tests * feat: WIP - added handlers.go unit tests using mocks * feat: WIP - added handlers.go unit tests using mocks * feat: added handlers.go unit tests using mocks * chore: Updated coverage badge. --------- Co-authored-by: Finsen Varghese <[email protected]> Co-authored-by: GitHub Action <[email protected]>
- Loading branch information
1 parent
5ea7575
commit a567e6f
Showing
8 changed files
with
152 additions
and
25 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 |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
*.dll | ||
*.so | ||
*.dylib | ||
*.idea | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
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
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,13 @@ | ||
package handlers_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestHandlers(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Handlers Suite") | ||
} |
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,99 @@ | ||
package handlers_test | ||
|
||
import ( | ||
"database/sql" | ||
"encoding/json" | ||
"net/http/httptest" | ||
|
||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/gin-gonic/gin" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
|
||
"github.com/guidewire/fern-reporter/pkg/api/handlers" | ||
"github.com/guidewire/fern-reporter/pkg/models" | ||
) | ||
|
||
var ( | ||
db *sql.DB | ||
gormDb *gorm.DB | ||
mock sqlmock.Sqlmock | ||
) | ||
|
||
var _ = BeforeSuite(func() { | ||
db, mock, _ = sqlmock.New() | ||
|
||
dialector := postgres.New(postgres.Config{ | ||
DSN: "sqlmock_db_0", | ||
DriverName: "postgres", | ||
Conn: db, | ||
PreferSimpleProtocol: true, | ||
}) | ||
gormDb, _ = gorm.Open(dialector, &gorm.Config{}) | ||
|
||
}) | ||
|
||
var _ = AfterSuite(func() { | ||
db.Close() | ||
}) | ||
|
||
var _ = Describe("Handlers", func() { | ||
Context("when GetTestRunAll handleer is invoked", func() { | ||
It("should query db to fetch all records", func() { | ||
|
||
rows := sqlmock.NewRows([]string{"ID", "TestProjectName"}). | ||
AddRow(1, "project 1"). | ||
AddRow(2, "project 2") | ||
|
||
mock.ExpectQuery("SELECT (.+) FROM \"test_runs\""). | ||
WithoutArgs(). | ||
WillReturnRows(rows) | ||
w := httptest.NewRecorder() | ||
c, _ := gin.CreateTestContext(w) | ||
handler := handlers.NewHandler(gormDb) | ||
|
||
handler.GetTestRunAll(c) | ||
|
||
Expect(w.Code).To(Equal(200)) | ||
|
||
var testRuns []models.TestRun | ||
if err := json.NewDecoder(w.Body).Decode(&testRuns); err != nil { | ||
Fail(err.Error()) | ||
} | ||
Expect(len(testRuns)).To(Equal(2)) | ||
Expect(testRuns[0].TestProjectName).To(Equal("project 1")) | ||
Expect(testRuns[1].TestProjectName).To(Equal("project 2")) | ||
}) | ||
}) | ||
|
||
Context("When GetTestRunByID handler is invoked", func() { | ||
It("should query DB with where clause filtering by id", func() { | ||
|
||
rows := sqlmock.NewRows([]string{"ID", "TestProjectName"}). | ||
AddRow(123, "project 123") | ||
|
||
mock.ExpectQuery("SELECT (.+) FROM \"test_runs\" WHERE id = \\$1"). | ||
WithArgs("123"). | ||
WillReturnRows(rows) | ||
|
||
w := httptest.NewRecorder() | ||
c, _ := gin.CreateTestContext(w) | ||
|
||
c.Params = append(c.Params, gin.Param{Key: "id", Value: "123"}) | ||
handler := handlers.NewHandler(gormDb) | ||
handler.GetTestRunByID(c) | ||
|
||
Expect(w.Code).To(Equal(200)) | ||
|
||
var testRun models.TestRun | ||
|
||
if err := json.NewDecoder(w.Body).Decode(&testRun); err != nil { | ||
Fail(err.Error()) | ||
} | ||
Expect(int(testRun.ID)).To(Equal(123)) | ||
Expect(testRun.TestProjectName).To(Equal("project 123")) | ||
}) | ||
}) | ||
}) |
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