From 0e14a86510c2f197c0abe013dbf56068ac835dce Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Tue, 24 Dec 2019 08:54:36 +0100 Subject: [PATCH] feat: unit dvstore --- .gitattributes | 3 +- go.mod | 1 + go.sum | 1 + internal/dvcore/run.go | 4 +- internal/dvcore/run_test.go | 94 ++++++++++++++++++ internal/dvstore/query_test.go | 81 +++++++++++++++ internal/dvstore/testdata/.gitkeep | 0 .../testdata/golden.all-depviz-test.pq | Bin 0 -> 11788 bytes ...viz-test_moul-and-moulbot-depviz-test.json | 52 ++++++++++ ...lden.all-depviz-test_moul-depviz-test.json | 31 ++++++ ...n.all-depviz-test_moulbot-depviz-test.json | 22 ++++ ...en.all-depviz-test_theworld-all-flags.json | 14 +++ ...golden.all-depviz-test_theworld-light.json | 18 ++++ ....all-depviz-test_theworld-with-closed.json | 60 +++++++++++ ...z-test_theworld-without-external-deps.json | 18 ++++ ...depviz-test_theworld-without-isolated.json | 14 +++ ....all-depviz-test_theworld-without-prs.json | 52 ++++++++++ .../golden.all-depviz-test_theworld.json | 52 ++++++++++ .../testdata/golden.moul-depviz-test.pq | Bin 0 -> 8102 bytes .../testdata/golden.moulbot-depviz-test.pq | Bin 0 -> 3697 bytes internal/dvstore/testing.go | 89 +++++++++++++++++ internal/dvstore/testing_test.go | 30 ++++++ internal/testutil/doc.go | 1 + internal/testutil/golden.go | 9 ++ internal/testutil/logging.go | 29 ++++++ internal/testutil/skip.go | 14 +++ 26 files changed, 687 insertions(+), 2 deletions(-) create mode 100644 internal/dvcore/run_test.go create mode 100644 internal/dvstore/query_test.go create mode 100644 internal/dvstore/testdata/.gitkeep create mode 100644 internal/dvstore/testdata/golden.all-depviz-test.pq create mode 100644 internal/dvstore/testdata/golden.all-depviz-test_moul-and-moulbot-depviz-test.json create mode 100644 internal/dvstore/testdata/golden.all-depviz-test_moul-depviz-test.json create mode 100644 internal/dvstore/testdata/golden.all-depviz-test_moulbot-depviz-test.json create mode 100644 internal/dvstore/testdata/golden.all-depviz-test_theworld-all-flags.json create mode 100644 internal/dvstore/testdata/golden.all-depviz-test_theworld-light.json create mode 100644 internal/dvstore/testdata/golden.all-depviz-test_theworld-with-closed.json create mode 100644 internal/dvstore/testdata/golden.all-depviz-test_theworld-without-external-deps.json create mode 100644 internal/dvstore/testdata/golden.all-depviz-test_theworld-without-isolated.json create mode 100644 internal/dvstore/testdata/golden.all-depviz-test_theworld-without-prs.json create mode 100644 internal/dvstore/testdata/golden.all-depviz-test_theworld.json create mode 100644 internal/dvstore/testdata/golden.moul-depviz-test.pq create mode 100644 internal/dvstore/testdata/golden.moulbot-depviz-test.pq create mode 100644 internal/dvstore/testing.go create mode 100644 internal/dvstore/testing_test.go create mode 100644 internal/testutil/doc.go create mode 100644 internal/testutil/golden.go create mode 100644 internal/testutil/logging.go create mode 100644 internal/testutil/skip.go diff --git a/.gitattributes b/.gitattributes index ff1fe4ea..0c23166c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,12 +4,13 @@ # Collapse vendored and generated files on GitHub vendor/* linguist-vendored rules.mk linguist-vendored -*/vendor/* linguist-vendored +vendor/* linguist-vendored *.gen.* linguist-generated *.pb.go linguist-generated go.sum linguist-generated go.mod linguist-generated gen.sum linguist-generated +golden.*.* linguist-generated # Reduce conflicts on markdown files *.md merge=union diff --git a/go.mod b/go.mod index dc2c1492..55f78c71 100644 --- a/go.mod +++ b/go.mod @@ -26,6 +26,7 @@ require ( github.com/prometheus/client_model v0.0.0-20191202183732-d1d2010b5bee // indirect github.com/prometheus/procfs v0.0.8 // indirect github.com/rs/cors v1.7.0 + github.com/stretchr/testify v1.4.0 github.com/treastech/logger v0.0.0-20180705232552-e381e9ecf2e3 go.uber.org/atomic v1.5.1 // indirect go.uber.org/multierr v1.4.0 // indirect diff --git a/go.sum b/go.sum index 08637f81..7a6c704a 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,7 @@ github.com/cayleygraph/cayley v0.7.7 h1:z+7xkAbg6bKiXJOtOkEG3zCm2K084sr/aGwFV7xc github.com/cayleygraph/cayley v0.7.7/go.mod h1:VUd+PInYf94/VY41ePeFtFyP99BAs953kFT4N+6F7Ko= github.com/cayleygraph/quad v1.1.0 h1:w1nXAmn+nz07+qlw89dke9LwWkYpeX+OcvfTvGQRBpM= github.com/cayleygraph/quad v1.1.0/go.mod h1:maWODEekEhrO0mdc9h5n/oP7cH1h/OTgqQ2qWbuI9M4= +github.com/cayleygraph/quad v1.2.0 h1:vqf+71ZINP3eSbtaEzpey0HTr9p4M2xHdmVCda8D7+Q= github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= diff --git a/internal/dvcore/run.go b/internal/dvcore/run.go index a4c5d64d..3536b627 100644 --- a/internal/dvcore/run.go +++ b/internal/dvcore/run.go @@ -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) diff --git a/internal/dvcore/run_test.go b/internal/dvcore/run_test.go new file mode 100644 index 00000000..cb950a4d --- /dev/null +++ b/internal/dvcore/run_test.go @@ -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.TestingGoldenDumpPath(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()) + } +} diff --git a/internal/dvstore/query_test.go b/internal/dvstore/query_test.go new file mode 100644 index 00000000..98e3a89c --- /dev/null +++ b/internal/dvstore/query_test.go @@ -0,0 +1,81 @@ +package dvstore + +import ( + "fmt" + "io/ioutil" + "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) + gp := TestingGoldenJSONPath(t, name) + 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 +} diff --git a/internal/dvstore/testdata/.gitkeep b/internal/dvstore/testdata/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/internal/dvstore/testdata/golden.all-depviz-test.pq b/internal/dvstore/testdata/golden.all-depviz-test.pq new file mode 100644 index 0000000000000000000000000000000000000000..1d8214ec5867c860510556fe159d1705e8d95f1c GIT binary patch literal 11788 zcmeHNOKclO7*3j|O*Rb;)3l+JJ_g*@NgHRq>-8fkZ5mqCCVh#vT#+{3alCZB>+Y@- zl@paXfjDqNs>BIV&J{=$3B-j1LINQnE=UNjfGAgl05Q9MWbB=FcN|k$5jWf8S^NLK z@Bin2Okep{Uw>a;-?p*-tZ)!c7G1aO%!}fx?iMR6vw5o|maK}ASg~9nz#&^J%)7VC z7?Ri8+WggZ6WfQu7<|IX7ja3QH>|vBENO5A44KFXN74PukU1lAZob0roWPKu@ef&aRwm! zz`VkClwzJJV;lW>29UpSoo~et143iO!5Ov3?U`Nb0(K`%x5_)f_&doy|FBxGRo$`I0SV)U?*?uWmmUMco8g}!)0u04zf%XQ$_{qZ3rwhDPl=C2)&kxMTt$u z81=6lml!<*M&K}EXR+cEH9vwLAL<5`(e?Jcr+?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!5_)S52LA>y(o#cNJZ=U8uS#*b`$ zoi!=q-0fEOBQJKp>_3ik@5L~p?Cg9sbRDw(qL+bFtdqJPY`g@QU`4{jbd8|S`awOM z-7`~ImN3RnO?M0&+RIiZ`FoEaLU8-)s)d&wGYMQzfQblK#CiqCWHbD@eHK%=y9H)V zkp`lzK*X1Xw_MLM@`6FuGvHL`AWMCO)?`AdVKBsyJHsB<^Y0 zn6B*`V$pKBg_5CPP7!Smys%nlRk9If9J^WYk#3q+n7IT|)*bP=yb~R7C_B#oG!+*G zDxL+IG!?mu@@zuGF)+%lixt#0jBz!WqTtiuM0j;mW%sFp&n757^6?#Z4@-BV*1vaP4U`s$(0^I0#;W z6)WC=Jz@b5z?uhO#Ts4$J8{YcIi+J|5moHsnas%N`~Eo|#eb>f6h&F8<~qe|u-@m~ zXI+dBDes5-Tb4J#121%%`yNL{$Ora8m%sK2M8EA}clVV`f*Ys`tIXu$e$^r2tt&wMl8>7Ab7brI&d-#@{#D2sxZ=YNx2dol_QpDPtjF zxHF}<04tH+J(C?>7w}pEF*&ifIpm+8&wu;z@#nHQ9s0H~{rUTsU(1^*D6XKjG!%_a z3XI%3T7?UVb56NKpnz6-@hjIm7ODZ71E%4WnEX9%oI1ck`XcF3bN|3l9R^|8v+=k0Nvf6B(iQ1@`x1$obg z4k}iBVfJM(vFmF{+@lkeh~a_M(tQYfD<|r@3ftNosBV z*E;J%KGo}z_HRgQ{V*4;Mw-^ELVF!I{pe;eZu$-5$&BNQQBypwYJIBhH?+T=^)q*z zRU^aaK+Cd7&(b(_>4d$XOTzTDgq?7$Sg2rR0j)`cem57;nj~m#kxpP1(5f`(w{rol KN`hXfOYCo@*P3Mj literal 0 HcmV?d00001 diff --git a/internal/dvstore/testing.go b/internal/dvstore/testing.go new file mode 100644 index 00000000..f5d5efd2 --- /dev/null +++ b/internal/dvstore/testing.go @@ -0,0 +1,89 @@ +package dvstore + +import ( + "io/ioutil" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/cayleygraph/cayley" + "github.com/cayleygraph/cayley/graph" + _ "github.com/cayleygraph/cayley/graph/kv/bolt" // required by cayley + "github.com/cayleygraph/quad" + _ "github.com/cayleygraph/quad/gml" // required by cayley + _ "github.com/cayleygraph/quad/graphml" // required by cayley + _ "github.com/cayleygraph/quad/json" // required by cayley + _ "github.com/cayleygraph/quad/jsonld" // required by cayley + _ "github.com/cayleygraph/quad/nquads" // required by cayley + _ "github.com/cayleygraph/quad/pquads" // required by cayley + "github.com/stretchr/testify/assert" +) + +const GoldenFormat = "pquads" + +func TestingGoldenDumpPath(t *testing.T, name string) string { + return filepath.Join("..", "dvstore", "testdata", "golden."+name+".pq") +} + +func TestingGoldenJSONPath(t *testing.T, name string) string { + nameWithoutSlashes := strings.Replace(name, "/", "_", -1) + return filepath.Join("..", "dvstore", "testdata", "golden."+nameWithoutSlashes+".json") +} + +func TestingGoldenStore(t *testing.T, name string) (*cayley.Handle, func()) { + t.Helper() + + store, closeFunc := TestingStore(t) + + gp := TestingGoldenDumpPath(t, name) + f, err := os.Open(gp) + assert.NoError(t, err, name) + defer f.Close() + + qw, err := store.NewQuadWriter() + assert.NoError(t, err, name) + assert.NotNil(t, qw, name) + defer qw.Close() + + format := quad.FormatByName(GoldenFormat) + assert.NotNil(t, format, name) + + qr := format.Reader(f) + assert.NotNil(t, qr, name) + defer qr.Close() + + n, err := quad.CopyBatch(qw, qr, quad.DefaultBatch) + assert.NoError(t, err, name) + assert.Greater(t, n, 0, name) + + return store, closeFunc +} + +func TestingStore(t *testing.T) (*cayley.Handle, func()) { + t.Helper() + + dir, err := ioutil.TempDir("", "depviz") + if !assert.NoError(t, err) { + t.Fatal("create temp dir") + } + + err = graph.InitQuadStore("bolt", dir, nil) + if !assert.NoError(t, err) { + t.Fatal("init quadstore") + } + + store, err := cayley.NewGraph("bolt", dir, nil) + if !assert.NoError(t, err) { + t.Fatal("init cayley") + } + + closeFunc := func() { + if store != nil { + _ = store.Close() + } + _ = os.RemoveAll(dir) + } + + return store, closeFunc +} diff --git a/internal/dvstore/testing_test.go b/internal/dvstore/testing_test.go new file mode 100644 index 00000000..24a9e56b --- /dev/null +++ b/internal/dvstore/testing_test.go @@ -0,0 +1,30 @@ +package dvstore + +import ( + "context" + "testing" + + "github.com/cayleygraph/cayley/schema" + "github.com/stretchr/testify/assert" +) + +var schemaConfig *schema.Config + +func init() { + schemaConfig = Schema() +} + +func TestTestingGoldenStore(t *testing.T) { + store, close := TestingGoldenStore(t, "all-depviz-test") + assert.NotNil(t, store) + defer close() + + ctx := context.Background() + it := store.QuadsAllIterator() + count := 0 + for it.Next(ctx) { + count++ + } + // FIXME: check if contain some specific data + assert.Greater(t, count, 0) +} diff --git a/internal/testutil/doc.go b/internal/testutil/doc.go new file mode 100644 index 00000000..b17ae8ae --- /dev/null +++ b/internal/testutil/doc.go @@ -0,0 +1 @@ +package testutil // import "moul.io/depviz/internal/testutil" diff --git a/internal/testutil/golden.go b/internal/testutil/golden.go new file mode 100644 index 00000000..30453ab8 --- /dev/null +++ b/internal/testutil/golden.go @@ -0,0 +1,9 @@ +package testutil + +import "flag" + +var update = flag.Bool("update", false, "update golden files") + +func UpdateGolden() bool { + return *update +} diff --git a/internal/testutil/logging.go b/internal/testutil/logging.go new file mode 100644 index 00000000..4a56c0f0 --- /dev/null +++ b/internal/testutil/logging.go @@ -0,0 +1,29 @@ +package testutil + +import ( + "flag" + "testing" + + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +var debug = flag.Bool("debug", false, "more verbose logging") + +func Logger(t *testing.T) *zap.Logger { + t.Helper() + if !*debug { + return zap.NewNop() + } + + config := zap.NewDevelopmentConfig() + config.DisableStacktrace = true + config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder + config.Level.SetLevel(zap.DebugLevel) + logger, err := config.Build() + if err != nil { + t.Errorf("debug logger: %v", err) + return zap.NewNop() + } + return logger +} diff --git a/internal/testutil/skip.go b/internal/testutil/skip.go new file mode 100644 index 00000000..09610764 --- /dev/null +++ b/internal/testutil/skip.go @@ -0,0 +1,14 @@ +package testutil + +import ( + "fmt" + "os" + "testing" +) + +func SkipSlow(t *testing.T) { + t.Helper() + if os.Getenv("SKIP_SLOW_TESTS") == "1" { + t.Skip(fmt.Sprintf("slow test skipped")) + } +}