C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
BaseMutableResourceTestSuite.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq.Expressions;
4using System.Net;
5using AnalyzeRe;
10#if MSTEST
11using Microsoft.VisualStudio.TestTools.UnitTesting;
12#elif NUNIT
13using NUnit.Framework;
14using TestClass = NUnit.Framework.TestFixtureAttribute;
15using TestMethod = NUnit.Framework.TestAttribute;
16using TestCategory = NUnit.Framework.CategoryAttribute;
17#endif
18
20{
21 [TestClass]
23 {
24 private const string TypeName = "BaseMutableResourceTest";
25
26 [TestMethod, TestCategory(TypeName)]
27 public virtual void Test_Resource_PUT_NoChanges()
28 {
29 // Show that PUT succeeds for mutable resources (even if no changes were made)
31 }
32
33 #region description
34 [TestMethod, TestCategory(TypeName)]
35 public virtual void Test_Resource_PUT_Description()
36 {
37 PUT_WithValue(a => a.description, "Modified Description", true);
38 }
39
40 [TestMethod, TestCategory(TypeName)]
42 {
43 PUT_Attribute_Null(a => a.description);
44 }
45
46 [TestMethod, TestCategory(TypeName)]
48 {
49 PUT_StringAttribute_Empty(a => a.description);
50 }
51 #endregion description
52
53 #region created and modified
54 [TestMethod, TestCategory(TypeName)]
56 {
58 {
59 T put = default(T);
60 AssertApi.MethodIsAllowed(delegate { put = posted.Put(); }, "PUT", true);
61
62 Assert.IsNotNull(put.created, "Expected a 'created' date to be assigned.");
63 Assert.AreEqual(posted.created, put.created,
64 "Created date changed, but it shouldn't have!");
65 Assert.IsNotNull(put.modified, "Expected a 'modified' date to be assigned.");
66 Assert.AreNotEqual(put.created, put.modified,
67 "Modified date is still the same as created date!");
68 });
69 }
70 #endregion created and modified
71
72 #region meta_data
73 [TestMethod, TestCategory(TypeName)]
74 public virtual void Test_Resource_PUT_Metadata_Null()
75 {
76 PUT_Attribute_Null(a => a.meta_data);
77 }
78 [TestMethod, TestCategory(TypeName)]
80 {
81 PUT_WithValue(l => l.meta_data, new Dictionary<string, object>(), true);
82 }
83 [TestMethod, TestCategory(TypeName)]
85 {
87 {
88 { "Test1", "TestValue" },
89 { "Test2", 2 },
90 { "Test 3", 3.33 },
91 { "Test 4", true }
92 };
93 PUT_WithValue(l => l.meta_data, mixedTypes, true);
94 }
95 [TestMethod, TestCategory(TypeName)]
97 {
98 Dictionary<string, object> TestDict(string key, object value) =>
99 new Dictionary<string, object> { { key, value } };
100
101 PUT_WithValue(l => l.meta_data, TestDict("Test1", TestDict("Nested", "Nested")), false);
102 PUT_WithValue(l => l.meta_data, TestDict("Test1", null), false);
103 PUT_WithValue(l => l.meta_data, TestDict("Test1", Double.PositiveInfinity), false);
104 PUT_WithValue(l => l.meta_data, TestDict("Test1", Double.NaN), false);
105 }
106 #endregion meta_data
107
108 #region PUT Test Method Helpers
119 GenericTest.POST_ThenDoAction(validToPost, putTest);
120
135
141 {
142 T putResponse = default(T);
144 {
147 AssertApi.PutResponseMatches(posted, toPut, putResponse);
148 // New: Perform additional validation tests to ensure the new resource
149 // doesn't result in errors that only manifest themselves downstream.
151 });
152 return putResponse;
153 }
154
163
177 {
178 if (!Equals(default(TProperty), null))
179 throw new ArgumentException("Can only be used on properties whose default type is null.");
182 }
183
187 bool? shouldSucceed = null)
188 {
189 if (!shouldSucceed.HasValue)
192 // TODO: Do a message check.
193 // Output.MsgMissingAttribute(((MemberExpression)PropertyExpression.Body).Member.Name, validResource._type));
194 }
195
196 #region List Attribute Test Helpers
199 {
200 if (!shouldSucceed.HasValue)
204 // TODO: Do a message check.
205 // Output.MsgMissingAttribute(((MemberExpression)PropertyExpression.Body).Member.Name, validResource._type));
206 }
211 {
214 // Perform the PUT
216 // TODO: Do a message check.
217 // Output.MsgMissingAttribute(((MemberExpression)PropertyExpression.Body).Member.Name, validResource._type));
218 }
219 #endregion List Attribute Test Helpers
220
221 #region Reference Attribute Test Helpers
265 #endregion Reference Attribute Test Helpers
266 #endregion Test Method Helpers
267 }
268}
void PUT_ListAttribute_Duplicates< TItem, TValue >(Expression< Func< T, ICollection< TItem > > > PropertyExpression, TValue valueToDuplicate, bool shouldSucceed)
virtual T PUT_WithChanges_Succeeds(Func< T, T > modifyTestResource)
POST a valid resource, then test doing a PUT on the resource after modifying it in some way,...
T PUT_WithValue< TProperty >(Expression< Func< T, TProperty > > PropertyExpression, TProperty newValue, bool shouldSucceed)
POST a valid resource, then test doing a PUT on the resource after changing a specified attribute.
void PUT_ListAttribute_Empty< TItem >(Expression< Func< T, ICollection< TItem > > > PropertyExpression, bool? shouldSucceed=null)
void PUT_Reference_NullId< TRefType >(Expression< Func< T, IReference< TRefType > > > PropertyExpression)
Modifies a reference with a null Id and verifies the correct error is returned.
void PUT_Reference_NonExistantId< TRefType >(Expression< Func< T, IReference< TRefType > > > PropertyExpression)
Modifies a reference with a non-existent Id and verifies the correct error is returned.
T PUT_WithChanges_Fails(Func< T, T > modifyTestResource)
POST a valid resource, then test doing a PUT on the resource after modifying it in some way,...
void PUT_ReferenceList_NonExistantId< TRefType >(Expression< Func< T, ICollection< IReference< TRefType > > > > PropertyExpression)
Modifies a reference list with a non-existent Id and verifies the correct error is returned.
void PUT_Attribute_Null< TProperty >(Expression< Func< T, TProperty > > PropertyExpression)
Ensure that when doing a PUT with the specified attribute null, the original value of the attribute i...
void PUT_StringAttribute_Empty(Expression< Func< T, string > > PropertyExpression, bool? shouldSucceed=null)
Ensure that when doing a PUT with the specified attribute set to an empty string, the PUT succeeds or...
void PUT_Reference_EmptyStringId< TRefType >(Expression< Func< T, IReference< TRefType > > > PropertyExpression)
Modifies a reference with a empty string Id and verifies the correct error is returned.
virtual T POST_ForPutTest(T validToPost, Action< T > putTest)
Used for all PUT tests and PUT helper methods. Similar to the base helper method BaseResourceTestSuit...
virtual void AdditionalValidResourceTests(T posted)
Deriving classes can optionally override this function to perform additional validation on every succ...
virtual T TestResource
The resource used for all unit tests that require a valid prepared but unPOSTed resource.
Exposes sample resource objects, with built-in methods for injecting dependencies.
Definition Samples.cs:14
static string Valid_NonExistant_UUID
Definition Samples.cs:41
static void MethodIsAllowed(Action request, string methodName, bool methodAllowed=true)
Wrap a request in a tryGet with some formatting for testing purposes.
Definition AssertApi.cs:98
static Action< APIRequestException > ApiExceptionTest(HttpStatusCode expectedStatusCode)
Generate a function that will test a REST request exception in a standard way.
Definition AssertApi.cs:539
Generic Unit test implementations that will test REST methods on arbitrary resources.
static void ExecuteWithConsoleErrorLogging(Action action, Func< string > additionalContext=null)
Wraps the call to the action with a try/catch that doesn't handle the exception, just logs some usefu...
Describes a collection of resources which can be listed.
Specifies that the enumerable property with this attribute cannot be empty when POSTing the resource ...
Specifies that the property with this attribute cannot be null when POSTing the resource to the serve...
Utilities that reflect on a type or property expression.
Interface for Base class used by all stored resources.
Interface for Base class used by all resources.