Skip to content

Commit

Permalink
Dependant collection replacement fix
Browse files Browse the repository at this point in the history
  • Loading branch information
marcwittke committed Dec 21, 2019
1 parent b563b4b commit 0c94286
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 144 deletions.
74 changes: 74 additions & 0 deletions tests/Backend.Fx.EfCorePersistence.Tests/TheDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Linq;
using Backend.Fx.EfCorePersistence.Tests.DummyImpl.Domain;
using Backend.Fx.EfCorePersistence.Tests.DummyImpl.Persistence;
using Backend.Fx.EfCorePersistence.Tests.Fixtures;
using Microsoft.EntityFrameworkCore;
using Xunit;

namespace Backend.Fx.EfCorePersistence.Tests
{
public class TheDbContext
{
private readonly DatabaseFixture _fixture;
private static int _nextTenantId = 2675;
private readonly int _tenantId = _nextTenantId++;

public TheDbContext()
{
_fixture = new SqliteDatabaseFixture();
_fixture.CreateDatabase();
}

[Fact]
public void CanClearAndReplaceDependentEntites()
{
using (DbSession dbs = _fixture.UseDbSession())
{
using (var dbContext = new TestDbContext(dbs.OptionsBuilder.Options))
{
var blog = new Blog(1, "original blog") {TenantId = _tenantId};
blog.Posts.Add(new Post(1, blog, "new name 1"));
blog.Posts.Add(new Post(2, blog, "new name 2"));
blog.Posts.Add(new Post(3, blog, "new name 3"));
blog.Posts.Add(new Post(4, blog, "new name 4"));
blog.Posts.Add(new Post(5, blog, "new name 5"));
dbContext.Add(blog);
dbContext.UpdateTrackingProperties("me", DateTime.Now);
dbContext.SaveChanges();
}

using (var dbContext = new TestDbContext(dbs.OptionsBuilder.Options))
{
Blog blog = dbContext.Blogs.Include(b => b.Posts).Single(b => b.Id == 1);
blog.Posts.Clear();
var post6 = new Post(6, blog, "new name 6");
blog.Posts.Add(post6);
blog.Posts.Add(new Post(7, blog, "new name 7"));
blog.Posts.Add(new Post(8, blog, "new name 8"));
blog.Posts.Add(new Post(9, blog, "new name 9"));
blog.Posts.Add(new Post(10, blog, "new name 10"));
dbContext.UpdateTrackingProperties("me", DateTime.Now);
dbContext.SaveChanges();
}

using (var dbContext = new TestDbContext(dbs.OptionsBuilder.Options))
{
Blog blog = dbContext.Blogs.Include(b => b.Posts).Single(b => b.Id == 1);

Assert.Equal(5, blog.Posts.Count);

for (int i = 1; i <= 5; i++)
{
Assert.DoesNotContain(blog.Posts, p => p.Id == i);
}

for (int i = 6; i <= 10; i++)
{
Assert.Contains(blog.Posts, p => p.Id == i);
}
}
}
}
}
}
134 changes: 0 additions & 134 deletions tests/Backend.Fx.EfCorePersistence.Tests/TheDbContextOptions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ namespace Backend.Fx.EfCorePersistence.Tests
public class TheRepositoryOfComposedAggregate
{
private static int _nextTenantId = 57839;
private static int _nextId = 1;
private readonly int _tenantId = _nextTenantId++;
private readonly IEqualityComparer<DateTime?> _tolerantDateTimeComparer = new TolerantDateTimeComparer(TimeSpan.FromMilliseconds(5000));
private readonly IEntityIdGenerator _idGenerator = A.Fake<IEntityIdGenerator>();
private readonly DatabaseFixture _fixture;
private readonly IClock _clock = new FrozenClock();
private int _nextId = 1;


public TheRepositoryOfComposedAggregate()
{
Expand Down Expand Up @@ -155,8 +156,8 @@ public void CanDeleteDependent()
IUnitOfWork uow = dbs.BeginUnitOfWork(clock:_clock);
var sut = new EfRepository<Blog>(uow.GetDbContext(), new BlogMapping(),
CurrentTenantIdHolder.Create(_tenantId), new AllowAll<Blog>());
var blog = sut.Single(id);
var firstPost = blog.Posts.First();
Blog blog = sut.Single(id);
Post firstPost = blog.Posts.First();
firstPost.SetName("sadfasfsadf");
blog.Posts.Remove(firstPost);
uow.Complete();
Expand All @@ -181,7 +182,7 @@ public void CanUpdateDependant()
IUnitOfWork uow = dbs.BeginUnitOfWork(clock:_clock);
var sut = new EfRepository<Blog>(uow.GetDbContext(), new BlogMapping(), CurrentTenantIdHolder.Create(_tenantId),
new AllowAll<Blog>());
var blog = sut.Single(id);
Blog blog = sut.Single(id);
post = blog.Posts.First();
post.SetName("modified");
uow.Complete();
Expand Down Expand Up @@ -224,7 +225,7 @@ public void CanUpdateDependant()
// string culture = dbs.Connection.ExecuteScalar<string>($"SELECT TargetAudience_Culture ame FROM Posts where id = {post.Id}");
// Assert.Equal("es-AR", culture);

// string strChangedOn = dbs.Connection.ExecuteScalar<string>($"SELECT changedon FROM Posts where id = {post.Id}");
// string strChangedOn = dbs.Connection.ExecuteScalar<string>($"SELECT ChangedOn FROM Posts where id = {post.Id}");
// DateTime changedOn = DateTime.Parse(strChangedOn);
// Assert.Equal(_clock.UtcNow, changedOn, new TolerantDateTimeComparer(TimeSpan.FromMilliseconds(500)));
// }
Expand All @@ -241,7 +242,7 @@ public void CanAddDependent()
{
IUnitOfWork uow = dbs.BeginUnitOfWork(clock:_clock);
var sut = new EfRepository<Blog>(uow.GetDbContext(), new BlogMapping(), CurrentTenantIdHolder.Create(_tenantId), new AllowAll<Blog>());
var blog = sut.Single(id);
Blog blog = sut.Single(id);
blog.Posts.Add(new Post(_idGenerator.NextId(), blog, "added"));
uow.Complete();

Expand All @@ -262,7 +263,7 @@ public void CanReplaceDependentCollection()
{
IUnitOfWork uow = dbs.BeginUnitOfWork(clock:_clock);
var sut = new EfRepository<Blog>(uow.GetDbContext(), new BlogMapping(), CurrentTenantIdHolder.Create(_tenantId), new AllowAll<Blog>());
var blog = sut.Single(id);
Blog blog = sut.Single(id);
blog.Posts.Clear();
blog.Posts.Add(new Post(_idGenerator.NextId(), blog, "new name 1"));
blog.Posts.Add(new Post(_idGenerator.NextId(), blog, "new name 2"));
Expand All @@ -287,20 +288,20 @@ public void UpdatesAggregateTrackingPropertiesOnDeleteOfDependant()
{
int id = CreateBlogWithPost(dbs, 10);

var expectedModifiedOn = _clock.UtcNow.AddHours(1);
DateTime expectedModifiedOn = _clock.UtcNow.AddHours(1);
_clock.OverrideUtcNow(expectedModifiedOn);

{
IUnitOfWork uow = dbs.BeginUnitOfWork(clock:_clock);
var sut = new EfRepository<Blog>(uow.GetDbContext(), new BlogMapping(), CurrentTenantIdHolder.Create(_tenantId), new AllowAll<Blog>());
var b = sut.Single(id);
Blog b = sut.Single(id);
b.Posts.Remove(b.Posts.First());
uow.Complete();
}

{
IUnitOfWork uow = dbs.BeginUnitOfWork(clock:_clock);
var blog = uow.GetDbContext().Set<Blog>().Find(id);
Blog blog = uow.GetDbContext().Set<Blog>().Find(id);
Assert.NotNull(blog.ChangedOn);
Assert.Equal(expectedModifiedOn, blog.ChangedOn.Value, _tolerantDateTimeComparer);
}
Expand Down

0 comments on commit 0c94286

Please sign in to comment.