forked from anthonyreilly/NetCoreForce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRUDTests.cs
207 lines (160 loc) · 10.1 KB
/
CRUDTests.cs
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
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Xunit;
using NetCoreForce.Client;
using NetCoreForce.Client.Models;
using NetCoreForce.Models;
using Newtonsoft.Json;
namespace NetCoreForce.FunctionalTests
{
public class CRUDTests : IClassFixture<ForceClientFixture>
{
ForceClientFixture forceClientFixture;
public CRUDTests(ForceClientFixture fixture)
{
this.forceClientFixture = fixture;
}
[Fact]
public async Task CrudChain()
{
ForceClient client = await forceClientFixture.GetForceClient();
//create new object
SfAccount newAccount = new SfAccount();
string accountName = string.Format("Test Object {0}", Guid.NewGuid().ToString());
newAccount.Name = accountName;
CreateResponse createResp = await client.CreateRecord<SfAccount>(SfAccount.SObjectTypeName, newAccount);
Assert.True(!string.IsNullOrEmpty(createResp.Id), "Failed to create new object");
//get newly created
string newAccountId = createResp.Id;
SfAccount account = await client.GetObjectById<SfAccount>(SfAccount.SObjectTypeName, newAccountId);
Assert.True(account != null, "Failed to retrieve new object");
//update object
string description = string.Format("Test Description {0}", Guid.NewGuid().ToString());
account.Description = description;
await client.UpdateRecord<SfAccount>(SfAccount.SObjectTypeName, account.Id, account);
//get newly updated
SfAccount udpatedAccount = await client.GetObjectById<SfAccount>(SfAccount.SObjectTypeName, newAccountId);
Assert.True(udpatedAccount != null, "Failed to retrieve udpated object");
Assert.Equal(description, udpatedAccount.Description);
//delete
await client.DeleteRecord(SfAccount.SObjectTypeName, newAccountId);
//use queryall to find deleted record?
}
[Fact]
public async Task CreateAndUpdateMultiple()
{
ForceClient client = await forceClientFixture.GetForceClient();
//create new object
SfAccount firstAccount = new SfAccount() { };
string firstAccountName = string.Format("Test Object {0}", Guid.NewGuid().ToString());
firstAccount.Name = firstAccountName;
CreateResponse createResp = await client.CreateRecord<SfAccount>(SfAccount.SObjectTypeName, firstAccount);
string firstAccountId = createResp.Id;
Assert.True(!string.IsNullOrEmpty(createResp.Id), "Failed to create new object");
//get new object
firstAccount = await client.GetObjectById<SfAccount>(SfAccount.SObjectTypeName, firstAccountId);
//create second new object for testing update multiple
SfAccount secondAccount = new SfAccount();
string secondAccountName = string.Format("Test Object {0}", Guid.NewGuid().ToString());
secondAccount.Name = secondAccountName;
CreateResponse secondCreateResp = await client.CreateRecord<SfAccount>(SfAccount.SObjectTypeName, secondAccount);
string secondAccountId = secondCreateResp.Id;
Assert.True(!string.IsNullOrEmpty(secondCreateResp.Id), "Failed to create second new object");
//get new object
secondAccount = await client.GetObjectById<SfAccount>(SfAccount.SObjectTypeName, secondAccountId);
//test update multiple
string firstUpdatedDescription = string.Format("Test Description {0}", Guid.NewGuid().ToString());
string secondUpdatedDescription = string.Format("Test Description {0}", Guid.NewGuid().ToString());
firstAccount.Description = firstUpdatedDescription;
secondAccount.Description = secondUpdatedDescription;
List<UpsertResponse> responses = await client.UpdateRecords(new List<SObject>() { firstAccount, secondAccount }, true);
Assert.True(responses.All(r => r.Success), "Failed to update multiple objects");
//get newly updated objects
string secondNewAccountId = secondCreateResp.Id;
SfAccount firstUpdatedAccount = await client.GetObjectById<SfAccount>(SfAccount.SObjectTypeName, firstAccountId);
SfAccount secondUpdatedAccount = await client.GetObjectById<SfAccount>(SfAccount.SObjectTypeName, secondAccountId);
Assert.True(firstUpdatedAccount != null && secondUpdatedAccount != null, "Failed to retrieve multiple updated objects");
Assert.Equal(firstUpdatedDescription, firstUpdatedAccount.Description);
Assert.Equal(secondUpdatedDescription, secondUpdatedAccount.Description);
//delete
await client.DeleteRecord(SfAccount.SObjectTypeName, firstAccountId);
await client.DeleteRecord(SfAccount.SObjectTypeName, secondNewAccountId);
}
[Fact]
public async Task CreateMultiple()
{
ForceClient client = await forceClientFixture.GetForceClient();
//create new objects
SfAccount firstAccount = new SfAccount() { };
string firstAccountRefId = Guid.NewGuid().ToString();
string firstAccountName = string.Format("Test Object refId {0}", firstAccountRefId);
firstAccount.Name = firstAccountName;
// firstAccount.Attributes = new SObjectAttributes() { ReferenceId = firstAccountRefId };
// firstAccount.Attributes.ReferenceId = firstAccountRefId;
SfAccount secondAccount = new SfAccount();
string secondAccountRefId = Guid.NewGuid().ToString();
string secondAccountName = string.Format("Test Object refId {0}", secondAccountRefId);
secondAccount.Name = secondAccountName;
// secondAccount.Attributes = new SObjectAttributes() { ReferenceId = secondAccountRefId };
// secondAccount.Attributes.ReferenceId = secondAccountRefId;
List<SObject> objects = new List<SObject>() { firstAccount, secondAccount };
SObjectTreeResponse response = await client.CreateMultiple(SfAccount.SObjectTypeName, objects);
Assert.True(response.HasErrors == "false", "Create multiple failed");
Assert.True(response.Results.All(r => r.Errors == null), "Create multiple failed");
// get newly created objects
SfAccount firstCreatedAccount = await client.GetObjectById<SfAccount>(SfAccount.SObjectTypeName, response.Results[0].Id);
SfAccount secondCreatedAccount = await client.GetObjectById<SfAccount>(SfAccount.SObjectTypeName, response.Results[1].Id);
Assert.True(firstCreatedAccount != null && secondCreatedAccount != null, "Failed to retrieve multiple updated objects");
Assert.Equal(firstAccountName, firstCreatedAccount.Name);
Assert.Equal(secondAccountName, secondCreatedAccount.Name);
//delete
await client.DeleteRecord(SfAccount.SObjectTypeName, firstCreatedAccount.Id);
await client.DeleteRecord(SfAccount.SObjectTypeName, secondCreatedAccount.Id);
}
[Fact]
public async Task CreateMultipleWithReferenceIds()
{
ForceClient client = await forceClientFixture.GetForceClient();
//create new objects
SfAccount firstAccount = new SfAccount() { };
string firstAccountRefId = Guid.NewGuid().ToString().Replace("-", "");
string firstAccountName = string.Format("Test_Object_refId_{0}", firstAccountRefId);
firstAccount.Name = firstAccountName;
firstAccount.Attributes = new SObjectAttributes() { ReferenceId = firstAccountRefId, Type = SfAccount.SObjectTypeName };
SfAccount secondAccount = new SfAccount();
string secondAccountRefId = Guid.NewGuid().ToString().Replace("-", "");
string secondAccountName = string.Format("Test_Object_refId_{0}", secondAccountRefId);
secondAccount.Name = secondAccountName;
secondAccount.Attributes = new SObjectAttributes() { ReferenceId = secondAccountRefId, Type = SfAccount.SObjectTypeName };
List<SObject> objects = new List<SObject>() { firstAccount, secondAccount };
SObjectTreeResponse response = await client.CreateMultiple(SfAccount.SObjectTypeName, objects, autoFillAttributes: false);
Assert.True(response.HasErrors == "false", "Create multiple failed");
Assert.True(response.Results.All(r => r.Errors == null), "Create multiple failed");
// check for matching reference IDs in results
Assert.Equal(firstAccountRefId, response.Results[0].ReferenceId);
Assert.Equal(secondAccountRefId, response.Results[1].ReferenceId);
//delete
await client.DeleteRecord(SfAccount.SObjectTypeName, response.Results[0].Id);
await client.DeleteRecord(SfAccount.SObjectTypeName, response.Results[1].Id);
}
// [Fact]
// public async Task DeleteMultiple()
// {
// ForceClient client = await forceClientFixture.GetForceClient();
// //create new objects
// SfAccount firstAccount = new SfAccount() { Name = string.Format("Test Object {0}", Guid.NewGuid().ToString()) };
// SfAccount secondAccount = new SfAccount() { Name = string.Format("Test Object {0}", Guid.NewGuid().ToString()) };
// List<SObject> objects = new List<SObject>() { firstAccount, secondAccount };
// SObjectTreeResponse createResponse = await client.CreateMultiple(SfAccount.SObjectTypeName, objects);
// Assert.True(createResponse.HasErrors == "false", "Create multiple failed");
// Assert.True(createResponse.Results.All(r => r.Errors == null), "Create multiple failed");
// // get newly created objects
// //delete
// // await client.DeleteRecord(SfAccount.SObjectTypeName, firstCreatedAccount.Id);
// // await client.DeleteRecord(SfAccount.SObjectTypeName, secondCreatedAccount.Id);
// }
}
}