Skip to content

Acceptance tests EF inheritance #610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ public UsersController(
: base(jsonApiOptions, resourceService, loggerFactory)
{ }
}

public class SuperUsersController : JsonApiController<SuperUser>
{
public SuperUsersController(
IJsonApiOptions jsonApiOptions,
IResourceService<SuperUser> resourceService,
ILoggerFactory loggerFactory)
: base(jsonApiOptions, resourceService, loggerFactory)
{ }
}
}
3 changes: 3 additions & 0 deletions src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class AppDbContext : DbContext
public DbSet<Author> AuthorDifferentDbContextName { get; set; }
public DbSet<NonJsonApiResource> NonJsonApiResources { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<SuperUser> SuperUsers { get; set; }
public DbSet<PersonRole> PersonRoles { get; set; }
public DbSet<ArticleTag> ArticleTags { get; set; }
public DbSet<IdentifiableArticleTag> IdentifiableArticleTags { get; set; }
Expand All @@ -23,6 +24,8 @@ public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SuperUser>().HasBaseType<User>();

modelBuilder.Entity<TodoItem>()
.Property(t => t.CreatedDate).HasDefaultValueSql("CURRENT_TIMESTAMP").IsRequired();

Expand Down
5 changes: 5 additions & 0 deletions src/Examples/JsonApiDotNetCoreExample/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ public class User : Identifiable
[Attr] public string Username { get; set; }
[Attr] public string Password { get; set; }
}

public class SuperUser : User
{
[Attr] public int SecurityLevel { get; set; }
}
}
1 change: 1 addition & 0 deletions src/JsonApiDotNetCore/Internal/DefaultRoutingConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void Apply(ApplicationModel application)
foreach (var controller in application.Controllers)
{
var resourceType = GetResourceTypeFromController(controller.ControllerType);

if (resourceType != null)
_registeredResources.Add(controller.ControllerName, resourceType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ public CreatingDataTests(TestFixture<Startup> fixture) : base(fixture)

}

[Fact]
public async Task CreateResource_ModelWithEntityFrameworkInHeritance_IsCreated()
{
// Arrange
var dbContext = PrepareTest<Startup>();
var serializer = GetSerializer<SuperUser>(e => new { e.SecurityLevel, e.Username, e.Password });
var superUser = new SuperUser { SecurityLevel = 1337, Username = "Super", Password = "User" };

// Act
var (body, response) = await Post("/api/v1/super-users", serializer.Serialize(superUser));

// Assert
AssertEqualStatusCode(HttpStatusCode.Created, response);
var createdSuperUser = _deserializer.DeserializeSingle<SuperUser>(body).Data;
var created = dbContext.SuperUsers.Where(e => e.Id.Equals(createdSuperUser.Id)).First();
}

[Fact]
public async Task CreateResource_GuidResource_IsCreated()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public AppDbContext GetDbContext()
return SendRequest("POST", route, content);
}

public Task<(string, HttpResponseMessage)> Patch(string route, string content)
{
return SendRequest("PATCH", route, content);
}

public IRequestSerializer GetSerializer<TResource>(Expression<Func<TResource, dynamic>> attributes = null, Expression<Func<TResource, dynamic>> relationships = null) where TResource : class, IIdentifiable
{
return _fixture.GetSerializer(attributes, relationships);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
{
[Collection("WebHostCollection")]
public class UpdatingDataTests
public class UpdatingDataTests : EndToEndTest
{
private TestFixture<Startup> _fixture;
private AppDbContext _context;
private Faker<TodoItem> _todoItemFaker;
private Faker<Person> _personFaker;

public UpdatingDataTests(TestFixture<Startup> fixture)
{
public UpdatingDataTests(TestFixture<Startup> fixture) : base(fixture)
{
_fixture = fixture;
_context = fixture.GetService<AppDbContext>();

Expand All @@ -41,6 +41,25 @@ public UpdatingDataTests(TestFixture<Startup> fixture)
.RuleFor(p => p.LastName, f => f.Name.LastName());
}

[Fact]
public async Task PatchResource_ModelWithEntityFrameworkInHeritance_IsPatched()
{
// Arrange
var dbContext = PrepareTest<Startup>();
var serializer = GetSerializer<SuperUser>(e => new { e.SecurityLevel });
var superUser = new SuperUser { SecurityLevel = 1337, Username = "Super", Password = "User" };
dbContext.SuperUsers.Add(superUser);
dbContext.SaveChanges();
var su = new SuperUser { Id = superUser.Id, SecurityLevel = 2674 };

// Act
var (body, response) = await Patch($"/api/v1/super-users/{su.Id}", serializer.Serialize(su));

// Assert
AssertEqualStatusCode(HttpStatusCode.OK, response);
var updated = _deserializer.DeserializeSingle<SuperUser>(body).Data;
Assert.Equal(2674, updated.SecurityLevel);
}

[Fact]
public async Task Response400IfUpdatingNotSettableAttribute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public IResponseDeserializer GetDeserializer()
.AddResource<Tag>()
.AddResource<CamelCasedModel>()
.AddResource<User>()
.AddResource<SuperUser>()
.AddResource<Person>()
.AddResource<Author>()
.AddResource<Passport>()
Expand Down