-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathRetrieveMethodTests.cs
More file actions
142 lines (129 loc) · 5.93 KB
/
RetrieveMethodTests.cs
File metadata and controls
142 lines (129 loc) · 5.93 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using DataverseProxyGenerator.Core.Domain;
using DataverseProxyGenerator.Core.Generation;
namespace DataverseProxyGenerator.Tests;
public sealed class RetrieveMethodTests
{
[Fact]
public void EntityClass_ShouldGenerateStaticRetrieveMethod()
{
// Arrange
var table = new TableModel
{
SchemaName = "Account",
LogicalName = "account",
DisplayName = "Account",
Description = "Business account",
EntityTypeCode = 1,
PrimaryNameAttribute = "name",
PrimaryIdAttribute = "accountid",
IsIntersect = false,
Columns = new ColumnModel[]
{
new StringColumnModel
{
SchemaName = "Name",
LogicalName = "name",
DisplayName = "Name",
MaxLength = 100,
},
},
Relationships = new List<RelationshipModel>(),
};
var generator = new CSharpProxyGenerator();
// Act
var files = generator.GenerateCode(
new[] { table },
new XrmGenerationConfig("Output", "TestNamespace", "TestContextName", new Dictionary<string, IReadOnlyList<string>>(StringComparer.InvariantCulture).AsReadOnly()));
var file = files.FirstOrDefault(f => f.Filename.EndsWith("Account.cs", StringComparison.InvariantCulture));
// Assert
file.Should().NotBeNull();
file!.Content.Should().Contain("using System.Linq.Expressions;");
file.Content.Should().Contain("public static Account Retrieve(IOrganizationService service, Guid id, params Expression<Func<Account, object>>[] columns)");
file.Content.Should().Contain("return service.Retrieve(id, columns);");
}
[Fact]
public void EntityClass_ShouldGenerateStaticGetColumnNameMethod()
{
// Arrange
var table = new TableModel
{
SchemaName = "Account",
LogicalName = "account",
DisplayName = "Account",
Description = "This is an Account",
EntityTypeCode = 1,
PrimaryNameAttribute = "name",
PrimaryIdAttribute = "accountid",
IsIntersect = false,
Columns = new ColumnModel[]
{
new StringColumnModel
{
SchemaName = "Name",
LogicalName = "name",
DisplayName = "Name",
MaxLength = 100,
},
},
Relationships = new List<RelationshipModel>(),
};
var generator = new CSharpProxyGenerator();
// Act
var files = generator.GenerateCode(
new[] { table },
new XrmGenerationConfig("Output", "TestNamespace", "TestContextName", new Dictionary<string, IReadOnlyList<string>>(StringComparer.InvariantCulture).AsReadOnly()));
var file = files.FirstOrDefault(f => f.Filename.EndsWith("Account.cs", StringComparison.InvariantCulture));
// Assert
file.Should().NotBeNull();
file!.Content.Should().Contain("using System.Linq.Expressions;");
file.Content.Should().Contain("/// <summary>");
file.Content.Should().Contain("/// Gets the logical column name for a property on the Account entity, using the AttributeLogicalNameAttribute if present.");
file.Content.Should().Contain("/// </summary>");
file.Content.Should().Contain("/// <param name=\"columns\">Expressions that specify columns to retrieve</param>");
file.Content.Should().Contain("/// <returns>Name of column</returns>");
file.Content.Should().Contain("/// <exception cref=\"ArgumentNullException\">If no expression is provided</exception>");
file.Content.Should().Contain("/// <exception cref=\"ArgumentException\">If the expression is not x => x.column</exception>");
file.Content.Should().Contain("public static string GetColumnName(Expression<Func<Account, object>> column)");
file.Content.Should().Contain("return TableAttributeHelpers.GetColumnName(column);");
}
[Fact]
public void TableAttributeHelpers_ShouldGenerateRetrieveExtensionMethod()
{
// Arrange
var table = new TableModel
{
SchemaName = "TestEntity",
LogicalName = "testentity",
DisplayName = "Test Entity",
Description = "Test entity",
EntityTypeCode = 1,
PrimaryNameAttribute = "name",
PrimaryIdAttribute = "testentityid",
IsIntersect = false,
Columns = new ColumnModel[]
{
new StringColumnModel
{
SchemaName = "Name",
LogicalName = "name",
DisplayName = "Name",
MaxLength = 100,
},
},
Relationships = new List<RelationshipModel>(),
};
var generator = new CSharpProxyGenerator();
// Act
var files = generator.GenerateCode(
new[] { table },
new XrmGenerationConfig("Output", "TestNamespace", "TestContextName", new Dictionary<string, IReadOnlyList<string>>(StringComparer.InvariantCulture).AsReadOnly()));
var file = files.FirstOrDefault(f => f.Filename.EndsWith("TableAttributeHelpers.cs", StringComparison.InvariantCulture));
// Assert
file.Should().NotBeNull();
file!.Content.Should().Contain("using Microsoft.Xrm.Sdk.Query;");
file.Content.Should().Contain("public static T Retrieve<T>(this IOrganizationService service, Guid id, params Expression<Func<T, object>>[] attrs)");
file.Content.Should().Contain("where T : Entity, new()");
file.Content.Should().Contain("var columnNames = attrs.Select(attr => GetColumnName(attr)).ToArray();");
file.Content.Should().Contain("return service.Retrieve(entityLogicalName, id, columnSet).ToEntity<T>();");
}
}