-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdatetime_test.go
More file actions
85 lines (64 loc) · 2.36 KB
/
datetime_test.go
File metadata and controls
85 lines (64 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package easypost
import (
"reflect"
"time"
"github.com/google/go-querystring/query"
)
func (c *ClientTests) TestDateTime() {
assert := c.Assert()
year := 2019
month := time.Month(1)
day := 1
hour := 0
minute := 0
second := 0
nanosecond := 0
location := time.UTC
_time := time.Date(year, month, day, hour, minute, second, nanosecond, location)
datetime := DateTime(_time)
assert.Equal(_time, datetime.AsTime())
datetimeFromTime := DateTimeFromTime(_time)
assert.Equal(_time, datetimeFromTime.AsTime())
datetimeFromNumbers := NewDateTime(year, month, day, hour, minute, second, nanosecond, location)
assert.Equal(_time, datetimeFromNumbers.AsTime())
}
func (c *ClientTests) TestDateTimeJSON() {
client := c.TestClient()
assert, require := c.Assert(), c.Require()
shipment, err := client.CreateShipment(c.fixture.OneCallBuyShipment())
require.NoError(err)
// fixture uses a DateTime object behind the scenes
pickupData := c.fixture.BasicPickup()
pickupData.Shipment = shipment
// will test serializing (during call) and deserializing (parsing response) the DateTime object
pickup, err := client.CreatePickup(pickupData)
require.NoError(err)
assert.Equal(reflect.TypeOf(&Pickup{}), reflect.TypeOf(pickup))
assert.NotNil(pickup.MaxDatetime)
assert.NotNil(pickup.MinDatetime)
maxDateTimeTime := pickup.MaxDatetime.AsTime()
minDateTimeTime := pickup.MinDatetime.AsTime()
assert.NotNil(maxDateTimeTime)
assert.NotNil(minDateTimeTime)
maxDatetimeString := pickup.MaxDatetime.String()
minDatetimeString := pickup.MinDatetime.String()
assert.NotNil(maxDatetimeString)
assert.NotNil(minDatetimeString)
}
// / TestDateTimeUrlQueryStringParameterInclusion tests that DateTime-type parameters are encoded properly (RFC3339) for use in HTTP payloads
func (c *ClientTests) TestDateTimeUrlQueryStringParameterInclusion() {
assert := c.Assert()
now := time.Now().UTC()
past := now.AddDate(0, -4, 0)
start := DateTimeFromTime(past)
end := DateTimeFromTime(now)
options := ListOptions{
StartDateTime: &start,
EndDateTime: &end,
PageSize: 100,
}
// This is the internals of the `convertOptsToURLValues` method in the `Client` struct (can't be used directly because it's private)
values, _ := query.Values(options)
assert.Equal(past.Format(time.RFC3339), values.Get("start_datetime"))
assert.Equal(now.Format(time.RFC3339), values.Get("end_datetime"))
}