Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TestGetTribe UT #1774

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions handlers/tribes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"github.com/google/uuid"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -86,21 +87,23 @@ func TestGetTribesByOwner(t *testing.T) {
}

func TestGetTribe(t *testing.T) {
mockDb := mocks.NewDatabase(t)
tHandler := NewTribeHandler(mockDb)
teardownSuite := SetupSuite(t)
defer teardownSuite(t)
tHandler := NewTribeHandler(db.TestDB)

tribe := db.Tribe{
UUID: uuid.New().String(),
OwnerPubKey: uuid.New().String(),
Name: "tribe",
Description: "description",
Tags: []string{"tag1", "tag2"},
Badges: pq.StringArray{},
}
db.TestDB.CreateOrEditTribe(tribe)

t.Run("Should test that a tribe can be returned when the right UUID is passed to the request parameter", func(t *testing.T) {
// Mock data
mockUUID := "valid_uuid"
mockTribe := db.Tribe{
UUID: mockUUID,
}
mockChannels := []db.Channel{
{ID: 1, TribeUUID: mockUUID},
{ID: 2, TribeUUID: mockUUID},
}
mockDb.On("GetTribe", mock.Anything).Return(mockTribe).Once()
mockDb.On("GetChannelsByTribe", mock.Anything).Return(mockChannels).Once()
mockUUID := tribe.UUID

// Serve request
rr := httptest.NewRecorder()
Expand All @@ -111,6 +114,8 @@ func TestGetTribe(t *testing.T) {
t.Fatal(err)
}

fetchedTribe := db.TestDB.GetTribe(mockUUID)

handler := http.HandlerFunc(tHandler.GetTribe)
handler.ServeHTTP(rr, req)

Expand All @@ -121,15 +126,13 @@ func TestGetTribe(t *testing.T) {
if err != nil {
t.Fatalf("Error decoding JSON response: %s", err)
}
assert.Equal(t, mockTribe.UUID, responseData["uuid"])
assert.Equal(t, tribe.UUID, responseData["uuid"])
assert.Equal(t, tribe, fetchedTribe)
})

t.Run("Should test that no tribe is returned when a nonexistent UUID is passed", func(t *testing.T) {
// Mock data
mockDb.ExpectedCalls = nil

nonexistentUUID := "nonexistent_uuid"
mockDb.On("GetTribe", nonexistentUUID).Return(db.Tribe{}).Once()
mockDb.On("GetChannelsByTribe", mock.Anything).Return([]db.Channel{}).Once()

// Serve request
rr := httptest.NewRecorder()
Expand Down
Loading