-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMovieDbFactoryTests.cs
236 lines (189 loc) · 6.89 KB
/
MovieDbFactoryTests.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System.Reflection;
namespace DM.MovieApi.IntegrationTests;
[TestClass]
public class MovieDbFactoryTests
{
[TestCleanup]
public void TestCleanup()
{
// Some of the factory tests will de-register the settings registered in AssemblyInit.
// Ensure other tests are able to run with the registered settings.
AssemblyInit.RegisterFactorySettings();
}
[TestMethod]
public async Task RegisterSettings_CanUse_RawStrings()
{
MovieDbFactory.ResetFactory();
MovieDbFactory.RegisterSettings( AssemblyInit.Settings.BearerToken );
var api = MovieDbFactory.Create<IApiMovieRequest>().Value;
ApiQueryResponse<Movie> response = await api.GetLatestAsync();
Assert.IsNull( response.Error );
Assert.IsNotNull( response.Item );
Assert.IsTrue( response.Item.Id > 391000 );
}
[TestMethod]
public void Create_ThrowsException_When_SettingsNotRegistered()
{
try
{
MovieDbFactory.ResetFactory();
MovieDbFactory.Create<IMockApiRequest>();
}
catch( InvalidOperationException ex )
{
Assert.IsTrue( ex.Message.StartsWith( "RegisterSettings must be called" ), $"Actual: {ex.Message}" );
return;
}
Assert.Fail( $"{nameof( InvalidOperationException )} was expected, but none were thrown." );
}
[TestMethod]
public void Create_ConstrainedBy_IApiRequest()
{
MethodInfo mi = typeof( MovieDbFactory ).GetMethod( nameof( MovieDbFactory.Create ) );
Assert.IsNotNull( mi );
Type t1 = mi.GetGenericArguments()
.Single()
.GetGenericParameterConstraints()
.Single();
Assert.AreEqual( typeof( IApiRequest ), t1 );
}
[TestMethod]
public void Create_Returns_Lazy_IApiMovieRequest()
{
Lazy<IApiMovieRequest> lazy = MovieDbFactory.Create<IApiMovieRequest>();
Assert.IsNotNull( lazy );
Assert.IsFalse( lazy.IsValueCreated );
IApiMovieRequest api = lazy.Value;
Assert.IsTrue( lazy.IsValueCreated );
Assert.IsNotNull( api );
}
[TestMethod]
public void GetAllApiRequests_CanCreate_IMovieApi()
{
IMovieDbApi api;
try
{
api = MovieDbFactory.GetAllApiRequests();
}
catch( NotImplementedException )
{
return;
}
Assert.Fail( $"See the {nameof( GetAllApiRequests_IsDisabled_WithEx )} for expected failure." );
// ReSharper disable HeuristicUnreachableCode
Assert.IsNotNull( api );
PropertyInfo[] dbApi = typeof( IMovieDbApi )
.GetProperties()
.Where( x => typeof( IApiRequest ).IsAssignableFrom( x.PropertyType ) )
.ToArray();
foreach( PropertyInfo pi in dbApi )
{
var val = pi.GetValue( api ) as IApiRequest;
Assert.IsNotNull( val );
}
// ReSharper restore HeuristicUnreachableCode
}
[TestMethod]
public void ResetFactory_Sets_IsFactoryComposed()
{
MovieDbFactory.ResetFactory();
Assert.IsFalse( MovieDbFactory.IsFactoryComposed );
AssemblyInit.RegisterFactorySettings();
Assert.IsTrue( MovieDbFactory.IsFactoryComposed );
MovieDbFactory.ResetFactory();
Assert.IsFalse( MovieDbFactory.IsFactoryComposed );
}
[TestMethod]
public void GetAllApiRequests_IsDisabled_WithEx()
{
try
{
MovieDbFactory.GetAllApiRequests();
}
catch( NotImplementedException )
{
return;
}
Assert.Fail( $"{nameof( MovieDbFactory.GetAllApiRequests )} " +
$"should be disabled with a {nameof( NotImplementedException )}." );
}
[TestMethod]
public void ImportingConstructorAttribute_IsMissing_ThrowsEx()
{
try
{
// ReSharper disable once UnusedVariable
var api = MovieDbFactory.Create<IMockApiRequestMultipleCtors>().Value;
}
catch( InvalidOperationException ex )
{
Assert.IsTrue( ex.Message.StartsWith( "Multiple public constructors found." ),
$"Actual: {ex.Message}" );
Assert.IsTrue( ex.Message.Contains( nameof( ImportingConstructorAttribute ) ),
$"Actual: {ex.Message}" );
return;
}
Assert.Fail( $"{nameof( MovieDbFactory.Create )} should throw an exception " +
"when multiple ctors are found without an decorated ctor for importing." );
}
[TestMethod]
public void IApiRequest_WithoutImplementation_ThrowsEx()
{
try
{
// ReSharper disable once UnusedVariable
var api = MovieDbFactory.Create<IMockApiRequestNotImplemented>().Value;
}
catch( NotSupportedException ex )
{
Assert.AreEqual( ex.Message,
$"{nameof( IMockApiRequestNotImplemented )} must have a concrete implementation." );
return;
}
Assert.Fail( $"{nameof( MovieDbFactory.Create )} should throw an exception " +
"when no concrete implementation is found." );
}
[TestMethod]
public void BearerToken_MustBe_GreaterThan_200chars()
{
const int minLength = 201;
AssertRegisterSettingsThrowsEx( null );
AssertRegisterSettingsThrowsEx( "" );
AssertRegisterSettingsThrowsEx( " " );
AssertRegisterSettingsThrowsEx( new string( '-', minLength - 1 ) );
MovieDbFactory.ResetFactory();
MovieDbFactory.RegisterSettings( new string( '*', minLength ) );
}
private void AssertRegisterSettingsThrowsEx( string bearerToken )
{
MovieDbFactory.ResetFactory();
try
{
MovieDbFactory.RegisterSettings( bearerToken );
Assert.Fail( "Bearer Token < 200 chars was expected to throw ex." );
}
catch( ArgumentException ex )
{
string msg = ex.Message;
Assert.IsTrue( msg.StartsWith( "Must provide a valid TheMovieDb.org Bearer token." ) );
Assert.IsTrue( msg.Contains( "A valid token can be found in your account page, under the API section." ) );
Assert.IsTrue( msg.Contains( "API Read Access Token" ) );
}
Assert.IsFalse( MovieDbFactory.IsFactoryComposed );
}
[SuppressMessage( "ReSharper", "UnusedType.Local" )]
private class MockApiRequestMultipleCtors : IMockApiRequestMultipleCtors
{
public MockApiRequestMultipleCtors()
{ }
[SuppressMessage( "ReSharper", "UnusedParameter.Local" )]
public MockApiRequestMultipleCtors( string val )
{ }
}
private interface IMockApiRequestMultipleCtors : IApiRequest
{ }
private interface IMockApiRequestNotImplemented : IApiRequest
{ }
private interface IMockApiRequest : IApiRequest
{ }
}