Skip to content

Commit

Permalink
test: Refactor tests to use testify
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed May 3, 2024
1 parent d40779f commit c1674d3
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 69 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/pelletier/go-toml/v2 v2.2.2
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
)

require (
Expand Down Expand Up @@ -42,4 +43,5 @@ require (
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/image v0.15.0 // indirect
golang.org/x/sys v0.17.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
Expand Down
5 changes: 2 additions & 3 deletions internal/nightscout/delta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/gabe565/nightscout-menu-bar/internal/config"
"github.com/stretchr/testify/assert"
)

func TestDelta_Display(t *testing.T) {
Expand Down Expand Up @@ -63,9 +64,7 @@ func TestDelta_Display(t *testing.T) {
Scaled: tt.fields.Scaled,
Times: tt.fields.Times,
}
if got := d.Display(); got != tt.want {
t.Errorf("Display() = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, d.Display())
})
}
}
26 changes: 10 additions & 16 deletions internal/nightscout/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
_ "embed"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"

"github.com/gabe565/nightscout-menu-bar/internal/config"
"github.com/hhsnopek/etag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

//go:embed fetch_test_properties.json
Expand Down Expand Up @@ -41,29 +42,22 @@ func TestFetch(t *testing.T) {
etag string
want *Properties
wantEtag string
wantErr bool
wantErr require.ErrorAssertionFunc
}{
{"no url", "", "", nil, "", true},
{"success", server.URL, "", properties, propertiesEtag, false},
{"same etag", server.URL, propertiesEtag, nil, propertiesEtag, true},
{"different etag", server.URL, differentEtag, properties, propertiesEtag, false},
{"no url", "", "", nil, "", require.Error},
{"success", server.URL, "", properties, propertiesEtag, require.NoError},
{"same etag", server.URL, propertiesEtag, nil, propertiesEtag, require.Error},
{"different etag", server.URL, differentEtag, properties, propertiesEtag, require.NoError},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.Default.URL = tt.url
lastEtag = tt.etag

got, err := Fetch()
if (err != nil) != tt.wantErr {
t.Errorf("Fetch() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Fetch() got = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(lastEtag, tt.wantEtag) {
t.Errorf("Fetch() got etag = %v, want etag %v", lastEtag, tt.wantEtag)
}
tt.wantErr(t, err)
assert.Equal(t, tt.want, got)
assert.Equal(t, tt.wantEtag, lastEtag)
})
}
}
34 changes: 13 additions & 21 deletions internal/nightscout/mills_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package nightscout

import (
"reflect"
"strconv"
"testing"
"time"

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

func TestMills_MarshalJSON(t *testing.T) {
Expand All @@ -20,24 +22,19 @@ func TestMills_MarshalJSON(t *testing.T) {
name string
fields fields
want []byte
wantErr bool
wantErr require.ErrorAssertionFunc
}{
{"0", fields{unix0}, []byte("0"), false},
{"now", fields{now}, []byte(nowStr), false},
{"0", fields{unix0}, []byte("0"), require.NoError},
{"now", fields{now}, []byte(nowStr), require.NoError},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := &Mills{
Time: tt.fields.Time,
}
got, err := m.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MarshalJSON() got = %v, want %v", got, tt.want)
}
tt.wantErr(t, err)
assert.Equal(t, tt.want, got)
})
}
}
Expand All @@ -52,21 +49,16 @@ func TestMills_UnmarshalJSON(t *testing.T) {
name string
args args
want Mills
wantErr bool
wantErr require.ErrorAssertionFunc
}{
{"now", args{[]byte(strconv.Itoa(int(now.UnixMilli())))}, Mills{now}, false},
{"error", args{[]byte("a")}, Mills{time.Time{}}, true},
{"now", args{[]byte(strconv.Itoa(int(now.UnixMilli())))}, Mills{now}, require.NoError},
{"error", args{[]byte("a")}, Mills{time.Time{}}, require.Error},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var m Mills
if err := m.UnmarshalJSON(tt.args.bytes); (err != nil) != tt.wantErr {
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(m, tt.want) {
t.Errorf("UnmarshalJSON() got = %v, want %v", m, tt.want)
}
tt.wantErr(t, m.UnmarshalJSON(tt.args.bytes))
assert.Equal(t, tt.want, m)
})
}
}
6 changes: 3 additions & 3 deletions internal/nightscout/properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package nightscout
import (
"testing"
"time"

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

func TestProperties_String(t *testing.T) {
Expand Down Expand Up @@ -38,9 +40,7 @@ func TestProperties_String(t *testing.T) {
Delta: tt.fields.Delta,
Direction: tt.fields.Direction,
}
if got := p.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, p.String())
})
}
}
26 changes: 10 additions & 16 deletions internal/nightscout/reading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"time"

"github.com/gabe565/nightscout-menu-bar/internal/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestReading_Arrow(t *testing.T) {
Expand Down Expand Up @@ -45,9 +47,7 @@ func TestReading_Arrow(t *testing.T) {
ToMills: tt.fields.ToMills,
Sgvs: tt.fields.Sgvs,
}
if got := r.Arrow(); got != tt.want {
t.Errorf("Arrow() = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, r.Arrow())
})
}
}
Expand Down Expand Up @@ -88,9 +88,7 @@ func TestReading_String(t *testing.T) {
ToMills: tt.fields.ToMills,
Sgvs: tt.fields.Sgvs,
}
if got := r.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, r.String())
})
}
}
Expand Down Expand Up @@ -140,9 +138,7 @@ func TestReading_DisplayBg(t *testing.T) {
ToMills: tt.fields.ToMills,
Sgvs: tt.fields.Sgvs,
}
if got := r.DisplayBg(); got != tt.want {
t.Errorf("DisplayBg() = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, r.DisplayBg())
})
}
}
Expand Down Expand Up @@ -205,25 +201,25 @@ func TestReading_UnmarshalJSON(t *testing.T) {
name string
fields fields
args args
wantErr bool
wantErr require.ErrorAssertionFunc
}{
{
"simple",
fields{},
args{[]byte(fmt.Sprintf(normalReading, now.UnixMilli(), now.UnixMilli()))},
false,
require.NoError,
},
{
"low",
fields{},
args{[]byte(fmt.Sprintf(lowReading, now.UnixMilli()))},
false,
require.NoError,
},
{
"error",
fields{},
args{[]byte("{")},
true,
require.Error,
},
}
for _, tt := range tests {
Expand All @@ -237,9 +233,7 @@ func TestReading_UnmarshalJSON(t *testing.T) {
ToMills: tt.fields.ToMills,
Sgvs: tt.fields.Sgvs,
}
if err := r.UnmarshalJSON(tt.args.bytes); (err != nil) != tt.wantErr {
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
tt.wantErr(t, r.UnmarshalJSON(tt.args.bytes))
})
}
}
10 changes: 6 additions & 4 deletions internal/util/bg_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package util

import "testing"
import (
"testing"

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

func TestToMmol(t *testing.T) {
type args struct {
Expand All @@ -17,9 +21,7 @@ func TestToMmol(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ToMmol(tt.args.mgdl); got != tt.want {
t.Errorf("ToMmol() = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, ToMmol(tt.args.mgdl))
})
}
}
10 changes: 4 additions & 6 deletions internal/util/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package util
import (
"testing"
"time"

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

func TestMinAgo(t *testing.T) {
Expand All @@ -25,9 +27,7 @@ func TestMinAgo(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MinAgo(tt.args.date); got != tt.want {
t.Errorf("MinAgo() = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, MinAgo(tt.args.date))
})
}
}
Expand Down Expand Up @@ -56,9 +56,7 @@ func TestGetNextMinChange(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tt.want = tt.want.Round(time.Second)
got := GetNextMinChange(tt.args.t).Round(time.Second)
if got != tt.want {
t.Errorf("GetNextMinChange() = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit c1674d3

Please sign in to comment.