Skip to content
Snippets Groups Projects
Commit 25900bf0 authored by Nick Thomas's avatar Nick Thomas
Browse files

Add indexer commit tests

parent abfc5ef0
No related branches found
No related tags found
1 merge request!1Initial implementation of an elasticsearch indexer in Go
Loading
Loading
@@ -13,18 +13,13 @@ import (
"gitlab.com/gitlab-org/es-git-go/indexer"
)
 
const (
sha = "9876543210987654321098765432109876543210"
oid = "0123456789012345678901234567890123456789"
)
func readerFunc(data string, err error) func() (io.ReadCloser, error) {
return func() (io.ReadCloser, error) {
return ioutil.NopCloser(strings.NewReader(data)), err
}
}
 
func validFile(path, content string) *git.File {
func gitFile(path, content string) *git.File {
return &git.File{
Path: path,
Blob: readerFunc(content, nil),
Loading
Loading
@@ -48,7 +43,7 @@ func validBlob(file *git.File, content, language string) *indexer.Blob {
}
 
func TestBuildBlob(t *testing.T) {
file := validFile("foo/bar", "foo")
file := gitFile("foo/bar", "foo")
expected := validBlob(file, "foo", "Text")
 
actual, err := indexer.BuildBlob(file, expected.RepoID, expected.CommitSHA)
Loading
Loading
@@ -73,7 +68,7 @@ func TestBuildBlob(t *testing.T) {
}
 
func TestBuildBlobSkipsLargeBlobs(t *testing.T) {
file := validFile("foo/bar", "foo")
file := gitFile("foo/bar", "foo")
file.Size = 1024*1024 + 1
 
blob, err := indexer.BuildBlob(file, parentID, sha)
Loading
Loading
@@ -82,7 +77,7 @@ func TestBuildBlobSkipsLargeBlobs(t *testing.T) {
}
 
func TestBuildBlobSkipsBinaryBlobs(t *testing.T) {
file := validFile("foo/bar", "foo\x00")
file := gitFile("foo/bar", "foo\x00")
 
blob, err := indexer.BuildBlob(file, parentID, sha)
assert.Equal(t, err, indexer.SkipBinaryBlob)
Loading
Loading
@@ -90,7 +85,7 @@ func TestBuildBlobSkipsBinaryBlobs(t *testing.T) {
}
 
func TestBuildBlobDetectsLanguageByFilename(t *testing.T) {
file := validFile("Makefile.am", "foo")
file := gitFile("Makefile.am", "foo")
blob, err := indexer.BuildBlob(file, parentID, sha)
 
assert.NoError(t, err)
Loading
Loading
@@ -98,7 +93,7 @@ func TestBuildBlobDetectsLanguageByFilename(t *testing.T) {
}
 
func TestBuildBlobDetectsLanguageByExtension(t *testing.T) {
file := validFile("foo.rb", "foo")
file := gitFile("foo.rb", "foo")
blob, err := indexer.BuildBlob(file, parentID, sha)
 
assert.NoError(t, err)
Loading
Loading
Loading
Loading
@@ -20,9 +20,8 @@ func GenerateCommitID(parentID, commitSHA string) string {
return fmt.Sprintf("%s_%s", parentID, commitSHA)
}
 
func (i *Indexer) BuildCommit(c *git.Commit) *Commit {
func BuildCommit(c *git.Commit, parentID string) *Commit {
sha := c.Hash
parentID := i.Submitter.ParentID()
 
return &Commit{
Type: "commit",
Loading
Loading
package indexer_test
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/es-git-go/git"
"gitlab.com/gitlab-org/es-git-go/indexer"
)
func gitCommit(message string) *git.Commit {
return &git.Commit{
Author: git.Signature{
Email: "job@gitlab.com",
Name: "Job van der Voort",
When: time.Date(2016, time.September, 27, 14, 37, 46, 0, time.UTC),
},
Committer: git.Signature{
Email: "nick@gitlab.com",
Name: "Nick Thomas",
When: time.Date(2017, time.October, 28, 15, 38, 47, 1, time.UTC),
},
Message: message,
Hash: sha,
}
}
func validCommit(gitCommit *git.Commit) *indexer.Commit {
return &indexer.Commit{
Type: "commit",
ID: indexer.GenerateCommitID(parentID, gitCommit.Hash),
Author: indexer.BuildPerson(gitCommit.Author),
Committer: indexer.BuildPerson(gitCommit.Committer),
RepoID: parentID,
Message: gitCommit.Message,
SHA: sha,
}
}
func TestBuildCommit(t *testing.T) {
gitCommit := gitCommit("Initial commit")
expected := validCommit(gitCommit)
actual := indexer.BuildCommit(gitCommit, expected.RepoID)
assert.Equal(t, expected, actual)
expectedJSON := `{
"sha" : "` + expected.SHA + `",
"message" : "` + expected.Message + `",
"author" : {
"name": "` + expected.Author.Name + `",
"email": "` + expected.Author.Email + `",
"time": "` + indexer.GenerateDate(gitCommit.Author.When) + `"
},
"committer" : {
"name": "` + expected.Committer.Name + `",
"email": "` + expected.Committer.Email + `",
"time": "` + indexer.GenerateDate(gitCommit.Committer.When) + `"
},
"rid" : "` + expected.RepoID + `",
"type" : "commit"
}`
actualJSON, err := json.Marshal(actual)
assert.NoError(t, err)
assert.JSONEq(t, expectedJSON, string(actualJSON))
}
func TestGenerateCommitID(t *testing.T) {
assert.Equal(t, "projectID_sha", indexer.GenerateCommitID("projectID", "sha"))
}
Loading
Loading
@@ -22,7 +22,7 @@ type Indexer struct {
}
 
func (i *Indexer) SubmitCommit(c *git.Commit) error {
commit := i.BuildCommit(c)
commit := BuildCommit(c, i.Submitter.ParentID())
 
i.Submitter.Index(commit.ID, map[string]interface{}{"commit": commit})
return nil
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment