add test case that demonstrates issue #2658

This commit is contained in:
Brad Rydzewski 2019-05-20 09:23:26 -07:00
parent 74b62fc08a
commit 440f192730
4 changed files with 99 additions and 1 deletions

View file

@ -21,6 +21,7 @@ import "context"
type Batch struct { type Batch struct {
Insert []*Repository `json:"insert"` Insert []*Repository `json:"insert"`
Update []*Repository `json:"update"` Update []*Repository `json:"update"`
Rename []*Repository `json:"rename"`
Revoke []*Repository `json:"revoke"` Revoke []*Repository `json:"revoke"`
} }

2
go.mod
View file

@ -15,7 +15,7 @@ require (
github.com/docker/distribution v2.7.1+incompatible github.com/docker/distribution v2.7.1+incompatible
github.com/docker/go-connections v0.3.0 github.com/docker/go-connections v0.3.0
github.com/docker/go-units v0.3.3 github.com/docker/go-units v0.3.3
github.com/drone/drone-go v0.0.0-20190217024616-3e8b71333e59 github.com/drone/drone-go v1.0.5-0.20190427184118-618e4496482e
github.com/drone/drone-runtime v1.0.6 github.com/drone/drone-runtime v1.0.6
github.com/drone/drone-ui v0.0.0-20190423061913-b758d7bee2eb github.com/drone/drone-ui v0.0.0-20190423061913-b758d7bee2eb
github.com/drone/drone-yaml v1.0.9-0.20190424150956-115b2ff5f99e github.com/drone/drone-yaml v1.0.9-0.20190424150956-115b2ff5f99e

3
go.sum
View file

@ -32,6 +32,9 @@ github.com/docker/go-units v0.3.3 h1:Xk8S3Xj5sLGlG5g67hJmYMmUgXv5N4PhkjJHHqrwnTk
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/drone/drone-go v0.0.0-20190217024616-3e8b71333e59 h1:tH7rJBME3tKWunUmqcg6NyyXNVX2vbNpJl7p/p/vX88= github.com/drone/drone-go v0.0.0-20190217024616-3e8b71333e59 h1:tH7rJBME3tKWunUmqcg6NyyXNVX2vbNpJl7p/p/vX88=
github.com/drone/drone-go v0.0.0-20190217024616-3e8b71333e59/go.mod h1:qVb1k1w9X5jgoGyLtbnfWNnd4XZfAwokxBmiutbpGqw= github.com/drone/drone-go v0.0.0-20190217024616-3e8b71333e59/go.mod h1:qVb1k1w9X5jgoGyLtbnfWNnd4XZfAwokxBmiutbpGqw=
github.com/drone/drone-go v1.0.4 h1:Yom1lix1Lmk3KmKIsBSQJF1bw0YR2lDGaFQrXxqHMko=
github.com/drone/drone-go v1.0.5-0.20190427184118-618e4496482e h1:Z9dJNcs1IpyS52xsf5vjXMq7vSuBt48Il2dJgOzyCFM=
github.com/drone/drone-go v1.0.5-0.20190427184118-618e4496482e/go.mod h1:GxyeGClYohaKNYJv/ZpsmVHtMJ7WhoT+uDaJNcDIrk4=
github.com/drone/drone-runtime v0.0.0-20190210191445-ad403a0ca24e h1:Eq0QI9lKe6T5pziU/Kes1xX6QKAA6ZfnYvaZZeyY5TU= github.com/drone/drone-runtime v0.0.0-20190210191445-ad403a0ca24e h1:Eq0QI9lKe6T5pziU/Kes1xX6QKAA6ZfnYvaZZeyY5TU=
github.com/drone/drone-runtime v0.0.0-20190210191445-ad403a0ca24e/go.mod h1:I+wJO4yvngCUAro6wKjkMbuPPDI/jRynqU0LTW+8J44= github.com/drone/drone-runtime v0.0.0-20190210191445-ad403a0ca24e/go.mod h1:I+wJO4yvngCUAro6wKjkMbuPPDI/jRynqU0LTW+8J44=
github.com/drone/drone-runtime v1.0.3 h1:0p7ASt0WXbLZRzMOw20e1ahV3YkamRhtZFkm8UvM+JA= github.com/drone/drone-runtime v1.0.3 h1:0p7ASt0WXbLZRzMOw20e1ahV3YkamRhtZFkm8UvM+JA=

View file

@ -43,6 +43,8 @@ func TestBatch(t *testing.T) {
t.Run("Update", testBatchUpdate(batcher, repos, perms, user)) t.Run("Update", testBatchUpdate(batcher, repos, perms, user))
t.Run("Delete", testBatchDelete(batcher, repos, perms, user)) t.Run("Delete", testBatchDelete(batcher, repos, perms, user))
t.Run("DuplicateID", testBatchDuplicateID(batcher, repos, perms, user)) t.Run("DuplicateID", testBatchDuplicateID(batcher, repos, perms, user))
t.Run("DuplicateSlug", testBatchDuplicateSlug(batcher, repos, perms, user))
t.Run("DuplicateRename", testBatchDuplicateRename(batcher, repos, perms, user))
} }
func testBatchInsert( func testBatchInsert(
@ -235,6 +237,98 @@ func testBatchDuplicateID(
} }
} }
// the purpose of this unit test is to understand what happens
// when a repository is deleted, re-created with the same name,
// but has a different unique identifier.
func testBatchDuplicateSlug(
batcher core.Batcher,
repos core.RepositoryStore,
perms core.PermStore,
user *core.User,
) func(t *testing.T) {
return func(t *testing.T) {
_, err := repos.FindName(noContext, "octocat", "hello-world")
if err != nil {
t.Errorf("Want repository, got error %q", err)
}
batch := &core.Batch{
Insert: []*core.Repository{
{
ID: 0,
UserID: 1,
UID: "99", // Updated ID
Namespace: "octocat",
Name: "hello-world",
Slug: "octocat/hello-world",
},
},
}
err = batcher.Batch(noContext, user, batch)
if err != nil {
t.Error(err)
}
}
}
// the purpose of this unit test is to understand what happens
// when a repository is deleted, re-created with a new name, and
// then updated back to the old name.
//
// TODO(bradrydzewski) for sqlite consider UPDATE OR REPLACE.
// TODO(bradrydzewski) for mysql consider UPDATE IGNORE.
// TODO(bradrydzewski) consider breaking rename into a separate set of logic that checks for existing records.
func testBatchDuplicateRename(
batcher core.Batcher,
repos core.RepositoryStore,
perms core.PermStore,
user *core.User,
) func(t *testing.T) {
return func(t *testing.T) {
batch := &core.Batch{
Insert: []*core.Repository{
{
ID: 0,
UserID: 1,
UID: "200",
Namespace: "octocat",
Name: "test-1",
Slug: "octocat/test-1",
},
{
ID: 0,
UserID: 1,
UID: "201",
Namespace: "octocat",
Name: "test-2",
Slug: "octocat/test-2",
},
},
}
err := batcher.Batch(noContext, user, batch)
if err != nil {
t.Error(err)
return
}
before, err := repos.FindName(noContext, "octocat", "test-2")
if err != nil {
t.Errorf("Want repository, got error %q", err)
return
}
before.Name = "test-1"
before.Slug = "octocat/test-1"
batch = &core.Batch{
Update: []*core.Repository{before},
}
err = batcher.Batch(noContext, user, batch)
if err != nil {
t.Skip(err)
}
}
}
func seedUser(db *db.DB) (*core.User, error) { func seedUser(db *db.DB) (*core.User, error) {
out := &core.User{Login: "octocat"} out := &core.User{Login: "octocat"}
err := user.New(db).Create(noContext, out) err := user.New(db).Create(noContext, out)