5
5
using System . Collections . Generic ;
6
6
using System . Reflection ;
7
7
using System . Threading ;
8
+ using System . Threading . Tasks ;
8
9
using Microsoft . Azure . WebJobs . Host . Bindings ;
9
10
using Microsoft . Azure . WebJobs . Host . Bindings . Data ;
10
11
using Xunit ;
@@ -13,6 +14,70 @@ namespace Microsoft.Azure.WebJobs.Host.UnitTests.Bindings.Data
13
14
{
14
15
public class DataBindingProviderTests
15
16
{
17
+ [ Fact ]
18
+ public async Task Create_HandlesNullableTypes ( )
19
+ {
20
+ // Arrange
21
+ IBindingProvider product = new DataBindingProvider ( ) ;
22
+
23
+ string parameterName = "p" ;
24
+ Type parameterType = typeof ( int ? ) ;
25
+ BindingProviderContext context = CreateBindingContext ( parameterName , parameterType ) ;
26
+
27
+ // Act
28
+ IBinding binding = await product . TryCreateAsync ( context ) ;
29
+
30
+ // Assert
31
+ Assert . NotNull ( binding ) ;
32
+
33
+ var functionBindingContext = new FunctionBindingContext ( Guid . NewGuid ( ) , CancellationToken . None , null ) ;
34
+ var valueBindingContext = new ValueBindingContext ( functionBindingContext , CancellationToken . None ) ;
35
+ var bindingData = new Dictionary < string , object >
36
+ {
37
+ { "p" , 123 }
38
+ } ;
39
+ var bindingContext = new BindingContext ( valueBindingContext , bindingData ) ;
40
+ var valueProvider = await binding . BindAsync ( bindingContext ) ;
41
+ var value = valueProvider . GetValue ( ) ;
42
+ Assert . Equal ( 123 , value ) ;
43
+
44
+ bindingData [ "p" ] = null ;
45
+ bindingContext = new BindingContext ( valueBindingContext , bindingData ) ;
46
+ valueProvider = await binding . BindAsync ( bindingContext ) ;
47
+ value = valueProvider . GetValue ( ) ;
48
+ Assert . Null ( value ) ;
49
+ }
50
+
51
+ [ Fact ]
52
+ public async Task Create_NullableTypeMismatch_ThrowsExpectedError ( )
53
+ {
54
+ // Arrange
55
+ IBindingProvider product = new DataBindingProvider ( ) ;
56
+
57
+ string parameterName = "p" ;
58
+ Type parameterType = typeof ( int ? ) ;
59
+ BindingProviderContext context = CreateBindingContext ( parameterName , parameterType ) ;
60
+
61
+ // Act
62
+ IBinding binding = await product . TryCreateAsync ( context ) ;
63
+
64
+ // Assert
65
+ Assert . NotNull ( binding ) ;
66
+
67
+ var functionBindingContext = new FunctionBindingContext ( Guid . NewGuid ( ) , CancellationToken . None , null ) ;
68
+ var valueBindingContext = new ValueBindingContext ( functionBindingContext , CancellationToken . None ) ;
69
+ var bindingData = new Dictionary < string , object >
70
+ {
71
+ { "p" , "123" }
72
+ } ;
73
+ var bindingContext = new BindingContext ( valueBindingContext , bindingData ) ;
74
+ var ex = await Assert . ThrowsAsync < InvalidOperationException > ( async ( ) =>
75
+ {
76
+ await binding . BindAsync ( bindingContext ) ;
77
+ } ) ;
78
+ Assert . Equal ( "Binding data for 'p' is not of expected type Nullable<Int32>." , ex . Message ) ;
79
+ }
80
+
16
81
[ Fact ]
17
82
public void Create_ReturnsNull_IfByRefParameter ( )
18
83
{
0 commit comments