Skip to content

Commit

Permalink
test: Improving coverage for model package
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <bob@vibioh.fr>
  • Loading branch information
ViBiOh committed May 5, 2021
1 parent 16fa66d commit 7335f8a
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 10 deletions.
7 changes: 2 additions & 5 deletions pkg/model/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,10 @@ func (a KetchupByPriority) Less(i, j int) bool {
}

if first.Semver == second.Semver {
if first.Repository.Name != second.Repository.Name {
return first.Repository.Name < second.Repository.Name
}

if first.Repository.Name != second.Repository.Name {
if first.Repository.Name == second.Repository.Name {
return first.Repository.Part < second.Repository.Part
}
return first.Repository.Name < second.Repository.Name
}

if first.Semver != "" && second.Semver == "" {
Expand Down
8 changes: 5 additions & 3 deletions pkg/model/ketchup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ func TestKetchupByPriority(t *testing.T) {
{Semver: "Patch", Repository: NewGithubRepository(0, "jkl")},
{Semver: "", Repository: NewGithubRepository(0, "abcd")},
{Semver: "", Repository: NewGithubRepository(0, "defg")},
{Semver: "Patch", Repository: NewHelmRepository(0, "jjl", "def")},
{Semver: "Patch", Repository: NewHelmRepository(0, "jjl", "abc")},
{Semver: "Patch", Repository: NewHelmRepository(0, "jjl", "def")},
},
},
}
Expand Down Expand Up @@ -203,11 +203,13 @@ func TestReleaseByRepositoryID(t *testing.T) {
args{
array: []Release{
NewRelease(NewGithubRepository(10, ""), DefaultPattern, semver.Version{}),
NewRelease(NewGithubRepository(1, ""), DefaultPattern, semver.Version{}),
NewRelease(NewGithubRepository(1, "stable"), DefaultPattern, semver.Version{}),
NewRelease(NewGithubRepository(1, "~1.10"), DefaultPattern, semver.Version{}),
},
},
[]Release{
NewRelease(NewGithubRepository(1, ""), DefaultPattern, semver.Version{}),
NewRelease(NewGithubRepository(1, "stable"), DefaultPattern, semver.Version{}),
NewRelease(NewGithubRepository(1, "~1.10"), DefaultPattern, semver.Version{}),
NewRelease(NewGithubRepository(10, ""), DefaultPattern, semver.Version{}),
},
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/model/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func PreparePatternMatching(patterns []string) (map[string]semver.Version, map[s
}

// CheckPatternsMatching check given version against pattern matching
func CheckPatternsMatching(versions map[string]semver.Version, compiledPatterns map[string]semver.Pattern, version semver.Version) {
func CheckPatternsMatching(versions map[string]semver.Version, patterns map[string]semver.Pattern, version semver.Version) {
for pattern, patternVersion := range versions {
if compiledPatterns[pattern].Check(version) && version.IsGreater(patternVersion) {
if patterns[pattern].Check(version) && version.IsGreater(patternVersion) {
versions[pattern] = version
}
}
Expand Down
97 changes: 97 additions & 0 deletions pkg/model/semver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package model

import (
"fmt"
"reflect"
"testing"

"github.com/ViBiOh/ketchup/pkg/semver"
)

func safeParse(version string) semver.Version {
output, err := semver.Parse(version)
if err != nil {
fmt.Println(err)
}
return output
}

func safeParsePattern(pattern string) semver.Pattern {
output, err := semver.ParsePattern(pattern)
if err != nil {
fmt.Println(err)
}
return output
}

func TestCheckPatternsMatching(t *testing.T) {
type args struct {
versions map[string]semver.Version
compiledPatterns map[string]semver.Pattern
version semver.Version
}

var cases = []struct {
intention string
args args
want map[string]semver.Version
}{
{
"no match",
args{
versions: map[string]semver.Version{},
compiledPatterns: map[string]semver.Pattern{
"stable": safeParsePattern("stable"),
},
version: safeParse("1.2.3"),
},
map[string]semver.Version{},
},
{
"greater",
args{
versions: map[string]semver.Version{
"stable": semver.NoneVersion,
},
compiledPatterns: map[string]semver.Pattern{
"stable": safeParsePattern("stable"),
},
version: safeParse("1.2.3"),
},
map[string]semver.Version{
"stable": safeParse("1.2.3"),
},
},
{
"mutiple match",
args{
versions: map[string]semver.Version{
"^2.0": semver.NoneVersion,
"stable": semver.NoneVersion,
"~1.2": semver.NoneVersion,
},
compiledPatterns: map[string]semver.Pattern{
"stable": safeParsePattern("stable"),
"^2.0": safeParsePattern("^2.0"),
"~1.0": safeParsePattern("~1.0"),
},
version: safeParse("1.2.3"),
},
map[string]semver.Version{
"^2.0": semver.NoneVersion,
"stable": safeParse("1.2.3"),
"~1.2": safeParse("1.2.3"),
},
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
CheckPatternsMatching(tc.args.versions, tc.args.compiledPatterns, tc.args.version)

if !reflect.DeepEqual(tc.args.versions, tc.want) {
t.Errorf("CheckPatternsMatching() = %+v, want %+v", tc.args.versions, tc.want)
}
})
}
}

0 comments on commit 7335f8a

Please sign in to comment.