Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: kuba-- <[email protected]>
  • Loading branch information
kuba-- committed Jun 28, 2019
1 parent 787d616 commit cf01f79
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 3 deletions.
58 changes: 58 additions & 0 deletions commit_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gitbase

import (
"io"
"strconv"
"testing"

"github.com/src-d/go-mysql-server/sql"
Expand Down Expand Up @@ -146,3 +147,60 @@ func TestPartitionRowsWithIndex(t *testing.T) {

require.ElementsMatch(expected, result)
}

func TestCommitFilesIndexIter(t *testing.T) {
require := require.New(t)

ctx, _, cleanup := setupRepos(t)
defer cleanup()

key := &commitFileIndexKey{
Repository: "zero",
Packfile: plumbing.ZeroHash.String(),
Hash: plumbing.ZeroHash.String(),
Offset: 0,
Name: "two",
Mode: 5,
Tree: plumbing.ZeroHash.String(),
Commit: plumbing.ZeroHash.String(),
}
limit := 10
it := newCommitFilesIndexIter(testIndexValueIter{key, int64(limit)}, poolFromCtx(t, ctx))
for off := 0; off < limit; off++ {
row, err := it.Next()
require.NoError(err)

require.Equal(key.Repository, row[0])
require.Equal(strconv.Itoa(off), row[2])
}
_, err := it.Next()
require.EqualError(err, io.EOF.Error())
}

type testIndexValueIter struct {
key *commitFileIndexKey
limit int64
}

func (it testIndexValueIter) Next() ([]byte, error) {
if it.key.Offset >= it.limit {
return nil, io.EOF
}

it.key.Name = strconv.Itoa(int(it.key.Offset))
val, err := it.key.encode()
if err != nil {
return nil, err
}
val, err = encoder.encode(val)
if err != nil {
return nil, err
}

it.key.Offset++
return val, nil
}

func (it testIndexValueIter) Close() error {
return nil
}
3 changes: 2 additions & 1 deletion common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ func buildSession(
if ok {
name := strings.TrimLeft(path, string(os.PathSeparator))
if err := lib.AddPlain(name, path, nil); err == nil {
_, err := pool.GetRepo(name)
r, err := pool.GetRepo(name)
require.NoError(err)
paths = append(paths, pathToName(path))
require.NoError(r.Close())
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions packfiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"path/filepath"
"testing"

fixtures "github.com/src-d/go-git-fixtures"
"github.com/stretchr/testify/require"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/cache"
)

var (
Expand Down Expand Up @@ -43,6 +45,40 @@ func TestRepositoryPackfiles(t *testing.T) {
require.NotNil(fs)
}

func TestRepositoryPackfilesNoBare(t *testing.T) {
require := require.New(t)

fs := fixtures.ByTag("worktree").One().Worktree()

dotgit, packfiles, err := repositoryPackfiles(fs)
require.NoError(err)
require.Equal([]plumbing.Hash{
plumbing.NewHash("323a4b6b5de684f9966953a043bc800154e5dbfa"),
}, packfiles)

require.NoError(dotgit.Close())
}

func TestGetUnpackedObject(t *testing.T) {
require := require.New(t)

fs := fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One().Worktree()
path := fs.Root()

lib, err := newMultiLibrary()
require.NoError(err)
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
require.NoError(lib.AddPlain(path, path, nil))

r, err := pool.GetRepo(path)
require.NoError(err)

obj, err := getUnpackedObject(r, plumbing.NewHash("3bf5d30ad4f23cf517676fee232e3bcb8537c1d0"))
require.NoError(err)
require.NotNil(obj)
require.NoError(r.Close())
}

func TestRepositoryIndex(t *testing.T) {
lib, pool, err := newMultiPool()
require.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions repository_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ func (r *Repository) Cache() cache.Object {
return r.cache
}

func (r *Repository) Close() {
func (r *Repository) Close() error {
if r != nil && r.repo != nil {
if closer, ok := r.repo.(io.Closer); ok {
closer.Close()
return closer.Close()
}
}
return nil
}

// RepositoryPool holds a pool git repository paths and
Expand Down
34 changes: 34 additions & 0 deletions squash_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"unicode"

"github.com/src-d/go-borges"
"github.com/src-d/go-borges/plain"
Expand All @@ -23,6 +25,38 @@ func TestAllReposIter(t *testing.T) {
require.Len(chainableIterRows(t, ctx, NewAllReposIter(nil)), 2)
}

func TestSquashedTableString(t *testing.T) {
require := require.New(t)
ctx, cleanup := setupIter(t)
defer cleanup()

const expected = `SquashedTable(test)├─Columns│└─Column(repository_id,TEXT,nullable=false)└─Filters└─NOT(1)`

notTrue := expression.NewNot(
expression.NewLiteral(1, sql.Int64),
)

st := &SquashedTable{
iter: NewAllReposIter(notTrue),
tables: []string{"test"},
schemaMappings: nil,
filters: []sql.Expression{notTrue},
indexedTables: nil,
}

str := strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, st.String())
require.EqualValues(expected, str)

rows, err := tableToRows(ctx, st)
require.NoError(err)
require.Empty(rows)
}

func TestSquashContextCancelled(t *testing.T) {
require := require.New(t)
ctx, cleanup := setupIter(t)
Expand Down

0 comments on commit cf01f79

Please sign in to comment.