diff --git a/db/test_config.go b/db/test_config.go index 5e07486df..ad1ae821e 100644 --- a/db/test_config.go +++ b/db/test_config.go @@ -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" @@ -35,7 +35,7 @@ func InitTestDB() { TestDB.db = db - fmt.Println("db connected") + fmt.Println("DB CONNECTED") // migrate table changes db.AutoMigrate(&Tribe{}) diff --git a/handlers/tribes_test.go b/handlers/tribes_test.go index c6d984623..b23c88e88 100644 --- a/handlers/tribes_test.go +++ b/handlers/tribes_test.go @@ -4,6 +4,8 @@ import ( "bytes" "context" "encoding/json" + "fmt" + "log" "net/http" "net/http/httptest" "strings" @@ -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)