Skip to content

Commit ad39fd7

Browse files
committed
Fix tests, cleanup warning (deprecations)
1 parent bbe0375 commit ad39fd7

File tree

17 files changed

+47
-69
lines changed

17 files changed

+47
-69
lines changed

src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsCustomController.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ public class CustomJsonApiController<T, TId>
3939
private readonly IResourceService<T, TId> _resourceService;
4040
private readonly IJsonApiContext _jsonApiContext;
4141

42-
protected IActionResult UnprocessableEntity()
43-
{
44-
return new StatusCodeResult(422);
45-
}
46-
4742
protected IActionResult Forbidden()
4843
{
4944
return new StatusCodeResult(403);

src/JsonApiDotNetCore/Controllers/JsonApiControllerMixin.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ namespace JsonApiDotNetCore.Controllers
77
{
88
public abstract class JsonApiControllerMixin : ControllerBase
99
{
10-
protected IActionResult UnprocessableEntity()
11-
{
12-
return new StatusCodeResult(422);
13-
}
14-
1510
protected IActionResult Forbidden()
1611
{
1712
return new StatusCodeResult(403);
@@ -23,18 +18,19 @@ protected IActionResult Error(Error error)
2318
{
2419
Errors = new List<Error> { error }
2520
};
26-
var result = new ObjectResult(errorCollection);
27-
result.StatusCode = error.StatusCode;
2821

29-
return result;
22+
return new ObjectResult(errorCollection)
23+
{
24+
StatusCode = error.StatusCode
25+
};
3026
}
3127

3228
protected IActionResult Errors(ErrorCollection errors)
3329
{
34-
var result = new ObjectResult(errors);
35-
result.StatusCode = GetErrorStatusCode(errors);
36-
37-
return result;
30+
return new ObjectResult(errors)
31+
{
32+
StatusCode = GetErrorStatusCode(errors)
33+
};
3834
}
3935

4036
private int GetErrorStatusCode(ErrorCollection errors)

test/JsonApiDotNetCoreExampleTests/Acceptance/Extensibility/CustomErrorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class CustomErrorTests
1111
public void Can_Return_Custom_Error_Types()
1212
{
1313
// arrange
14-
var error = new CustomError("507", "title", "detail", "custom");
14+
var error = new CustomError(507, "title", "detail", "custom");
1515
var errorCollection = new ErrorCollection();
1616
errorCollection.Add(error);
1717

@@ -36,7 +36,7 @@ public void Can_Return_Custom_Error_Types()
3636
}
3737

3838
class CustomError : Error {
39-
public CustomError(string status, string title, string detail, string myProp)
39+
public CustomError(int status, string title, string detail, string myProp)
4040
: base(status, title, detail)
4141
{
4242
MyCustomProperty = myProp;

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/AttributeFilterTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public async Task Can_Filter_On_Not_Equal_Values()
131131

132132
// assert
133133
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
134-
Assert.False(deserializedTodoItems.Any(i => i.Ordinal == todoItem.Ordinal));
134+
Assert.DoesNotContain(deserializedTodoItems, x => x.Ordinal == todoItem.Ordinal);
135135
}
136136

137137
[Fact]
@@ -170,8 +170,8 @@ public async Task Can_Filter_On_In_Array_Values()
170170
Assert.Equal(guids.Count(), deserializedTodoItems.Count());
171171
foreach (var item in deserializedTodoItems)
172172
{
173-
Assert.True(guids.Contains(item.GuidProperty));
174-
Assert.False(notInGuids.Contains(item.GuidProperty));
173+
Assert.Contains(item.GuidProperty, guids);
174+
Assert.DoesNotContain(item.GuidProperty, notInGuids);
175175
}
176176
}
177177

@@ -207,7 +207,7 @@ public async Task Can_Filter_On_Related_In_Array_Values()
207207
Assert.NotNull(included);
208208
Assert.NotEmpty(included);
209209
foreach (var item in included)
210-
Assert.True(ownerFirstNames.Contains(item.Attributes["first-name"]));
210+
Assert.Contains(item.Attributes["first-name"], ownerFirstNames);
211211

212212
}
213213
}

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/DocumentTests/Included.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Linq;
33
using System.Net;
44
using System.Net.Http;
@@ -174,7 +174,7 @@ public async Task GET_Included_DoesNot_Duplicate_Records_ForMultipleRelationship
174174
// assert
175175
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
176176
Assert.NotEmpty(documents.Included);
177-
Assert.Equal(1, documents.Included.Count);
177+
Assert.Single(documents.Included);
178178
}
179179

180180
[Fact]
@@ -209,7 +209,7 @@ public async Task GET_Included_DoesNot_Duplicate_Records_If_HasOne_Exists_Twice(
209209
// assert
210210
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
211211
Assert.NotEmpty(documents.Included);
212-
Assert.Equal(1, documents.Included.Count);
212+
Assert.Single(documents.Included);
213213
}
214214

215215
[Fact]

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/DocumentTests/PagingTests.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using System.Net;
1+
using System.Net;
22
using System.Net.Http;
33
using System.Threading.Tasks;
44
using JsonApiDotNetCoreExample;
55
using Microsoft.AspNetCore.Hosting;
66
using Microsoft.AspNetCore.TestHost;
77
using Newtonsoft.Json;
88
using Xunit;
9-
using Person = JsonApiDotNetCoreExample.Models.Person;
109
using JsonApiDotNetCore.Models;
1110
using JsonApiDotNetCoreExample.Data;
1211
using Bogus;
@@ -18,28 +17,16 @@ namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec.DocumentTests
1817
[Collection("WebHostCollection")]
1918
public class PagingTests
2019
{
21-
private TestFixture<TestStartup> _fixture;
22-
private AppDbContext _context;
23-
private Faker<Person> _personFaker;
24-
private Faker<TodoItem> _todoItemFaker;
25-
private Faker<TodoItemCollection> _todoItemCollectionFaker;
26-
private DateTime CurrentTime;
20+
private readonly AppDbContext _context;
21+
private readonly Faker<TodoItem> _todoItemFaker;
2722

2823
public PagingTests(TestFixture<TestStartup> fixture)
2924
{
30-
_fixture = fixture;
3125
_context = fixture.GetService<AppDbContext>();
32-
_personFaker = new Faker<Person>()
33-
.RuleFor(p => p.FirstName, f => f.Name.FirstName())
34-
.RuleFor(p => p.LastName, f => f.Name.LastName());
35-
3626
_todoItemFaker = new Faker<TodoItem>()
3727
.RuleFor(t => t.Description, f => f.Lorem.Sentence())
3828
.RuleFor(t => t.Ordinal, f => f.Random.Number())
3929
.RuleFor(t => t.CreatedDate, f => f.Date.Past());
40-
41-
_todoItemCollectionFaker = new Faker<TodoItemCollection>()
42-
.RuleFor(t => t.Name, f => f.Company.CatchPhrase());
4330
}
4431

4532
[Fact]

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/QueryParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public async Task Server_Returns_400_ForUnknownQueryParam()
3939

4040
// assert
4141
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
42-
Assert.Equal(1, body.Errors.Count);
42+
Assert.Single(body.Errors);
4343
Assert.Equal($"[{queryKey}, {queryValue}] is not a valid query.", body.Errors[0].Title);
4444
}
4545
}

test/OperationsExampleTests/Add/AddTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public async Task Can_Create_Author_With_Article()
156156
Assert.Equal(author.Name, lastAuthor.Name);
157157

158158
// article validation
159-
Assert.Equal(1, lastAuthor.Articles.Count);
159+
Assert.Single(lastAuthor.Articles);
160160
Assert.Equal(article.Name, lastAuthor.Articles[0].Name);
161161
Assert.Equal(articleOperationResult.DataObject.Id, lastAuthor.Articles[0].StringId);
162162
}

test/OperationsExampleTests/Get/GetByIdTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public async Task Can_Get_Author_By_Id()
3636
};
3737

3838
// act
39-
var result = await PatchAsync<OperationsDocument>("api/bulk", content);
39+
var (response, data) = await PatchAsync<OperationsDocument>("api/bulk", content);
4040

4141
// assert
42-
Assert.NotNull(result.response);
43-
Assert.NotNull(result.data);
44-
Assert.Equal(HttpStatusCode.OK, result.response.StatusCode);
45-
Assert.Equal(1, result.data.Operations.Count);
46-
Assert.Equal(author.Id.ToString(), result.data.Operations.Single().DataObject.Id);
42+
Assert.NotNull(response);
43+
Assert.NotNull(data);
44+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
45+
Assert.Single(data.Operations);
46+
Assert.Equal(author.Id.ToString(), data.Operations.Single().DataObject.Id);
4747
}
4848

4949
[Fact]
@@ -69,7 +69,7 @@ public async Task Get_Author_By_Id_Returns_404_If_NotFound()
6969
Assert.NotNull(response);
7070
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
7171
Assert.NotNull(data);
72-
Assert.Equal(1, data.Errors.Count);
72+
Assert.Single(data.Errors);
7373
Assert.True(data.Errors[0].Detail.Contains("authors"), "The error detail should contain the name of the entity that could not be found.");
7474
Assert.True(data.Errors[0].Detail.Contains(authorId), "The error detail should contain the entity id that could not be found");
7575
Assert.True(data.Errors[0].Title.Contains("operation[0]"), "The error title should contain the operation identifier that failed");

test/OperationsExampleTests/Get/GetRelationshipTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public async Task Can_Get_Article_Author()
4343
Assert.NotNull(response);
4444
Assert.NotNull(data);
4545
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
46-
Assert.Equal(1, data.Operations.Count);
46+
Assert.Single(data.Operations);
4747
var resourceObject = data.Operations.Single().DataObject;
4848
Assert.Equal(author.Id.ToString(), resourceObject.Id);
4949
Assert.Equal("authors", resourceObject.Type);

test/OperationsExampleTests/Get/GetTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task Can_Get_Authors()
4444
Assert.NotNull(result.response);
4545
Assert.NotNull(result.data);
4646
Assert.Equal(HttpStatusCode.OK, result.response.StatusCode);
47-
Assert.Equal(1, result.data.Operations.Count);
47+
Assert.Single(result.data.Operations);
4848
Assert.Equal(expectedCount, result.data.Operations.Single().DataList.Count);
4949
}
5050
}

test/OperationsExampleTests/Transactions/TransactionFailureTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public async Task Cannot_Create_Author_If_Article_Creation_Fails()
6868
// for now, it is up to application implementations to perform validation and
6969
// provide the proper HTTP response code
7070
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
71-
Assert.Equal(1, data.Errors.Count);
71+
Assert.Single(data.Errors);
7272
Assert.Contains("operation[1] (add)", data.Errors[0].Title);
7373

7474
var dbAuthors = await context.Authors.Where(a => a.Name == author.Name).ToListAsync();

test/OperationsExampleTests/Update/UpdateTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public async Task Can_Update_Author()
5252
Assert.NotNull(response);
5353
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
5454
Assert.NotNull(data);
55-
Assert.Equal(1, data.Operations.Count);
55+
Assert.Single(data.Operations);
5656

5757
var attrs = data.Operations.Single().DataObject.Attributes;
5858
Assert.Equal(updates.Name, attrs["name"]);

test/UnitTests/Builders/DocumentBuilder_Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public void Build_Can_Build_Arrays()
190190

191191
var documents = documentBuilder.Build(entities);
192192

193-
Assert.Equal(1, documents.Data.Count);
193+
Assert.Single(documents.Data);
194194
}
195195

196196
[Fact]
@@ -201,7 +201,7 @@ public void Build_Can_Build_CustomIEnumerables()
201201

202202
var documents = documentBuilder.Build(entities);
203203

204-
Assert.Equal(1, documents.Data.Count);
204+
Assert.Single(documents.Data);
205205
}
206206

207207

test/UnitTests/Controllers/JsonApiControllerMixin_Tests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@ public void Errors_Correctly_Infers_Status_Code()
1616
// arrange
1717
var errors422 = new ErrorCollection {
1818
Errors = new List<Error> {
19-
new Error("422", "bad specific"),
20-
new Error("422", "bad other specific"),
19+
new Error(422, "bad specific"),
20+
new Error(422, "bad other specific"),
2121
}
2222
};
2323

2424
var errors400 = new ErrorCollection {
2525
Errors = new List<Error> {
26-
new Error("200", "weird"),
27-
new Error("400", "bad"),
28-
new Error("422", "bad specific"),
26+
new Error(200, "weird"),
27+
new Error(400, "bad"),
28+
new Error(422, "bad specific"),
2929
}
3030
};
3131

3232
var errors500 = new ErrorCollection {
3333
Errors = new List<Error> {
34-
new Error("200", "weird"),
35-
new Error("400", "bad"),
36-
new Error("422", "bad specific"),
37-
new Error("500", "really bad"),
38-
new Error("502", "really bad specific"),
34+
new Error(200, "weird"),
35+
new Error(400, "bad"),
36+
new Error(422, "bad specific"),
37+
new Error(500, "really bad"),
38+
new Error(502, "really bad specific"),
3939
}
4040
};
4141

test/UnitTests/Internal/ContextGraphBuilder_Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void Adding_DbContext_Members_That_DoNot_Implement_IIdentifiable_Creates_
3333
var contextGraph = contextGraphBuilder.Build() as ContextGraph;
3434

3535
// assert
36-
Assert.Equal(1, contextGraph.ValidationResults.Count);
36+
Assert.Single(contextGraph.ValidationResults);
3737
Assert.Contains(contextGraph.ValidationResults, v => v.LogLevel == LogLevel.Warning);
3838
}
3939

test/UnitTests/Serialization/JsonApiDeSerializerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public void Immutable_Attrs_Are_Not_Included_In_AttributesToUpdate()
181181

182182
// assert
183183
Assert.NotNull(result.ComplexMember);
184-
Assert.Equal(1, attributesToUpdate.Count);
184+
Assert.Single(attributesToUpdate);
185185

186186
foreach (var attr in attributesToUpdate)
187187
Assert.False(attr.Key.IsImmutable);

0 commit comments

Comments
 (0)