Skip to content

Commit

Permalink
added a database type by using the repository pattern exclusively for…
Browse files Browse the repository at this point in the history
… unit tests in package handlers in order to avoid a setup of a postgres database for testing purposes only
  • Loading branch information
jagottsicher committed Jul 14, 2023
1 parent 890d810 commit 8a5768e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
8 changes: 8 additions & 0 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func NewRepo(a *config.AppConfig, db *driver.DB) *Repository {
}
}

// NewTestRepo creates a new repository for basic
func NewTestRepo(a *config.AppConfig) *Repository {
return &Repository{
App: a,
DB: dbrepo.NewTestingRepo(a),
}
}

// NewHandlers sets the repository for the handlers
func NewHandlers(r *Repository) {
Repo = r
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func getRoutes() http.Handler {
app.TemplateCache = tc
app.UseCache = true

repo := NewRepo(&app)
repo := NewTestRepo(&app)
NewHandlers(repo)

render.NewRenderer(&app)
Expand Down
4 changes: 2 additions & 2 deletions internal/render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestAddDefaultData(t *testing.T) {
}
}

func TestRenderTemplate(t *testing.T) {
func TestTemplate(t *testing.T) {
pathToTemplates = "./../../templates"

tc, err := CreateTemplateCache()
Expand Down Expand Up @@ -65,7 +65,7 @@ func getSession() (*http.Request, error) {
return r, nil
}

func TestNewTemplates(t *testing.T) {
func TestNewRenderer(t *testing.T) {
NewRenderer(app)
}

Expand Down
11 changes: 11 additions & 0 deletions internal/repository/dbrepo/dbrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ func NewPostgresRepo(conn *sql.DB, a *config.AppConfig) repository.DatabaseRepo
DB: conn,
}
}

type testDBRepo struct {
App *config.AppConfig
DB *sql.DB
}

func NewTestingRepo(a *config.AppConfig) repository.DatabaseRepo {
return &testDBRepo{
App: a,
}
}
40 changes: 40 additions & 0 deletions internal/repository/dbrepo/testrepo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dbrepo

import (
"time"

"github.com/jagottsicher/myGoWebApplication/internal/models"
)

func (m *testDBRepo) AllUsers() bool {
return true
}

// InsertReservation stores a reservation in the database
func (m *testDBRepo) InsertReservation(res models.Reservation) (int, error) {
return 1, nil
}

// InsertBungalowRestriction places a restriction in the database
func (m *testDBRepo) InsertBungalowRestriction(r models.BungalowRestriction) error {
return nil
}

// SearchAvailabilityByDatesByBungalowID returns true if there is availablity for a bungalowID for a date range, false if not
func (m *testDBRepo) SearchAvailabilityByDatesByBungalowID(start, end time.Time, bungalowID int) (bool, error) {
return false, nil
}

// SearchAvailabilityByDatesForAllBungalows returns a slice of available bungalows, if any for a queried date range
func (m *testDBRepo) SearchAvailabilityByDatesForAllBungalows(start, end time.Time) ([]models.Bungalow, error) {
var bungalows []models.Bungalow

return bungalows, nil
}

// GetBungalowByID gets a bungalow by id
func (m *testDBRepo) GetBungalowByID(id int) (models.Bungalow, error) {
var bungalow models.Bungalow

return bungalow, nil
}

0 comments on commit 8a5768e

Please sign in to comment.