From 440f192730a523c632db8ab40fa5ca13d5584fc0 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Mon, 20 May 2019 09:23:26 -0700 Subject: [PATCH] add test case that demonstrates issue #2658 --- core/batch.go | 1 + go.mod | 2 +- go.sum | 3 ++ store/batch/batch_test.go | 94 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) diff --git a/core/batch.go b/core/batch.go index 62228e50..0303fa0f 100644 --- a/core/batch.go +++ b/core/batch.go @@ -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"` } diff --git a/go.mod b/go.mod index 692235f0..8cb37320 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 18355874..23d737a9 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/store/batch/batch_test.go b/store/batch/batch_test.go index 69aac06e..026ae6db 100644 --- a/store/batch/batch_test.go +++ b/store/batch/batch_test.go @@ -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( @@ -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)