Skip to content

Commit ffb75b9

Browse files
mcuadrossmola
authored andcommitted
git: change NewDatabase, now accepts a git.Repository, tests now using a fixture (#24)
1 parent 5969a3d commit ffb75b9

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

git/commits.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io"
55

66
"github.com/mvader/gitql/sql"
7+
78
"gopkg.in/src-d/go-git.v4"
89
)
910

git/commits_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,35 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7-
"github.com/mvader/gitql/sql"
7+
"gopkg.in/src-d/go-git.v4"
8+
"gopkg.in/src-d/go-git.v4/fixtures"
89
)
910

1011
func TestCommitsRelation(t *testing.T) {
1112
assert := assert.New(t)
12-
var db sql.Database = NewDatabase("https://github.com/smola/galimatias.git")
13+
14+
f := fixtures.Basic().One()
15+
r, err := git.NewFilesystemRepository(f.DotGit().Base())
16+
assert.Nil(err)
17+
18+
db := NewDatabase("foo", r)
1319
assert.NotNil(db)
20+
1421
relations := db.Relations()
1522
rel, ok := relations[commitsRelationName]
1623
assert.True(ok)
1724
assert.NotNil(rel)
1825
assert.Equal(commitsRelationName, rel.Name())
1926
assert.Equal(0, len(rel.Children()))
27+
2028
iter, err := rel.RowIter()
2129
assert.Nil(err)
2230
assert.NotNil(iter)
31+
2332
row, err := iter.Next()
2433
assert.Nil(err)
2534
assert.NotNil(row)
35+
2636
fields := row.Fields()
2737
assert.NotNil(fields)
2838
assert.IsType("", fields[1])

git/database.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
package git
22

33
import (
4-
"gopkg.in/src-d/go-git.v4"
54
"github.com/mvader/gitql/sql"
5+
6+
"gopkg.in/src-d/go-git.v4"
67
)
78

89
const (
910
commitsRelationName = "commits"
1011
)
1112

1213
type Database struct {
13-
url string
14-
cr sql.PhysicalRelation
14+
name string
15+
cr sql.PhysicalRelation
1516
}
1617

17-
func NewDatabase(url string) sql.Database {
18-
r := git.NewMemoryRepository()
19-
r.Clone(&git.CloneOptions{
20-
URL: url,
21-
})
18+
func NewDatabase(name string, r *git.Repository) sql.Database {
2219
return &Database{
23-
url: url,
24-
cr: newCommitsRelation(r),
20+
name: name,
21+
cr: newCommitsRelation(r),
2522
}
2623
}
2724

2825
func (d Database) Name() string {
29-
return d.url
26+
return d.name
3027
}
3128

3229
func (d Database) Relations() map[string]sql.PhysicalRelation {

git/database_test.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,37 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7-
"github.com/mvader/gitql/sql"
7+
"gopkg.in/src-d/go-git.v4"
8+
"gopkg.in/src-d/go-git.v4/fixtures"
89
)
910

10-
func TestDatabase(t *testing.T) {
11+
func init() {
12+
fixtures.RootFolder = "../../../../gopkg.in/src-d/go-git.v4/fixtures/"
13+
}
14+
15+
func TestDatabaseRelations(t *testing.T) {
1116
assert := assert.New(t)
12-
var db sql.Database = NewDatabase("https://github.com/smola/galimatias.git")
17+
18+
f := fixtures.Basic().One()
19+
r, err := git.NewFilesystemRepository(f.DotGit().Base())
20+
assert.Nil(err)
21+
22+
db := NewDatabase("foo", r)
1323
assert.NotNil(db)
24+
1425
relations := db.Relations()
1526
_, ok := relations[commitsRelationName]
1627
assert.True(ok)
1728
}
29+
30+
func TestDatabaseName(t *testing.T) {
31+
assert := assert.New(t)
32+
33+
f := fixtures.Basic().One()
34+
r, err := git.NewFilesystemRepository(f.DotGit().Base())
35+
assert.Nil(err)
36+
37+
db := NewDatabase("foo", r)
38+
assert.NotNil(db)
39+
assert.Equal(db.Name(), "foo")
40+
}

0 commit comments

Comments
 (0)