Skip to content

Commit

Permalink
Add mock package for tests with mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
EnoRage committed Nov 17, 2020
1 parent 966df0a commit 1ca7d1b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.15

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/imroc/req v0.3.0
github.com/kr/pretty v0.1.0 // indirect
github.com/shopspring/decimal v1.2.0
github.com/stretchr/testify v1.6.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/imroc/req v0.3.0 h1:3EioagmlSG+z+KySToa+Ylo3pTFZs+jh3Brl7ngU12U=
github.com/imroc/req v0.3.0/go.mod h1:F+NZ+2EFSo6EFXdeIbpfE9hcC233id70kf0byW97Caw=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down
34 changes: 34 additions & 0 deletions mock/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package mock

import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
)

func ParseJsonFromFilePath(file string, intoStruct interface{}) error {
jsonFile, err := os.Open(file)
if err != nil {
return err
}
defer jsonFile.Close()

byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
return err
}
err = json.Unmarshal(byteValue, &intoStruct)
if err != nil {
return err
}
return nil
}

func CreateMockedAPI(funcsMap map[string]func(http.ResponseWriter, *http.Request)) http.Handler {
r := http.NewServeMux()
for pattern, f := range funcsMap {
r.HandleFunc(pattern, f)
}
return r
}
43 changes: 43 additions & 0 deletions mock/mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package mock

import (
"fmt"
"github.com/imroc/req"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)

func TestCreateMockedAPI(t *testing.T) {
data := make(map[string]func(http.ResponseWriter, *http.Request))
data["/1"] = func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if _, err := fmt.Fprint(w, `{"status": true}`); err != nil {
panic(err)
}
}

server := httptest.NewServer(CreateMockedAPI(data))
defer server.Close()

resp, err := req.Get(server.URL + "/1")
assert.Nil(t, err)
type S struct {
Status bool
}
var s S

assert.Nil(t, resp.ToJSON(&s))
assert.True(t, s.Status)
}

func TestParseJsonFromFilePath(t *testing.T) {
type S struct {
Status bool
}
var s S
err := ParseJsonFromFilePath("test.json", &s)
assert.Nil(t, err)
assert.True(t, s.Status)
}
3 changes: 3 additions & 0 deletions mock/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"status": true
}

0 comments on commit 1ca7d1b

Please sign in to comment.