Extract test api test helpers
Signed-off-by: Steven Kriegler <sk.bunsenbrenner@gmail.com>
This commit is contained in:
parent
34e2783cb1
commit
4d28133b12
|
@ -5,8 +5,64 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
giteaSdk "gitea-sonarqube-pr-bot/internal/clients/gitea"
|
||||
sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube"
|
||||
"gitea-sonarqube-pr-bot/internal/settings"
|
||||
webhook "gitea-sonarqube-pr-bot/internal/webhooks/sonarqube"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// Default SDK mocking
|
||||
type HandlerPartialMock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (h *HandlerPartialMock) fetchDetails(w *webhook.Webhook) {
|
||||
h.Called(w)
|
||||
}
|
||||
|
||||
type GiteaSdkMock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (h *GiteaSdkMock) PostComment(_ settings.GiteaRepository, _ int, _ string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *GiteaSdkMock) DetermineHEAD(_ settings.GiteaRepository, _ int64) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (h *GiteaSdkMock) UpdateStatus(_ settings.GiteaRepository, _ string, _ giteaSdk.StatusDetails) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type SQSdkMock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (h *SQSdkMock) GetMeasures(project string, branch string) (*sqSdk.MeasuresResponse, error) {
|
||||
return &sqSdk.MeasuresResponse{}, nil
|
||||
}
|
||||
|
||||
func (h *SQSdkMock) GetPullRequestUrl(project string, index int64) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (h *SQSdkMock) GetPullRequest(project string, index int64) (*sqSdk.PullRequest, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (h *SQSdkMock) ComposeGiteaComment(data *sqSdk.CommentComposeData) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func defaultMockPreparation(h *HandlerPartialMock) {
|
||||
h.On("fetchDetails", mock.Anything).Return(nil)
|
||||
}
|
||||
|
||||
// SETUP: mute logs
|
||||
func TestMain(m *testing.M) {
|
||||
log.SetOutput(ioutil.Discard)
|
||||
|
|
|
@ -6,63 +6,11 @@ import (
|
|||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
giteaSdk "gitea-sonarqube-pr-bot/internal/clients/gitea"
|
||||
sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube"
|
||||
"gitea-sonarqube-pr-bot/internal/settings"
|
||||
webhook "gitea-sonarqube-pr-bot/internal/webhooks/sonarqube"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type HandlerPartialMock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (h *HandlerPartialMock) fetchDetails(w *webhook.Webhook) {
|
||||
h.Called(w)
|
||||
}
|
||||
|
||||
type GiteaSdkMock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (h *GiteaSdkMock) PostComment(_ settings.GiteaRepository, _ int, _ string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *GiteaSdkMock) DetermineHEAD(_ settings.GiteaRepository, _ int64) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (h *GiteaSdkMock) UpdateStatus(_ settings.GiteaRepository, _ string, _ giteaSdk.StatusDetails) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type SQSdkMock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (h *SQSdkMock) GetMeasures(project string, branch string) (*sqSdk.MeasuresResponse, error) {
|
||||
return &sqSdk.MeasuresResponse{}, nil
|
||||
}
|
||||
|
||||
func (h *SQSdkMock) GetPullRequestUrl(project string, index int64) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (h *SQSdkMock) GetPullRequest(project string, index int64) (*sqSdk.PullRequest, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (h *SQSdkMock) ComposeGiteaComment(data *sqSdk.CommentComposeData) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func defaultMockPreparation(h *HandlerPartialMock) {
|
||||
h.On("fetchDetails", mock.Anything).Return(nil)
|
||||
}
|
||||
|
||||
func withValidRequestData(t *testing.T, mockPreparation func(*HandlerPartialMock), jsonBody []byte) (*http.Request, *httptest.ResponseRecorder, http.HandlerFunc, *HandlerPartialMock) {
|
||||
partialMock := new(HandlerPartialMock)
|
||||
mockPreparation(partialMock)
|
||||
|
|
Loading…
Reference in a new issue