-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathFileControllerTests.cs
118 lines (99 loc) · 3.77 KB
/
FileControllerTests.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Parse.Abstractions.Infrastructure;
using Parse.Abstractions.Infrastructure.Execution;
using Parse.Infrastructure.Execution;
using Parse.Platform.Files;
namespace Parse.Tests;
[TestClass]
public class FileControllerTests
{
[TestMethod]
public async Task TestFileControllerSaveWithInvalidResultAsync()
{
var response = new Tuple<HttpStatusCode, IDictionary<string, object>>(HttpStatusCode.Accepted, null);
var mockRunner = CreateMockRunner(response);
var state = new FileState
{
Name = "bekti.png",
MediaType = "image/png"
};
var controller = new ParseFileController(mockRunner.Object);
await Assert.ThrowsExceptionAsync<NullReferenceException>(async () =>
{
await controller.SaveAsync(state, new MemoryStream(), null, null);
});
}
[TestMethod]
public async Task TestFileControllerSaveWithEmptyResultAsync()
{
var response = new Tuple<HttpStatusCode, IDictionary<string, object>>(HttpStatusCode.Accepted, new Dictionary<string, object>());
var mockRunner = CreateMockRunner(response);
var state = new FileState
{
Name = "bekti.png",
MediaType = "image/png"
};
var controller = new ParseFileController(mockRunner.Object);
await Assert.ThrowsExceptionAsync<KeyNotFoundException>(async () =>
{
await controller.SaveAsync(state, new MemoryStream(), null, null);
});
}
[TestMethod]
public async Task TestFileControllerSaveWithIncompleteResultAsync()
{
var response = new Tuple<HttpStatusCode, IDictionary<string, object>>(HttpStatusCode.Accepted, new Dictionary<string, object> { ["name"] = "newBekti.png" });
var mockRunner = CreateMockRunner(response);
var state = new FileState
{
Name = "bekti.png",
MediaType = "image/png"
};
var controller = new ParseFileController(mockRunner.Object);
await Assert.ThrowsExceptionAsync<KeyNotFoundException>(async () =>
{
await controller.SaveAsync(state, new MemoryStream(), null, null);
});
}
[TestMethod]
public async Task TestFileControllerSaveAsync()
{
var state = new FileState
{
Name = "bekti.png",
MediaType = "image/png"
};
var mockRunner = CreateMockRunner(new Tuple<HttpStatusCode, IDictionary<string, object>>(
HttpStatusCode.Accepted,
new Dictionary<string, object>
{
["name"] = "newBekti.png",
["url"] = "https://www.parse.com/newBekti.png"
}));
var controller = new ParseFileController(mockRunner.Object);
var newState = await controller.SaveAsync(state, new MemoryStream(), null, null);
// Assertions
Assert.AreEqual(state.MediaType, newState.MediaType);
Assert.AreEqual("newBekti.png", newState.Name);
Assert.AreEqual("https://www.parse.com/newBekti.png", newState.Location.AbsoluteUri);
}
private Mock<IParseCommandRunner> CreateMockRunner(Tuple<HttpStatusCode, IDictionary<string, object>> response)
{
var mockRunner = new Mock<IParseCommandRunner>();
mockRunner
.Setup(obj => obj.RunCommandAsync(
It.IsAny<ParseCommand>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<IProgress<IDataTransferLevel>>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(response);
return mockRunner;
}
}