Skip to content

Commit

Permalink
chore: test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
crispgm committed Sep 14, 2023
1 parent b1f1971 commit 6193687
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
15 changes: 15 additions & 0 deletions internal/entity/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package entity

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewEvent(t *testing.T) {
p := NewEvent(".", "test", 50)

assert.NotEmpty(t, p.ID)
assert.Equal(t, p.Name, "test")
assert.Equal(t, p.Points, 50)
}
26 changes: 26 additions & 0 deletions internal/util/io_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package util

import (
"fmt"
"math/rand"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCopyFile(t *testing.T) {
path := GetCIPath("../..")
seed := rand.Int()
src := path + "/../README.md"
dst := fmt.Sprintf("/tmp/README.md.%d", seed)
CopyFile(src, dst)
if assert.FileExists(t, dst) {
os.Remove(dst)
}

assert.Panics(t, func() {
CopyFile("REAMDE.md", "/tmp/xxxx/xxxxx")
})
assert.Error(t, CopyFile(src, "/tmp/xxx/xxx"))
}
3 changes: 1 addition & 2 deletions internal/util/md5.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/md5"
"encoding/hex"
"io"
"log"
"os"
)

Expand All @@ -13,7 +12,7 @@ func MD5CheckSum(path string) (string, error) {
h := md5.New()
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
return "", err
}
defer f.Close()
if _, err := io.Copy(h, f); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/util/md5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ func TestMD5(t *testing.T) {
md5, err := MD5CheckSum(path)
assert.NoError(t, err)
assert.Equal(t, "3ba7c806a52baf4efbfb4962f62a36d1", md5)

_, err = MD5CheckSum("/tmp/some/123/3ba7c806a52baf4efbfb4962f62a36d1")
assert.Error(t, err)
}
13 changes: 12 additions & 1 deletion pkg/rating/elo/elo_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package elo

import (
"math"
"testing"

"github.com/crispgm/kicker-cli/pkg/rating"
Expand Down Expand Up @@ -38,12 +39,22 @@ func TestEloScore(t *testing.T) {
},
want: 1020,
},
{
name: "diff rating loss",
args: args{
Ra: 1400,
Rb: 1500,
K: 40,
WinDrawLoss: rating.Loss,
},
want: 1386,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
er := Elo{K: float64(tt.args.K)}
er.InitialScore(float64(tt.args.Ra), float64(tt.args.Rb))
if got := er.Calculate(tt.args.WinDrawLoss); got != tt.want {
if got := er.Calculate(tt.args.WinDrawLoss); math.Round(got) != tt.want {
t.Errorf("Calculate() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit 6193687

Please sign in to comment.