Skip to content

Commit

Permalink
Added tests for config models
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed May 27, 2023
1 parent 08b97ec commit d19ec59
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
8 changes: 5 additions & 3 deletions internal/config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"time"

"github.com/evg4b/uncors/internal/helpers"
)

type Response struct {
Expand All @@ -15,7 +17,7 @@ type Response struct {
func (r Response) Clone() Response {
return Response{
Code: r.Code,
Headers: r.Headers,
Headers: helpers.CloneMap(r.Headers),
RawContent: r.RawContent,
File: r.File,
Delay: r.Delay,
Expand All @@ -34,8 +36,8 @@ func (m Mock) Clone() Mock {
return Mock{
Path: m.Path,
Method: m.Method,
Queries: m.Queries,
Headers: m.Headers,
Queries: helpers.CloneMap(m.Queries),
Headers: helpers.CloneMap(m.Headers),
Response: m.Response.Clone(),
}
}
78 changes: 78 additions & 0 deletions internal/config/model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package config_test

import (
"github.com/evg4b/uncors/internal/config"

Check failure on line 4 in internal/config/model_test.go

View workflow job for this annotation

GitHub Actions / build

File is not `goimports`-ed (goimports)
"github.com/go-http-utils/headers"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
"time"
)

func TestResponseClone(t *testing.T) {
object := config.Response{
Code: http.StatusOK,
Headers: map[string]string{
headers.ContentType: "plain/text",
headers.CacheControl: "none",
},
RawContent: "this is plain text",
File: "~/projects/uncors/response/demo.json",
Delay: time.Hour,
}

actual := object.Clone()

t.Run("not same", func(t *testing.T) {
assert.NotSame(t, &object, &actual)
})

t.Run("equals values", func(t *testing.T) {
assert.EqualValues(t, object, actual)
})

t.Run("not same Headers map", func(t *testing.T) {
assert.NotSame(t, &object.Headers, &actual.Headers)
})
}

func TestMockClone(t *testing.T) {
object := config.Mock{
Path: "/constants",
Method: http.MethodGet,
Queries: map[string]string{
"page": "10",
"size": "50",
},
Headers: map[string]string{
headers.ContentType: "plain/text",
headers.CacheControl: "none",
},
Response: config.Response{
Code: http.StatusOK,
RawContent: `{ "status": "ok" }`,
},
}

actual := object.Clone()

t.Run("not same", func(t *testing.T) {
assert.NotSame(t, &object, &actual)
})

t.Run("equals values", func(t *testing.T) {
assert.EqualValues(t, object, actual)
})

t.Run("not same Headers map", func(t *testing.T) {
assert.NotSame(t, &object.Headers, &actual.Headers)
})

t.Run("not same Queries map", func(t *testing.T) {
assert.NotSame(t, &object.Headers, &actual.Headers)
})

t.Run("not same Response", func(t *testing.T) {
assert.NotSame(t, &object.Response, &actual.Response)
})
}
4 changes: 4 additions & 0 deletions internal/helpers/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package helpers
import "github.com/samber/lo"

func CloneMap[K comparable, V any](data map[K]V) map[K]V {
if data == nil {
return nil
}

cloned := make(map[K]V, len(data))
for key, value := range data {
if cloneable, ok := any(value).(lo.Clonable[V]); ok {
Expand Down
6 changes: 6 additions & 0 deletions internal/helpers/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ func TestCloneMap(t *testing.T) {
assert.NotSame(t, &data, &actual)
assert.EqualValues(t, &data, &actual)
})

t.Run("nil", func(t *testing.T) {
actual := helpers.CloneMap[string, string](nil)

assert.Nil(t, actual)
})
}

func assertClone[K comparable, V any](t *testing.T, data map[K]V) {
Expand Down

0 comments on commit d19ec59

Please sign in to comment.