Skip to content

Commit

Permalink
entries: added feature description & test implementation
Browse files Browse the repository at this point in the history
Also fixed some bugs in the process
  • Loading branch information
phiros committed Apr 18, 2019
1 parent 77c2103 commit 446a5cd
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func projectAndServiceId() (projectId, servicesId string) {
createServiceId = application.Conf.Get("serviceId")
}

return projectId, servicesId
return createProjectId, createServiceId
}

var entriesEditCommand = &cobra.Command{
Expand Down Expand Up @@ -271,7 +271,7 @@ var entriesDeleteCommand = &cobra.Command{
Use: "delete",
Short: "deletes a time entry",
RunE: func(cmd *cobra.Command, args []string) error {
entryId, err := domain.ParseTimeEntryId(editTimeEntryId)
entryId, err := domain.ParseTimeEntryId(deleteTimeEntryId)
if err != nil {
return err
}
Expand Down
85 changes: 80 additions & 5 deletions tests/feature_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tests_test

import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
Expand All @@ -12,6 +14,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -54,16 +57,23 @@ func FeatureContext(s *godog.Suite) {
s.Step(`^A local mock server is setup for the http method "([^"]*)" and path "([^"]*)" which returns:$`, c.aLocalMockServerIsSetupForTheHttpMethodAndPathWhichReturns)
s.Step(`^Mite is setup to connect to this mock server$`, c.miteIsSetupToConnectToThisMockServer)
s.Step(`^"([^"]*)" should return the following:$`, c.shouldReturnTheFollowing)
s.Step(`^A local mock server is setup for the http method "([^"]*)" and path "([^"]*)" which expects a body of:$`, c.aLocalMockServerIsSetupForTheHttpMethodAndPathWhichExpectsABodyOf)
s.Step(`^The mock server returns the following if the expectation is met:$`, c.theMockServerReturnsTheFollowingIfTheExpectationIsMet)
}

var opt = godog.Options{
Output: colors.Colored(os.Stdout),
Format: "progress", // can define default values
}

type ReplyGenerator func() string

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

func (c *cmdTest) reset(interface{}, error) {
Expand All @@ -73,8 +83,8 @@ func (c *cmdTest) reset(interface{}, error) {
}
c.executor = executor.Executor(buildDirectory)

if c.mockServer != nil {
c.mockServer.Close()
if c.mockServer.Server != nil {
c.mockServer.Server.Close()
}
}

Expand Down Expand Up @@ -118,7 +128,7 @@ func (c *cmdTest) aLocalMockServerIsSetupForTheHttpMethodAndPathWhichReturns(arg
}
}
handlerFunc := http.HandlerFunc(handler)
c.mockServer = httptest.NewServer(handlerFunc)
c.mockServer.Server = httptest.NewServer(handlerFunc)
return nil
}

Expand All @@ -127,7 +137,7 @@ func (c *cmdTest) miteIsSetupToConnectToThisMockServer() error {
if err != nil {
return err
}
err = c.iExecute(fmt.Sprintf("-c .mite.toml config api.url=%s", c.mockServer.URL))
err = c.iExecute(fmt.Sprintf("-c .mite.toml config api.url=%s", c.mockServer.Server.URL))
if err != nil {
return err
}
Expand All @@ -142,9 +152,74 @@ func (c *cmdTest) shouldReturnTheFollowing(arg1 string, arg2 *gherkin.DocString)
return assertEqual(strings.TrimSpace(arg2.Content), strings.TrimSpace(string(actualOutput)))
}

func (c *cmdTest) aLocalMockServerIsSetupForTheHttpMethodAndPathWhichExpectsABodyOf(method, path string, expectedBody *gherkin.DocString) error {
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
}
body := buf.String()
err = assertEqualJson(strings.TrimSpace(body), strings.TrimSpace(expectedBody.Content))
if err != nil {
w.WriteHeader(400)
return
}

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

func (c *cmdTest) theMockServerReturnsTheFollowingIfTheExpectationIsMet(replyBody *gherkin.DocString) error {
c.mockServer.ReplyGenerator = func() string {
return replyBody.Content
}

return nil
}

func assertEqual(expected, actual string) error {
if strings.Compare(expected, actual) != 0 {
return errors.New(fmt.Sprintf("expected: \n%s\n to equal: \n%s", expected, actual))
}
return nil
}

func assertEqualJson(s1, s2 string) error {
var o1 interface{}
var o2 interface{}

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

if !reflect.DeepEqual(o1, o2) {
return errors.New("json not deep equal")
}

return nil
}
53 changes: 53 additions & 0 deletions tests/features/entries.feature
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,56 @@ Feature: entries
-- ----- ---- ---- ------- -------
36159117 Feedback einarbeiten 2015-10-16 15m API v2 Entwurf
"""
Scenario: create entries
Given A local mock server is setup for the http method "POST" and path "/time_entries.json" which expects a body of:
"""
{
"time_entry": {
"date_at": "2015-09-12",
"minutes": 185,
"note": "foo",
"service_id": 243,
"project_id": 123
}
}
"""
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 Mite is setup to connect to this mock server
Then "-c .mite.toml entries create -D 2015-09-12 -d 185m -p 123 -s 243 -n foo" 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:
"""
"""
And Mite is setup to connect to this mock server
Then "-c .mite.toml entries delete -i123" should return the following:
"""
"""


0 comments on commit 446a5cd

Please sign in to comment.