Skip to content

Commit

Permalink
refactor: Removing some of the duplicates sonarcloud suggest
Browse files Browse the repository at this point in the history
  • Loading branch information
ViBiOh committed Feb 13, 2021
1 parent b14b823 commit 79b0f99
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 117 deletions.
15 changes: 8 additions & 7 deletions pkg/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
)

var (
testEmail = "nobody@localhost"
repositoryName = "vibioh/ketchup"
repositoryVersion = "1.0.0"
repositoryBetaVersion = "1.0.0-beta1"
Expand Down Expand Up @@ -292,12 +293,12 @@ func TestGetKetchupToNotify(t *testing.T) {
{
Pattern: model.DefaultPattern,
Repository: model.NewRepository(1, model.Github, repositoryName).AddVersion(model.DefaultPattern, repositoryVersion).AddVersion("latest", repositoryBetaVersion),
User: model.NewUser(1, "nobody@localhost", authModel.NewUser(0, "")),
User: model.NewUser(1, testEmail, authModel.NewUser(0, "")),
},
{
Pattern: "latest",
Repository: model.NewRepository(1, model.Github, repositoryName).AddVersion(model.DefaultPattern, repositoryVersion).AddVersion("latest", repositoryBetaVersion),
User: model.NewUser(1, "nobody@localhost", authModel.NewUser(0, "")),
User: model.NewUser(1, testEmail, authModel.NewUser(0, "")),
},
{
Pattern: model.DefaultPattern,
Expand Down Expand Up @@ -332,7 +333,7 @@ func TestGetKetchupToNotify(t *testing.T) {
},
Repository: model.NewRepository(1, model.Github, repositoryName).AddVersion(model.DefaultPattern, repositoryVersion).AddVersion("latest", repositoryBetaVersion),
}},
{ID: 1, Email: "nobody@localhost"}: {{
{ID: 1, Email: testEmail}: {{
Pattern: "latest",
Version: semver.Version{
Name: "1.1.0-beta",
Expand Down Expand Up @@ -402,7 +403,7 @@ func TestSendNotification(t *testing.T) {
ketchupToNotify: map[model.User][]model.Release{
{
ID: 1,
Email: "nobody@localhost",
Email: testEmail,
}: {
{
Repository: model.NewRepository(1, model.Github, repositoryName),
Expand All @@ -425,7 +426,7 @@ func TestSendNotification(t *testing.T) {
ketchupToNotify: map[model.User][]model.Release{
{
ID: 1,
Email: "nobody@localhost",
Email: testEmail,
}: {
{
Repository: model.NewRepository(1, model.Github, repositoryName),
Expand All @@ -448,7 +449,7 @@ func TestSendNotification(t *testing.T) {
ketchupToNotify: map[model.User][]model.Release{
{
ID: 1,
Email: "nobody@localhost",
Email: testEmail,
}: {
{
Repository: model.NewRepository(1, model.Github, repositoryName),
Expand All @@ -471,7 +472,7 @@ func TestSendNotification(t *testing.T) {
ketchupToNotify: map[model.User][]model.Release{
{
ID: 1,
Email: "nobody@localhost",
Email: testEmail,
}: {
{
Repository: model.NewRepository(1, model.Github, repositoryName),
Expand Down
23 changes: 14 additions & 9 deletions pkg/semver/pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"testing"
)

var (
stableVersion = "1.0.0"
betaVersion = "1.0.0-beta1"
)

func TestCheck(t *testing.T) {
type args struct {
version Version
Expand All @@ -20,55 +25,55 @@ func TestCheck(t *testing.T) {
"too short",
safeParsePattern("tst"),
args{
version: safeParse("1.0.0"),
version: safeParse(stableVersion),
},
true,
},
{
"no veersion",
safeParsePattern("^latest"),
args{
version: safeParse("1.0.0"),
version: safeParse(stableVersion),
},
true,
},
{
"latest",
safeParsePattern("latest"),
args{
version: safeParse("1.0.0"),
version: safeParse(stableVersion),
},
true,
},
{
"latest beta",
safeParsePattern("latest"),
args{
version: safeParse("1.0.0-beta1"),
version: safeParse(betaVersion),
},
true,
},
{
"stable",
safeParsePattern("stable"),
args{
version: safeParse("1.0.0"),
version: safeParse(stableVersion),
},
true,
},
{
"stable beta",
safeParsePattern("stable"),
args{
version: safeParse("1.0.0-beta1"),
version: safeParse(betaVersion),
},
false,
},
{
"caret",
safeParsePattern("^1.0"),
args{
version: safeParse("1.0.0"),
version: safeParse(stableVersion),
},
true,
},
Expand Down Expand Up @@ -100,7 +105,7 @@ func TestCheck(t *testing.T) {
"caret no beta",
safeParsePattern("^1.0"),
args{
version: safeParse("1.0.0-beta1"),
version: safeParse(betaVersion),
},
false,
},
Expand Down Expand Up @@ -140,7 +145,7 @@ func TestCheck(t *testing.T) {
"tilde no beta",
safeParsePattern("~1.0"),
args{
version: safeParse("1.0.0-beta1"),
version: safeParse(betaVersion),
},
false,
},
Expand Down
55 changes: 29 additions & 26 deletions pkg/service/ketchup/ketchup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
)

var (
ketchupRepository = "vibioh/ketchup"
viwsRepository = "vibioh/viws"

errAtomicStart = errors.New("invalid context")
errAtomicEnd = errors.New("invalid context")
)
Expand All @@ -36,15 +39,15 @@ func TestList(t *testing.T) {
{
"simple",
New(ketchuptest.New().SetList([]model.Ketchup{
model.NewKetchup(model.DefaultPattern, "1.2.3", model.NewRepository(2, model.Github, "vibioh/viws").AddVersion(model.DefaultPattern, "1.2.3")),
model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.2")),
model.NewKetchup(model.DefaultPattern, "1.2.3", model.NewRepository(2, model.Github, viwsRepository).AddVersion(model.DefaultPattern, "1.2.3")),
model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.2")),
}, 2, nil), nil),
args{
page: 1,
},
[]model.Ketchup{
{Pattern: model.DefaultPattern, Version: "1.0.0", Semver: "Patch", Repository: model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.2")},
{Pattern: model.DefaultPattern, Version: "1.2.3", Repository: model.NewRepository(2, model.Github, "vibioh/viws").AddVersion(model.DefaultPattern, "1.2.3")},
{Pattern: model.DefaultPattern, Version: "1.0.0", Semver: "Patch", Repository: model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.2")},
{Pattern: model.DefaultPattern, Version: "1.2.3", Repository: model.NewRepository(2, model.Github, viwsRepository).AddVersion(model.DefaultPattern, "1.2.3")},
},
2,
nil,
Expand Down Expand Up @@ -97,8 +100,8 @@ func TestListForRepositories(t *testing.T) {
{
"simple",
New(ketchuptest.New().SetListByRepositoriesID([]model.Ketchup{
model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.2")),
model.NewKetchup(model.DefaultPattern, "1.2.3", model.NewRepository(2, model.Github, "vibioh/viws").AddVersion(model.DefaultPattern, "1.2.3")),
model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.2")),
model.NewKetchup(model.DefaultPattern, "1.2.3", model.NewRepository(2, model.Github, viwsRepository).AddVersion(model.DefaultPattern, "1.2.3")),
}, nil), nil),
args{
repositories: []model.Repository{
Expand All @@ -107,8 +110,8 @@ func TestListForRepositories(t *testing.T) {
},
},
[]model.Ketchup{
{Pattern: model.DefaultPattern, Version: "1.0.0", Semver: "Patch", Repository: model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.2")},
{Pattern: model.DefaultPattern, Version: "1.2.3", Repository: model.NewRepository(2, model.Github, "vibioh/viws").AddVersion(model.DefaultPattern, "1.2.3")},
{Pattern: model.DefaultPattern, Version: "1.0.0", Semver: "Patch", Repository: model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.2")},
{Pattern: model.DefaultPattern, Version: "1.2.3", Repository: model.NewRepository(2, model.Github, viwsRepository).AddVersion(model.DefaultPattern, "1.2.3")},
},
nil,
},
Expand Down Expand Up @@ -178,7 +181,7 @@ func TestCreate(t *testing.T) {
New(ketchuptest.New(), repositorytest.New()),
args{
ctx: context.Background(),
item: model.NewKetchup(model.DefaultPattern, "", model.NewRepository(1, model.Github, "vibioh/ketchup")),
item: model.NewKetchup(model.DefaultPattern, "", model.NewRepository(1, model.Github, ketchupRepository)),
},
model.NoneKetchup,
httpModel.ErrInvalid,
Expand All @@ -188,19 +191,19 @@ func TestCreate(t *testing.T) {
New(ketchuptest.New().SetCreate(0, errors.New("failed")), repositorytest.New()),
args{
ctx: model.StoreUser(context.Background(), model.NewUser(1, "", authModel.NewUser(0, ""))),
item: model.NewKetchup(model.DefaultPattern, "0.0.0", model.NewRepository(1, model.Github, "vibioh/ketchup")),
item: model.NewKetchup(model.DefaultPattern, "0.0.0", model.NewRepository(1, model.Github, ketchupRepository)),
},
model.NoneKetchup,
httpModel.ErrInternalError,
},
{
"success",
New(ketchuptest.New(), repositorytest.New().SetGetOrCreate(model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0"), nil)),
New(ketchuptest.New(), repositorytest.New().SetGetOrCreate(model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0"), nil)),
args{
ctx: model.StoreUser(context.Background(), model.NewUser(1, "", authModel.NewUser(0, ""))),
item: model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, "vibioh/ketchup")),
item: model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, ketchupRepository)),
},
model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.0.0")),
model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.0.0")),
nil,
},
}
Expand Down Expand Up @@ -259,20 +262,20 @@ func TestUpdate(t *testing.T) {
},
{
"check error",
New(ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "0.9.0", model.NewRepository(1, model.Github, "vibioh/ketchup")), nil), repositorytest.New()),
New(ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "0.9.0", model.NewRepository(1, model.Github, ketchupRepository)), nil), repositorytest.New()),
args{
ctx: context.Background(),
item: model.NewKetchup(model.DefaultPattern, "", model.NewRepository(1, 0, "vibioh/ketchup")),
item: model.NewKetchup(model.DefaultPattern, "", model.NewRepository(1, 0, ketchupRepository)),
},
model.NoneKetchup,
httpModel.ErrInvalid,
},
{
"pattern change error",
New(ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "0.9.0", model.NewRepository(1, model.Github, "vibioh/ketchup")), nil), repositorytest.New().SetGetOrCreate(model.NoneRepository, errors.New("failed"))),
New(ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "0.9.0", model.NewRepository(1, model.Github, ketchupRepository)), nil), repositorytest.New().SetGetOrCreate(model.NoneRepository, errors.New("failed"))),
args{
ctx: model.StoreUser(context.Background(), model.NewUser(1, "", authModel.NewUser(0, ""))),
item: model.NewKetchup("latest", "1.0.0", model.NewRepository(1, 0, "vibioh/ketchup")),
item: model.NewKetchup("latest", "1.0.0", model.NewRepository(1, 0, ketchupRepository)),
},
model.NoneKetchup,
httpModel.ErrInternalError,
Expand All @@ -282,24 +285,24 @@ func TestUpdate(t *testing.T) {
New(ketchuptest.New().SetGetByRepositoryID(model.Ketchup{
Pattern: model.DefaultPattern,
Version: "0.9.0",
Repository: model.NewRepository(1, model.Github, "vibioh/ketchup"),
Repository: model.NewRepository(1, model.Github, ketchupRepository),
User: model.NewUser(1, "", authModel.NewUser(0, "")),
}, nil), repositorytest.New().SetGetOrCreate(model.NewRepository(1, 0, "vibioh/ketchup").AddVersion("latest", "1.0.1"), nil)),
}, nil), repositorytest.New().SetGetOrCreate(model.NewRepository(1, 0, ketchupRepository).AddVersion("latest", "1.0.1"), nil)),
args{
ctx: model.StoreUser(context.Background(), model.NewUser(1, "", authModel.NewUser(0, ""))),
item: model.NewKetchup("latest", "1.0.0", model.NewRepository(1, 0, "vibioh/ketchup")),
item: model.NewKetchup("latest", "1.0.0", model.NewRepository(1, 0, ketchupRepository)),
},
model.Ketchup{
Pattern: "latest",
Version: "1.0.0",
Repository: model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion("latest", "1.0.1"),
Repository: model.NewRepository(1, model.Github, ketchupRepository).AddVersion("latest", "1.0.1"),
User: model.NewUser(1, "", authModel.NewUser(0, "")),
},
nil,
},
{
"update error",
New(ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "0.9.0", model.NewRepository(1, model.Github, "vibioh/ketchup")), nil).SetUpdate(errors.New("failed")), repositorytest.New()),
New(ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "0.9.0", model.NewRepository(1, model.Github, ketchupRepository)), nil).SetUpdate(errors.New("failed")), repositorytest.New()),
args{
ctx: model.StoreUser(context.Background(), model.NewUser(1, "", authModel.NewUser(0, ""))),
item: model.NewKetchup(model.DefaultPattern, "0.0.0", model.NewRepository(2, 0, "")),
Expand All @@ -312,7 +315,7 @@ func TestUpdate(t *testing.T) {
New(ketchuptest.New().SetGetByRepositoryID(model.Ketchup{
Pattern: model.DefaultPattern,
Version: "0.9.0",
Repository: model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.2.3"),
Repository: model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.2.3"),
User: model.NewUser(1, "", authModel.NewUser(0, "")),
}, nil), repositorytest.New()),
args{
Expand All @@ -322,7 +325,7 @@ func TestUpdate(t *testing.T) {
model.Ketchup{
Pattern: model.DefaultPattern,
Version: "1.0.0",
Repository: model.NewRepository(1, model.Github, "vibioh/ketchup").AddVersion(model.DefaultPattern, "1.2.3"),
Repository: model.NewRepository(1, model.Github, ketchupRepository).AddVersion(model.DefaultPattern, "1.2.3"),
User: model.NewUser(1, "", authModel.NewUser(0, "")),
},
nil,
Expand Down Expand Up @@ -496,10 +499,10 @@ func TestCheck(t *testing.T) {
},
{
"create already exists",
app{ketchupStore: ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, "vibioh/ketchup")), nil)},
app{ketchupStore: ketchuptest.New().SetGetByRepositoryID(model.NewKetchup(model.DefaultPattern, "1.0.0", model.NewRepository(1, model.Github, ketchupRepository)), nil)},
args{
ctx: model.StoreUser(context.Background(), model.NewUser(1, "", authModel.NewUser(0, ""))),
new: model.Ketchup{Pattern: model.DefaultPattern, Version: "1.0.0", Repository: model.NewRepository(2, model.Github, "vibioh/ketchup"), User: model.NewUser(1, "", authModel.NewUser(0, ""))},
new: model.Ketchup{Pattern: model.DefaultPattern, Version: "1.0.0", Repository: model.NewRepository(2, model.Github, ketchupRepository), User: model.NewUser(1, "", authModel.NewUser(0, ""))},
},
errors.New("ketchup for vibioh/ketchup already exists"),
},
Expand Down
Loading

0 comments on commit 79b0f99

Please sign in to comment.