Skip to content
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

Fix generated key when initial entities have ids #56

Open
wants to merge 1 commit into
base: develop
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
6 changes: 6 additions & 0 deletions src/EntityFrameworkCoreMock.Shared/KeyContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@ public sealed class KeyContext
private long _nextIdentity = 1;

public long NextIdentity => _nextIdentity++;

public long CurrentIdentity
{
get => _nextIdentity;
set => _nextIdentity = value + 1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ private static Func<T, KeyContext, object> BuildIdentityKeyFactory<T>(PropertyIn

if (keyProperty.PropertyType == typeof(int))
{
return BuildIdentityKeyFactory<T, int>(keyProperty, ctx => Expression.Property(ctx, nameof(KeyContext.NextIdentity)));
return BuildAutoincrementIdentityKeyFactory<T, int>(keyProperty, ctx => Expression.Property(ctx, nameof(KeyContext.NextIdentity)));
}
else if (keyProperty.PropertyType == typeof(long))
{
return BuildIdentityKeyFactory<T, long>(keyProperty, ctx => Expression.Property(ctx, nameof(KeyContext.NextIdentity)));
return BuildAutoincrementIdentityKeyFactory<T, long>(keyProperty, ctx => Expression.Property(ctx, nameof(KeyContext.NextIdentity)));
}
else if (keyProperty.PropertyType == typeof(Guid))
{
Expand Down Expand Up @@ -71,6 +71,39 @@ private static Func<TEntity, KeyContext, object> BuildIdentityKeyFactory<TEntity

return Expression.Lambda<Func<TEntity, KeyContext, object>>(body, entityArgument, keyContextArgument).Compile();
}

private static Func<TEntity, KeyContext, object> BuildAutoincrementIdentityKeyFactory<TEntity, TKey>(
PropertyInfo keyProperty,
Func<ParameterExpression, Expression> nextIdentity)
{
var entityArgument = Expression.Parameter(typeof(TEntity));
var keyContextArgument = Expression.Parameter(typeof(KeyContext));
var keyValueVariable = Expression.Variable(typeof(TKey));
var body = Expression.Block(typeof(object),
new[] { keyValueVariable },
Expression.Assign(keyValueVariable, Expression.Convert(Expression.Property(entityArgument, keyProperty), typeof(TKey))),
Expression.IfThenElse(Expression.Equal(keyValueVariable, Expression.Default(typeof(TKey))),
Expression.Block(
Expression.Assign(keyValueVariable, Expression.Convert(nextIdentity(keyContextArgument), typeof(TKey))),
Expression.Assign(Expression.Property(entityArgument, keyProperty), keyValueVariable)
),
Expression.Block(
Expression.Assign(
Expression.Property(keyContextArgument, nameof(KeyContext.CurrentIdentity)),
Expression.Call(
typeof(Math),
nameof(Math.Max),
Type.EmptyTypes,
Expression.Convert(keyValueVariable, typeof(long)),
Expression.Property(keyContextArgument, nameof(KeyContext.CurrentIdentity))
)
)
)
),
Expression.Convert(keyValueVariable, typeof(object)));

return Expression.Lambda<Func<TEntity, KeyContext, object>>(body, entityArgument, keyContextArgument).Compile();
}

private static Func<T, KeyContext, object> BuildDefaultKeyFactory<T>(PropertyInfo[] keyProperties)
{
Expand Down
19 changes: 19 additions & 0 deletions tests/EntityFrameworkCoreMock.Moq.Tests/DbContextMockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,25 @@ public void DbContextMock_CreateDbSetMock_AddMultipleModelsWithDatabaseGenerated
Assert.That(dbSetMock.Object.First(x => x.Id == 2).Value, Is.EqualTo("second"));
Assert.That(dbSetMock.Object.First(x => x.Id == 3).Value, Is.EqualTo("third"));
}

[Test]
public void DbContextMock_CreateDbSetMock_AddWithDatabaseGeneratedIdentityKeyWithIdsOnInitialEntities_ShouldGenerateSequentialKey()
{
var dbContextMock = new DbContextMock<TestDbContext>(Options);
var dbSetMock = dbContextMock.CreateDbSetMock(x => x.GeneratedKeyModels, new[]
{
new GeneratedKeyModel {Id = 1, Value = "first"},
new GeneratedKeyModel {Id = 2, Value = "second"}
});
dbSetMock.Object.Add(new GeneratedKeyModel { Value = "third" });
dbContextMock.Object.SaveChanges();

Assert.That(dbSetMock.Object.Min(x => x.Id), Is.EqualTo(1));
Assert.That(dbSetMock.Object.Max(x => x.Id), Is.EqualTo(3));
Assert.That(dbSetMock.Object.First(x => x.Id == 1).Value, Is.EqualTo("first"));
Assert.That(dbSetMock.Object.First(x => x.Id == 2).Value, Is.EqualTo("second"));
Assert.That(dbSetMock.Object.First(x => x.Id == 3).Value, Is.EqualTo("third"));
}

[Test]
public void DbContextMock_CreateDbSetMock_AddMultipleModelsWithGuidAsDatabaseGeneratedIdentityKey_ShouldGenerateRandomGuidAsKey()
Expand Down