Skip to content

Commit

Permalink
tested DB connection
Browse files Browse the repository at this point in the history
  • Loading branch information
elraphty committed Jun 14, 2024
1 parent d1abab0 commit 22d80a7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions db/test_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
var TestDB database

func InitTestDB() {
rdsHost := "test_db"
rdsHost := "test_postgres"
rdsPort := fmt.Sprintf("%d", 5532)
rdsDbName := "test_posrgres"
rdsUsername := "test_user"
Expand All @@ -35,7 +35,7 @@ func InitTestDB() {

TestDB.db = db

fmt.Println("db connected")
fmt.Println("DB CONNECTED")

// migrate table changes
db.AutoMigrate(&Tribe{})
Expand Down
49 changes: 49 additions & 0 deletions handlers/tribes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -85,6 +87,53 @@ func TestGetTribesByOwner(t *testing.T) {
})
}

func setupSuite(_ *testing.T) func(tb testing.TB) {
db.InitTestDB()

return func(_ testing.TB) {
log.Println("Teardown test")
}
}

func TestGetPeopleReal(t *testing.T) {
teardownSuite := setupSuite(t)
defer teardownSuite(t)

tHandler := NewPeopleHandler(db.TestDB)
// Initialize test database connection and populate test data

// Create a request to your handler
req, err := http.NewRequest("GET", "/people", nil)
if err != nil {
t.Fatal(err)
}

// Create a ResponseRecorder to record the response
rr := httptest.NewRecorder()

// Create a Chi router and register your handler
r := chi.NewRouter()
r.Get("/people", tHandler.GetListedPeople)

// Serve the request to the handler
r.ServeHTTP(rr, req)

// Check the response status code
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}

dbPeople := db.TestDB.GetListedPeople(req)

var returnedPersons []db.Person
err = json.Unmarshal(rr.Body.Bytes(), &returnedPersons)
assert.NoError(t, err)
fmt.Println("Returned People ==", returnedPersons)
assert.Equal(t, len(dbPeople), len(returnedPersons))
// Check the response body or any other expected behavior
}

func TestGetTribe(t *testing.T) {
mockDb := mocks.NewDatabase(t)
tHandler := NewTribeHandler(mockDb)
Expand Down

0 comments on commit 22d80a7

Please sign in to comment.