Skip to content

Commit

Permalink
entries: added feature description for edit sub-command
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed May 6, 2019
1 parent 662ed5c commit 016af31
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 20 deletions.
95 changes: 75 additions & 20 deletions tests/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,23 @@ var opt = godog.Options{
Format: "progress", // can define default values
}

type ReplyGenerator func() string

type cmdTest struct {
executor *executor.Config
mockServer struct {
Server *httptest.Server
ReplyGenerator ReplyGenerator
}
mockServer MockServer
}

type MockServer struct {
Server *httptest.Server
Handlers []*MockHandler
}

type ReplyGenerator func() string

type MockHandler struct {
ExpectedPath string
ExpectedMethod string
ExpectedBody string
ReplyGenerator ReplyGenerator
}

func (c *cmdTest) reset(interface{}, error) {
Expand Down Expand Up @@ -153,43 +162,61 @@ func (c *cmdTest) shouldReturnTheFollowing(arg1 string, arg2 *gherkin.DocString)
}

func (c *cmdTest) aLocalMockServerIsSetupForTheHttpMethodAndPathWhichExpectsABodyOf(method, path string, expectedBody *gherkin.DocString) error {
mockHandler := MockHandler{
ExpectedPath: path,
ExpectedMethod: method,
ExpectedBody: strings.TrimSpace(expectedBody.Content),
ReplyGenerator: func() string {
return ""
},
}

c.mockServer.Handlers = append(c.mockServer.Handlers, &mockHandler)

handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method != method {
w.WriteHeader(400)
return
}
if r.URL.Path != path {
w.WriteHeader(400)
return
}
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(r.Body)
if err != nil {
w.WriteHeader(400)
return
panic(err)
}
body := buf.String()
err = assertEqualJson(strings.TrimSpace(body), strings.TrimSpace(expectedBody.Content))
if err != nil {
mockHandler := c.mockServer.getMockHandlerFor(r.Method, r.URL.Path, strings.TrimSpace(body))
if mockHandler == nil {
w.WriteHeader(400)
return
}
err = assertEqualJson(strings.TrimSpace(body), mockHandler.ExpectedBody)
if err != nil {
panic(err)
}

w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
replyBody := c.mockServer.ReplyGenerator()
replyBody := mockHandler.ReplyGenerator()
_, err = w.Write([]byte(replyBody))
if err != nil {
panic(err)
}
}

handlerFunc := http.HandlerFunc(handler)
c.mockServer.Server = httptest.NewServer(handlerFunc)
return nil
}

func (ms MockServer) getMockHandlerFor(method, path, expectedBody string) *MockHandler {
for _, handler := range ms.Handlers {
if handler.ExpectedMethod == method &&
handler.ExpectedPath == path &&
isEqualJson(handler.ExpectedBody, expectedBody) {
return handler
}
}
return nil
}

func (c *cmdTest) theMockServerReturnsTheFollowingIfTheExpectationIsMet(replyBody *gherkin.DocString) error {
c.mockServer.ReplyGenerator = func() string {
c.mockServer.Handlers[len(c.mockServer.Handlers)-1].ReplyGenerator = func() string {
return replyBody.Content
}

Expand All @@ -207,6 +234,10 @@ func assertEqualJson(s1, s2 string) error {
var o1 interface{}
var o2 interface{}

if len(s1) == 0 && len(s2) == 0 {
return nil
}

var err error
err = json.Unmarshal([]byte(s1), &o1)
if err != nil {
Expand All @@ -223,3 +254,27 @@ func assertEqualJson(s1, s2 string) error {

return nil
}

func isEqualJson(s1, s2 string) bool {
var o1 interface{}
var o2 interface{}

if len(s1) == 0 && len(s2) == 0 {
return true
}

err := json.Unmarshal([]byte(s1), &o1)
if err != nil {
return false
}
err = json.Unmarshal([]byte(s2), &o2)
if err != nil {
return false
}

if !reflect.DeepEqual(o1, o2) {
return false
}

return true
}
49 changes: 49 additions & 0 deletions tests/features/entries.feature
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,55 @@ Feature: entries
-- ----- ---- ---- ------- -------
52324 foo 2015-09-12 3h5m Mite Dokumentation
"""
Scenario: edit entries
Given A local mock server is setup for the http method "GET" and path "/time_entries/52324.json" which expects a body of:
"""
"""
And The mock server returns the following if the expectation is met:
"""
{
"time_entry": {
"id": 52324,
"minutes": 185,
"date_at": "2015-09-12",
"note": "foo",
"billable": true,
"locked": false,
"revenue": null,
"hourly_rate": 0,
"user_id": 211,
"user_name": "Fridolin Frei",
"project_id": 123,
"project_name": "Mite",
"service_id": 243,
"service_name": "Dokumentation",
"created_at": "2015-09-13T18:54:45+02:00",
"updated_at": "2015-09-13T18:54:45+02:00"
}
}
"""
And A local mock server is setup for the http method "PATCH" and path "/time_entries/52324.json" which expects a body of:
"""
{
"time_entry": {
"date_at": "2015-09-12",
"minutes": 200,
"note": "bar",
"service_id": 243,
"project_id": 123
}
}
"""
And The mock server returns the following if the expectation is met:
"""
"""
And Mite is setup to connect to this mock server
Then "-c .mite.toml entries edit -i 52324 -D 2015-09-12 -d 200m -p 123 -s 243 -n bar" should return the following:
"""
id notes date time project service
-- ----- ---- ---- ------- -------
52324 foo 2015-09-12 3h5m Mite Dokumentation
"""
Scenario: delete entries
Given A local mock server is setup for the http method "DELETE" and path "/time_entries/123.json" which returns:
"""
Expand Down

0 comments on commit 016af31

Please sign in to comment.