-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from tokopedia/support_multi_stub_file
feat: support multiple stubs in a file
- Loading branch information
Showing
5 changed files
with
200 additions
and
20 deletions.
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
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 |
---|---|---|
@@ -1,15 +1,32 @@ | ||
{ | ||
"service": "Gripmock", | ||
"method": "SayHello", | ||
"input": { | ||
"equals": { | ||
"name": "tokopedia" | ||
[ | ||
{ | ||
"service": "Gripmock", | ||
"method": "SayHello", | ||
"input": { | ||
"equals": { | ||
"name": "tokopedia" | ||
} | ||
}, | ||
"output": { | ||
"data": { | ||
"message": "Hello Tokopedia", | ||
"return_code": 1 | ||
} | ||
} | ||
}, | ||
"output": { | ||
"data": { | ||
"message": "Hello Tokopedia", | ||
"return_code": 1 | ||
{ | ||
"service": "Gripmock", | ||
"method": "SayHello", | ||
"input": { | ||
"equals": { | ||
"name": "world" | ||
} | ||
}, | ||
"output": { | ||
"data": { | ||
"message": "Hello World", | ||
"return_code": 1 | ||
} | ||
} | ||
} | ||
} | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package stub | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_readStubFromFile(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
mock func(service, method string, data []storage) (path string) | ||
service string | ||
method string | ||
data []storage | ||
}{ | ||
{ | ||
name: "single file, single stub", | ||
mock: func(service, method string, data []storage) (path string) { | ||
dir, err := ioutil.TempDir("", "") | ||
require.NoError(t, err) | ||
tempF, err := ioutil.TempFile(dir, "") | ||
require.NoError(t, err) | ||
defer tempF.Close() | ||
|
||
var stubs []Stub | ||
for _, d := range data { | ||
stubs = append(stubs, Stub{ | ||
Service: service, | ||
Method: method, | ||
Input: d.Input, | ||
Output: d.Output, | ||
}) | ||
} | ||
byt, err := json.Marshal(stubs) | ||
require.NoError(t, err) | ||
_, err = tempF.Write(byt) | ||
require.NoError(t, err) | ||
|
||
return dir | ||
}, | ||
service: "user", | ||
method: "getname", | ||
data: []storage{ | ||
{ | ||
Input: Input{Equals: map[string]interface{}{"id": float64(1)}}, | ||
Output: Output{Data: map[string]interface{}{"name": "user1"}}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "single file, multiple stub", | ||
mock: func(service, method string, data []storage) (path string) { | ||
dir, err := ioutil.TempDir("", "") | ||
require.NoError(t, err) | ||
tempF, err := ioutil.TempFile(dir, "") | ||
require.NoError(t, err) | ||
defer tempF.Close() | ||
|
||
var stubs []Stub | ||
for _, d := range data { | ||
stubs = append(stubs, Stub{ | ||
Service: service, | ||
Method: method, | ||
Input: d.Input, | ||
Output: d.Output, | ||
}) | ||
} | ||
byt, err := json.Marshal(stubs) | ||
require.NoError(t, err) | ||
_, err = tempF.Write(byt) | ||
require.NoError(t, err) | ||
|
||
return dir | ||
}, | ||
service: "user", | ||
method: "getname", | ||
data: []storage{ | ||
{ | ||
Input: Input{Equals: map[string]interface{}{"id": float64(1)}}, | ||
Output: Output{Data: map[string]interface{}{"name": "user1"}}, | ||
}, | ||
{ | ||
Input: Input{Equals: map[string]interface{}{"id": float64(2)}}, | ||
Output: Output{Data: map[string]interface{}{"name": "user2"}}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "multiple file, single stub", | ||
mock: func(service, method string, data []storage) (path string) { | ||
dir, err := ioutil.TempDir("", "") | ||
require.NoError(t, err) | ||
|
||
for _, d := range data { | ||
tempF, err := ioutil.TempFile(dir, "") | ||
require.NoError(t, err) | ||
defer tempF.Close() | ||
|
||
stub := Stub{ | ||
Service: service, | ||
Method: method, | ||
Input: d.Input, | ||
Output: d.Output, | ||
} | ||
byt, err := json.Marshal(stub) | ||
require.NoError(t, err) | ||
_, err = tempF.Write(byt) | ||
require.NoError(t, err) | ||
} | ||
|
||
return dir | ||
}, | ||
service: "user", | ||
method: "getname", | ||
data: []storage{ | ||
{ | ||
Input: Input{Equals: map[string]interface{}{"id": float64(1)}}, | ||
Output: Output{Data: map[string]interface{}{"name": "user1"}}, | ||
}, | ||
{ | ||
Input: Input{Equals: map[string]interface{}{"id": float64(2)}}, | ||
Output: Output{Data: map[string]interface{}{"name": "user2"}}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
sm := stubMapping{} | ||
sm.readStubFromFile(tt.mock(tt.service, tt.method, tt.data)) | ||
require.ElementsMatch(t, tt.data, sm[tt.service][tt.method]) | ||
}) | ||
} | ||
} |
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