-
Notifications
You must be signed in to change notification settings - Fork 66
/
test.go
90 lines (74 loc) · 2.37 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Package test contains a set of tests to ensure a repo implementation conforms
// to expected behaviors, calling RunRepoTests on a given repo implementation should
// pass all checks in order to properly work with Qri.
// test also has a TestRepo, which uses an in-memory implementation of Repo
// suited for tests that require a repo
package test
import (
"os"
"path/filepath"
"testing"
"github.com/qri-io/qri/repo"
)
// RepoMakerFunc produces a new instance of a repository when called
// the returned cleanup function will be called at the end of each test,
// and can be used to do things like remove temp files
type RepoMakerFunc func(t *testing.T) (r repo.Repo, cleanup func())
// repoTestFunc is a function for testing a repo
type repoTestFunc func(t *testing.T, rm RepoMakerFunc)
func testdataPath(path string) string {
return filepath.Join(os.Getenv("GOPATH"), "/src/github.com/qri-io/qri/repo/test/testdata", path)
}
// RunRepoTests tests that this repo conforms to expected behaviors
func RunRepoTests(t *testing.T, rmf RepoMakerFunc) {
tests := map[string]repoTestFunc{
"testProfile": testProfile,
"testRefSelector": testRefSelector,
"testRefstoreInvalidRefs": testRefstoreInvalidRefs,
"testRefstoreRefs": testRefstoreRefs,
"testRefstore": testRefstoreMain,
"testProfileStore": testProfileStore,
}
for key, test := range tests {
t.Run(key, func(t *testing.T) {
test(t, rmf)
})
}
}
func testProfile(t *testing.T, rmf RepoMakerFunc) {
r, cleanup := rmf(t)
defer cleanup()
p, err := r.Profile()
if err != nil {
t.Errorf("%s", string(p.ID))
t.Errorf("Unexpected Profile error: %s", err.Error())
return
}
err = r.SetProfile(p)
if err != nil {
t.Errorf("Unexpected SetProfile error: %s", err.Error())
return
}
}
func testRefSelector(t *testing.T, rmf RepoMakerFunc) {
r, cleanup := rmf(t)
defer cleanup()
if rs, ok := r.(repo.RefSelector); ok {
sel := []repo.DatasetRef{
{Peername: "foo"},
}
err := rs.SetSelectedRefs(sel)
if err != nil {
t.Errorf("Error setting selection: %s", err)
}
got, err := rs.SelectedRefs()
if len(sel) != len(got) {
t.Errorf("Selected length mismatch. Expected: %d. Got: %d.", len(sel), len(got))
}
for i, a := range sel {
if err := repo.CompareDatasetRef(a, got[i]); err != nil {
t.Errorf("comparing selected reference %d: %s", i, err)
}
}
}
}