Skip to content

Commit

Permalink
Refactor project and services tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 9, 2019
1 parent 7418522 commit 86ae270
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 44 deletions.
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

0 comments on commit 86ae270

Please sign in to comment.