Skip to content

Commit

Permalink
add test case that demonstrates issue #2658
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed May 20, 2019
1 parent 74b62fc commit 440f192
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/batch.go
Expand Up @@ -21,6 +21,7 @@ import "context"
type Batch struct {
Insert []*Repository `json:"insert"`
Update []*Repository `json:"update"`
Rename []*Repository `json:"rename"`
Revoke []*Repository `json:"revoke"`
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -15,7 +15,7 @@ require (
github.com/docker/distribution v2.7.1+incompatible
github.com/docker/go-connections v0.3.0
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-ui v0.0.0-20190423061913-b758d7bee2eb
github.com/drone/drone-yaml v1.0.9-0.20190424150956-115b2ff5f99e
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Expand Up @@ -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/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 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/go.mod h1:I+wJO4yvngCUAro6wKjkMbuPPDI/jRynqU0LTW+8J44=
github.com/drone/drone-runtime v1.0.3 h1:0p7ASt0WXbLZRzMOw20e1ahV3YkamRhtZFkm8UvM+JA=
Expand Down
94 changes: 94 additions & 0 deletions store/batch/batch_test.go
Expand Up @@ -43,6 +43,8 @@ func TestBatch(t *testing.T) {
t.Run("Update", testBatchUpdate(batcher, repos, perms, user))
t.Run("Delete", testBatchDelete(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(
Expand Down Expand Up @@ -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) {
out := &core.User{Login: "octocat"}
err := user.New(db).Create(noContext, out)
Expand Down

0 comments on commit 440f192

Please sign in to comment.