Repository navigation
Expand file tree
/
Copy pathcustomfields_test.go
More file actions
124 lines (108 loc) · 2.89 KB
/
Copy pathcustomfields_test.go
File metadata and controls
124 lines (108 loc) · 2.89 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package asana
import (
"encoding/json"
"net/http"
"testing"
"github.com/h2non/gock"
)
func TestCustomFieldBase_Precision_ParseZero(t *testing.T) {
cf := &CustomFieldBase{}
if err := json.Unmarshal([]byte(`
{
"precision": 0
}
`), cf); err != nil {
t.Fatal(err)
}
if cf.Precision == nil || *cf.Precision != 0 {
t.Errorf("Expected Precision to be a pointer to the integer zero, but saw %v", cf.Precision)
}
}
func TestCustomFieldBase_Precision_ParseMissing(t *testing.T) {
cf := &CustomFieldBase{}
if err := json.Unmarshal([]byte(`
{
"name": "name"
}
`), cf); err != nil {
t.Fatal(err)
}
if cf.Precision != nil {
t.Errorf("Expected Precision to be a nil, but saw %v", cf.Precision)
}
}
func TestCustomFieldBase_Precision_SerializeZero(t *testing.T) {
val := 0
cf := &CustomFieldBase{Precision: &val}
if bs, err := json.Marshal(cf); err != nil {
t.Fatal(err)
} else {
if string(bs) != `{"precision":0,"resource_subtype":""}` {
t.Errorf("Expected Precision to be a zero, but saw %v", string(bs))
}
}
}
func TestCustomField_AsanaCreatedField_Parse(t *testing.T) {
cf := &CustomField{}
if err := json.Unmarshal([]byte(`
{
"gid": "123",
"name": "Deal size",
"resource_subtype": "number",
"asana_created_field": "deal_size"
}
`), cf); err != nil {
t.Fatal(err)
}
if cf.AsanaCreatedField != "deal_size" {
t.Errorf("Expected AsanaCreatedField to be %q, but saw %q", "deal_size", cf.AsanaCreatedField)
}
if !cf.IsAsanaCreated() {
t.Errorf("Expected IsAsanaCreated to be true for an Asana-created field")
}
}
func TestCustomField_AsanaCreatedField_Absent(t *testing.T) {
cf := &CustomField{}
if err := json.Unmarshal([]byte(`
{
"gid": "123",
"name": "Effort",
"resource_subtype": "number"
}
`), cf); err != nil {
t.Fatal(err)
}
if cf.AsanaCreatedField != "" {
t.Errorf("Expected AsanaCreatedField to be empty, but saw %q", cf.AsanaCreatedField)
}
if cf.IsAsanaCreated() {
t.Errorf("Expected IsAsanaCreated to be false for a user-created field")
}
}
// TestCustomField_AsanaCreatedField_RequestedInFields ensures the read-only
// asana_created_field property is included in opt_fields when requesting all
// fields for a CustomField, so it is populated on reads.
func TestCustomField_AsanaCreatedField_RequestedInFields(t *testing.T) {
options := Fields(CustomField{})
for _, name := range options.Fields {
if name == "asana_created_field" {
return
}
}
t.Errorf("Expected Fields(CustomField{}) to include %q, got %v", "asana_created_field", options.Fields)
}
func TestCustomField_Delete(t *testing.T) {
defer gock.Off()
gock.New("https://app.asana.com").
Delete("/api/1.0/custom_fields/1234").
Reply(200).
JSON(map[string]any{"data": map[string]any{}})
client := NewClient(http.DefaultClient)
field := &CustomField{ID: "1234"}
if err := field.Delete(client); err != nil {
t.Fatal(err)
}
if !gock.IsDone() {
t.Error("Expected DELETE request to be made")
}
}