forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamespaces_test.go
More file actions
119 lines (107 loc) · 3.19 KB
/
namespaces_test.go
File metadata and controls
119 lines (107 loc) · 3.19 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package database
import (
"context"
"reflect"
"testing"
"github.com/sourcegraph/log/logtest"
"github.com/sourcegraph/sourcegraph/internal/database/dbtest"
)
func TestNamespaces(t *testing.T) {
if testing.Short() {
t.Skip()
}
t.Parallel()
logger := logtest.Scoped(t)
db := NewDB(logger, dbtest.NewDB(logger, t))
ctx := context.Background()
// Create user and organization to test lookups.
user, err := db.Users().Create(ctx, NewUser{Username: "alice"})
if err != nil {
t.Fatal(err)
}
org, err := db.Orgs().Create(ctx, "Acme", nil)
if err != nil {
t.Fatal(err)
}
t.Run("GetByID", func(t *testing.T) {
t.Run("no ID", func(t *testing.T) {
ns, err := db.Namespaces().GetByID(ctx, 0, 0)
if ns != nil {
t.Errorf("unexpected non-nil namespace: %v", ns)
}
if want := ErrNamespaceNoID; err != want {
t.Errorf("unexpected error: have=%v want=%v", err, want)
}
})
t.Run("multiple IDs", func(t *testing.T) {
ns, err := db.Namespaces().GetByID(ctx, 123, 456)
if ns != nil {
t.Errorf("unexpected non-nil namespace: %v", ns)
}
if want := ErrNamespaceMultipleIDs; err != want {
t.Errorf("unexpected error: have=%v want=%v", err, want)
}
})
t.Run("user not found", func(t *testing.T) {
ns, err := db.Namespaces().GetByID(ctx, user.ID+1, 0)
if ns != nil {
t.Errorf("unexpected non-nil namespace: %v", ns)
}
if want := ErrNamespaceNotFound; err != want {
t.Errorf("unexpected error: have=%v want=%v", err, want)
}
})
t.Run("organization not found", func(t *testing.T) {
ns, err := db.Namespaces().GetByID(ctx, 0, org.ID+1)
if ns != nil {
t.Errorf("unexpected non-nil namespace: %v", ns)
}
if want := ErrNamespaceNotFound; err != want {
t.Errorf("unexpected error: have=%v want=%v", err, want)
}
})
t.Run("user", func(t *testing.T) {
ns, err := db.Namespaces().GetByID(ctx, 0, user.ID)
if err != nil {
t.Errorf("unexpected non-nil error: %v", err)
}
if want := (&Namespace{Name: "alice", User: user.ID}); !reflect.DeepEqual(ns, want) {
t.Errorf("unexpected namespace: have=%v want=%v", ns, want)
}
})
t.Run("organization", func(t *testing.T) {
ns, err := db.Namespaces().GetByID(ctx, org.ID, 0)
if err != nil {
t.Errorf("unexpected non-nil error: %v", err)
}
if want := (&Namespace{Name: "Acme", Organization: org.ID}); !reflect.DeepEqual(ns, want) {
t.Errorf("unexpected namespace: have=%v want=%v", ns, want)
}
})
})
t.Run("GetByName", func(t *testing.T) {
t.Run("user", func(t *testing.T) {
ns, err := db.Namespaces().GetByName(ctx, "Alice")
if err != nil {
t.Fatal(err)
}
if want := (&Namespace{Name: "alice", User: user.ID}); !reflect.DeepEqual(ns, want) {
t.Errorf("got %+v, want %+v", ns, want)
}
})
t.Run("organization", func(t *testing.T) {
ns, err := db.Namespaces().GetByName(ctx, "acme")
if err != nil {
t.Fatal(err)
}
if want := (&Namespace{Name: "Acme", Organization: org.ID}); !reflect.DeepEqual(ns, want) {
t.Errorf("got %+v, want %+v", ns, want)
}
})
t.Run("not found", func(t *testing.T) {
if _, err := db.Namespaces().GetByName(ctx, "doesntexist"); err != ErrNamespaceNotFound {
t.Fatal(err)
}
})
})
}