Skip to content

Commit cf01f79

Browse files
author
kuba--
committed
Improve test coverage
Signed-off-by: kuba-- <[email protected]>
1 parent 787d616 commit cf01f79

5 files changed

+133
-3
lines changed

commit_files_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gitbase
22

33
import (
44
"io"
5+
"strconv"
56
"testing"
67

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

147148
require.ElementsMatch(expected, result)
148149
}
150+
151+
func TestCommitFilesIndexIter(t *testing.T) {
152+
require := require.New(t)
153+
154+
ctx, _, cleanup := setupRepos(t)
155+
defer cleanup()
156+
157+
key := &commitFileIndexKey{
158+
Repository: "zero",
159+
Packfile: plumbing.ZeroHash.String(),
160+
Hash: plumbing.ZeroHash.String(),
161+
Offset: 0,
162+
Name: "two",
163+
Mode: 5,
164+
Tree: plumbing.ZeroHash.String(),
165+
Commit: plumbing.ZeroHash.String(),
166+
}
167+
limit := 10
168+
it := newCommitFilesIndexIter(testIndexValueIter{key, int64(limit)}, poolFromCtx(t, ctx))
169+
for off := 0; off < limit; off++ {
170+
row, err := it.Next()
171+
require.NoError(err)
172+
173+
require.Equal(key.Repository, row[0])
174+
require.Equal(strconv.Itoa(off), row[2])
175+
}
176+
_, err := it.Next()
177+
require.EqualError(err, io.EOF.Error())
178+
}
179+
180+
type testIndexValueIter struct {
181+
key *commitFileIndexKey
182+
limit int64
183+
}
184+
185+
func (it testIndexValueIter) Next() ([]byte, error) {
186+
if it.key.Offset >= it.limit {
187+
return nil, io.EOF
188+
}
189+
190+
it.key.Name = strconv.Itoa(int(it.key.Offset))
191+
val, err := it.key.encode()
192+
if err != nil {
193+
return nil, err
194+
}
195+
val, err = encoder.encode(val)
196+
if err != nil {
197+
return nil, err
198+
}
199+
200+
it.key.Offset++
201+
return val, nil
202+
}
203+
204+
func (it testIndexValueIter) Close() error {
205+
return nil
206+
}

common_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ func buildSession(
5757
if ok {
5858
name := strings.TrimLeft(path, string(os.PathSeparator))
5959
if err := lib.AddPlain(name, path, nil); err == nil {
60-
_, err := pool.GetRepo(name)
60+
r, err := pool.GetRepo(name)
6161
require.NoError(err)
6262
paths = append(paths, pathToName(path))
63+
require.NoError(r.Close())
6364
}
6465
}
6566
}

packfiles_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"path/filepath"
66
"testing"
77

8+
fixtures "github.com/src-d/go-git-fixtures"
89
"github.com/stretchr/testify/require"
910
"gopkg.in/src-d/go-git.v4/plumbing"
11+
"gopkg.in/src-d/go-git.v4/plumbing/cache"
1012
)
1113

1214
var (
@@ -43,6 +45,40 @@ func TestRepositoryPackfiles(t *testing.T) {
4345
require.NotNil(fs)
4446
}
4547

48+
func TestRepositoryPackfilesNoBare(t *testing.T) {
49+
require := require.New(t)
50+
51+
fs := fixtures.ByTag("worktree").One().Worktree()
52+
53+
dotgit, packfiles, err := repositoryPackfiles(fs)
54+
require.NoError(err)
55+
require.Equal([]plumbing.Hash{
56+
plumbing.NewHash("323a4b6b5de684f9966953a043bc800154e5dbfa"),
57+
}, packfiles)
58+
59+
require.NoError(dotgit.Close())
60+
}
61+
62+
func TestGetUnpackedObject(t *testing.T) {
63+
require := require.New(t)
64+
65+
fs := fixtures.ByURL("https://github.com/git-fixtures/submodule.git").One().Worktree()
66+
path := fs.Root()
67+
68+
lib, err := newMultiLibrary()
69+
require.NoError(err)
70+
pool := NewRepositoryPool(cache.NewObjectLRUDefault(), lib)
71+
require.NoError(lib.AddPlain(path, path, nil))
72+
73+
r, err := pool.GetRepo(path)
74+
require.NoError(err)
75+
76+
obj, err := getUnpackedObject(r, plumbing.NewHash("3bf5d30ad4f23cf517676fee232e3bcb8537c1d0"))
77+
require.NoError(err)
78+
require.NotNil(obj)
79+
require.NoError(r.Close())
80+
}
81+
4682
func TestRepositoryIndex(t *testing.T) {
4783
lib, pool, err := newMultiPool()
4884
require.NoError(t, err)

repository_pool.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ func (r *Repository) Cache() cache.Object {
5959
return r.cache
6060
}
6161

62-
func (r *Repository) Close() {
62+
func (r *Repository) Close() error {
6363
if r != nil && r.repo != nil {
6464
if closer, ok := r.repo.(io.Closer); ok {
65-
closer.Close()
65+
return closer.Close()
6666
}
6767
}
68+
return nil
6869
}
6970

7071
// RepositoryPool holds a pool git repository paths and

squash_iterator_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"context"
55
"os"
66
"path/filepath"
7+
"strings"
78
"testing"
9+
"unicode"
810

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

28+
func TestSquashedTableString(t *testing.T) {
29+
require := require.New(t)
30+
ctx, cleanup := setupIter(t)
31+
defer cleanup()
32+
33+
const expected = `SquashedTable(test)├─Columns│└─Column(repository_id,TEXT,nullable=false)└─Filters└─NOT(1)`
34+
35+
notTrue := expression.NewNot(
36+
expression.NewLiteral(1, sql.Int64),
37+
)
38+
39+
st := &SquashedTable{
40+
iter: NewAllReposIter(notTrue),
41+
tables: []string{"test"},
42+
schemaMappings: nil,
43+
filters: []sql.Expression{notTrue},
44+
indexedTables: nil,
45+
}
46+
47+
str := strings.Map(func(r rune) rune {
48+
if unicode.IsSpace(r) {
49+
return -1
50+
}
51+
return r
52+
}, st.String())
53+
require.EqualValues(expected, str)
54+
55+
rows, err := tableToRows(ctx, st)
56+
require.NoError(err)
57+
require.Empty(rows)
58+
}
59+
2660
func TestSquashContextCancelled(t *testing.T) {
2761
require := require.New(t)
2862
ctx, cleanup := setupIter(t)

0 commit comments

Comments
 (0)