Skip to content

Adding a unit test to demonstrate and detect issue #130. #131

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions GraphDiff/GraphDiff.Tests/Models/TestModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ public class NullableKeyModel
public Guid? Id { get; set; }
}

public class PKFKModelPrimary
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int? Id { get; set; }

public PKFKModelSecondary Secondary { get; set; }
public string Name { get; set; }
}

public class PKFKModelSecondary
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int? Id { get; set; }
public string Name { get; set; }
}

// ====================================
// Second tier models
// ====================================
Expand Down
23 changes: 14 additions & 9 deletions GraphDiff/GraphDiff.Tests/TestDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace RefactorThis.GraphDiff.Tests
{
public class TestDbContext : DbContext
{
public class TestDbContext : DbContext
{
public IDbSet<TestNode> Nodes { get; set; }
public IDbSet<TestNodeWithBaseReference> NodesWithReference { get; set; }

Expand All @@ -16,7 +16,7 @@ public class TestDbContext : DbContext
public IDbSet<OneToManyAssociatedModel> OneToManyAssociatedModels { get; set; }
public IDbSet<OneToManyOwnedModel> OneToManyOwnedModels { get; set; }

public IDbSet<MultiKeyModel> MultiKeyModels { get; set; }
public IDbSet<MultiKeyModel> MultiKeyModels { get; set; }

public IDbSet<RootEntity> RootEntities { get; set; }

Expand All @@ -25,8 +25,11 @@ public class TestDbContext : DbContext
public IDbSet<InternalKeyModel> InternalKeyModels { get; set; }
public IDbSet<NullableKeyModel> NullableKeyModels { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
public IDbSet<PKFKModelPrimary> PKFKModelPrimaries { get; set; }
public IDbSet<PKFKModelSecondary> PKFKModelSecondaries { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// second tier mappings

modelBuilder.Entity<TestNode>().HasOptional(p => p.OneToOneAssociated).WithOptionalPrincipal(p => p.OneParent);
Expand Down Expand Up @@ -61,9 +64,11 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)

modelBuilder.Entity<InternalKeyModel>()
.HasMany(ikm => ikm.Associates)
.WithRequired(ikm => ikm.Parent);
}
.WithRequired(ikm => ikm.Parent);

modelBuilder.Entity<PKFKModelPrimary>().HasOptional(p => p.Secondary).WithRequired();
}

public TestDbContext() : base("GraphDiff") {}
}
public TestDbContext() : base("GraphDiff") {}
}
}
26 changes: 26 additions & 0 deletions GraphDiff/GraphDiff.Tests/Tests/OwnedEntityBehaviours.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,31 @@ public void ShouldRemoveEntityIfRemovedFromParent()
Assert.IsNull(context.OneToOneOwnedModels.SingleOrDefault(p => p.Id == oneToOne.Id));
}
}

[TestMethod]
public void ShouldCreateEntityWhenParentCreated_NullablePkFk()
{
var primary = new PKFKModelPrimary()
{
Name = "Primary",
Secondary = new PKFKModelSecondary()
{
Name = "Secondary"
}
};

PKFKModelPrimary persisted = null;

using (var context = new TestDbContext())
{
persisted = context.UpdateGraph<PKFKModelPrimary>(primary, mapping => mapping.OwnedEntity(p => p.Secondary));
context.SaveChanges();
}

Assert.IsNotNull(persisted);
Assert.AreEqual(persisted.Id, persisted.Secondary.Id);
Assert.AreEqual(primary.Name, persisted.Name);
Assert.AreEqual(primary.Secondary.Name, persisted.Secondary.Name);
}
}
}