Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 10, 2019
2 parents f9a0dcb + a51ab61 commit f045d51
Show file tree
Hide file tree
Showing 6 changed files with 361 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A [mite](https://mite.yo.lk/en/) time tracking command line interface.

## Setup

1. Grab a release from https://github.com/leanovate/mite/releases for your operating system and unpack it into your
1. Grab a release from https://github.com/leanovate/mite-go/releases for your operating system and unpack it into your
$PATH (or %PATH% on windows).
2. Make sure that the `mite` command is executable by executing `mite version` in your shell
3. Setup `mite` to use your API key by:
Expand Down
16 changes: 16 additions & 0 deletions mite/api_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
package mite_test

import (
"bytes"
"encoding/json"
)

const testApiKey = "f00bar"
const testClientVersion = "vX"
const testUserAgent = "mite-go/" + testClientVersion + " (+github.com/leanovate/mite-go)"

type recorder struct {
method string
url string
body []byte
contentType string
userAgent string
miteKey string
}

func prettifyJson(b []byte, indent string) []byte {
buf := &bytes.Buffer{}
err := json.Indent(buf, b, "", indent)
if err != nil {
panic(err)
}

return buf.Bytes()
}
65 changes: 36 additions & 29 deletions mite/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,40 @@ import (
)

const projectResponse = `{
"project": {
"id": 643,
"name": "Open-Source",
"note": "valvat, memento et all.",
"customer_id": 291,
"customer_name": "Yolk",
"budget": 0,
"budget_type": "minutes",
"hourly_rate": 6000,
"archived": false,
"active_hourly_rate": "hourly_rate",
"hourly_rates_per_service": [
{
"service_id": 31272,
"hourly_rate": 4500
},
{
"service_id": 149228,
"hourly_rate": 5500
}
],
"created_at": "2011-08-17T12:06:57+02:00",
"updated_at": "2015-02-19T10:53:10+01:00"
}
"project": {
"id": 643,
"name": "Open-Source",
"note": "valvat, memento et all.",
"customer_id": 291,
"customer_name": "Yolk",
"budget": 0,
"budget_type": "minutes",
"hourly_rate": 6000,
"archived": false,
"active_hourly_rate": "hourly_rate",
"hourly_rates_per_service": [
{
"service_id": 31272,
"hourly_rate": 4500
},
{
"service_id": 149228,
"hourly_rate": 5500
}
],
"created_at": "2011-08-17T12:06:57+02:00",
"updated_at": "2015-02-19T10:53:10+01:00"
}
}`

var projectObject = domain.Project{
Id: domain.NewProjectId(643),
Name: "Open-Source",
Note: "valvat, memento et all.",
}

func TestApi_Projects(t *testing.T) {
// given
rec := recorder{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec.method = r.Method
Expand All @@ -47,19 +54,19 @@ func TestApi_Projects(t *testing.T) {

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

defer srv.Close()

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

// when
projects, err := api.Projects()

// then
assert.Nil(t, err)
assert.Equal(t, []*domain.Project{{
Id: domain.NewProjectId(643),
Name: "Open-Source",
Note: "valvat, memento et all."}}, projects)
assert.Equal(t, []*domain.Project{&projectObject}, projects)

assert.Equal(t, http.MethodGet, rec.method)
assert.Equal(t, "/projects.json", rec.url)
Expand Down
37 changes: 22 additions & 15 deletions mite/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@ import (
)

const serviceResponse = `{
"service": {
"id": 38672,
"name": "Coding",
"note": "will code for food",
"hourly_rate": 3300,
"archived": false,
"billable": true,
"created_at": "2009-12-13T12:12:00+01:00",
"updated_at": "2015-12-13T07:20:04+01:00"
}
"service": {
"id": 38672,
"name": "Coding",
"note": "will code for food",
"hourly_rate": 3300,
"archived": false,
"billable": true,
"created_at": "2009-12-13T12:12:00+01:00",
"updated_at": "2015-12-13T07:20:04+01:00"
}
}`

var serviceObject = domain.Service{
Id: domain.NewServiceId(38672),
Name: "Coding",
Note: "will code for food",
}

func TestApi_Services(t *testing.T) {
// given
rec := recorder{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec.method = r.Method
Expand All @@ -33,19 +40,19 @@ func TestApi_Services(t *testing.T) {

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

defer srv.Close()

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

// when
services, err := api.Services()

// then
assert.Nil(t, err)
assert.Equal(t, []*domain.Service{{
Id: domain.NewServiceId(38672),
Name: "Coding",
Note: "will code for food"}}, services)
assert.Equal(t, []*domain.Service{&serviceObject}, services)

assert.Equal(t, http.MethodGet, rec.method)
assert.Equal(t, "/services.json", rec.url)
Expand Down
12 changes: 6 additions & 6 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func (r *timeEntryResponse) toTimeEntry() *domain.TimeEntry {
CustomerName: r.TimeEntry.CustomerName,
ServiceId: domain.NewServiceId(r.TimeEntry.ServiceId),
ServiceName: r.TimeEntry.ServiceName,
CreatedAt: r.TimeEntry.CreatedAt,
UpdatedAt: r.TimeEntry.UpdatedAt,
CreatedAt: r.TimeEntry.CreatedAt.UTC(),
UpdatedAt: r.TimeEntry.UpdatedAt.UTC(),
}
}

Expand All @@ -121,7 +121,7 @@ func (a *api) TimeEntries(query *domain.TimeEntryQuery) ([]*domain.TimeEntry, er

func (a *api) TimeEntry(id domain.TimeEntryId) (*domain.TimeEntry, error) {
ter := timeEntryResponse{}
err := a.get(fmt.Sprintf("/time_entries/%s.json", id), &ter)
err := a.get(fmt.Sprintf("time_entries/%s.json", id), &ter)
if err != nil {
return nil, err
}
Expand All @@ -131,7 +131,7 @@ func (a *api) TimeEntry(id domain.TimeEntryId) (*domain.TimeEntry, error) {

func (a *api) CreateTimeEntry(command *domain.TimeEntryCommand) (*domain.TimeEntry, error) {
ter := timeEntryResponse{}
err := a.post("/time_entries.json", fromCommand(command), &ter)
err := a.post("time_entries.json", fromCommand(command), &ter)
if err != nil {
return nil, err
}
Expand All @@ -140,9 +140,9 @@ func (a *api) CreateTimeEntry(command *domain.TimeEntryCommand) (*domain.TimeEnt
}

func (a *api) EditTimeEntry(id domain.TimeEntryId, command *domain.TimeEntryCommand) error {
return a.patch(fmt.Sprintf("/time_entries/%s.json", id), fromCommand(command), nil)
return a.patch(fmt.Sprintf("time_entries/%s.json", id), fromCommand(command), nil)
}

func (a *api) DeleteTimeEntry(id domain.TimeEntryId) error {
return a.delete(fmt.Sprintf("/time_entries/%s.json", id), nil)
return a.delete(fmt.Sprintf("time_entries/%s.json", id), nil)
}
Loading

0 comments on commit f045d51

Please sign in to comment.