Skip to content

Commit

Permalink
Adding a unit test to demonstrate and detect issue zzzprojects#130.
Browse files Browse the repository at this point in the history
  • Loading branch information
Winslow Dalpe committed Feb 13, 2015
1 parent e15cb40 commit f804f15
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
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);
}
}
}

0 comments on commit f804f15

Please sign in to comment.