-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions_test.go
223 lines (214 loc) · 5.75 KB
/
functions_test.go
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2019 James Cote
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package intacct_test
import (
"context"
"encoding/xml"
"fmt"
"net/http"
"strings"
"testing"
"github.com/jfcote87/testutils"
"github.com/jfcote87/intacct"
)
func TestReader(t *testing.T) {
var rdrTests = []struct {
Rdr *intacct.Reader
Name string
Flds []intacct.CustomField
}{
{
Rdr: intacct.Read("VENDOR", "1", "2").Fields("A", "B", "C"),
Name: "read",
Flds: []intacct.CustomField{
{Name: "object", Value: "VENDOR"},
{Name: "keys", Value: "1,2"},
{Name: "fields", Value: "A,B,C"},
{Name: "returnFormat", Value: "xml"},
},
},
{
Rdr: intacct.Read("VENDOR", ""),
Name: "read",
Flds: []intacct.CustomField{
{Name: "object", Value: "VENDOR"},
{Name: "keys", Value: ""},
{Name: "fields", Value: "*"},
{Name: "returnFormat", Value: "xml"},
},
},
{
Rdr: intacct.ReadByName("VENDOR", "A").PageSize(100),
Name: "readByName",
Flds: []intacct.CustomField{
{Name: "object", Value: "VENDOR"},
{Name: "keys", Value: "A"},
{Name: "fields", Value: "*"},
{Name: "returnFormat", Value: "xml"},
},
},
{
Rdr: intacct.ReadByQuery("VENDOR", "").Fields("fld1", "fld2"),
Name: "readByQuery",
Flds: []intacct.CustomField{
{Name: "object", Value: "VENDOR"},
{Name: "query", Value: ""},
{Name: "fields", Value: "fld1,fld2"},
{Name: "returnFormat", Value: "xml"},
},
},
{
Rdr: intacct.ReadByQuery("VENDOR", "A > B").PageSize(100),
Name: "readByQuery",
Flds: []intacct.CustomField{
{Name: "object", Value: "VENDOR"},
{Name: "query", Value: "A > B"},
{Name: "fields", Value: "*"},
{Name: "returnFormat", Value: "xml"},
{Name: "pagesize", Value: "100"},
},
},
{
Rdr: intacct.ReadRelated("asset", "Rasset_class"),
Name: "readRelated",
Flds: []intacct.CustomField{
{Name: "object", Value: "asset"},
{Name: "keys", Value: ""},
{Name: "fields", Value: "*"},
{Name: "returnFormat", Value: "xml"},
{Name: "relationship_id", Value: "Rasset_class"},
},
},
}
for idx, tt := range rdrTests {
var keyRdr = struct {
CustomFlds []intacct.CustomField `xml:",any"`
}{}
b, _ := xml.Marshal(tt.Rdr)
if !strings.HasPrefix(string(b), "<"+tt.Name) {
t.Errorf("test #%d expected element name <%s>; got %s", idx, tt.Name, strings.Split(string(b), ">")[0]+">")
continue
}
if err := xml.Unmarshal(b, &keyRdr); err != nil {
t.Errorf("unable to unmarshal test %d", idx)
return
}
if !cmpCustomFields(tt.Flds, keyRdr.CustomFlds) {
t.Errorf("test %d expected %#v; got %#v", idx, tt.Flds, keyRdr.CustomFlds)
}
}
}
func TestReadAll(t *testing.T) {
testTransport := &testutils.Transport{}
testTransport.Add(
&testutils.RequestTester{
ResponseFunc: func(req *http.Request) (*http.Response, error) {
return testutils.MakeResponse(200, []byte(readMore1), xmlHeader), nil
},
},
&testutils.RequestTester{
ResponseFunc: func(r *http.Request) (*http.Response, error) {
var iReq *Request
defer r.Body.Close()
if err := xml.NewDecoder(r.Body).Decode(&iReq); err != nil {
return testutils.MakeResponse(http.StatusBadRequest, []byte(err.Error()), nil), nil
}
var res = struct {
ResultID string `xml:"resultId"`
}{}
xml.Unmarshal([]byte(iReq.Op.Content[0].Payload), &res)
if res.ResultID != "READMOREID" {
return nil, fmt.Errorf("expected resultId = READMOREID; got %s", res.ResultID)
}
return testutils.MakeResponse(200, []byte(readMore2), xmlHeader), nil
},
},
)
testClient := &http.Client{
Transport: testTransport,
}
ctx := context.Background()
sv := &intacct.Service{
SenderID: "SENDERID",
Password: "*******",
Authenticator: intacct.SessionID("SESSIONID"),
HTTPClientFunc: func(ctx context.Context) (*http.Client, error) {
return testClient, nil
},
}
var projects []Project
if err := intacct.ReadByQuery("PROJECT", "PROJECTID LIKE 'P%'").PageSize(10).GetAll(ctx, sv, &projects); err != nil {
t.Errorf("readAll failed: %v", err)
}
if len(projects) != 12 {
t.Errorf("expected 12 Project records; got %d", len(projects))
}
}
func cmpCustomFields(a, b []intacct.CustomField) bool {
if len(a) != len(b) {
return false
}
var tMap = make(map[string]string)
for _, cf := range a {
tMap[cf.Name] = cf.Value
}
for _, cf := range b {
v, ok := tMap[cf.Name]
if !ok || v != cf.Value {
return false
}
}
return true
}
func TestWriter(t *testing.T) {
var wTests = []struct {
w *intacct.Writer
nm string
}{
{
w: intacct.Create("PROJECT", Project{ProjectID: "PX", Projectname: "Name1"}),
nm: "create",
},
{
w: intacct.Update("", []Project{{ProjectID: "PX", Projectname: "Name1"}, {ProjectID: "PY", Projectname: "Name2"}}),
nm: "update",
},
{
w: intacct.Delete("NOTPROJECT", Project{RecordNumber: 1}),
nm: "delete",
},
{
w: intacct.GetAPISession("Loc1").(*intacct.Writer),
nm: "getAPISession",
},
{
w: intacct.GetDimensions().(*intacct.Writer),
nm: "getDimensions",
},
{
w: intacct.GetDimensionAutofillDetails().(*intacct.Writer),
nm: "getDimensionAutofillDetails",
},
{
w: intacct.GetDimensionRelationships().(*intacct.Writer),
nm: "getDimensionRelationships",
},
{
w: intacct.GetDimensionRestrictedData("dim", "val").(*intacct.Writer),
nm: "getDimensionRestrictedData",
},
{
w: intacct.InstallApp("App Definition").(*intacct.Writer),
nm: "installApp",
},
}
for idx, tt := range wTests {
b, _ := xml.Marshal(tt.w)
if !strings.HasPrefix(string(b), "<"+tt.nm) {
t.Errorf("test #%d expected element name <%s>; got %s", idx, tt.nm, strings.Split(string(b), ">")[0]+">")
continue
}
}
}