Skip to content

Commit

Permalink
Fix snapshot generation with HiLo (#1996)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji authored Sep 23, 2021
1 parent 3729e68 commit 80c359a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ private static readonly MethodInfo _propertyUseIdentityByDefaultColumnMethodInfo
= typeof(NpgsqlPropertyBuilderExtensions).GetRequiredRuntimeMethod(
nameof(NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn), typeof(PropertyBuilder));

private static readonly MethodInfo _propertyUseHiLoMethodInfo
= typeof(NpgsqlPropertyBuilderExtensions).GetRequiredRuntimeMethod(
nameof(NpgsqlPropertyBuilderExtensions.UseHiLo), typeof(PropertyBuilder), typeof(string), typeof(string));

private static readonly MethodInfo _propertyHasIdentityOptionsMethodInfo
= typeof(NpgsqlPropertyBuilderExtensions).GetRequiredRuntimeMethod(
nameof(NpgsqlPropertyBuilderExtensions.HasIdentityOptions), typeof(PropertyBuilder), typeof(long?), typeof(long?),
Expand Down Expand Up @@ -272,7 +276,7 @@ private IReadOnlyList<MethodCallCodeFragment> GenerateValueGenerationStrategy(
return new List<MethodCallCodeFragment>
{
new(
_modelUseHiLoMethodInfo,
onModel ? _modelUseHiLoMethodInfo : _propertyUseHiLoMethodInfo,
(name, schema) switch
{
(null, null) => Array.Empty<object>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,72 @@ public void GenerateFluentApi_identity_sequence_options()
Assert.Equal(10L, result.Arguments[5]);
}

[ConditionalFact]
public void GenerateFluentApi_IModel_works_with_IdentityByDefault()
{
var generator = CreateGenerator();
var modelBuilder = new ModelBuilder(NpgsqlConventionSetBuilder.Build());
modelBuilder.UseIdentityByDefaultColumns();

var annotations = modelBuilder.Model.GetAnnotations().ToDictionary(a => a.Name, a => a);
var result = generator.GenerateFluentApiCalls(((IModel)modelBuilder.Model), annotations).Single();

Assert.Equal("UseIdentityByDefaultColumns", result.Method);
Assert.Equal("NpgsqlModelBuilderExtensions", result.DeclaringType);

Assert.Empty(result.Arguments);
}

[ConditionalFact]
public void GenerateFluentApi_IProperty_works_with_IdentityByDefault()
{
var generator = CreateGenerator();
var modelBuilder = new ModelBuilder(NpgsqlConventionSetBuilder.Build());
modelBuilder.Entity("Post", x => x.Property<int>("Id").UseIdentityByDefaultColumn());
var property = (IProperty)modelBuilder.Model.FindEntityType("Post").FindProperty("Id");

var annotations = property.GetAnnotations().ToDictionary(a => a.Name, a => a);
var result = generator.GenerateFluentApiCalls(property, annotations).Single();

Assert.Equal("UseIdentityByDefaultColumn", result.Method);
Assert.Equal("NpgsqlPropertyBuilderExtensions", result.DeclaringType);

Assert.Empty(result.Arguments);
}

[ConditionalFact]
public void GenerateFluentApi_IModel_works_with_IdentityAlways()
{
var generator = CreateGenerator();
var modelBuilder = new ModelBuilder(NpgsqlConventionSetBuilder.Build());
modelBuilder.UseIdentityAlwaysColumns();

var annotations = modelBuilder.Model.GetAnnotations().ToDictionary(a => a.Name, a => a);
var result = generator.GenerateFluentApiCalls(((IModel)modelBuilder.Model), annotations).Single();

Assert.Equal("UseIdentityAlwaysColumns", result.Method);
Assert.Equal("NpgsqlModelBuilderExtensions", result.DeclaringType);

Assert.Empty(result.Arguments);
}

[ConditionalFact]
public void GenerateFluentApi_IProperty_works_with_IdentityAlways()
{
var generator = CreateGenerator();
var modelBuilder = new ModelBuilder(NpgsqlConventionSetBuilder.Build());
modelBuilder.Entity("Post", x => x.Property<int>("Id").UseIdentityAlwaysColumn());
var property = (IProperty)modelBuilder.Model.FindEntityType("Post").FindProperty("Id");

var annotations = property.GetAnnotations().ToDictionary(a => a.Name, a => a);
var result = generator.GenerateFluentApiCalls(property, annotations).Single();

Assert.Equal("UseIdentityAlwaysColumn", result.Method);
Assert.Equal("NpgsqlPropertyBuilderExtensions", result.DeclaringType);

Assert.Empty(result.Arguments);
}

[ConditionalFact]
public void GenerateFluentApi_IModel_works_with_HiLo()
{
Expand All @@ -122,6 +188,7 @@ public void GenerateFluentApi_IModel_works_with_HiLo()
var result = generator.GenerateFluentApiCalls((IModel)modelBuilder.Model, annotations).Single();

Assert.Equal("UseHiLo", result.Method);
Assert.Equal("NpgsqlModelBuilderExtensions", result.DeclaringType);

Assert.Collection(
result.Arguments,
Expand All @@ -141,6 +208,7 @@ public void GenerateFluentApi_IProperty_works_with_HiLo()
var result = generator.GenerateFluentApiCalls(property, annotations).Single();

Assert.Equal("UseHiLo", result.Method);
Assert.Equal("NpgsqlPropertyBuilderExtensions", result.DeclaringType);

Assert.Collection(
result.Arguments,
Expand Down

0 comments on commit 80c359a

Please sign in to comment.