1
+
1
2
using System ;
2
3
using System . Collections . Generic ;
3
4
using System . Net ;
13
14
14
15
namespace Parse . Tests ;
15
16
16
- #warning Class refactoring requires completion.
17
-
18
17
[ TestClass ]
19
18
public class CloudControllerTests
20
19
{
21
- ParseClient Client { get ; set ; }
20
+ private Mock < IParseCommandRunner > _mockRunner ;
21
+ private ParseCloudCodeController _cloudCodeController ;
22
+ private ParseClient Client { get ; set ; }
22
23
23
24
[ TestInitialize ]
24
- public void SetUp ( ) => Client = new ParseClient ( new ServerConnectionData { ApplicationID = "" , Key = "" , Test = true } ) ;
25
+ public void SetUp ( )
26
+ {
27
+ Client = new ParseClient ( new ServerConnectionData { ApplicationID = "" , Key = "" , Test = true } ) ;
28
+ _mockRunner = new Mock < IParseCommandRunner > ( ) ;
29
+ }
30
+
31
+ [ TestCleanup ]
32
+ public void Cleanup ( )
33
+ {
34
+ _mockRunner = null ;
35
+ _cloudCodeController = null ;
36
+ Client = null ;
37
+ }
38
+
25
39
26
40
[ TestMethod ]
27
41
public async Task TestEmptyCallFunction ( )
28
42
{
29
- // Arrange: Create a mock runner that simulates a response with an accepted status but no data
30
- var mockRunner = CreateMockRunner (
31
- new Tuple < HttpStatusCode , IDictionary < string , object > > ( HttpStatusCode . Accepted , null )
32
- ) ;
43
+ // Arrange: Setup mock runner and controller
44
+
45
+ _mockRunner . Setup ( obj => obj . RunCommandAsync (
46
+ It . IsAny < ParseCommand > ( ) ,
47
+ It . IsAny < IProgress < IDataTransferLevel > > ( ) ,
48
+ It . IsAny < IProgress < IDataTransferLevel > > ( ) ,
49
+ It . IsAny < CancellationToken > ( )
50
+ ) ) . Returns ( Task . FromResult ( new Tuple < HttpStatusCode , IDictionary < string , object > > ( HttpStatusCode . Accepted , null ) ) ) ;
51
+
52
+ _cloudCodeController = new ParseCloudCodeController ( _mockRunner . Object , Client . Decoder ) ;
33
53
34
- var controller = new ParseCloudCodeController ( mockRunner . Object , Client . Decoder ) ;
35
54
36
55
// Act & Assert: Call the function and verify the task faults as expected
37
56
try
38
57
{
39
- await controller . CallFunctionAsync < string > ( "someFunction" , null , null , Client , CancellationToken . None ) ;
58
+ await _cloudCodeController . CallFunctionAsync < string > ( "someFunction" , null , null , Client , CancellationToken . None ) ;
40
59
Assert . Fail ( "Expected the task to fault, but it succeeded." ) ;
41
60
}
42
61
catch ( ParseFailureException ex )
43
62
{
44
63
Assert . AreEqual ( ParseFailureException . ErrorCode . OtherCause , ex . Code ) ;
45
64
Assert . AreEqual ( "Cloud function returned no data." , ex . Message ) ;
46
65
}
47
-
48
66
}
49
67
50
68
51
69
[ TestMethod ]
52
70
public async Task TestCallFunction ( )
53
71
{
54
- // Arrange: Create a mock runner with a predefined response
72
+ // Arrange: Setup mock runner and controller with a response
55
73
var responseDict = new Dictionary < string , object > { [ "result" ] = "gogo" } ;
56
- var response = new Tuple < HttpStatusCode , IDictionary < string , object > > ( HttpStatusCode . Accepted , responseDict ) ;
57
- var mockRunner = CreateMockRunner ( response ) ;
74
+ _mockRunner . Setup ( obj => obj . RunCommandAsync (
75
+ It . IsAny < ParseCommand > ( ) ,
76
+ It . IsAny < IProgress < IDataTransferLevel > > ( ) ,
77
+ It . IsAny < IProgress < IDataTransferLevel > > ( ) ,
78
+ It . IsAny < CancellationToken > ( )
79
+ ) ) . Returns ( Task . FromResult ( new Tuple < HttpStatusCode , IDictionary < string , object > > ( HttpStatusCode . Accepted , responseDict ) ) ) ;
80
+
81
+ _cloudCodeController = new ParseCloudCodeController ( _mockRunner . Object , Client . Decoder ) ;
58
82
59
- var cloudCodeController = new ParseCloudCodeController ( mockRunner . Object , Client . Decoder ) ;
60
83
61
84
// Act: Call the function and capture the result
62
- var result = await cloudCodeController . CallFunctionAsync < string > (
85
+ var result = await _cloudCodeController . CallFunctionAsync < string > (
63
86
"someFunction" ,
64
87
parameters : null ,
65
88
sessionToken : null ,
@@ -76,24 +99,29 @@ public async Task TestCallFunction()
76
99
[ TestMethod ]
77
100
public async Task TestCallFunctionWithComplexType ( )
78
101
{
79
- // Arrange: Create a mock runner with a complex type response
102
+ // Arrange: Setup mock runner and controller with a complex type response
80
103
var complexResponse = new Dictionary < string , object >
81
104
{
82
105
{ "result" , new Dictionary < string , object >
83
- {
84
- { "fosco" , "ben" } ,
85
- { "list" , new List < object > { 1 , 2 , 3 } }
86
- }
106
+ {
107
+ { "fosco" , "ben" } ,
108
+ { "list" , new List < object > { 1 , 2 , 3 } }
109
+ }
87
110
}
88
111
} ;
89
- var mockRunner = CreateMockRunner (
90
- new Tuple < HttpStatusCode , IDictionary < string , object > > ( HttpStatusCode . Accepted , complexResponse )
91
- ) ;
92
112
93
- var cloudCodeController = new ParseCloudCodeController ( mockRunner . Object , Client . Decoder ) ;
113
+ _mockRunner . Setup ( obj => obj . RunCommandAsync (
114
+ It . IsAny < ParseCommand > ( ) ,
115
+ It . IsAny < IProgress < IDataTransferLevel > > ( ) ,
116
+ It . IsAny < IProgress < IDataTransferLevel > > ( ) ,
117
+ It . IsAny < CancellationToken > ( )
118
+ ) ) . Returns ( Task . FromResult ( new Tuple < HttpStatusCode , IDictionary < string , object > > ( HttpStatusCode . Accepted , complexResponse ) ) ) ;
119
+
120
+ _cloudCodeController = new ParseCloudCodeController ( _mockRunner . Object , Client . Decoder ) ;
121
+
94
122
95
123
// Act: Call the function with a complex return type
96
- var result = await cloudCodeController . CallFunctionAsync < IDictionary < string , object > > (
124
+ var result = await _cloudCodeController . CallFunctionAsync < IDictionary < string , object > > (
97
125
"someFunction" ,
98
126
parameters : null ,
99
127
sessionToken : null ,
@@ -107,25 +135,32 @@ public async Task TestCallFunctionWithComplexType()
107
135
Assert . AreEqual ( "ben" , result [ "fosco" ] ) ;
108
136
Assert . IsInstanceOfType ( result [ "list" ] , typeof ( IList < object > ) ) ;
109
137
}
138
+
110
139
[ TestMethod ]
111
140
public async Task TestCallFunctionWithWrongType ( )
112
141
{
113
142
// a mock runner with a response that doesn't match the expected type
143
+
114
144
var wrongTypeResponse = new Dictionary < string , object >
115
- {
116
- { "result" , "gogo" }
117
- } ;
118
- var mockRunner = CreateMockRunner (
119
- new Tuple < HttpStatusCode , IDictionary < string , object > > ( HttpStatusCode . Accepted , wrongTypeResponse )
120
- ) ;
145
+ {
146
+ { "result" , "gogo" }
147
+ } ;
148
+
149
+ _mockRunner . Setup ( obj => obj . RunCommandAsync (
150
+ It . IsAny < ParseCommand > ( ) ,
151
+ It . IsAny < IProgress < IDataTransferLevel > > ( ) ,
152
+ It . IsAny < IProgress < IDataTransferLevel > > ( ) ,
153
+ It . IsAny < CancellationToken > ( )
154
+ ) ) . Returns ( Task . FromResult ( new Tuple < HttpStatusCode , IDictionary < string , object > > ( HttpStatusCode . Accepted , wrongTypeResponse ) ) ) ;
121
155
122
- var cloudCodeController = new ParseCloudCodeController ( mockRunner . Object , Client . Decoder ) ;
156
+
157
+ _cloudCodeController = new ParseCloudCodeController ( _mockRunner . Object , Client . Decoder ) ;
123
158
124
159
// Act & Assert: Expect the call to fail with a ParseFailureException || This is fun!
125
160
126
161
await Assert . ThrowsExceptionAsync < ParseFailureException > ( async ( ) =>
127
162
{
128
- await cloudCodeController . CallFunctionAsync < int > (
163
+ await _cloudCodeController . CallFunctionAsync < int > (
129
164
"someFunction" ,
130
165
parameters : null ,
131
166
sessionToken : null ,
@@ -134,20 +169,4 @@ await cloudCodeController.CallFunctionAsync<int>(
134
169
) ;
135
170
} ) ;
136
171
}
137
-
138
-
139
-
140
- private Mock < IParseCommandRunner > CreateMockRunner ( Tuple < HttpStatusCode , IDictionary < string , object > > response )
141
- {
142
- var mockRunner = new Mock < IParseCommandRunner > ( ) ;
143
- mockRunner . Setup ( obj => obj . RunCommandAsync (
144
- It . IsAny < ParseCommand > ( ) ,
145
- It . IsAny < IProgress < IDataTransferLevel > > ( ) ,
146
- It . IsAny < IProgress < IDataTransferLevel > > ( ) ,
147
- It . IsAny < CancellationToken > ( )
148
- ) ) . Returns ( Task . FromResult ( response ) ) ;
149
-
150
- return mockRunner ;
151
- }
152
-
153
- }
172
+ }
0 commit comments