Skip to content

Commit

Permalink
Merge pull request stakwork#2002 from stakwork/feat/get-all-phase-tic…
Browse files Browse the repository at this point in the history
…kets

Feat: add function to get phase tickets and wrote tests
  • Loading branch information
elraphty authored Nov 29, 2024
2 parents cec8684 + e37d411 commit 5b3d418
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 0 deletions.
1 change: 1 addition & 0 deletions db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,5 @@ type Database interface {
GetProductBrief(workspaceUuid string) (string, error)
GetFeatureBrief(featureUuid string) (string, error)
GetTicketsByPhaseUUID(featureUUID string, phaseUUID string) ([]Tickets, error)
GetTicketsByPhase(phaseUUID string) ([]Tickets, error)
}
19 changes: 19 additions & 0 deletions db/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,22 @@ func (db database) DeleteTicket(uuid string) error {
}
return nil
}

func (db database) GetTicketsByPhase(phaseUUID string) ([]Tickets, error) {
var tickets []Tickets

result := db.db.Where("phase_uuid = ?", phaseUUID).
Order("sequence ASC").
Find(&tickets)

if result.Error != nil {
return nil, fmt.Errorf("failed to fetch tickets for phase: %w", result.Error)
}

// Return empty slice if no tickets found
if result.RowsAffected == 0 {
return []Tickets{}, nil
}

return tickets, nil
}
108 changes: 108 additions & 0 deletions db/tickets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,111 @@ func TestGetTicket(t *testing.T) {
}
})
}

func TestGetTicketsByPhase(t *testing.T) { // create person
InitTestDB()

// create person
now := time.Now()

person := Person{
Uuid: uuid.New().String(),
OwnerPubKey: "testfeaturepubkey",
OwnerAlias: "testfeaturealias",
Description: "testfeaturedescription",
Created: &now,
Updated: &now,
Deleted: false,
}

// create person
TestDB.CreateOrEditPerson(person)

workspace := Workspace{
Uuid: uuid.New().String(),
Name: "Test tickets space",
Created: &now,
Updated: &now,
}

// create workspace
TestDB.CreateOrEditWorkspace(workspace)

workspaceFeatures := WorkspaceFeatures{
Uuid: uuid.New().String(),
WorkspaceUuid: workspace.Uuid,
Name: "test",
Brief: "test get brief",
Requirements: "Test get requirements",
Architecture: "Test get architecture",
Url: "Test get url",
Priority: 1,
Created: &now,
Updated: &now,
CreatedBy: "test",
UpdatedBy: "test",
}

// create WorkspaceFeatures
TestDB.CreateOrEditFeature(workspaceFeatures)

featurePhase := FeaturePhase{
Uuid: uuid.New().String(),
FeatureUuid: workspaceFeatures.Uuid,
Name: "test get feature phase",
Priority: 1,
Created: &now,
Updated: &now,
}

// create FeaturePhase
TestDB.CreateOrEditFeaturePhase(featurePhase)

ticket := Tickets{
UUID: uuid.New(),
FeatureUUID: workspaceFeatures.Uuid,
PhaseUUID: featurePhase.Uuid,
Name: "test get tickets 1",
CreatedAt: now,
UpdatedAt: now,
}

// create ticket
TestDB.CreateOrEditTicket(&ticket)

ticket2 := Tickets{
UUID: uuid.New(),
FeatureUUID: workspaceFeatures.Uuid,
PhaseUUID: featurePhase.Uuid,
Name: "test get tickets 2",
CreatedAt: now,
UpdatedAt: now,
}

// create ticket
TestDB.CreateOrEditTicket(&ticket2)

// should return an empty slice if no tickets are found for the phase
t.Run("should return an empty slice if no tickets are found for the phase", func(t *testing.T) {
result, err := TestDB.GetTicketsByPhase(uuid.New().String())
if err != nil {
t.Errorf("expected no error but got %v", err)
}

if len(result) != 0 {
t.Errorf("expected an empty slice but got %v", result)
}
})

// should return a slice of tickets if the phase exists
t.Run("should return a slice of tickets if the phase exists", func(t *testing.T) {
result, err := TestDB.GetTicketsByPhase(featurePhase.Uuid)
if err != nil {
t.Errorf("expected no error but got %v", err)
}

if len(result) != 2 {
t.Errorf("expected 2 tickets but got %v", len(result))
}
})
}
58 changes: 58 additions & 0 deletions mocks/Database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5b3d418

Please sign in to comment.