Skip to content

Commit

Permalink
feat: unit dvstore
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Dec 24, 2019
1 parent 39f69b0 commit 3ae81a6
Show file tree
Hide file tree
Showing 25 changed files with 681 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion internal/dvcore/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ func pullBatches(targets []multipmuri.Entity, h *cayley.Handle, githubToken stri
logger.Warn("failed to get last updated issue", zap.Error(err))

}
ghOpts.Since = &since
if !since.IsZero() && since.Unix() > 0 {
ghOpts.Since = &since
}
}

githubprovider.FetchRepo(ctx, repo, githubToken, out, ghOpts)
Expand Down
94 changes: 94 additions & 0 deletions internal/dvcore/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package dvcore

import (
"bytes"
"io/ioutil"
"os"
"testing"

"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/quad"
"github.com/stretchr/testify/assert"
"moul.io/depviz/internal/dvstore"
"moul.io/depviz/internal/testutil"
"moul.io/multipmuri"
)

func TestPullAndSave(t *testing.T) {
if testing.Short() {
t.Skip("skipping slow test (--short)")
}
githubToken := os.Getenv("GITHUB_TOKEN")
if githubToken == "" {
t.Skip("missing GITHUB_TOKEN")
}
schema := dvstore.Schema()
gitlabToken := ""
logger := testutil.Logger(t)

tests := []struct {
name string
targets []multipmuri.Entity
}{
{
"moul-depviz-test",
[]multipmuri.Entity{
multipmuri.NewGitHubRepo("github.com", "moul", "depviz-test"),
},
},
{
"moulbot-depviz-test",
[]multipmuri.Entity{
multipmuri.NewGitHubRepo("github.com", "moul-bot", "depviz-test"),
},
},
{
"all-depviz-test",
[]multipmuri.Entity{
multipmuri.NewGitHubRepo("github.com", "moul", "depviz-test"),
multipmuri.NewGitHubRepo("github.com", "moul-bot", "depviz-test"),
},
},
}

for _, test := range tests {
store, close := dvstore.TestingStore(t)
defer close()
changed, err := PullAndSave(test.targets, store, schema, githubToken, gitlabToken, false, logger)
assert.NoError(t, err, test.name)
assert.True(t, changed, test.name)
changed, err = PullAndSave(test.targets, store, schema, githubToken, gitlabToken, false, logger)
assert.NoError(t, err, test.name)
assert.False(t, changed, test.name)
changed, err = PullAndSave(test.targets, store, schema, githubToken, gitlabToken, true, logger)
assert.NoError(t, err, test.name)
assert.True(t, changed, test.name)

var b bytes.Buffer
qr := graph.NewQuadStoreReader(store.QuadStore)
assert.NotNil(t, qr, test.name)
defer qr.Close()

format := quad.FormatByName(dvstore.GoldenFormat)
assert.NotNil(t, format, test.name)

qw := format.Writer(&b)
assert.NotNil(t, qw, test.name)
defer qw.Close()

n, err := quad.Copy(qw, qr)
assert.Greater(t, n, 0, test.name)
assert.NoError(t, err, test.name)

gp := dvstore.TestingGoldenPath(t, test.name)
if testutil.UpdateGolden() {
t.Logf("update golden file: %s", gp)
err := ioutil.WriteFile(gp, b.Bytes(), 0644)
assert.NoError(t, err, test.name)
}

g, err := ioutil.ReadFile(gp)
assert.NoError(t, err, test.name)
assert.Equal(t, string(g), b.String())
}
}
83 changes: 83 additions & 0 deletions internal/dvstore/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package dvstore

import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"testing"

_ "github.com/cayleygraph/quad/json"
"github.com/stretchr/testify/assert"
"moul.io/depviz/internal/dvparser"
"moul.io/depviz/internal/testutil"
"moul.io/godev"
"moul.io/multipmuri"
)

func TestLoadTasks(t *testing.T) {
tests := []struct {
golden string
name string
filters LoadTasksFilters
expectedErr error
}{
{"all-depviz-test", "theworld", LoadTasksFilters{TheWorld: true}, nil},
{"all-depviz-test", "theworld-light", LoadTasksFilters{TheWorld: true, WithoutPRs: true, WithClosed: false, WithoutExternalDeps: true}, nil},
{"all-depviz-test", "theworld-with-closed", LoadTasksFilters{TheWorld: true, WithClosed: true}, nil},
{"all-depviz-test", "theworld-without-prs", LoadTasksFilters{TheWorld: true, WithoutPRs: true}, nil},
{"all-depviz-test", "theworld-without-isolated", LoadTasksFilters{TheWorld: true, WithoutIsolated: true}, nil},
{"all-depviz-test", "theworld-without-external-deps", LoadTasksFilters{TheWorld: true, WithoutExternalDeps: true}, nil},
{"all-depviz-test", "theworld-all-flags", LoadTasksFilters{TheWorld: true, WithClosed: true, WithoutPRs: true, WithoutIsolated: true, WithoutExternalDeps: true}, nil},
{"all-depviz-test", "moul-depviz-test", LoadTasksFilters{Targets: parseTargets(t, "moul/depviz-test")}, nil},
{"all-depviz-test", "moulbot-depviz-test", LoadTasksFilters{Targets: parseTargets(t, "moul-bot/depviz-test")}, nil},
{"all-depviz-test", "moul-and-moulbot-depviz-test", LoadTasksFilters{Targets: parseTargets(t, "moul/depviz-test, moul-bot/depviz-test")}, nil},
}
alreadySeen := map[string]bool{}
for _, test := range tests {
name := fmt.Sprintf("%s/%s", test.golden, test.name)
nameWithoutSlashes := strings.Replace(name, "/", "_", -1)
gp := filepath.Join("testdata", nameWithoutSlashes+".load-tasks.golden")
if _, found := alreadySeen[gp]; found {
t.Fatalf("duplicate key: %q (golden files conflict)", gp)
}
alreadySeen[gp] = true

t.Run(name, func(t *testing.T) {
logger := testutil.Logger(t)
store, close := TestingGoldenStore(t, test.golden)
defer close()
tasks, err := LoadTasks(store, schemaConfig, test.filters, logger)
assert.Equal(t, test.expectedErr, err)
if err != nil {
return
}
assert.NotNil(t, tasks)
assert.NoError(t, err)

actual := godev.JSON(test.filters) + "\n"
for _, task := range tasks {
actual += godev.JSON(task) + "\n"
}

if testutil.UpdateGolden() {
t.Logf("update golden file: %s", gp)
err := ioutil.WriteFile(gp, []byte(actual), 0644)
assert.NoError(t, err, name)
}

g, err := ioutil.ReadFile(gp)
assert.NoError(t, err, name)
assert.Equal(t, len(string(g)), len(actual), gp)
})
}
}

func parseTargets(t *testing.T, input string) []multipmuri.Entity {
t.Helper()
targets, err := dvparser.ParseTargets(strings.Split(input, ", "))
if !assert.NoError(t, err) {
t.Fatalf("parse targets: %v", input)
}
return targets
}
Empty file.
Binary file not shown.
Loading

0 comments on commit 3ae81a6

Please sign in to comment.