github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/modules/repo/service_test.go (about)

     1  package repo_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/covergates/covergates/config"
     8  	"github.com/covergates/covergates/core"
     9  	"github.com/covergates/covergates/mock"
    10  	"github.com/covergates/covergates/modules/repo"
    11  	"github.com/golang/mock/gomock"
    12  )
    13  
    14  func TestSynchronize(t *testing.T) {
    15  	ctrl := gomock.NewController(t)
    16  	defer ctrl.Finish()
    17  	config := &config.Config{
    18  		Github: config.Github{Server: "url"},
    19  	}
    20  
    21  	// test data
    22  	user := &core.User{Login: "user"}
    23  	repos := []*core.Repo{
    24  		{
    25  			URL: "http://github/repo1",
    26  		},
    27  		{
    28  			URL: "http://github/repo2",
    29  		},
    30  	}
    31  
    32  	// mock
    33  	mockSCM := mock.NewMockSCMService(ctrl)
    34  	mockClient := mock.NewMockClient(ctrl)
    35  	mockGitRepoService := mock.NewMockGitRepoService(ctrl)
    36  	mockUserStore := mock.NewMockUserStore(ctrl)
    37  	mockRepoStore := mock.NewMockRepoStore(ctrl)
    38  
    39  	mockSCM.EXPECT().Client(gomock.Eq(core.Github)).Return(mockClient, nil)
    40  	mockClient.EXPECT().Repositories().Return(mockGitRepoService)
    41  	mockGitRepoService.EXPECT().List(gomock.Any(), gomock.Eq(user)).Return(repos, nil)
    42  	mockRepoStore.EXPECT().BatchUpdateOrCreate(gomock.Eq(repos)).Return(nil)
    43  	mockUserStore.EXPECT().UpdateRepositories(gomock.Eq(user), gomock.Eq(repos)).Return(nil)
    44  	// testing
    45  	service := repo.NewService(config, mockSCM, mockUserStore, mockRepoStore)
    46  	if err := service.Synchronize(context.Background(), user); err != nil {
    47  		t.Fatal(err)
    48  	}
    49  }