Skip to content

Commit b75caae

Browse files
committed
added a check to ensure that the service provider is set for the SubSonic Context
1 parent 38ce344 commit b75caae

File tree

8 files changed

+133
-9
lines changed

8 files changed

+133
-9
lines changed

SubSonic.Core.DataAccessLayer/src/SubSonicContext/SubSonicContext.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Microsoft.Extensions.DependencyInjection;
2-
using SubSonic;
32
using System;
43
using System.Reflection;
54
using System.Runtime.CompilerServices;
@@ -13,7 +12,7 @@
1312

1413
namespace SubSonic
1514
{
16-
using Collections;
15+
using src;
1716
using Linq;
1817

1918
public partial class SubSonicContext
@@ -53,6 +52,11 @@ private void ConfigureSubSonic(DbContextOptionsBuilder builder)
5352
{
5453
OnDbConfiguring(builder);
5554

55+
if (Instance is null)
56+
{
57+
throw Error.InvalidOperation(SubSonicErrorMessages.ConfigurationInvalid.Format(nameof(DbContextOptionsBuilder.SetServiceProvider)));
58+
}
59+
5660
IServiceCollection services = Instance.GetService<IServiceCollection>();
5761

5862
if (services.IsNotNull())

SubSonic.Core.DataAccessLayer/src/SubSonicErrorMessages.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SubSonic.Core.DataAccessLayer/src/SubSonicErrorMessages.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="ConfigurationInvalid" xml:space="preserve">
121+
<value>Ensure that "{0}" is called during the configuration of the SubSonic Context.</value>
122+
</data>
120123
<data name="DbExpressionMustBeOfType" xml:space="preserve">
121124
<value>DbExpression must be of {0}.</value>
122125
</data>

SubSonic.Tests/DAL/SUT/BaseTestFixture.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ namespace SubSonic.Tests.DAL.SUT
1515
{
1616
public class BaseTestFixture
1717
{
18-
protected TestSubSonicContext Context { get => SetUpSubSonic.DbContext; }
18+
protected TestSubSonicContext Context
19+
{
20+
get
21+
{
22+
if (SetUpSubSonic.DbContext is null)
23+
{
24+
SetUpSubSonic.SetDbContext();
25+
}
26+
return SetUpSubSonic.DbContext;
27+
}
28+
}
1929

2030
protected ISubSonicLogger Logger { get; set; }
2131

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using SubSonic.Extensions;
2+
using SubSonic.Extensions.Test;
3+
using Models = SubSonic.Extensions.Test.Models;
4+
5+
namespace SubSonic.Tests.DAL.SUT
6+
{
7+
public class BadSubSonicContext
8+
: SubSonic.SubSonicContext
9+
{
10+
public BadSubSonicContext()
11+
: base()
12+
{
13+
14+
}
15+
16+
public ISubSonicSetCollection<Models.RealEstateProperty> RealEstateProperties { get; private set; }
17+
18+
public ISubSonicSetCollection<Models.Status> Statuses { get; private set; }
19+
20+
public ISubSonicSetCollection<Models.Unit> Units { get; private set; }
21+
22+
public ISubSonicSetCollection<Models.Renter> Renters { get; private set; }
23+
24+
public ISubSonicSetCollection<Models.Person> People { get; private set; }
25+
26+
protected override void OnDbConfiguring(DbContextOptionsBuilder config)
27+
{
28+
config.UseMockDbClient((builder, options) =>
29+
{
30+
builder
31+
.SetDatasource("localhost")
32+
.SetInitialCatalog("test")
33+
.SetIntegratedSecurity(true);
34+
});
35+
}
36+
37+
protected override void OnDbModeling(DbModelBuilder builder)
38+
{
39+
builder.AddEntityModel<Models.RealEstateProperty>();
40+
builder.AddEntityModel<Models.Status>();
41+
builder.AddEntityModel<Models.Unit>();
42+
builder.AddEntityModel<Models.Renter>();
43+
builder.AddEntityModel<Models.Person>();
44+
45+
builder.AddRelationshipFor<Models.RealEstateProperty>(() =>
46+
builder.GetRelationshipFor<Models.RealEstateProperty>()
47+
.HasMany(Model => Model.Units)
48+
.WithOne(Model => Model.RealEstateProperty));
49+
50+
builder.AddRelationshipFor<Models.RealEstateProperty>(() =>
51+
builder.GetRelationshipFor<Models.RealEstateProperty>()
52+
.HasOne(Model => Model.Status)
53+
.WithOne());
54+
55+
builder.AddRelationshipFor<Models.Unit>(() =>
56+
builder.GetRelationshipFor<Models.Unit>()
57+
.HasOne(Model => Model.RealEstateProperty)
58+
.WithMany(Model => Model.Units));
59+
60+
builder.AddRelationshipFor<Models.Unit>(() =>
61+
builder.GetRelationshipFor<Models.Unit>()
62+
.HasMany(Model => Model.Renters)
63+
.WithOne(Model => Model.Unit));
64+
65+
builder.AddRelationshipFor<Models.Unit>(() =>
66+
builder.GetRelationshipFor<Models.Unit>()
67+
.HasOne(Model => Model.Status)
68+
.WithOne());
69+
70+
builder.AddRelationshipFor<Models.Person>(() =>
71+
builder.GetRelationshipFor<Models.Person>()
72+
.HasMany(Model => Model.Renters)
73+
.WithOne(Model => Model.Person));
74+
75+
builder.AddRelationshipFor<Models.Person>(() =>
76+
builder.GetRelationshipFor<Models.Person>()
77+
.HasMany(Model => Model.Units)
78+
.UsingLookup(Model => Model.Renters)
79+
.WithMany());
80+
}
81+
}
82+
}

SubSonic.Tests/DAL/SetUpSubSonicContext.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,12 @@ public class SetUpSubSonic
1111
{
1212
public static TestSubSonicContext DbContext { get; private set; }
1313

14-
[OneTimeSetUp]
15-
public void OneTimeSetUp()
16-
{
17-
DbContext = new TestSubSonicContext();
18-
}
14+
public static void SetDbContext() => DbContext = new TestSubSonicContext();
1915

2016
[OneTimeTearDown]
2117
public void OneTimeTearDown()
2218
{
23-
DbContext.Dispose();
19+
DbContext?.Dispose();
2420
DbContext = null;
2521
}
2622
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using FluentAssertions;
2+
using NUnit.Framework;
3+
using SubSonic.src;
4+
using SubSonic.Tests.DAL.SUT;
5+
using System;
6+
7+
namespace SubSonic.Tests.DAL
8+
{
9+
[TestFixture]
10+
[Order(-100)]
11+
public class SubSonicContextInitializationTests
12+
{
13+
[Test]
14+
public void SubSonicContextShouldFailIfServiceProviderIsNotSet()
15+
{
16+
FluentActions.Invoking(() => new BadSubSonicContext()).Should().Throw<InvalidOperationException>().WithMessage(SubSonicErrorMessages.ConfigurationInvalid.Format(nameof(DbContextOptionsBuilder.SetServiceProvider)));
17+
}
18+
}
19+
}

SubSonic.Tests/DAL/SubSonicContext/SubSonicContextTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace SubSonic.Tests.DAL
1111
{
1212
using SubSonic.Extensions.Test.Data.Builders;
1313
using SubSonic.Linq;
14+
using SubSonic.src;
1415
using SUT;
1516
using Models = Extensions.Test.Models;
1617

0 commit comments

Comments
 (0)