1-
2- using System . Linq ;
1+ using System . Linq ;
32using NUnit . Framework ;
43using SharpRepository . EfCoreRepository ;
5- using SharpRepository . Repository ;
6- using SharpRepository . Repository . Caching ;
7- using SharpRepository . Tests . Integration . Data ;
84using SharpRepository . Tests . Integration . TestObjects ;
95using Shouldly ;
106using System . Collections . Generic ;
11- using System . Diagnostics ;
127using SharpRepository . Repository . FetchStrategies ;
138using SharpRepository . Repository . Queries ;
149using SharpRepository . Repository . Specifications ;
1510using Microsoft . EntityFrameworkCore ;
16- using Microsoft . Extensions . Logging ;
1711using System ;
12+ using Microsoft . Extensions . Configuration ;
1813
1914namespace SharpRepository . Tests . Integration . Spikes
2015{
@@ -23,13 +18,26 @@ public class EfCoreLazyLoadSpike
2318 {
2419 private TestObjectContextCore dbContext ;
2520
26- private Func < string , bool > filterSelects = q => q . StartsWith ( "Executing DbCommand" ) && q . Contains ( "SELECT" ) && ! q . Contains ( "sqlite_master" ) ;
21+ private Func < string , bool > filterSelects = q => q . StartsWith ( "Executing DbCommand" ) && q . Contains ( "SELECT" ) ;
22+
23+ public static IConfigurationRoot GetIConfigurationRoot ( string outputPath )
24+ {
25+ return new ConfigurationBuilder ( )
26+ . SetBasePath ( outputPath )
27+ . AddJsonFile ( "appsettings.json" , optional : true )
28+ . AddUserSecrets ( "627a7ed1-b2c9-408a-a341-c01fc197a606" )
29+ . Build ( ) ;
30+ }
2731
2832 [ SetUp ]
2933 public void SetupRepository ( )
3034 {
35+ var configurationRoot = GetIConfigurationRoot ( TestContext . CurrentContext . TestDirectory ) ;
36+
37+ var connectionString = configurationRoot . GetConnectionString ( "EfCoreConnectionString" ) ;
38+
3139 var options = new DbContextOptionsBuilder < TestObjectContextCore > ( )
32- . UseInMemoryDatabase ( "integration test" )
40+ . UseSqlServer ( connectionString )
3341 . Options ;
3442
3543 // Create the schema in the database
@@ -48,8 +56,8 @@ public void SetupRepository()
4856 new EmailAddress {
4957 ContactId = i . ToString ( ) ,
5058 EmailAddressId = i ,
51- Email = "omar.piani ." + i . ToString ( ) + "@email.com" ,
52- Label = "omar.piani ." + i . ToString ( )
59+ Email = "test.addr ." + i . ToString ( ) + "@email.com" ,
60+ Label = "test.addr ." + i . ToString ( )
5361 }
5462 }
5563 } ) ;
@@ -66,7 +74,7 @@ public void EfCore_GetAll_Without_Includes_LazyLoads_Email()
6674 var contact = repository . GetAll ( ) . First ( ) ;
6775 contact . Name . ShouldBe ( "Test User 1" ) ;
6876 dbContext . QueryLog . Count ( filterSelects ) . ShouldBe ( 1 ) ;
69- contact . EmailAddresses . First ( ) . Email . ShouldBe ( "omar.piani [email protected] " ) ; 77+ contact . EmailAddresses . First ( ) . Email . ShouldBe ( "test.addr [email protected] " ) ; 7078 // dbContext.QueryLog.Count(filterSelects).ShouldBe(2); may be that dbcontext is disposed and the successive queries are not logged, quieries does not contains email so query was made in a lazy way but after.
7179 }
7280
@@ -80,7 +88,7 @@ public void EfCore_GetAll_With_Includes_In_Strategy_LazyLoads_Email()
8088 var contact = repository . GetAll ( strategy ) . First ( ) ;
8189 contact . Name . ShouldBe ( "Test User 1" ) ;
8290 dbContext . QueryLog . Count ( filterSelects ) . ShouldBe ( 2 ) ;
83- contact . EmailAddresses . First ( ) . Email . ShouldBe ( "omar.piani [email protected] " ) ; 91+ contact . EmailAddresses . First ( ) . Email . ShouldBe ( "test.addr [email protected] " ) ; 8492 dbContext . QueryLog . Count ( filterSelects ) . ShouldBe ( 2 ) ;
8593 }
8694
@@ -95,7 +103,7 @@ public void EfCore_GetAll_With_Includes_In_Strategy_String_LazyLoads_Email()
95103 var contact = repository . GetAll ( strategy ) . First ( ) ;
96104 contact . Name . ShouldBe ( "Test User 1" ) ;
97105 dbContext . QueryLog . Count ( filterSelects ) . ShouldBe ( 2 ) ;
98- contact . EmailAddresses . First ( ) . Email . ShouldBe ( "omar.piani [email protected] " ) ; 106+ contact . EmailAddresses . First ( ) . Email . ShouldBe ( "test.addr [email protected] " ) ; 99107 dbContext . QueryLog . Count ( filterSelects ) . ShouldBe ( 2 ) ;
100108 }
101109
@@ -107,7 +115,7 @@ public void EfCore_GetAll_With_Text_Include_LazyLoads_Email()
107115 var contact = repository . GetAll ( "EmailAddresses" ) . First ( ) ;
108116 contact . Name . ShouldBe ( "Test User 1" ) ;
109117 dbContext . QueryLog . Count ( filterSelects ) . ShouldBe ( 2 ) ;
110- contact . EmailAddresses . First ( ) . Email . ShouldBe ( "omar.piani [email protected] " ) ; 118+ contact . EmailAddresses . First ( ) . Email . ShouldBe ( "test.addr [email protected] " ) ; 111119 dbContext . QueryLog . Count ( filterSelects ) . ShouldBe ( 2 ) ;
112120 }
113121
@@ -121,7 +129,7 @@ public void EfCore_GetAll_With_Text_Include_And_Pagination_LazyLoads_Email()
121129 var contact = repository . GetAll ( pagination , "EmailAddresses" ) . First ( ) ;
122130 contact . Name . ShouldBe ( "Test User 1" ) ;
123131 dbContext . QueryLog . Count ( filterSelects ) . ShouldBe ( 3 ) ; // first query is count for total records
124- contact . EmailAddresses . First ( ) . Email . ShouldBe ( "omar.piani [email protected] " ) ; 132+ contact . EmailAddresses . First ( ) . Email . ShouldBe ( "test.addr [email protected] " ) ; 125133 dbContext . QueryLog . Count ( filterSelects ) . ShouldBe ( 3 ) ;
126134 }
127135
@@ -131,7 +139,7 @@ public void EfCore_FindAll_With_Include_And_Predicate_In_Specs_LazyLoads_Email()
131139 var repository = new EfCoreRepository < Contact , string > ( dbContext ) ;
132140
133141 var findAllBySpec = new Specification < Contact > ( obj => obj . ContactId == "1" )
134- . And ( obj
=> obj . EmailAddresses . Any ( m
=> m . Email == "omar.piani [email protected] " ) ) ; 142+ . And ( obj
=> obj . EmailAddresses . Any ( m
=> m . Email == "test.addr [email protected] " ) ) ; 135143
136144 var specification = new Specification < Contact > ( obj => obj . Name == "Test User 1" ) ;
137145
@@ -148,7 +156,7 @@ public void EfCore_FindAll_With_Include_And_Predicate_In_Specs_LazyLoads_Email()
148156 var contact = repository . FindAll ( findAllBySpec ) . First ( ) ;
149157 contact . Name . ShouldBe ( "Test User 1" ) ;
150158 dbContext . QueryLog . Count ( filterSelects ) . ShouldBe ( 2 ) ;
151- contact . EmailAddresses . First ( ) . Email . ShouldBe ( "omar.piani [email protected] " ) ; 159+ contact . EmailAddresses . First ( ) . Email . ShouldBe ( "test.addr [email protected] " ) ; 152160 dbContext . QueryLog . Count ( filterSelects ) . ShouldBe ( 2 ) ;
153161
154162 repository . FindAll ( findAllBySpec ) . Count ( ) . ShouldBe ( 1 ) ;
0 commit comments