forked from nimajalali/go-force
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add detail log when reconnecting and add unit test for mapper
- Loading branch information
1 parent
e9d7528
commit 46ec71f
Showing
4 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
package mapper_test | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"testing" | ||
|
||
"bou.ke/monkey" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/dewisuryani/go-force/common/mapper" | ||
) | ||
|
||
type Foo struct { | ||
A float64 `json:"a"` | ||
B string `json:"b"` | ||
} | ||
|
||
func TestStructToMapString(t *testing.T) { | ||
v := &Foo{ | ||
A: 42, | ||
B: "foo", | ||
} | ||
|
||
t.Run("marshal error", func(t *testing.T) { | ||
errObj := errors.New("foo") | ||
patch1 := monkey.Patch(json.Marshal, func(v interface{}) ([]byte, error) { | ||
return nil, errObj | ||
}) | ||
defer patch1.Unpatch() | ||
|
||
_, err := mapper.StructToMapString(v) | ||
assert.Error(t, err) | ||
assert.Equal(t, err, errObj) | ||
}) | ||
|
||
t.Run("success", func(t *testing.T) { | ||
result, err := mapper.StructToMapString(v) | ||
assert.NoError(t, err) | ||
assert.Equal(t, result["a"], v.A) | ||
assert.Equal(t, result["b"], v.B) | ||
}) | ||
} | ||
|
||
func TestStructToType(t *testing.T) { | ||
v := &Foo{ | ||
A: 42, | ||
B: "foo", | ||
} | ||
d := `[{"A":42, "B":"foo"}]` | ||
|
||
t.Run("marshal error", func(t *testing.T) { | ||
errObj := errors.New("foo") | ||
patch1 := monkey.Patch(json.Marshal, func(v interface{}) ([]byte, error) { | ||
return nil, errObj | ||
}) | ||
defer patch1.Unpatch() | ||
|
||
err := mapper.StructToType(v, d) | ||
assert.Error(t, err) | ||
assert.Equal(t, err, errObj) | ||
}) | ||
|
||
t.Run("unmarshal error", func(t *testing.T) { | ||
err := mapper.StructToType(v, d) | ||
assert.Error(t, err) | ||
}) | ||
} | ||
|
||
func TestMapStringToStruct(t *testing.T) { | ||
d := &Foo{} | ||
v := map[string]interface{}{ | ||
"A": 42, | ||
"B": "foo", | ||
} | ||
|
||
errObj := errors.New("foo") | ||
|
||
t.Run("marshal error", func(t *testing.T) { | ||
patch1 := monkey.Patch(json.Marshal, func(v interface{}) ([]byte, error) { | ||
return nil, errObj | ||
}) | ||
defer patch1.Unpatch() | ||
|
||
err := mapper.MapStringToStruct(v, d) | ||
assert.Error(t, err) | ||
assert.Equal(t, err, errObj) | ||
}) | ||
|
||
t.Run("unmarshal error", func(t *testing.T) { | ||
patch1 := monkey.Patch(json.Unmarshal, func(data []byte, v interface{}) error { | ||
return errObj | ||
}) | ||
defer patch1.Unpatch() | ||
|
||
err := mapper.MapStringToStruct(v, d) | ||
assert.Error(t, err) | ||
assert.Equal(t, err, errObj) | ||
}) | ||
|
||
t.Run("success", func(t *testing.T) { | ||
err := mapper.MapStringToStruct(v, d) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
|
||
func TestStructToArrMapString(t *testing.T) { | ||
v := &Foo{ | ||
A: 42, | ||
B: "foo", | ||
} | ||
|
||
t.Run("marshal error", func(t *testing.T) { | ||
errObj := errors.New("foo") | ||
patch1 := monkey.Patch(json.Marshal, func(v interface{}) ([]byte, error) { | ||
return nil, errObj | ||
}) | ||
defer patch1.Unpatch() | ||
|
||
_, err := mapper.StructToArrMapString(v) | ||
assert.Error(t, err) | ||
assert.Equal(t, err, errObj) | ||
}) | ||
|
||
t.Run("success", func(t *testing.T) { | ||
patch1 := monkey.Patch(json.Unmarshal, func(data []byte, v interface{}) error { | ||
return nil | ||
}) | ||
defer patch1.Unpatch() | ||
|
||
_, err := mapper.StructToArrMapString(v) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
|
||
func TestArrMapStringToStruct(t *testing.T) { | ||
d := &Foo{} | ||
v := []map[string]interface{}{ | ||
{ | ||
"A": 42, | ||
"B": "foo", | ||
}, | ||
} | ||
|
||
errObj := errors.New("foo") | ||
|
||
t.Run("marshal error", func(t *testing.T) { | ||
patch1 := monkey.Patch(json.Marshal, func(v interface{}) ([]byte, error) { | ||
return nil, errObj | ||
}) | ||
defer patch1.Unpatch() | ||
|
||
err := mapper.ArrMapStringToStruct(v, d) | ||
assert.Error(t, err) | ||
assert.Equal(t, err, errObj) | ||
}) | ||
|
||
t.Run("unmarshal error", func(t *testing.T) { | ||
patch1 := monkey.Patch(json.Unmarshal, func(data []byte, v interface{}) error { | ||
return errObj | ||
}) | ||
defer patch1.Unpatch() | ||
|
||
err := mapper.ArrMapStringToStruct(v, d) | ||
assert.Error(t, err) | ||
assert.Equal(t, err, errObj) | ||
}) | ||
|
||
t.Run("success", func(t *testing.T) { | ||
patch1 := monkey.Patch(json.Unmarshal, func(data []byte, v interface{}) error { | ||
return nil | ||
}) | ||
defer patch1.Unpatch() | ||
|
||
err := mapper.ArrMapStringToStruct(v, d) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
|
||
func TestArrStringToMapString(t *testing.T) { | ||
source := []string{"abc", "def"} | ||
|
||
mockResult := map[string]string{ | ||
source[0]: source[0], | ||
source[1]: source[1], | ||
} | ||
|
||
result := mapper.ArrStringToMapString(source) | ||
assert.Equal(t, mockResult, result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters