Skip to content

Commit

Permalink
Make SQL CLR optional for running EF tests (#32143)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers authored Oct 25, 2023
1 parent d8e9f61 commit eb449a2
Show file tree
Hide file tree
Showing 13 changed files with 450 additions and 140 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2915,6 +2915,7 @@ FROM [Missions] AS [m]

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
[SqlServerCondition(SqlServerCondition.SupportsSqlClr)]
public virtual async Task Where_AtTimeZone_datetime_constant(bool async)
{
using var context = CreateContext();
Expand All @@ -2938,6 +2939,7 @@ FROM [Missions] AS [m]

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
[SqlServerCondition(SqlServerCondition.SupportsSqlClr)]
public virtual async Task Where_AtTimeZone_datetime_parameter(bool async)
{
using var context = CreateContext();
Expand Down Expand Up @@ -2966,6 +2968,7 @@ FROM [Missions] AS [m]

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
[SqlServerCondition(SqlServerCondition.SupportsSqlClr)]
public virtual async Task Where_AtTimeZone_datetime_column(bool async)
{
using var context = CreateContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ FROM [Owner] AS [o]

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
[SqlServerCondition(SqlServerCondition.SupportsSqlClr)]
public virtual async Task Where_AtTimeZone_datetimeoffset_constant(bool async)
{
var contextFactory = await InitializeAsync<OperatorsContext>(seed: Seed);
Expand Down Expand Up @@ -163,6 +164,7 @@ FROM [OperatorEntityDateTimeOffset] AS [o]

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
[SqlServerCondition(SqlServerCondition.SupportsSqlClr)]
public virtual async Task Where_AtTimeZone_datetimeoffset_parameter(bool async)
{
var contextFactory = await InitializeAsync<OperatorsContext>(seed: Seed);
Expand Down Expand Up @@ -198,6 +200,7 @@ FROM [OperatorEntityDateTimeOffset] AS [o]

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
[SqlServerCondition(SqlServerCondition.SupportsSqlClr)]
public virtual async Task Where_AtTimeZone_datetimeoffset_column(bool async)
{
var contextFactory = await InitializeAsync<OperatorsContext>(seed: Seed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8937,6 +8937,7 @@ public class MyEntity10295
#region Issue23282

[ConditionalFact]
[SqlServerCondition(SqlServerCondition.SupportsSqlClr)]
public virtual async Task Can_query_point_with_buffered_data_reader()
{
var contextFactory = await InitializeAsync<MyContext23282>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Microsoft.EntityFrameworkCore.Query;

[SqlServerCondition(SqlServerCondition.SupportsSqlClr)]
public class SpatialQuerySqlServerGeographyTest : SpatialQueryRelationalTestBase<SpatialQuerySqlServerGeographyFixture>
{
public SpatialQuerySqlServerGeographyTest(SpatialQuerySqlServerGeographyFixture fixture, ITestOutputHelper testOutputHelper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Microsoft.EntityFrameworkCore.Query;

[SqlServerCondition(SqlServerCondition.SupportsSqlClr)]
public class SpatialQuerySqlServerGeometryTest : SpatialQueryRelationalTestBase<SpatialQuerySqlServerGeometryFixture>
{
public SpatialQuerySqlServerGeometryTest(SpatialQuerySqlServerGeometryFixture fixture, ITestOutputHelper testOutputHelper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Microsoft.EntityFrameworkCore;

[SqlServerCondition(SqlServerCondition.SupportsSqlClr)]
public class SpatialSqlServerTest : SpatialTestBase<SpatialSqlServerFixture>
{
public SpatialSqlServerTest(SpatialSqlServerFixture fixture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
b.Property(e => e.Id).HasSentinel(IntSentinel);
b.Property(e => e.Name).HasSentinel(StringSentinel);
b.Property(e => e.CreatedOn).HasSentinel(DateTimeSentinel);
b.Property(e => e.GeometryCollection).HasSentinel(GeometryCollectionSentinel);
b.Property(e => e.OtherId).HasSentinel(NullableIntSentinel);
b.Property(e => e.NeedsConverter).HasSentinel(new NeedsConverter(IntSentinel));
});

modelBuilder.Entity<BlogWithSpatial>(
b =>
{
b.Property(e => e.Id).HasSentinel(IntSentinel);
b.Property(e => e.Name).HasSentinel(StringSentinel);
b.Property(e => e.GeometryCollection).HasSentinel(GeometryCollectionSentinel);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -712,17 +712,14 @@ public void Insert_with_non_key_default_value()
Id = IntSentinel,
Name = "One Unicorn",
CreatedOn = DateTimeSentinel,
NeedsConverter = NeedsConverterSentinel,
GeometryCollection = GeometryCollectionSentinel
NeedsConverter = NeedsConverterSentinel
},
new()
{
Id = IntSentinel,
Name = "Two Unicorns",
CreatedOn = new DateTime(1969, 8, 3, 0, 10, 0),
NeedsConverter = new NeedsConverter(111),
GeometryCollection = GeometryFactory.CreateGeometryCollection(
new Geometry[] { GeometryFactory.CreatePoint(new Coordinate(1, 3)) })
NeedsConverter = new NeedsConverter(111)
}
};

Expand All @@ -733,10 +730,6 @@ public void Insert_with_non_key_default_value()
Assert.NotEqual(new DateTime(), blogs[0].CreatedOn);
Assert.NotEqual(new DateTime(), blogs[1].CreatedOn);
Assert.Equal(111, blogs[1].NeedsConverter.Value);

var point = ((Point)blogs[1].GeometryCollection.Geometries[0]);
Assert.Equal(1, point.X);
Assert.Equal(3, point.Y);
}

using (var context = new BlogContextNonKeyDefaultValue(testStore.Name, OnModelCreating))
Expand All @@ -748,23 +741,13 @@ public void Insert_with_non_key_default_value()
Assert.Equal(new DateTime(1969, 8, 3, 0, 10, 0), blogs[1].CreatedOn);
Assert.Equal(new DateTime(1974, 8, 3, 0, 10, 0), blogs[2].CreatedOn);

var point1 = ((Point)blogs[1].GeometryCollection.Geometries[0]);
Assert.Equal(1, point1.X);
Assert.Equal(3, point1.Y);

var point2 = ((Point)blogs[2].GeometryCollection.Geometries[0]);
Assert.Equal(1, point2.X);
Assert.Equal(2, point2.Y);

blogs[0].CreatedOn = new DateTime(1973, 9, 3, 0, 10, 0);

blogs[1].Name = "X Unicorns";
blogs[1].NeedsConverter = new NeedsConverter(222);
blogs[1].GeometryCollection.Geometries[0] = GeometryFactory.CreatePoint(new Coordinate(1, 11));

blogs[2].Name = "Y Unicorns";
blogs[2].NeedsConverter = new NeedsConverter(333);
blogs[2].GeometryCollection.Geometries[0] = GeometryFactory.CreatePoint(new Coordinate(1, 22));

context.SaveChanges();
}
Expand All @@ -779,6 +762,68 @@ public void Insert_with_non_key_default_value()
Assert.Equal(222, blogs[1].NeedsConverter.Value);
Assert.Equal(new DateTime(1974, 8, 3, 0, 10, 0), blogs[2].CreatedOn);
Assert.Equal(333, blogs[2].NeedsConverter.Value);
}
}

[ConditionalFact]
[SqlServerCondition(SqlServerCondition.SupportsSqlClr)]
public void Insert_with_non_key_default_spatial_value()
{
using var testStore = SqlServerTestStore.CreateInitialized(DatabaseName);

using (var context = new BlogContextNonKeyDefaultSpatialValue(testStore.Name, OnModelCreating))
{
context.Database.EnsureCreatedResiliently();

var blogs = new List<BlogWithSpatial>
{
new()
{
Id = IntSentinel,
Name = "One Unicorn",
GeometryCollection = GeometryCollectionSentinel
},
new()
{
Id = IntSentinel,
Name = "Two Unicorns",
GeometryCollection = GeometryFactory.CreateGeometryCollection(
new Geometry[] { GeometryFactory.CreatePoint(new Coordinate(1, 3)) })
}
};

context.AddRange(blogs);

context.SaveChanges();

var point = ((Point)blogs[1].GeometryCollection.Geometries[0]);
Assert.Equal(1, point.X);
Assert.Equal(3, point.Y);
}

using (var context = new BlogContextNonKeyDefaultSpatialValue(testStore.Name, OnModelCreating))
{
var blogs = context.SpatialBlogs.OrderBy(e => e.Name).ToList();
Assert.Equal(3, blogs.Count);

var point1 = ((Point)blogs[1].GeometryCollection.Geometries[0]);
Assert.Equal(1, point1.X);
Assert.Equal(3, point1.Y);

var point2 = ((Point)blogs[2].GeometryCollection.Geometries[0]);
Assert.Equal(1, point2.X);
Assert.Equal(2, point2.Y);

blogs[1].GeometryCollection.Geometries[0] = GeometryFactory.CreatePoint(new Coordinate(1, 11));
blogs[2].GeometryCollection.Geometries[0] = GeometryFactory.CreatePoint(new Coordinate(1, 22));

context.SaveChanges();
}

using (var context = new BlogContextNonKeyDefaultSpatialValue(testStore.Name, OnModelCreating))
{
var blogs = context.SpatialBlogs.OrderBy(e => e.Name).ToList();
Assert.Equal(3, blogs.Count);

var point1 = ((Point)blogs[1].GeometryCollection.Geometries[0]);
Assert.Equal(1, point1.X);
Expand All @@ -805,7 +850,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
b =>
{
b.Property(e => e.CreatedOn).HasDefaultValueSql("getdate()");
b.Property(e => e.GeometryCollection).HasDefaultValue(GeometryFactory.CreateGeometryCollection());

b.HasData(
new Blog
Expand All @@ -814,6 +858,32 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
Name = "W Unicorns",
CreatedOn = new DateTime(1974, 8, 3, 0, 10, 0),
NeedsConverter = new NeedsConverter(111),
});
});
}
}

public class BlogContextNonKeyDefaultSpatialValue : ContextBase
{
public BlogContextNonKeyDefaultSpatialValue(string databaseName, Action<ModelBuilder> modelBuilder)
: base(databaseName, modelBuilder)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<BlogWithSpatial>(
b =>
{
b.Property(e => e.GeometryCollection).HasDefaultValue(GeometryFactory.CreateGeometryCollection());

b.HasData(
new BlogWithSpatial
{
Id = 9979,
Name = "W Unicorns",
GeometryCollection = GeometryFactory.CreateGeometryCollection(
new Geometry[] { GeometryFactory.CreatePoint(new Coordinate(1, 2)) })
});
Expand Down Expand Up @@ -1728,7 +1798,6 @@ protected Blog CreateBlog(string name)
Id = IntSentinel,
Name = name,
CreatedOn = DateTimeSentinel,
GeometryCollection = GeometryCollectionSentinel,
NeedsConverter = NeedsConverterSentinel,
OtherId = NullableIntSentinel
};
Expand All @@ -1739,10 +1808,24 @@ public class Blog
public string Name { get; set; }
public DateTime CreatedOn { get; set; }
public NeedsConverter NeedsConverter { get; set; }
public GeometryCollection GeometryCollection { get; set; }
public int? OtherId { get; set; }
}

protected BlogWithSpatial CreateBlogWithSpatial(string name)
=> new()
{
Id = IntSentinel,
Name = name,
GeometryCollection = GeometryCollectionSentinel,
};

public class BlogWithSpatial
{
public int Id { get; set; }
public string Name { get; set; }
public GeometryCollection GeometryCollection { get; set; }
}

public class NeedsConverter
{
public NeedsConverter(int value)
Expand Down Expand Up @@ -1812,6 +1895,7 @@ protected ContextBase(string databaseName, Action<ModelBuilder> modelBuilder)
}

public DbSet<Blog> Blogs { get; set; }
public DbSet<BlogWithSpatial> SpatialBlogs { get; set; }
public DbSet<NullableKeyBlog> NullableKeyBlogs { get; set; }
public DbSet<FullNameBlog> FullNameBlogs { get; set; }
public DbSet<GuidBlog> GuidBlogs { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public enum SqlServerCondition
SupportsFunctions2019 = 1 << 10,
SupportsFunctions2017 = 1 << 11,
SupportsJsonPathExpressions = 1 << 12,
SupportsSqlClr = 1 << 13,
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public ValueTask<bool> IsMetAsync()
isMet &= TestEnvironment.SupportsJsonPathExpressions;
}

if (Conditions.HasFlag(SqlServerCondition.SupportsSqlClr))
{
isMet &= TestEnvironment.IsSqlClrSupported;
}

return ValueTask.FromResult(isMet);
}

Expand Down
Loading

0 comments on commit eb449a2

Please sign in to comment.