marketo-api-ruby 0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,133 @@
1
+ require "minitest_helper"
2
+
3
+ class TestMarketoAPILeads < Minitest::Test
4
+ include MarketoTestHelper
5
+
6
+ def setup
7
+ super
8
+ @subject = @client.leads
9
+ end
10
+
11
+ def test_get_invalid_lead_key_hash
12
+ assert_raises(ArgumentError) { subject.get({}) }
13
+ end
14
+
15
+ GET_LEAD_STUB = ->(method, key) {
16
+ {
17
+ lead_record_list: {
18
+ lead_record: {
19
+ id: key[:lead_key][:key_value].to_i,
20
+ lead_attribute_list: {
21
+ attribute: [
22
+ { attr_name: 'Email', attr_value: nil, attr_type: 'string' },
23
+ { attr_name: 'Method', attr_value: method, attr_type: 'string' },
24
+ { attr_name: 'LeadKey', attr_value: key, attr_type: 'string' }
25
+ ]
26
+ }
27
+ }
28
+ }
29
+ }
30
+ }
31
+
32
+ def test_get_valid_lead_key
33
+ lead_key = { lead_key: { key_type: 'id', key_value: 416 } }
34
+ subject.stub :call, GET_LEAD_STUB do
35
+ lead = subject.get(lead_key)
36
+ assert_instance_of MarketoAPI::Lead, lead
37
+ assert_equal 416, lead.id
38
+ assert_equal :get_lead, lead[:Method]
39
+ assert_equal lead_key, lead[:LeadKey]
40
+ end
41
+ end
42
+
43
+ def test_get_lead
44
+ search_lead = MarketoAPI::Lead.new(id: 416)
45
+ lead_key = search_lead.params_for_get
46
+ subject.stub :call, GET_LEAD_STUB do
47
+ lead = subject.get(lead_key)
48
+ assert_instance_of MarketoAPI::Lead, lead
49
+ assert_equal 416, lead.id
50
+ assert_equal :get_lead, lead[:Method]
51
+ assert_equal lead_key, lead[:LeadKey]
52
+ end
53
+ end
54
+
55
+ def test_named_getters
56
+ MarketoAPI::Lead::NAMED_KEYS.each_pair do |name, key|
57
+ method = :"get_by_#{name}"
58
+ assert subject.respond_to? method
59
+
60
+ stub_specialized :get do
61
+ assert_equal key, subject.send(method, 42).first
62
+ end
63
+ end
64
+ end
65
+
66
+ def test_get_type_and_value
67
+ MarketoAPI::Lead::NAMED_KEYS.each_pair do |name, key|
68
+ subject.stub :call, GET_LEAD_STUB do
69
+ lead_key = MarketoAPI::Lead.key(name, 416)
70
+ lead = subject.get(name, 416)
71
+ assert_instance_of MarketoAPI::Lead, lead
72
+ assert_equal 416, lead.id
73
+ assert_equal :get_lead, lead[:Method]
74
+ assert_equal lead_key, lead[:LeadKey]
75
+
76
+ lead_key = MarketoAPI::Lead.key(key, 416)
77
+ lead = subject.get(key, 416)
78
+ assert_instance_of MarketoAPI::Lead, lead
79
+ assert_equal 416, lead.id
80
+ assert_equal :get_lead, lead[:Method]
81
+ assert_equal lead_key, lead[:LeadKey]
82
+ end
83
+ end
84
+ end
85
+
86
+ EMPTY_LEAD_HASH = {
87
+ lead_record: {
88
+ lead_attribute_list: {
89
+ attribute: []
90
+ }
91
+ }
92
+ }
93
+
94
+ def test_sync
95
+ subject.stub :extract_from_response, ARGS_STUB, EMPTY_LEAD_HASH do
96
+ stub_soap_call do
97
+ hash = GET_LEAD_STUB[:sync_lead, lead_key(416)]
98
+ lead = MarketoAPI::Lead.
99
+ from_soap_hash(hash[:lead_record_list][:lead_record])
100
+
101
+ result = subject.send(:transform_param, :sync, lead)
102
+
103
+ method, params = subject.sync(lead)
104
+ assert_equal :sync_lead, method
105
+ assert_equal result, params
106
+ end
107
+ end
108
+ end
109
+
110
+ def test_sync_multiple
111
+ subject.stub :extract_from_response, ARGS_STUB, [] do
112
+ stub_soap_call do
113
+ hashes = [
114
+ GET_LEAD_STUB[:sync_multiple_leads, lead_key(416)],
115
+ GET_LEAD_STUB[:sync_multiple_leads, lead_key(905)],
116
+ ]
117
+ leads = hashes.map { |hash|
118
+ MarketoAPI::Lead.
119
+ from_soap_hash(hash[:lead_record_list][:lead_record])
120
+ }
121
+ lead_list = subject.send(:transform_param_list, :sync, leads)
122
+ result = {
123
+ dedup_enabled: true,
124
+ lead_record_list: lead_list
125
+ }
126
+
127
+ method, params = subject.sync_multiple(leads)
128
+ assert_equal :sync_multiple_leads, method
129
+ assert_equal result, params
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,95 @@
1
+ require "minitest_helper"
2
+
3
+ class TestMarketoAPILists < Minitest::Test
4
+ include MarketoTestHelper
5
+
6
+ def setup
7
+ super
8
+ @subject = @client.lists
9
+ end
10
+
11
+ def test_add
12
+ stub_specialized :list_operation do
13
+ assert_equal :ADDTOLIST, subject.add(:list_key).first
14
+ end
15
+ stub_soap_call do
16
+ result = {
17
+ list_operation: :ADDTOLIST,
18
+ list_key: :list_key,
19
+ strict: false,
20
+ list_member_list: [ lead_key(416) ]
21
+ }
22
+
23
+ method, options = subject.add(:list_key, lead: lead_key(416))
24
+ assert_equal :list_operation, method
25
+ assert_equal result, options
26
+ end
27
+ end
28
+
29
+ def test_add_no_leads
30
+ assert_raises(ArgumentError) { subject.add(:list_key) }
31
+ end
32
+
33
+ def test_add_merged_leads
34
+ stub_soap_call do
35
+ result = {
36
+ list_operation: :ADDTOLIST,
37
+ list_key: :list_key,
38
+ strict: false,
39
+ list_member_list: [ lead_key(905), lead_key(416) ]
40
+ }
41
+
42
+ method, options = subject.add(:list_key, leads: lead_key(905),
43
+ lead: lead_key(416))
44
+ assert_equal :list_operation, method
45
+ assert_equal result, options
46
+ end
47
+ end
48
+
49
+ def test_remove
50
+ stub_specialized :list_operation do
51
+ assert_equal :REMOVEFROMLIST, subject.remove(:list_key).first
52
+ end
53
+ stub_soap_call do
54
+ result = {
55
+ list_operation: :REMOVEFROMLIST,
56
+ list_key: :list_key,
57
+ strict: false,
58
+ list_member_list: [ lead_key(416) ]
59
+ }
60
+
61
+ method, options = subject.remove(:list_key, lead: lead_key(416))
62
+ assert_equal :list_operation, method
63
+ assert_equal result, options
64
+ end
65
+ end
66
+
67
+ def test_member_q
68
+ stub_specialized :list_operation do
69
+ assert_equal :ISMEMBEROFLIST, subject.member?(:list_key).first
70
+ end
71
+ stub_soap_call do
72
+ result = {
73
+ list_operation: :ISMEMBEROFLIST,
74
+ list_key: :list_key,
75
+ strict: false,
76
+ list_member_list: [ lead_key(416) ]
77
+ }
78
+
79
+ method, options = subject.member?(:list_key, lead: lead_key(416))
80
+ assert_equal :list_operation, method
81
+ assert_equal result, options
82
+ end
83
+ end
84
+
85
+ def test_class_key
86
+ subject.class::NAMED_TYPES.each { |k, v|
87
+ result = { list_key: { key_type: v, key_value: 'value' } }
88
+
89
+ assert_equal result, subject.class.key(k, 'value')
90
+ assert_equal result, subject.class.key(v, 'value')
91
+ }
92
+
93
+ assert_raises(ArgumentError) { subject.class.key('invalid', 'value') }
94
+ end
95
+ end
@@ -0,0 +1,273 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestMarketoAPIMObject < Minitest::Test
4
+ include MarketoTestHelper
5
+
6
+ def setup
7
+ super
8
+ @id = 99
9
+ @attributes = {
10
+ String: 'string',
11
+ Int: 5,
12
+ }
13
+ @types = {
14
+ Tag: {
15
+ String: 'string',
16
+ Int: 5
17
+ }
18
+ }
19
+ @subject = MarketoAPI::MObject.new(:Opportunity, @id) do |lead|
20
+ @attributes.each { |k, v| lead.attributes[k] = v }
21
+ @types.each { |k, v| lead.types[k] = v }
22
+ end
23
+ end
24
+
25
+ def test_type
26
+ assert_equal :Opportunity, subject.type
27
+ end
28
+
29
+ def test_initialization
30
+ MarketoAPI::MObject::ALL_TYPES.each do |type|
31
+ assert_equal type, MarketoAPI::MObject.new(type).type
32
+ assert_equal type, MarketoAPI::MObject.send(type.downcase).type
33
+ end
34
+
35
+ assert_raises(ArgumentError) {
36
+ MarketoAPI::MObject.new(:UnknownType)
37
+ }
38
+ end
39
+
40
+ def test_id
41
+ assert_equal 99, subject.id
42
+ subject.id = 33
43
+ assert_equal 33, subject.id
44
+ end
45
+
46
+ def test_include_details
47
+ refute subject.include_details
48
+ subject.include_details = true
49
+ assert subject.include_details
50
+ end
51
+
52
+ def test_attributes
53
+ assert_equal @attributes, subject.attributes
54
+ end
55
+
56
+ def test_types
57
+ local = MarketoAPI::MObject.opportunity
58
+ assert_equal({}, local.types)
59
+ local.types[:Tag]
60
+ assert_equal({ Tag: {} }, local.types)
61
+ end
62
+
63
+ def test_criteria_empty
64
+ assert_empty subject.criteria
65
+ end
66
+
67
+ def test_criteria_correct_name_translation
68
+ MarketoAPI::MObject::Criteria::TYPES.each do |k, v|
69
+ c = subject.criteria(k, 'value', :eq).last
70
+ assert_same v, c.name
71
+ c = subject.criteria(c.name.dup, 'value', :eq).last
72
+ assert_same v, c.name
73
+ end
74
+ end
75
+
76
+ def test_criteria_correct_comparison
77
+ MarketoAPI::MObject::Criteria::CMP.each do |cmp|
78
+ c = subject.criteria(:name, 'value', cmp).last
79
+ assert_equal cmp, c.comparison
80
+ c = subject.criteria(:name, 'value', cmp.downcase).last
81
+ assert_equal cmp, c.comparison
82
+ end
83
+
84
+ assert_raises(ArgumentError) {
85
+ subject.criteria(:foo, 'value', :xx)
86
+ }
87
+ end
88
+
89
+ def test_criteria_bad_params
90
+ assert_raises(ArgumentError) {
91
+ subject.criteria(:foo, 'value', :eq)
92
+ }
93
+ assert_raises(ArgumentError) {
94
+ subject.criteria(:name)
95
+ }
96
+ assert_raises(ArgumentError) {
97
+ subject.criteria(:name, 'value')
98
+ }
99
+ end
100
+
101
+ def test_criteria_hash
102
+ MarketoAPI::MObject::Criteria::TYPES.each do |k, v|
103
+ MarketoAPI::MObject::Criteria::CMP.each do |cmp|
104
+ c = subject.criteria(k, 'value', cmp).last
105
+ e = {
106
+ attr_name: v,
107
+ attr_value: 'value',
108
+ comparison: cmp
109
+ }
110
+
111
+ assert_equal e, c.to_h
112
+ end
113
+ end
114
+ end
115
+
116
+ def test_association_empty
117
+ assert_empty subject.associations
118
+ end
119
+
120
+ def test_association_correct_name_translation
121
+ MarketoAPI::MObject::Association::TYPES.each do |k|
122
+ c = subject.association(k, id: 1).last
123
+ assert_same k, c.type
124
+ c = subject.association(k.downcase, id: 1).last
125
+ assert_same k, c.type
126
+ end
127
+ end
128
+
129
+ def test_association_bad_params
130
+ assert_raises(ArgumentError) {
131
+ subject.association(:foo, id: 2)
132
+ }
133
+ assert_raises(KeyError) {
134
+ subject.association(:lead)
135
+ }
136
+ end
137
+
138
+ def test_association_options
139
+ z = ->(options) { subject.association(:lead, options).last }
140
+
141
+ c = z.call(id: 1)
142
+ assert_equal 1, c.id
143
+ assert_nil c.external
144
+
145
+ c = z.call(external: 1)
146
+ assert_nil c.id
147
+ assert_equal 1, c.external_key
148
+
149
+ c = z.call(id: 1, external_key: 2)
150
+ assert_equal 1, c.id
151
+ assert_equal 2, c.external
152
+ end
153
+
154
+ def test_association_hash
155
+ MarketoAPI::MObject::Association::TYPES.each do |k|
156
+ c = subject.association(k, id: 3, external: 4).last
157
+ e = {
158
+ m_obj_type: k,
159
+ id: 3,
160
+ external_key: 4
161
+ }
162
+
163
+ assert_equal e, c.to_h
164
+ end
165
+ end
166
+
167
+ def test_params_for_delete_invalid_type
168
+ assert_raises(ArgumentError) {
169
+ MarketoAPI::MObject.program(32).params_for_delete
170
+ }
171
+ end
172
+
173
+ def test_params_for_delete_missing_id
174
+ assert_raises(ArgumentError) {
175
+ MarketoAPI::MObject.opportunity.params_for_delete
176
+ }
177
+ end
178
+
179
+ def test_params_for_delete
180
+ assert_equal({ type: subject.type, id: subject.id },
181
+ subject.params_for_delete)
182
+ end
183
+
184
+ def test_params_for_get_invalid_type
185
+ assert_raises(ArgumentError) {
186
+ MarketoAPI::MObject.activityrecord.params_for_get
187
+ }
188
+ end
189
+
190
+ def test_params_for_get
191
+ assert_equal({
192
+ type: subject.type, id: subject.id, include_details: false
193
+ }, subject.params_for_get)
194
+ end
195
+
196
+ def test_params_for_get_minimal
197
+ subject.id = nil
198
+ assert_equal({
199
+ type: subject.type, include_details: false
200
+ }, subject.params_for_get)
201
+ end
202
+
203
+ def test_params_for_get_with_details
204
+ subject.include_details = true
205
+ assert_equal({
206
+ type: subject.type, id: subject.id, include_details: true
207
+ }, subject.params_for_get)
208
+ end
209
+
210
+ def test_params_for_get_with_stream_position
211
+ subject.stream_position = 'position'
212
+ assert_equal({
213
+ type: subject.type, id: subject.id, include_details: false,
214
+ stream_position: 'position'
215
+ }, subject.params_for_get)
216
+ end
217
+
218
+ def test_params_for_get_with_criteria
219
+ subject.criteria(:name, 'value', :eq)
220
+ assert_equal({
221
+ type: subject.type, id: subject.id, include_details: false,
222
+ m_obj_criteria_list: [
223
+ {
224
+ attr_name: 'Name',
225
+ attr_value: 'value',
226
+ comparison: :EQ
227
+ }
228
+ ]
229
+ }, subject.params_for_get)
230
+ end
231
+
232
+ def test_params_for_get_with_association
233
+ subject.association(:Lead, id: 3, external: 4)
234
+ assert_equal({
235
+ type: subject.type, id: subject.id, include_details: false,
236
+ m_obj_association_list: [
237
+ {
238
+ m_obj_type: :Lead,
239
+ id: 3,
240
+ external_key: 4
241
+ }
242
+ ]
243
+ }, subject.params_for_get)
244
+ end
245
+
246
+ def test_class_from_soap_hash
247
+ hash = {
248
+ type: :Opportunity,
249
+ id: 99,
250
+ attrib_list: {
251
+ attrib: [
252
+ { name: 'String', value: 'string' },
253
+ { name: 'Int', value: 5 },
254
+ ]
255
+ },
256
+ type_attrib_list: {
257
+ type_attrib: [
258
+ {
259
+ attr_type: 'Tag',
260
+ attr_list: {
261
+ attrib: [
262
+ { name: 'String', value: 'string' },
263
+ { name: 'Int', value: 5 },
264
+ ]
265
+ }
266
+ }
267
+ ]
268
+ }
269
+ }
270
+
271
+ assert_equal subject, MarketoAPI::MObject.from_soap_hash(hash)
272
+ end
273
+ end