Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for estimates #44

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions harvest/estimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type Estimate struct {
Notes *string `json:"notes,omitempty"`
// The currency code associated with this estimate.
Currency *string `json:"currency,omitempty"`
// The current state of the estimate: draft, sent, accepted, or declined.
State *string `json:"state,omitempty"`
// Date the estimate was issued.
IssueDate *Date `json:"issue_date,omitempty"`
// Date and time the estimate was sent.
Expand Down
228 changes: 228 additions & 0 deletions harvest/estimate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package harvest_test

import (
"context"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/becoded/go-harvest/harvest"
)

func TestEstimateService_List(t *testing.T) {
t.Parallel()

service, mux, teardown := setup(t)
t.Cleanup(teardown)

mux.HandleFunc("/estimates", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{})
testBody(t, r, "estimate/list/body_1.json")
testWriteResponse(t, w, "estimate/list/response_1.json")
})

clientList, _, err := service.Estimate.List(context.Background(), &harvest.EstimateListOptions{})
assert.NoError(t, err)

issueOne := time.Date(
2017, 6, 1, 0, 0, 0, 0, time.UTC)
sentOne := time.Date(
2017, 6, 27, 16, 11, 33, 0, time.UTC)
createdOne := time.Date(
2017, 6, 27, 16, 11, 24, 0, time.UTC)
updatedOne := time.Date(
2017, 6, 27, 16, 13, 56, 0, time.UTC)
issueTwo := time.Date(
2017, 1, 1, 0, 0, 0, 0, time.UTC)
sentTwo := time.Date(
2017, 6, 27, 16, 10, 30, 0, time.UTC)
createdTwo := time.Date(
2017, 6, 27, 16, 9, 33, 0, time.UTC)
updatedTwo := time.Date(
2017, 6, 27, 16, 12, 0o0, 0, time.UTC)
acceptedTwo := time.Date(
2017, 6, 27, 16, 10, 32, 0, time.UTC)

lineItemsOne := []harvest.EstimateLineItem{
{
ID: harvest.Int64(53334195),
Kind: harvest.String("Service"),
Description: harvest.String("Phase 2 of the Online Store"),
Quantity: harvest.Int64(100),
UnitPrice: harvest.Float64(100.0),
Amount: harvest.Float64(10000),
Taxed: harvest.Bool(true),
Taxed2: harvest.Bool(true),
},
}

lineItemsTwo := []harvest.EstimateLineItem{
{
ID: harvest.Int64(57531966),
Kind: harvest.String("Service"),
Description: harvest.String("Phase 1 of the Online Store"),
Quantity: harvest.Int64(1),
UnitPrice: harvest.Float64(20000),
Amount: harvest.Float64(20000),
Taxed: harvest.Bool(true),
Taxed2: harvest.Bool(false),
},
}

want := &harvest.EstimateList{
Estimates: []*harvest.Estimate{
{
ID: harvest.Int64(1439818),
Client: &harvest.Client{
ID: harvest.Int64(5735776),
Name: harvest.String("123 Industries"),
},
LineItems: &lineItemsOne,
Creator: &harvest.User{
ID: harvest.Int64(1782884),
},
ClientKey: harvest.String("13dc088aa7d51ec687f186b146730c3c75dc7423"),
Number: harvest.String("1001"),
PurchaseOrder: harvest.String("5678"),
Amount: harvest.Float64(9630.0),
Tax: harvest.Float64(5.0),
TaxAmount: harvest.Float64(450.0),
Tax2: harvest.Float64(2.0),
Tax2Amount: harvest.Float64(180.0),
Discount: harvest.Float64(10.0),
DiscountAmount: harvest.Float64(1000.0),
Subject: harvest.String("Online Store - Phase 2"),
Notes: harvest.String("Some notes about the estimate"),
Currency: harvest.String("USD"),
State: harvest.String("sent"),
IssueDate: &harvest.Date{
Time: issueOne,
},
SentAt: &sentOne,
CreatedAt: &createdOne,
UpdatedAt: &updatedOne,
}, {
ID: harvest.Int64(1439814),
Client: &harvest.Client{
ID: harvest.Int64(5735776),
Name: harvest.String("123 Industries"),
},
LineItems: &lineItemsTwo,
Creator: &harvest.User{
ID: harvest.Int64(1782884),
},
ClientKey: harvest.String("a5ffaeb30c55776270fcd3992b70332d769f97e7"),
Number: harvest.String("1000"),
PurchaseOrder: harvest.String("1234"),
Amount: harvest.Float64(21000.0),
Tax: harvest.Float64(5.0),
TaxAmount: harvest.Float64(1000.0),
Tax2Amount: harvest.Float64(0.0),
DiscountAmount: harvest.Float64(0.0),
Subject: harvest.String("Online Store - Phase 1"),
Notes: harvest.String("Some notes about the estimate"),
Currency: harvest.String("USD"),
State: harvest.String("accepted"),
IssueDate: &harvest.Date{
Time: issueTwo,
},
SentAt: &sentTwo,
CreatedAt: &createdTwo,
UpdatedAt: &updatedTwo,
AcceptedAt: &acceptedTwo,
},
},
Pagination: harvest.Pagination{
PerPage: harvest.Int(2000),
TotalPages: harvest.Int(1),
TotalEntries: harvest.Int(2),
NextPage: nil,
PreviousPage: nil,
Page: harvest.Int(1),
Links: &harvest.PageLinks{
First: harvest.String("https://api.harvestapp.com/v2/estimates?page=1&per_page=2000"),
Next: nil,
Previous: nil,
Last: harvest.String("https://api.harvestapp.com/v2/estimates?page=1&per_page=2000"),
},
},
}

assert.Equal(t, want, clientList)
}

func TestEstimateService_Get(t *testing.T) {
t.Parallel()

service, mux, teardown := setup(t)
t.Cleanup(teardown)

mux.HandleFunc("/estimates/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{})
testBody(t, r, "estimate/get/body_1.json")
testWriteResponse(t, w, "estimate/get/response_1.json")
})

client, _, err := service.Estimate.Get(context.Background(), 1)
assert.NoError(t, err)

issueOne := time.Date(
2017, 6, 1, 0, 0, 0, 0, time.UTC)
sentOne := time.Date(
2017, 6, 27, 16, 11, 33, 0, time.UTC)
createdOne := time.Date(
2017, 6, 27, 16, 11, 24, 0, time.UTC)
updatedOne := time.Date(
2017, 6, 27, 16, 13, 56, 0, time.UTC)

lineItemsOne := []harvest.EstimateLineItem{
{
ID: harvest.Int64(53334195),
Kind: harvest.String("Service"),
Description: harvest.String("Phase 2 of the Online Store"),
Quantity: harvest.Int64(100),
UnitPrice: harvest.Float64(100.0),
Amount: harvest.Float64(10000),
Taxed: harvest.Bool(true),
Taxed2: harvest.Bool(true),
},
}

want := &harvest.Estimate{
ID: harvest.Int64(1439818),
Client: &harvest.Client{
ID: harvest.Int64(5735776),
Name: harvest.String("123 Industries"),
},
LineItems: &lineItemsOne,
Creator: &harvest.User{
ID: harvest.Int64(1782884),
},
ClientKey: harvest.String("13dc088aa7d51ec687f186b146730c3c75dc7423"),
Number: harvest.String("1001"),
PurchaseOrder: harvest.String("5678"),
Amount: harvest.Float64(9630.0),
Tax: harvest.Float64(5.0),
TaxAmount: harvest.Float64(450.0),
Tax2: harvest.Float64(2.0),
Tax2Amount: harvest.Float64(180.0),
Discount: harvest.Float64(10.0),
DiscountAmount: harvest.Float64(1000.0),
Subject: harvest.String("Online Store - Phase 2"),
Notes: harvest.String("Some notes about the estimate"),
Currency: harvest.String("USD"),
State: harvest.String("sent"),
IssueDate: &harvest.Date{
Time: issueOne,
},
SentAt: &sentOne,
CreatedAt: &createdOne,
UpdatedAt: &updatedOne,
}

assert.Equal(t, want, client)
}
Empty file.
43 changes: 43 additions & 0 deletions testdata/estimate/get/response_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"id":1439818,
"client_key":"13dc088aa7d51ec687f186b146730c3c75dc7423",
"number":"1001",
"purchase_order":"5678",
"amount":9630.0,
"tax":5.0,
"tax_amount":450.0,
"tax2":2.0,
"tax2_amount":180.0,
"discount":10.0,
"discount_amount":1000.0,
"subject":"Online Store - Phase 2",
"notes":"Some notes about the estimate",
"state":"sent",
"issue_date":"2017-06-01",
"sent_at":"2017-06-27T16:11:33Z",
"created_at":"2017-06-27T16:11:24Z",
"updated_at":"2017-06-27T16:13:56Z",
"accepted_at":null,
"declined_at":null,
"currency":"USD",
"client":{
"id":5735776,
"name":"123 Industries"
},
"creator":{
"id":1782884,
"name":"Bob Powell"
},
"line_items":[
{
"id":53334195,
"kind":"Service",
"description":"Phase 2 of the Online Store",
"quantity":100,
"unit_price":100.0,
"amount":10000.0,
"taxed":true,
"taxed2":true
}
]
}
Empty file.
102 changes: 102 additions & 0 deletions testdata/estimate/list/response_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"estimates":[
{
"id":1439818,
"client_key":"13dc088aa7d51ec687f186b146730c3c75dc7423",
"number":"1001",
"purchase_order":"5678",
"amount":9630.0,
"tax":5.0,
"tax_amount":450.0,
"tax2":2.0,
"tax2_amount":180.0,
"discount":10.0,
"discount_amount":1000.0,
"subject":"Online Store - Phase 2",
"notes":"Some notes about the estimate",
"state":"sent",
"issue_date":"2017-06-01",
"sent_at":"2017-06-27T16:11:33Z",
"created_at":"2017-06-27T16:11:24Z",
"updated_at":"2017-06-27T16:13:56Z",
"accepted_at":null,
"declined_at":null,
"currency":"USD",
"client":{
"id":5735776,
"name":"123 Industries"
},
"creator":{
"id":1782884,
"name":"Bob Powell"
},
"line_items":[
{
"id":53334195,
"kind":"Service",
"description":"Phase 2 of the Online Store",
"quantity":100,
"unit_price":100,
"amount":10000,
"taxed":true,
"taxed2":true
}
]
},
{
"id":1439814,
"client_key":"a5ffaeb30c55776270fcd3992b70332d769f97e7",
"number":"1000",
"purchase_order":"1234",
"amount":21000.0,
"tax":5.0,
"tax_amount":1000.0,
"tax2":null,
"tax2_amount":0.0,
"discount":null,
"discount_amount":0.0,
"subject":"Online Store - Phase 1",
"notes":"Some notes about the estimate",
"state":"accepted",
"issue_date":"2017-01-01",
"sent_at":"2017-06-27T16:10:30Z",
"created_at":"2017-06-27T16:09:33Z",
"updated_at":"2017-06-27T16:12:00Z",
"accepted_at":"2017-06-27T16:10:32Z",
"declined_at":null,
"currency":"USD",
"client":{
"id":5735776,
"name":"123 Industries"
},
"creator":{
"id":1782884,
"name":"Bob Powell"
},
"line_items":[
{
"id":57531966,
"kind":"Service",
"description":"Phase 1 of the Online Store",
"quantity":1,
"unit_price":20000,
"amount":20000,
"taxed":true,
"taxed2":false
}
]
}
],
"per_page":2000,
"total_pages":1,
"total_entries":2,
"next_page":null,
"previous_page":null,
"page":1,
"links":{
"first":"https://api.harvestapp.com/v2/estimates?page=1&per_page=2000",
"next":null,
"previous":null,
"last":"https://api.harvestapp.com/v2/estimates?page=1&per_page=2000"
}
}
Loading