Verified Commit 1d4d4bb8 authored by SlevinWasAlreadyTaken's avatar SlevinWasAlreadyTaken 🔥
Browse files

feat(patch): add unit testing

parent fb0ef120
......@@ -8,7 +8,7 @@ type Info struct {
Model interface{}
}
// Filter removes from Input fields given in parameters
// Whitelist fields given in parameters from the Info Input member
// it also removes elements saved at same position in Output slice.
func (i *Info) Whitelist(whitelistFields []string) {
filteredInput := []string{}
......
package patch
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestWhiteList(t *testing.T) {
info := Info{
Input: []string{"Field1", "Field2", "Field3"},
Output: []string{"field_1", "field_2", "field_3"},
}
info.Whitelist([]string{"Field1", "Field3", "Field4"})
// Field4 should be removed from Input/Output because unknown from the structure
// Field2 should be removed from Input/Output because not in the whitelist
assert.Equal(t, []string{"Field1", "Field3"}, info.Input)
assert.Equal(t, []string{"field_1", "field_3"}, info.Output)
}
......@@ -30,7 +30,7 @@ type Map struct {
}
// FromHTTP decode a HTTP request into a ready-to-be-used patch.Map
// Its does define JSON keys found inside request body as input map
// It does define JSON keys found inside request body as input map
// It ignores errors if faced, considering it should be handled outside of this function
// It instantiates later used output map and it does restaure body's io.Reader after consuming it
func FromHTTP(req *http.Request) Map {
......
......@@ -11,9 +11,9 @@ var mapper *Mapper
func init() {
if mapper == nil {
mapper = &Mapper{
inputType: "json",
outputType: "boil",
fieldByTags: make(map[string]Map),
inputType: "json",
outputType: "boil",
modelMaps: make(map[string]Map),
}
}
}
......@@ -29,7 +29,7 @@ type Mapper struct {
outputType string
// model PatchMaps memory storage
fieldByTags map[string]Map
modelMaps map[string]Map
}
func (m *Mapper) loadOrInitMap(model interface{}) Map {
......@@ -42,7 +42,7 @@ func (m *Mapper) loadOrInitMap(model interface{}) Map {
name = t.Name()
// try to load map
modelMap, ok := mapper.fieldByTags[name]
modelMap, ok := mapper.modelMaps[name]
if !ok {
// load map if not existing yet
return mapper.initMap(t, name)
......@@ -70,7 +70,7 @@ func (m *Mapper) initMap(model reflect.Type, name string) Map {
modelMap.output[field.Name] = outputV
}
}
mapper.fieldByTags[name] = modelMap
mapper.modelMaps[name] = modelMap
return modelMap
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment