Skip to content

Commit

Permalink
Merge pull request ezeoleaf#24 from ezeoleaf/feature/improve_testing_…
Browse files Browse the repository at this point in the history
…coverage

Added tests on providers
  • Loading branch information
ezeoleaf authored May 17, 2021
2 parents 9e91e9b + 3f7bb41 commit d22b840
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
1 change: 0 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func TestGetEmptyProviderAndEmptyPublishers(t *testing.T) {
errors := []string{}

logFatalf = func(format string, args ...interface{}) {
fmt.Println("alksndlaksndlaksndlaksndalksdnalksnd")
if len(args) > 0 {
errors = append(errors, fmt.Sprintf(format, args))
} else {
Expand Down
14 changes: 10 additions & 4 deletions providers/github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ import (
var repositories *github.RepositoriesSearchResult
var rdb cache.Repository
var cfg config.Config
var client *github.Client
var client githubClient
var ctx context.Context

// repository represent the repository model
type githubProvider struct {
Provider interface{}
}

type githubClient interface {
Repositories(ctx context.Context, query string, opt *github.SearchOptions) (*github.RepositoriesSearchResult, *github.Response, error)
}

func NewGithubRepository(config config.Config) providers.IContent {
cfg = config
ro := &redis.Options{
Expand Down Expand Up @@ -95,12 +99,14 @@ func getContent(repo *github.Repository) string {

func setClient() {
ctx = context.Background()

ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: cfg.AccessCfg.GithubAccessToken},
)
tc := oauth2.NewClient(ctx, ts)

client = github.NewClient(tc)
client = github.NewClient(tc).Search

}

func getRepositories() ([]github.Repository, int) {
Expand All @@ -116,7 +122,7 @@ func getRepositories() ([]github.Repository, int) {

so := github.SearchOptions{ListOptions: github.ListOptions{PerPage: 1}}

repositories, _, e = client.Search.Repositories(ctx, qs, &so)
repositories, _, e = client.Repositories(ctx, qs, &so)

if e != nil {
panic(e)
Expand All @@ -138,7 +144,7 @@ func getSpecificRepo(pos int) *github.Repository {

so := github.SearchOptions{ListOptions: github.ListOptions{PerPage: 1, Page: pos}}

repositories, _, e = client.Search.Repositories(ctx, qs, &so)
repositories, _, e = client.Repositories(ctx, qs, &so)

if e != nil {
return nil
Expand Down
75 changes: 75 additions & 0 deletions providers/github/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package github

import (
"context"
"errors"
"testing"

"github.com/alicebob/miniredis/v2"

"github.com/ezeoleaf/GobotTweet/cache"
"github.com/ezeoleaf/GobotTweet/config"
"github.com/go-redis/redis/v8"
Expand Down Expand Up @@ -190,3 +193,75 @@ func TestTweetRepoWithHashtags(t *testing.T) {
t.Errorf("Expected: %s, got %s", expected, result)
}
}

type MockClient struct {
RepositoriesFunc func(ctx context.Context, query string, opt *github.SearchOptions) (*github.RepositoriesSearchResult, *github.Response, error)
}

var GetRepositoriesFunc func(ctx context.Context, query string, opt *github.SearchOptions) (*github.RepositoriesSearchResult, *github.Response, error)

// Do is the mock client's `Do` func
func (m *MockClient) Repositories(ctx context.Context, query string, opt *github.SearchOptions) (*github.RepositoriesSearchResult, *github.Response, error) {
return GetRepositoriesFunc(ctx, query, opt)
}

func TestGetRepositories(t *testing.T) {
GetRepositoriesFunc = func(ctx context.Context, query string, opt *github.SearchOptions) (*github.RepositoriesSearchResult, *github.Response, error) {
i := 1
var repId int64 = 1
reps := []github.Repository{
{ID: &repId},
}
r := github.RepositoriesSearchResult{Total: &i, Repositories: reps}
return &r, nil, nil
}

client = &MockClient{}
repos, total := getRepositories()

if total != 1 {
t.Errorf("Expected total one repository, got %v", total)
}

if len(repos) != 1 {
t.Errorf("Expected one repo in slice, got %v", len(repos))
}
}

func TestGetSpecificRepo(t *testing.T) {
GetRepositoriesFunc = func(ctx context.Context, query string, opt *github.SearchOptions) (*github.RepositoriesSearchResult, *github.Response, error) {
i := 1
var repId int64 = 1
reps := []github.Repository{
{ID: &repId},
}
r := github.RepositoriesSearchResult{Total: &i, Repositories: reps}
return &r, nil, nil
}

client = &MockClient{}
repo := getSpecificRepo(1)

if repo == nil {
t.Error("Expected repository got nil")
}

cfg.Topic = "topic"
repo = getSpecificRepo(1)
if repo == nil {
t.Error("Expected repository got nil")
}
}

func TestGetSpecificRepoError(t *testing.T) {
GetRepositoriesFunc = func(ctx context.Context, query string, opt *github.SearchOptions) (*github.RepositoriesSearchResult, *github.Response, error) {
return nil, nil, errors.New("Error")
}

client = &MockClient{}
repo := getSpecificRepo(1)

if repo != nil {
t.Error("Expected nil got repository")
}
}

0 comments on commit d22b840

Please sign in to comment.