This repository has been archived by the owner on Jun 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 182
/
OrgServiceContextTests.cs
97 lines (80 loc) · 3.45 KB
/
OrgServiceContextTests.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
using Crm;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using System;
using System.Linq;
using Xunit;
namespace FakeXrmEasy.Tests.FakeContextTests.OrgServiceContextTests
{
public class OrgServiceContextTests
{
[Fact]
public void When_calling_context_add_and_save_changes_entity_is_added_to_the_faked_context()
{
var context = new XrmFakedContext();
var service = context.GetFakedOrganizationService();
using (var ctx = new XrmServiceContext(service))
{
ctx.AddObject(new Account() { Name = "Test account" });
ctx.SaveChanges();
var account = ctx.CreateQuery<Account>()
.ToList()
.FirstOrDefault();
Assert.Equal("Test account", account.Name);
}
}
[Fact]
public void When_calling_context_add_and_save_changes_returns_correct_result()
{
var context = new XrmFakedContext();
var service = context.GetFakedOrganizationService();
using (var ctx = new XrmServiceContext(service))
{
ctx.AddObject(new Account() { Name = "Test account" });
var result = ctx.SaveChanges();
Assert.NotNull(result);
Assert.NotEmpty(result);
Assert.Equal("Create", result.First().Response.ResponseName);
Assert.IsType<CreateResponse>(result.First().Response);
Assert.NotEqual(Guid.Empty, ((CreateResponse)result.First().Response).id);
}
}
[Fact]
public void When_calling_context_add_addrelated_and_save_changes_entities_are_added_to_the_faked_context()
{
var context = new XrmFakedContext();
var relationship = new XrmFakedRelationship()
{
IntersectEntity = "accountleads",
Entity1Attribute = "accountid",
Entity2Attribute = "leadid",
Entity1LogicalName = "account",
Entity2LogicalName = "lead"
};
context.AddRelationship("accountleads", relationship);
var service = context.GetFakedOrganizationService();
using (var ctx = new XrmServiceContext(service))
{
var account = new Account() { Name = "Test account" };
ctx.AddObject(account);
var contact = new Lead() { FirstName = "Jane", LastName = "Doe" };
ctx.AddRelatedObject(account, new Relationship("accountleads"), contact);
var result = ctx.SaveChanges();
var resultaccount = ctx.CreateQuery<Account>()
.ToList()
.FirstOrDefault();
Assert.NotNull(resultaccount);
Assert.Equal("Test account", resultaccount.Name);
var reaultlead = ctx.CreateQuery<Lead>()
.ToList()
.FirstOrDefault();
Assert.NotNull(reaultlead);
Assert.Equal("Jane", reaultlead.FirstName);
Assert.Equal("Doe", reaultlead.LastName);
var relationshipRecords = ctx.CreateQuery("accountleads")
.ToList();
Assert.NotEmpty(relationshipRecords);
}
}
}
}