Skip to content

Added unit tests for the go backend#99

Open
ssannkkallpp wants to merge 1 commit intogittuf:mainfrom
ssannkkallpp:go-backend-tests
Open

Added unit tests for the go backend#99
ssannkkallpp wants to merge 1 commit intogittuf:mainfrom
ssannkkallpp:go-backend-tests

Conversation

@ssannkkallpp
Copy link
Copy Markdown
Contributor

@ssannkkallpp ssannkkallpp commented Mar 23, 2026

Adds a comprehensive unit test suite for the Go backend. Tests cover the handlers, services, and validation layers using temporary in-memory git repositories.

Running the Tests

From the go-backend/ directory:

# Run all tests
go test ./tests/...

# Run with verbose output (shows logs and per-test results)
go test ./tests/... -v

Test Structure


go-backend/tests/
├── main_test.go        # TestMain entry point — initializes the logger
├── handlers_test.go    # HTTP-level tests for API endpoints via httptest
├── services_test.go    # Unit tests for git and metadata service functions
├── validation_test.go  # Unit tests for path/repo validation utilities
└── helpers/
    └── helpers.go      # SetupTestRepo — creates a temporary git repo with a 
                        # gittuf policy commit for cross-suite use

Signed-off-by: ssannkkallpp <ssannkkallpp@gmail.com>
// SetupTestRepo creates a temporary git repository with two commits: an initial commit
// and a second commit containing a gittuf metadata envelope at metadata/root.json,
// with refs/gittuf/policy pointing at that second commit.
func SetupTestRepo(t *testing.T) (string, string, func()) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using gittuf's testing helpers to create a repository here, e.g. copy/use https://github.com/gittuf/gittuf/blob/main/internal/policy/helpers_test.go.

Verification of gittuf repos is likely on the roadmap for the visualizer so having the test methods ready to plug in to that would be good.

"github.com/stretchr/testify/assert"
)

func TestCloneAndFetchRepo_Success(t *testing.T) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest bundling the tests into a parent test with child t.Runs, e.g. TestCloneAndFetchRepo with t.Run for each individual case. See https://github.com/gittuf/gittuf/blob/main/internal/policy/policy_test.go#L25 for an example.

Comment thread .gitignore
backend/venv/

# Go test artifacts
go-backend/tests/TEST_DESCRIPTIONS.md
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a listing/description file that you intended to add?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants