Skip to content

Commit

Permalink
entries: fixed edit feature description & corresponding test code
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed May 6, 2019
1 parent 016af31 commit f57240a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 40 deletions.
70 changes: 33 additions & 37 deletions tests/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -41,7 +42,8 @@ func TestMain(m *testing.M) {

func FeatureContext(s *godog.Suite) {
c := cmdTest{
executor: executor.Executor(buildDirectory),
executor: executor.Executor(buildDirectory),
mockServer: &MockServer{},
}

s.AfterScenario(c.reset)
Expand All @@ -68,12 +70,13 @@ var opt = godog.Options{

type cmdTest struct {
executor *executor.Config
mockServer MockServer
mockServer *MockServer
}

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

type ReplyGenerator func() string
Expand All @@ -95,6 +98,8 @@ func (c *cmdTest) reset(interface{}, error) {
if c.mockServer.Server != nil {
c.mockServer.Server.Close()
}

c.mockServer = &MockServer{}
}

func (c *cmdTest) anEmptyConfigFileCalled(arg1 string) error {
Expand Down Expand Up @@ -156,8 +161,14 @@ func (c *cmdTest) miteIsSetupToConnectToThisMockServer() error {
func (c *cmdTest) shouldReturnTheFollowing(arg1 string, arg2 *gherkin.DocString) error {
actualOutput, err := c.executor.Execute(arg1)
if err != nil {
exitErr, ok := err.(*exec.ExitError)
if ok {
verboseCmdError := fmt.Sprintf("%s\n%s", exitErr.String(), exitErr.Stderr)
return errors.New(verboseCmdError)
}
return err
}

return assertEqual(strings.TrimSpace(arg2.Content), strings.TrimSpace(string(actualOutput)))
}

Expand All @@ -180,11 +191,21 @@ func (c *cmdTest) aLocalMockServerIsSetupForTheHttpMethodAndPathWhichExpectsABod
panic(err)
}
body := buf.String()
mockHandler := c.mockServer.getMockHandlerFor(r.Method, r.URL.Path, strings.TrimSpace(body))
mockHandler := c.mockServer.nextMockHandler()
if mockHandler == nil {
w.WriteHeader(400)
return
}
err = assertEqual(mockHandler.ExpectedMethod, r.Method)
if err != nil {
panic(err)
}

err = assertEqual(mockHandler.ExpectedPath, r.URL.Path)
if err != nil {
panic(err)
}

err = assertEqualJson(strings.TrimSpace(body), mockHandler.ExpectedBody)
if err != nil {
panic(err)
Expand All @@ -204,15 +225,14 @@ func (c *cmdTest) aLocalMockServerIsSetupForTheHttpMethodAndPathWhichExpectsABod
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
}
func (ms *MockServer) nextMockHandler() *MockHandler {
if len(ms.Handlers)-1 < ms.CurrentMockHandler {
return nil
}
return nil

handler := ms.Handlers[ms.CurrentMockHandler]
ms.CurrentMockHandler++
return handler
}

func (c *cmdTest) theMockServerReturnsTheFollowingIfTheExpectationIsMet(replyBody *gherkin.DocString) error {
Expand Down Expand Up @@ -254,27 +274,3 @@ 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
}
32 changes: 29 additions & 3 deletions tests/features/entries.feature
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,38 @@ Feature: entries
And The mock server returns the following if the expectation is met:
"""
"""
And 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": 200,
"date_at": "2015-09-12",
"note": "bar",
"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 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
id notes date time project service
-- ----- ---- ---- ------- -------
52324 bar 2015-09-12 3h20m 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 f57240a

Please sign in to comment.