5
5
using Microsoft . Xrm . Sdk . Metadata ;
6
6
using NSubstitute ;
7
7
using Shouldly ;
8
+ using System . Linq . Expressions ;
8
9
using System . Web ;
9
10
10
11
namespace Dataverse . ConfigurationMigrationTool . Console . Tests . Features . Import . ValueConverters ;
11
12
public class DataverseValueConverterTests
12
13
{
13
14
private readonly IMainConverter mainConverter = Substitute . For < IMainConverter > ( ) ;
14
15
private readonly DataverseValueConverter converter ;
15
- public static TheoryData < AttributeMetadata , Field , object > TestData => new ( )
16
- {
17
- {
18
- FakeAttributeMetadataBuilder . New ( )
19
- . WithLogicalName ( "firstname" )
20
- . Build < StringAttributeMetadata > ( ) ,
21
- new ( ) { Name = "firstname" , Value = "Root <""> ''é&&@ \\ /" } ,
22
- "Root <\" \" > ''é&&@ \\ /" } ,
23
- {
24
- FakeAttributeMetadataBuilder
25
- . New ( )
26
- . WithLogicalName ( "customertypecode" )
27
- . Build < PicklistAttributeMetadata > ( ) ,
28
- new ( ) { Name = "customertypecode" , Value = "1" } ,
29
- new OptionSetValue ( 1 )
30
- }
31
- } ;
16
+
32
17
public DataverseValueConverterTests ( )
33
18
{
34
19
converter = new DataverseValueConverter ( mainConverter ) ;
@@ -37,55 +22,123 @@ public DataverseValueConverterTests()
37
22
[ Fact ]
38
23
public void GivenAStringAttributeAndAField_WhenConverted_ThenItShouldConvertProperly ( )
39
24
{
40
- // Arrange
41
- var attributeMD =
42
- FakeAttributeMetadataBuilder . New ( )
43
- . WithLogicalName ( "firstname" )
44
- . Build < StringAttributeMetadata > ( ) ;
45
- var field = new Field ( ) { Name = attributeMD . LogicalName , Value = "Root <""> ''é&&@ \\ /" } ;
46
- // Act
47
- var result = converter . Convert ( attributeMD , field ) ;
48
- // Assert
49
- result . ShouldBe ( HttpUtility . HtmlDecode ( field . Value ) ) ;
25
+ var value = "Root \" \" ''é&&@ \\ /" ;
26
+ var encodedValue = HttpUtility . HtmlEncode ( value ) ;
27
+ RunTest < string , StringAttributeMetadata > ( value , encodedValue ) ;
50
28
}
51
29
[ Fact ]
52
30
public void GivenAPicklistAttributeAndAField_WhenConverted_ThenItShouldConvertProperly ( )
53
31
{
54
- // Arrange
55
32
int expectedValue = 1 ;
33
+ RunTest < OptionSetValue , PicklistAttributeMetadata > ( new OptionSetValue ( expectedValue ) , expectedValue . ToString ( ) ) ;
34
+ }
35
+ [ Fact ]
36
+ public void GivenAIntegerAttributeAndAField_WhenConverted_ThenItShouldConvertProperly ( )
37
+ {
38
+ RunTest < int ? , IntegerAttributeMetadata > ( 1 ) ;
39
+ }
40
+ [ Fact ]
41
+ public void GivenABooleanAttributeAndAField_WhenConverted_ThenItShouldConvertProperly ( )
42
+ {
43
+ RunTest < bool ? , BooleanAttributeMetadata > ( true ) ;
44
+ }
45
+ [ Fact ]
46
+ public void GivenAMoneyAttributeAndAField_WhenConverted_ThenItShouldConvertProperly ( )
47
+ {
48
+ var moneyValue = 123.4m ;
49
+ RunTest < Money , MoneyAttributeMetadata > ( new Money ( moneyValue ) , moneyValue . ToString ( ) ) ;
50
+ }
51
+ [ Fact ]
52
+ public void GivenAGuidAttributeAndAField_WhenConverted_ThenItShouldConvertProperly ( )
53
+ {
54
+ var value = Guid . NewGuid ( ) ;
55
+ RunTest < Guid ? , UniqueIdentifierAttributeMetadata > ( value , value . ToString ( ) ) ;
56
+ }
57
+ [ Fact ]
58
+ public void GivenADatetimeAttributeAndAField_WhenConverted_ThenItShouldConvertProperly ( )
59
+ {
60
+ var value = DateTime . UtcNow ;
61
+ RunTest < DateTime ? , DateTimeAttributeMetadata > ( value , value . ToString ( "o" ) ) ;
62
+ }
63
+ [ Fact ]
64
+ public void GivenADecimalAttributeAndAField_WhenConverted_ThenItShouldConvertProperly ( )
65
+ {
66
+ var value = 123.4m ;
67
+ RunTest < decimal ? , DecimalAttributeMetadata > ( value ) ;
68
+ }
69
+ [ Fact ]
70
+ public void GivenADoubleAttributeAndAField_WhenConverted_ThenItShouldConvertProperly ( )
71
+ {
72
+ var value = 123.4 ;
73
+ RunTest < double ? , DoubleAttributeMetadata > ( value ) ;
74
+ }
75
+ [ Fact ]
76
+ public void GivenALookupAttributeAndAField_WhenConverted_ThenItShouldConvertProperly ( )
77
+ {
78
+ var value = new EntityReference { Id = Guid . NewGuid ( ) , LogicalName = "randomentity" } ;
79
+ RunLookupTest ( value ) ;
80
+ }
81
+ [ Fact ]
82
+ public void GivenAnEmptyValueRegardlessOfAttributeType_WhenConverted_ThenItShouldReturnNull ( )
83
+ {
84
+ //Arrange
85
+ var field = new Field ( ) { Name = "test" , Value = string . Empty } ;
86
+ //Act
87
+ var result = converter . Convert ( new AttributeMetadata ( ) , field ) ;
88
+ //Assert
89
+ result . ShouldBeNull ( ) ;
90
+ }
91
+ [ Fact ]
92
+ public void GivenAnUnsupportedAttributeMetadata_WhenConverted_ThenItShouldThrowProperException ( )
93
+ {
94
+ //Arrange
95
+ var field = new Field ( ) { Name = "test" , Value = "125462" } ;
96
+ var amd = new MemoAttributeMetadata ( ) ;
97
+ //Act
98
+ Action act = ( ) => converter . Convert ( amd , field ) ;
99
+ //Assert
100
+ act . ShouldThrow < NotImplementedException > ( )
101
+ . Message . ShouldBe ( $ "{ amd . AttributeType . Value } is not implemented.") ;
102
+ }
103
+ private void RunTest < T , TMD > ( T expectedValue , string fieldValue = null ) where TMD : AttributeMetadata , new ( )
104
+ {
105
+ // Arrange
56
106
var attributeMD =
57
107
FakeAttributeMetadataBuilder . New ( )
58
- . WithLogicalName ( "customertypecode " )
59
- . Build < PicklistAttributeMetadata > ( ) ;
60
- var field = new Field ( ) { Name = attributeMD . LogicalName , Value = expectedValue . ToString ( ) } ;
61
- mainConverter . Convert < OptionSetValue > ( field . Value )
62
- . Returns ( new OptionSetValue ( expectedValue ) ) ;
108
+ . WithLogicalName ( "randomfield " )
109
+ . Build < TMD > ( ) ;
110
+ var field = new Field ( ) { Name = attributeMD . LogicalName , Value = fieldValue ?? expectedValue . ToString ( ) } ;
111
+ mainConverter . Convert < T > ( field . Value )
112
+ . Returns ( expectedValue ) ;
63
113
// Act
64
114
var result = converter . Convert ( attributeMD , field ) ;
65
115
// Assert
66
- result . ShouldBeOfType < OptionSetValue > ( ) ;
67
- ( ( OptionSetValue ) result ) . Value . ShouldBe ( expectedValue ) ;
116
+ result . ShouldBe ( expectedValue ) ;
68
117
mainConverter . Received ( 1 )
69
- . Convert < OptionSetValue > ( field . Value ) ;
118
+ . Convert < T > ( field . Value ) ;
70
119
}
71
- [ Fact ]
72
- public void GivenAIntegerAttributeAndAField_WhenConverted_ThenItShouldConvertProperly ( )
120
+ private void RunLookupTest ( EntityReference reference )
73
121
{
74
122
// Arrange
75
- int expectedValue = 1 ;
123
+ Expression < Predicate < Dictionary < string , string > > > extraPropertiesPredicate = d =>
124
+ d . ContainsKey ( "lookuptype" ) &&
125
+ d [ "lookuptype" ] == reference . LogicalName ;
126
+
76
127
var attributeMD =
77
128
FakeAttributeMetadataBuilder . New ( )
78
- . WithLogicalName ( "customertypecode" )
79
- . Build < IntegerAttributeMetadata > ( ) ;
80
- var field = new Field ( ) { Name = attributeMD . LogicalName , Value = expectedValue . ToString ( ) } ;
81
- mainConverter . Convert < int ? > ( field . Value )
82
- . Returns ( expectedValue ) ;
129
+ . WithLogicalName ( "randomfield" )
130
+ . Build < LookupAttributeMetadata > ( ) ;
131
+ var field = new Field ( ) { Name = attributeMD . LogicalName , Value = reference . Id . ToString ( ) , Lookupentity = reference . LogicalName } ;
132
+ mainConverter . Convert < EntityReference > (
133
+ field . Value ,
134
+ Arg . Is ( extraPropertiesPredicate )
135
+ )
136
+ . Returns ( reference ) ;
83
137
// Act
84
138
var result = converter . Convert ( attributeMD , field ) ;
85
139
// Assert
86
- result . ShouldBeOfType < int > ( ) ;
87
- ( ( int ? ) result ) . ShouldBe ( expectedValue ) ;
140
+ result . ShouldBe ( reference ) ;
88
141
mainConverter . Received ( 1 )
89
- . Convert < int ? > ( field . Value ) ;
142
+ . Convert < EntityReference > ( field . Value , Arg . Is ( extraPropertiesPredicate ) ) ;
90
143
}
91
144
}
0 commit comments