Skip to content

Commit

Permalink
Add tracker tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 10, 2019
1 parent a51ab61 commit 519c8f6
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cmd/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/cheynewallace/tabby"
"github.com/leanovate/mite-go/domain"
"github.com/spf13/cobra"
"time"
)

var (
Expand Down Expand Up @@ -68,7 +69,7 @@ var trackerStartCommand = &cobra.Command{

t := tabby.New()
t.AddHeader("id", "time", "state", "since")
t.AddLine(tracking.Id, tracking.Minutes, "tracking", tracking.Since)
t.AddLine(tracking.Id, tracking.Minutes, "tracking", tracking.Since.In(time.Local))
if stopped != nil {
t.AddLine(stopped.Id, stopped.Minutes, "stopped")
}
Expand Down
8 changes: 4 additions & 4 deletions mite/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (r *trackerResponse) toTrackingTimeEntry() *domain.TrackingTimeEntry {
return &domain.TrackingTimeEntry{
Id: domain.NewTimeEntryId(r.Tracker.TrackingTimeEntry.Id),
Minutes: domain.NewMinutes(r.Tracker.TrackingTimeEntry.Minutes),
Since: r.Tracker.TrackingTimeEntry.Since,
Since: r.Tracker.TrackingTimeEntry.Since.UTC(),
}
}

Expand All @@ -45,7 +45,7 @@ func (r *trackerResponse) toStoppedTimeEntry() *domain.StoppedTimeEntry {

func (a *api) Tracker() (*domain.TrackingTimeEntry, error) {
tr := trackerResponse{}
err := a.get("/tracker.json", &tr)
err := a.get("tracker.json", &tr)
if err != nil {
return nil, err
}
Expand All @@ -55,7 +55,7 @@ func (a *api) Tracker() (*domain.TrackingTimeEntry, error) {

func (a *api) StartTracker(id domain.TimeEntryId) (*domain.TrackingTimeEntry, *domain.StoppedTimeEntry, error) {
tr := &trackerResponse{}
err := a.patch(fmt.Sprintf("/tracker/%s.json", id), nil, tr)
err := a.patch(fmt.Sprintf("tracker/%s.json", id), nil, tr)
if err != nil {
return nil, nil, err
}
Expand All @@ -65,7 +65,7 @@ func (a *api) StartTracker(id domain.TimeEntryId) (*domain.TrackingTimeEntry, *d

func (a *api) StopTracker(id domain.TimeEntryId) (*domain.StoppedTimeEntry, error) {
tr := &trackerResponse{}
err := a.delete(fmt.Sprintf("/tracker/%s.json", id), tr)
err := a.delete(fmt.Sprintf("tracker/%s.json", id), tr)
if err != nil {
return nil, err
}
Expand Down
215 changes: 215 additions & 0 deletions mite/tracker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package mite_test

import (
"fmt"
"github.com/leanovate/mite-go/domain"
"github.com/leanovate/mite-go/mite"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
"time"
)

const emptyResponse = `{"tracker":{}}`

const trackingTimeEntryResponse = `{
"tracker": {
"tracking_time_entry": {
"id": 36135322,
"minutes": 0,
"since": "2015-10-15T17:33:52+02:00"
}
}
}`

const combinedTimeEntryResponse = `{
"tracker": {
"tracking_time_entry": {
"id": 36135322,
"minutes": 0,
"since": "2015-10-15T17:33:52+02:00"
},
"stopped_time_entry": {
"id": 36134329,
"minutes": 46
}
}
}`

const stoppedTimeEntryResponse = `{
"tracker": {
"stopped_time_entry": {
"id": 36134329,
"minutes": 46
}
}
}`

var trackingTimeEntryObject = domain.TrackingTimeEntry{
Id: domain.NewTimeEntryId(36135322),
Minutes: domain.NewMinutes(0),
Since: time.Date(2015, time.October, 15, 15, 33, 52, 0, time.UTC),
}

var stoppedTimeEntryObject = domain.StoppedTimeEntry{
Id: domain.NewTimeEntryId(36134329),
Minutes: domain.NewMinutes(46),
}

func TestApi_Tracker(t *testing.T) {
// given
rec := recorder{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec.method = r.Method
rec.url = r.RequestURI
rec.miteKey = r.Header.Get("X-MiteApiKey")
rec.userAgent = r.Header.Get("User-Agent")

w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
_, _ = w.Write([]byte(trackingTimeEntryResponse))
}))

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)

// when
tracking, err := api.Tracker()

// then
assert.Nil(t, err)
assert.Equal(t, &trackingTimeEntryObject, tracking)

assert.Equal(t, http.MethodGet, rec.method)
assert.Equal(t, "/tracker.json", rec.url)
assert.Equal(t, testApiKey, rec.miteKey)
assert.Equal(t, testUserAgent, rec.userAgent)
}

func TestApi_Tracker_Empty(t *testing.T) {
// given
rec := recorder{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec.method = r.Method
rec.url = r.RequestURI
rec.miteKey = r.Header.Get("X-MiteApiKey")
rec.userAgent = r.Header.Get("User-Agent")

w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
_, _ = w.Write([]byte(emptyResponse))
}))

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)

// when
tracking, err := api.Tracker()

// then
assert.Nil(t, err)
assert.Nil(t, tracking)

assert.Equal(t, http.MethodGet, rec.method)
assert.Equal(t, "/tracker.json", rec.url)
assert.Equal(t, testApiKey, rec.miteKey)
assert.Equal(t, testUserAgent, rec.userAgent)
}

func TestApi_StartTracker(t *testing.T) {
// given
rec := recorder{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec.method = r.Method
rec.url = r.RequestURI
rec.miteKey = r.Header.Get("X-MiteApiKey")
rec.userAgent = r.Header.Get("User-Agent")

w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
_, _ = w.Write([]byte(trackingTimeEntryResponse))
}))

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)

// when
tracking, stopped, err := api.StartTracker(trackingTimeEntryObject.Id)

// then
assert.Nil(t, err)
assert.Equal(t, &trackingTimeEntryObject, tracking)
assert.Nil(t, stopped)

assert.Equal(t, http.MethodPatch, rec.method)
assert.Equal(t, fmt.Sprintf("/tracker/%s.json", trackingTimeEntryObject.Id), rec.url)
assert.Equal(t, testApiKey, rec.miteKey)
assert.Equal(t, testUserAgent, rec.userAgent)
}

func TestApi_StartTracker_Running(t *testing.T) {
// given
rec := recorder{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec.method = r.Method
rec.url = r.RequestURI
rec.miteKey = r.Header.Get("X-MiteApiKey")
rec.userAgent = r.Header.Get("User-Agent")

w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
_, _ = w.Write([]byte(combinedTimeEntryResponse))
}))

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)

// when
tracking, stopped, err := api.StartTracker(trackingTimeEntryObject.Id)

// then
assert.Nil(t, err)
assert.Equal(t, &trackingTimeEntryObject, tracking)
assert.Equal(t, &stoppedTimeEntryObject, stopped)

assert.Equal(t, http.MethodPatch, rec.method)
assert.Equal(t, fmt.Sprintf("/tracker/%s.json", trackingTimeEntryObject.Id), rec.url)
assert.Equal(t, testApiKey, rec.miteKey)
assert.Equal(t, testUserAgent, rec.userAgent)
}

func TestApi_StopTracker(t *testing.T) {
// given
rec := recorder{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec.method = r.Method
rec.url = r.RequestURI
rec.miteKey = r.Header.Get("X-MiteApiKey")
rec.userAgent = r.Header.Get("User-Agent")

w.Header().Add("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
_, _ = w.Write([]byte(stoppedTimeEntryResponse))
}))

defer srv.Close()

api := mite.NewApi(srv.URL, testApiKey, testClientVersion)

// when
stopped, err := api.StopTracker(stoppedTimeEntryObject.Id)

// then
assert.Nil(t, err)
assert.Equal(t, &stoppedTimeEntryObject, stopped)

assert.Equal(t, http.MethodDelete, rec.method)
assert.Equal(t, fmt.Sprintf("/tracker/%s.json", stoppedTimeEntryObject.Id), rec.url)
assert.Equal(t, testApiKey, rec.miteKey)
assert.Equal(t, testUserAgent, rec.userAgent)
}

0 comments on commit 519c8f6

Please sign in to comment.