Skip to content

Commit

Permalink
Remove code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Nov 30, 2024
1 parent b0d78fc commit 19e285e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
7 changes: 4 additions & 3 deletions internal/config/validators/fakedata_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

func TestFakedataNodeValidator(t *testing.T) {
const field = "fake"
const unknownErrorMessage = "'unknown' is not a valid option"

t.Run("valid options", func(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -60,7 +61,7 @@ func TestFakedataNodeValidator(t *testing.T) {
{
name: "unknown fake data type",
value: &fakedata.Node{Type: "unknown"},
err: "'unknown' is not a valid option",
err: unknownErrorMessage,
},
{
name: "unknown fake data type",
Expand Down Expand Up @@ -91,7 +92,7 @@ func TestFakedataNodeValidator(t *testing.T) {
},
Count: 1,
},
err: "'unknown' is not a valid option",
err: unknownErrorMessage,
},
{
name: "object with count property",
Expand Down Expand Up @@ -137,7 +138,7 @@ func TestFakedataNodeValidator(t *testing.T) {
"key": {Type: "unknown"},
},
},
err: "'unknown' is not a valid option",
err: unknownErrorMessage,
},
}
for _, testCase := range tests {
Expand Down
50 changes: 22 additions & 28 deletions internal/helpers/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import (
"github.com/stretchr/testify/assert"
)

type testCase struct {
code int
expected bool
}

func describe(c testCase) string {
return fmt.Sprintf("should return %t for code %d", c.expected, c.code)
}

func TestNormaliseRequest(t *testing.T) {
url, err := urlx.Parse(hosts.Localhost.HTTP())
testutils.CheckNoError(t, err)
Expand Down Expand Up @@ -56,10 +65,7 @@ func TestNormaliseRequest(t *testing.T) {
}

func TestIs1xxCode(t *testing.T) {
cases := []struct {
code int
expected bool
}{
cases := []testCase{
{http.StatusContinue, true},
{http.StatusSwitchingProtocols, true},
{http.StatusOK, false},
Expand All @@ -68,19 +74,16 @@ func TestIs1xxCode(t *testing.T) {
{http.StatusInternalServerError, false},
}

for _, testCase := range cases {
t.Run(fmt.Sprintf("shoul return %t for code %d", testCase.expected, testCase.code), func(t *testing.T) {
actual := helpers.Is1xxCode(testCase.code)
assert.Equal(t, testCase.expected, actual)
for _, c := range cases {
t.Run(describe(c), func(t *testing.T) {
actual := helpers.Is1xxCode(c.code)
assert.Equal(t, c.expected, actual)
})
}
}

func TestIs2xxCode(t *testing.T) {
cases := []struct {
code int
expected bool
}{
cases := []testCase{
{http.StatusOK, true},
{http.StatusCreated, true},
{http.StatusAccepted, true},
Expand All @@ -91,7 +94,7 @@ func TestIs2xxCode(t *testing.T) {
}

for _, c := range cases {
t.Run(fmt.Sprintf("shoul return %t for code %d", c.expected, c.code), func(t *testing.T) {
t.Run(describe(c), func(t *testing.T) {
actual := helpers.Is2xxCode(c.code)

assert.Equal(t, c.expected, actual)
Expand All @@ -100,10 +103,7 @@ func TestIs2xxCode(t *testing.T) {
}

func TestIs3xxCode(t *testing.T) {
cases := []struct {
code int
expected bool
}{
cases := []testCase{
{http.StatusMultipleChoices, true},
{http.StatusMovedPermanently, true},
{http.StatusFound, true},
Expand All @@ -114,7 +114,7 @@ func TestIs3xxCode(t *testing.T) {
}

for _, c := range cases {
t.Run(fmt.Sprintf("shoul return %t for code %d", c.expected, c.code), func(t *testing.T) {
t.Run(describe(c), func(t *testing.T) {
actual := helpers.Is3xxCode(c.code)

assert.Equal(t, c.expected, actual)
Expand All @@ -123,10 +123,7 @@ func TestIs3xxCode(t *testing.T) {
}

func TestIs4xxCode(t *testing.T) {
cases := []struct {
code int
expected bool
}{
cases := []testCase{
{http.StatusBadRequest, true},
{http.StatusUnauthorized, true},
{http.StatusForbidden, true},
Expand All @@ -137,7 +134,7 @@ func TestIs4xxCode(t *testing.T) {
}

for _, c := range cases {
t.Run(fmt.Sprintf("shoul return %t for code %d", c.expected, c.code), func(t *testing.T) {
t.Run(describe(c), func(t *testing.T) {
actual := helpers.Is4xxCode(c.code)

assert.Equal(t, c.expected, actual)
Expand All @@ -146,10 +143,7 @@ func TestIs4xxCode(t *testing.T) {
}

func TestIs5xxCode(t *testing.T) {
cases := []struct {
code int
expected bool
}{
cases := []testCase{
{http.StatusBadRequest, false},
{http.StatusUnauthorized, false},
{http.StatusForbidden, false},
Expand All @@ -162,7 +156,7 @@ func TestIs5xxCode(t *testing.T) {
}

for _, c := range cases {
t.Run(fmt.Sprintf("shoul return %t for code %d", c.expected, c.code), func(t *testing.T) {
t.Run(describe(c), func(t *testing.T) {
actual := helpers.Is5xxCode(c.code)

assert.Equal(t, c.expected, actual)
Expand Down

0 comments on commit 19e285e

Please sign in to comment.