Skip to content

Commit

Permalink
feat: add extractTestOpsID function with unit tests
Browse files Browse the repository at this point in the history
- Implemented the `extractTestOpsID` method in the `Parser` struct to extract a numeric ID from strings formatted as `name-ID`.
- Added unit tests to cover various edge cases, including invalid input, missing ID, and successful extraction.
- Improved error handling with logging for invalid ID parsing.
- Used a table-driven approach for comprehensive test coverage.
  • Loading branch information
gibiw committed Nov 27, 2024
1 parent 80ce3fc commit 0aac299
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
35 changes: 35 additions & 0 deletions internal/parsers/allure/allure.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path"
"path/filepath"
"strconv"
"strings"
)

Expand Down Expand Up @@ -116,6 +117,7 @@ func (p *Parser) convertTest(test Test) models.Result {
d := test.Stop - test.Start
result := models.Result{
Title: test.Name,
TestOpsID: p.getTestOpsID(test.Links),
Steps: make([]models.Step, 0, len(test.Steps)),
Attachments: p.convertAttachments(test.Attachments),
StepType: "text",
Expand Down Expand Up @@ -210,3 +212,36 @@ func (p *Parser) convertStepResultStatus(status string) string {
}
return "blocked"
}

func (p *Parser) getTestOpsID(links []Link) *int64 {
if len(links) == 0 {
return nil
}

for _, link := range links {
if link.Type == "tms" {
return p.extractTestOpsID(link.Name)
}
}

return nil
}

func (p *Parser) extractTestOpsID(name string) *int64 {
if name == "" {
return nil
}

parts := strings.Split(name, "-")
if len(parts) == 0 {
return nil
}

id := strings.TrimSpace(parts[len(parts)-1])
if testOpsID, err := strconv.ParseInt(id, 10, 64); err == nil {
return &testOpsID
}

slog.Warn("failed to parse testops id", "id", id)
return nil
}
34 changes: 34 additions & 0 deletions internal/parsers/allure/allure_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package allure

import (
"testing"
)

func TestParser_extractTestOpsID(t *testing.T) {
parser := &Parser{}

tests := []struct {
name string
input string
expected *int64
}{
{name: "empty string", input: "", expected: nil},
{name: "no separator", input: "test", expected: nil},
{name: "invalid ID", input: "test-case-abc", expected: nil},
{name: "valid case", input: "test-case-12345", expected: func() *int64 { i := int64(12345); return &i }()},
{name: "empty ID", input: "test-case-", expected: nil},
{name: "ID at the start", input: "12345-case", expected: nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := parser.extractTestOpsID(tt.input)

if (result == nil && tt.expected != nil) || (result != nil && tt.expected == nil) {
t.Errorf("expected %v, got %v", tt.expected, result)
} else if result != nil && *result != *tt.expected {
t.Errorf("expected %v, got %v", *tt.expected, *result)
}
})
}
}

0 comments on commit 0aac299

Please sign in to comment.