public async Task TestFiltersWithXmlSerializer() { // While this seems ideal for a Theory test case, the IEnumerable<Product> would need to be serialize-able in // order to generate an Xunit 2.0 test case. // Some of these test cases may fail rarely because of the random data - the seed for randomization changes every hour though. var testCases = SpecialCharacterData .Union(OperatorData) .Union(StringFunctionData) .Union(DateAndTimeOfDayData); //.Union(MixQueries) 1559 //.Union*AdHocTests); 396 foreach (var testCase in testCases) { string filter = (string)testCase[0]; IEnumerable <Product> expected = (IEnumerable <Product>)testCase[1]; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, this.BaseAddress + "/api/FilterTests/GetProducts?$filter=" + filter); request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/xml")); var response = await this.Client.SendAsync(request); var result = await response.Content.ReadAsObject <IEnumerable <Product> >(); Assert.Equal(expected.Count(), result.Count()); for (int i = 0; i < expected.Count(); i++) { Assert.Equal(expected.ElementAt(i).ID, result.ElementAt(i).ID); Assert.Equal(expected.ElementAt(i).Name, result.ElementAt(i).Name); Assert.Equal(expected.ElementAt(i).Description, result.ElementAt(i).Description); } } }
public async Task TestFiltersOnAnonymousType() { // While this seems ideal for a Theory test case, the IEnumerable<Product> would need to be serialize-able in // order to generate an Xunit 2.0 test case. var testCases = SpecialCharacterData .Union(OperatorData) .Union(StringFunctionData) .Union(DateAndTimeOfDayData); //.Union(MixQueries) 1559 //.Union*AdHocTests); 396 foreach (var testCase in testCases) { string filter = (string)testCase[0]; IEnumerable <Product> expected = (IEnumerable <Product>)testCase[1]; var response = await this.Client.GetAsync(this.BaseAddress + "/api/FilterTests/GetProductsAsAnonymousType?$filter=" + filter); var result = await response.Content.ReadAsObject <IEnumerable <Product> >(); Assert.Equal(expected.Count(), result.Count()); for (int i = 0; i < expected.Count(); i++) { Assert.Equal(expected.ElementAt(i).ID, result.ElementAt(i).ID); Assert.Equal(expected.ElementAt(i).Name, result.ElementAt(i).Name); Assert.Equal(expected.ElementAt(i).Description, result.ElementAt(i).Description); } } }
public async Task TestFilters() { // While this seems ideal for a Theory test case, the IEnumerable<Product> would need to be serialize-able in // order to generate an Xunit 2.0 test case. var testCases = SpecialCharacterData .Union(OperatorData) .Union(StringFunctionData) .Union(DateAndTimeOfDayData); //.Union(MixQueries) 1559 //.Union*AdHocTests); foreach (var testCase in testCases) { string filter = (string)testCase[0]; IEnumerable <Product> expected = (IEnumerable <Product>)testCase[1]; var requestUri = this.BaseAddress + "/api/FilterTests/GetProducts?$filter=" + filter; var response = await this.Client.GetAsync(requestUri); if (response.StatusCode != HttpStatusCode.OK) { /* * This if statement is added due to that the test data is generated randomly, and sometimes the test case fails on CI, * but we have no way to investigate as both the request and response are not logged. */ // C:\Users\{user}\AppData\Local\Temp\ var now = DateTimeOffset.Now; var path = System.IO.Path.GetTempPath() + "FilterTests.TestFilters.Error." + now.ToString("yyyy-MM-dd_HH-mm-ss_fffffff.") + Guid.NewGuid().ToString() + ".log"; var traceListener = new TextWriterTraceListener(path, "FilterTests.TestFilters"); Trace.Listeners.Add(traceListener); Trace.TraceInformation("Request: {0}", requestUri); Trace.TraceError("StatusCode: {0}", response.StatusCode); Trace.TraceError(await response.Content.ReadAsStringAsync()); Trace.Flush(); Trace.Listeners.Remove(traceListener); traceListener.Close(); Assert.True(false); } var result = await response.Content.ReadAsObject <IEnumerable <Product> >(); Assert.Equal(expected.Count(), result.Count()); for (int i = 0; i < expected.Count(); i++) { Assert.Equal(expected.ElementAt(i).ID, result.ElementAt(i).ID); Assert.Equal(expected.ElementAt(i).Name, result.ElementAt(i).Name); Assert.Equal(expected.ElementAt(i).Description, result.ElementAt(i).Description); } } }