Skip to content

Commit

Permalink
Use testify everywhere (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaspoignant authored Apr 1, 2021
1 parent b914fa3 commit c493ecc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 72 deletions.
44 changes: 18 additions & 26 deletions internal/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,15 @@ func Test_FlagCache_yaml(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
fCache := New(NewNotificationService([]notifier.Notifier{}))
err := fCache.UpdateCache(tt.args.loadedFlags, "yaml")
if (err != nil) != tt.wantErr {
t.Errorf("UpdateCache() error = %v, wantErr %v", err, tt.wantErr)
if tt.wantErr {
assert.Error(t, err, "UpdateCache() error = %v, wantErr %v", err, tt.wantErr)
return
}

assert.NoError(t, err, "UpdateCache() error = %v, wantErr %v", err, tt.wantErr)
// If no error we compare with expected
if err == nil {
for key, value := range tt.expected {
got, _ := fCache.GetFlag(key)
assert.Equal(t, value, got)
}
for key, value := range tt.expected {
got, _ := fCache.GetFlag(key)
assert.Equal(t, value, got)
}
fCache.Close()
})
Expand Down Expand Up @@ -146,17 +144,14 @@ func Test_FlagCache_json(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
fCache := New(NewNotificationService([]notifier.Notifier{}))
err := fCache.UpdateCache(tt.args.loadedFlags, "json")
if (err != nil) != tt.wantErr {
t.Errorf("UpdateCache() error = %v, wantErr %v", err, tt.wantErr)
if tt.wantErr {
assert.Error(t, err, "UpdateCache() error = %v, wantErr %v", err, tt.wantErr)
return
}

// If no error we compare with expected
if err == nil {
for key, value := range tt.expected {
got, _ := fCache.GetFlag(key)
assert.Equal(t, value, got)
}
assert.NoError(t, err, "UpdateCache() error = %v, wantErr %v", err, tt.wantErr)
for key, value := range tt.expected {
got, _ := fCache.GetFlag(key)
assert.Equal(t, value, got)
}
fCache.Close()
})
Expand Down Expand Up @@ -215,17 +210,14 @@ default = false`)
t.Run(tt.name, func(t *testing.T) {
fCache := New(NewNotificationService([]notifier.Notifier{}))
err := fCache.UpdateCache(tt.args.loadedFlags, "TOML")
if (err != nil) != tt.wantErr {
t.Errorf("UpdateCache() error = %v, wantErr %v", err, tt.wantErr)
if tt.wantErr {
assert.Error(t, err, "UpdateCache() error = %v, wantErr %v", err, tt.wantErr)
return
}

// If no error we compare with expected
if err == nil {
for key, value := range tt.expected {
got, _ := fCache.GetFlag(key)
assert.Equal(t, value, got)
}
assert.NoError(t, err, "UpdateCache() error = %v, wantErr %v", err, tt.wantErr)
for key, value := range tt.expected {
got, _ := fCache.GetFlag(key)
assert.Equal(t, value, got)
}
fCache.Close()
})
Expand Down
5 changes: 2 additions & 3 deletions internal/model/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,8 @@ func TestFlag_String(t *testing.T) {
False: tt.fields.False,
Default: tt.fields.Default,
}
if got := f.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
got := f.String()
assert.Equal(t, tt.want, got, "String() = %v, want %v", got, tt.want)
})
}
}
10 changes: 4 additions & 6 deletions internal/retriever/retriever_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package retriever_test

import (
"context"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"testing"

"github.com/thomaspoignant/go-feature-flag/internal/retriever"
Expand Down Expand Up @@ -54,13 +54,11 @@ func Test_localRetriever_Retrieve(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
l := retriever.NewLocalRetriever(tt.fields.path)
got, err := l.Retrieve(context.Background())
if (err != nil) != tt.wantErr {
t.Errorf("Retrieve() error = %v, wantErr %v", err, tt.wantErr)
if tt.wantErr {
assert.Error(t, err, "Retrieve() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !cmp.Equal(got, tt.want) {
t.Errorf(cmp.Diff(got, tt.want))
}
assert.Equal(t, tt.want, got)
})
}
}
5 changes: 1 addition & 4 deletions internal/retriever/retriever_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"errors"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -167,9 +166,7 @@ func Test_httpRetriever_Retrieve(t *testing.T) {
}

if !t.Failed() {
if !cmp.Equal(got, tt.want) {
t.Errorf(cmp.Diff(got, tt.want))
}
assert.Equal(t, tt.want, got)
}
})
}
Expand Down
51 changes: 18 additions & 33 deletions variation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ffclient

import (
"errors"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"io/ioutil"
"log"
Expand Down Expand Up @@ -176,13 +175,11 @@ func TestBoolVariation(t *testing.T) {

got, err := BoolVariation(tt.args.flagKey, tt.args.user, tt.args.defaultValue)

if (err != nil) != tt.wantErr {
t.Errorf("BoolVariation() error = %v, wantErr %v", err, tt.wantErr)
if tt.wantErr {
assert.Error(t, err, "BoolVariation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("BoolVariation() got = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, got, "BoolVariation() got = %v, want %v", got, tt.want)

if tt.expectedLog != "" {
content, _ := ioutil.ReadFile(file.Name())
Expand Down Expand Up @@ -338,14 +335,11 @@ func TestFloat64Variation(t *testing.T) {

got, err := Float64Variation(tt.args.flagKey, tt.args.user, tt.args.defaultValue)

if (err != nil) != tt.wantErr {
t.Errorf("Float64Variation() error = %v, wantErr %v", err, tt.wantErr)
if tt.wantErr {
assert.Error(t, err, "Float64Variation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Float64Variation() got = %v, want %v", got, tt.want)
}

assert.Equal(t, tt.want, got, "Float64Variation() got = %v, want %v", got, tt.want)
if tt.expectedLog != "" {
content, _ := ioutil.ReadFile(file.Name())
assert.Regexp(t, tt.expectedLog, string(content))
Expand Down Expand Up @@ -500,14 +494,11 @@ func TestJSONArrayVariation(t *testing.T) {

got, err := JSONArrayVariation(tt.args.flagKey, tt.args.user, tt.args.defaultValue)

if (err != nil) != tt.wantErr {
t.Errorf("JSONArrayVariation() error = %v, wantErr %v", err, tt.wantErr)
if tt.wantErr {
assert.Error(t, err, "JSONArrayVariation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !cmp.Equal(got, tt.want) {
t.Errorf("JSONArrayVariation() got = %v, want %v", got, tt.want)
}

assert.Equal(t, tt.want, got, "JSONArrayVariation() got = %v, want %v", got, tt.want)
if tt.expectedLog != "" {
content, _ := ioutil.ReadFile(file.Name())
assert.Regexp(t, tt.expectedLog, string(content))
Expand Down Expand Up @@ -662,13 +653,11 @@ func TestJSONVariation(t *testing.T) {

got, err := JSONVariation(tt.args.flagKey, tt.args.user, tt.args.defaultValue)

if (err != nil) != tt.wantErr {
t.Errorf("JSONVariation() error = %v, wantErr %v", err, tt.wantErr)
if tt.wantErr {
assert.Error(t, err, "JSONVariation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !cmp.Equal(got, tt.want) {
t.Errorf("JSONVariation() got = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, got, "JSONVariation() got = %v, want %v", got, tt.want)

if tt.expectedLog != "" {
content, _ := ioutil.ReadFile(file.Name())
Expand Down Expand Up @@ -825,13 +814,11 @@ func TestStringVariation(t *testing.T) {
}
got, err := StringVariation(tt.args.flagKey, tt.args.user, tt.args.defaultValue)

if (err != nil) != tt.wantErr {
t.Errorf("StringVariation() error = %v, wantErr %v", err, tt.wantErr)
if tt.wantErr {
assert.Error(t, err, "StringVariation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("StringVariation() got = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, got, "StringVariation() got = %v, want %v", got, tt.want)

if tt.expectedLog != "" {
content, _ := ioutil.ReadFile(file.Name())
Expand Down Expand Up @@ -987,13 +974,11 @@ func TestIntVariation(t *testing.T) {
}
got, err := IntVariation(tt.args.flagKey, tt.args.user, tt.args.defaultValue)

if (err != nil) != tt.wantErr {
t.Errorf("IntVariation() error = %v, wantErr %v", err, tt.wantErr)
if tt.wantErr {
assert.Error(t, err, "IntVariation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("IntVariation() got = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, got, "IntVariation() got = %v, want %v", got, tt.want)

if tt.expectedLog != "" {
content, _ := ioutil.ReadFile(file.Name())
Expand Down

0 comments on commit c493ecc

Please sign in to comment.