|
| 1 | + |
| 2 | +using System.Linq; |
| 3 | +using NUnit.Framework; |
| 4 | +using SharpRepository.EfCoreRepository; |
| 5 | +using SharpRepository.Repository; |
| 6 | +using SharpRepository.Repository.Caching; |
| 7 | +using SharpRepository.Tests.Integration.Data; |
| 8 | +using SharpRepository.Tests.Integration.TestObjects; |
| 9 | +using Shouldly; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Diagnostics; |
| 12 | +using SharpRepository.Repository.FetchStrategies; |
| 13 | +using SharpRepository.Repository.Queries; |
| 14 | +using SharpRepository.Repository.Specifications; |
| 15 | +using Microsoft.EntityFrameworkCore; |
| 16 | +using Microsoft.Data.Sqlite; |
| 17 | + |
| 18 | +namespace SharpRepository.Tests.Integration.Spikes |
| 19 | +{ |
| 20 | + public class MyEfCoreRepository : EfCoreRepository<Contact, string> |
| 21 | + { |
| 22 | + public MyEfCoreRepository(DbContext dbContext, ICachingStrategy<Contact, string> cachingStrategy = null) : base(dbContext, cachingStrategy) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + public bool LazyLoadValue |
| 27 | + { |
| 28 | + get { return false; } // TODO: return Context.Configuration.LazyLoadingEnabled; } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + [TestFixture] |
| 33 | + public class EfCoreLazyLoadSpike |
| 34 | + { |
| 35 | + private TestObjectContextCore dbContext; |
| 36 | + private List<string> queries; |
| 37 | + |
| 38 | + [SetUp] |
| 39 | + public void SetupRepository() |
| 40 | + { |
| 41 | + queries = new List<string>(); |
| 42 | + var dbPath = EfDataDirectoryFactory.Build(); |
| 43 | + |
| 44 | + var connection = new SqliteConnection("DataSource=:memory:"); |
| 45 | + connection.Open(); |
| 46 | + |
| 47 | + var options = new DbContextOptionsBuilder<TestObjectContextCore>() |
| 48 | + .UseSqlite(connection) |
| 49 | + .Options; |
| 50 | + |
| 51 | + // Create the schema in the database |
| 52 | + var context = new TestObjectContextCore(options); |
| 53 | + |
| 54 | + dbContext = new TestObjectContextCore(options); |
| 55 | + dbContext.Database.EnsureCreated(); |
| 56 | + const int totalItems = 5; |
| 57 | + |
| 58 | + for (int i = 1; i <= totalItems; i++) |
| 59 | + { |
| 60 | + dbContext.Contacts.Add( |
| 61 | + new Contact |
| 62 | + { |
| 63 | + ContactId = i.ToString(), |
| 64 | + Name = "Test User " + i, |
| 65 | + EmailAddresses = new List<EmailAddress> { |
| 66 | + new EmailAddress { |
| 67 | + ContactId = i.ToString(), |
| 68 | + EmailAddressId = i, |
| 69 | + Email = "omar.piani." + i.ToString() + "@email.com", |
| 70 | + Label = "omar.piani." + i.ToString() |
| 71 | + } |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + dbContext.SaveChanges(); |
| 77 | + |
| 78 | + // TODO: reistantiate in order to lose caches |
| 79 | + //dbContext = new TestObjectContext(options); |
| 80 | + //dbContext.Database.EnsureCreated(); |
| 81 | + } |
| 82 | + |
| 83 | + [Test] |
| 84 | + public void EfCore_LazyLoad_Set_To_False() |
| 85 | + { |
| 86 | + //dbContext.Configuration.LazyLoadingEnabled = false; |
| 87 | + var repository = new MyEfCoreRepository(dbContext); |
| 88 | + repository.LazyLoadValue.ShouldBeFalse(); |
| 89 | + } |
| 90 | + |
| 91 | + [Test] |
| 92 | + public void EfCore_GetAll_Without_Includes_LazyLoads_Email() |
| 93 | + { |
| 94 | + //dbContext.Configuration.LazyLoadingEnabled = true; |
| 95 | + //dbContext.Database.Log = sql => |
| 96 | + //{ |
| 97 | + // if (sql.Contains("SELECT")) |
| 98 | + // { |
| 99 | + // queries.Add(sql); |
| 100 | + // } |
| 101 | + //}; |
| 102 | + var repository = new MyEfCoreRepository(dbContext); |
| 103 | + |
| 104 | + var contact = repository.GetAll().First(); |
| 105 | + contact.Name.ShouldBe("Test User 1"); |
| 106 | + dbContext.QueryLog.Count(q => q.Contains("SELECT")).ShouldBe(3); |
| 107 | + contact.EmailAddresses.First().Email.ShouldBe("[email protected]"); |
| 108 | + dbContext.QueryLog.Count(q => q.Contains("SELECT")).ShouldBe(4); |
| 109 | + } |
| 110 | + |
| 111 | + [Test] |
| 112 | + public void EfCore_GetAll_With_Includes_In_Strategy_LazyLoads_Email() |
| 113 | + { |
| 114 | + //dbContext.Configuration.LazyLoadingEnabled = true; |
| 115 | + //dbContext.Database.Log = sql => |
| 116 | + //{ |
| 117 | + // if (sql.Contains("SELECT")) |
| 118 | + // { |
| 119 | + // queries.Add(sql); |
| 120 | + // } |
| 121 | + //}; |
| 122 | + var repository = new MyEfCoreRepository(dbContext); |
| 123 | + |
| 124 | + |
| 125 | + var strategy = new GenericFetchStrategy<Contact>(); |
| 126 | + strategy.Include(x => x.EmailAddresses); |
| 127 | + |
| 128 | + var contact = repository.GetAll(strategy).First(); |
| 129 | + contact.Name.ShouldBe("Test User 1"); |
| 130 | + dbContext.QueryLog.Count(q => q.Contains("SELECT")).ShouldBe(4); |
| 131 | + contact.EmailAddresses.First().Email.ShouldBe("[email protected]"); |
| 132 | + dbContext.QueryLog.Count(q => q.Contains("SELECT")).ShouldBe(4); |
| 133 | + } |
| 134 | + |
| 135 | + [Test] |
| 136 | + public void EfCore_GetAll_With_Includes_In_Strategy_String_LazyLoads_Email() |
| 137 | + { |
| 138 | + //dbContext.Configuration.LazyLoadingEnabled = true; |
| 139 | + //dbContext.Database.Log = sql => |
| 140 | + //{ |
| 141 | + // if (sql.Contains("SELECT")) |
| 142 | + // { |
| 143 | + // queries.Add(sql); |
| 144 | + // } |
| 145 | + //}; |
| 146 | + var repository = new MyEfCoreRepository(dbContext); |
| 147 | + |
| 148 | + |
| 149 | + var strategy = new GenericFetchStrategy<Contact>(); |
| 150 | + strategy.Include(x => x.EmailAddresses); |
| 151 | + |
| 152 | + var contact = repository.GetAll(strategy).First(); |
| 153 | + contact.Name.ShouldBe("Test User 1"); |
| 154 | + queries.Count().ShouldBe(1); |
| 155 | + contact.EmailAddresses.First().Email.ShouldBe("[email protected]"); |
| 156 | + queries.Count().ShouldBe(1); |
| 157 | + } |
| 158 | + |
| 159 | + [Test] |
| 160 | + public void EfCore_GetAll_With_Text_Include_LazyLoads_Email() |
| 161 | + { |
| 162 | + //dbContext.Configuration.LazyLoadingEnabled = true; |
| 163 | + //dbContext.Database.Log = sql => |
| 164 | + //{ |
| 165 | + // if (sql.Contains("SELECT")) |
| 166 | + // { |
| 167 | + // queries.Add(sql); |
| 168 | + // } |
| 169 | + //}; |
| 170 | + var repository = new MyEfCoreRepository(dbContext); |
| 171 | + |
| 172 | + var contact = repository.GetAll("EmailAddresses").First(); |
| 173 | + contact.Name.ShouldBe("Test User 1"); |
| 174 | + queries.Count().ShouldBe(1); |
| 175 | + contact.EmailAddresses.First().Email.ShouldBe("[email protected]"); |
| 176 | + queries.Count().ShouldBe(1); |
| 177 | + } |
| 178 | + |
| 179 | + [Test] |
| 180 | + public void EfCore_GetAll_With_Text_Include_And_Pagination_LazyLoads_Email() |
| 181 | + { |
| 182 | + //dbContext.Configuration.LazyLoadingEnabled = true; |
| 183 | + //dbContext.Database.Log = sql => |
| 184 | + //{ |
| 185 | + // if (sql.Contains("SELECT")) |
| 186 | + // { |
| 187 | + // queries.Add(sql); |
| 188 | + // } |
| 189 | + //}; |
| 190 | + var repository = new MyEfCoreRepository(dbContext); |
| 191 | + |
| 192 | + var pagination = new PagingOptions<Contact>(1, 4, "ContactId"); |
| 193 | + |
| 194 | + var contact = repository.GetAll(pagination, "EmailAddresses").First(); |
| 195 | + contact.Name.ShouldBe("Test User 1"); |
| 196 | + queries.Count().ShouldBe(2); // first query is count for total records |
| 197 | + contact.EmailAddresses.First().Email.ShouldBe("[email protected]"); |
| 198 | + queries.Count().ShouldBe(2); |
| 199 | + } |
| 200 | + |
| 201 | + [Test] |
| 202 | + public void EfCore_FindAll_With_Include_And_Predicate_In_Specs_LazyLoads_Email() |
| 203 | + { |
| 204 | + //dbContext.Configuration.LazyLoadingEnabled = true; |
| 205 | + //dbContext.Database.Log = sql => |
| 206 | + //{ |
| 207 | + // if (sql.Contains("SELECT")) |
| 208 | + // { |
| 209 | + // queries.Add(sql); |
| 210 | + // } |
| 211 | + //}; |
| 212 | + |
| 213 | + ; |
| 214 | + |
| 215 | + var repository = new MyEfCoreRepository(dbContext); |
| 216 | + |
| 217 | + var findAllBySpec = new Specification<Contact>(obj => obj.ContactId == "1") |
| 218 | + .And(obj => obj.EmailAddresses.Any(m => m.Email == "[email protected]")); |
| 219 | + |
| 220 | + var specification = new Specification<Contact>(obj => obj.Name == "Test User 1"); |
| 221 | + |
| 222 | + findAllBySpec.FetchStrategy = new GenericFetchStrategy<Contact>(); |
| 223 | + findAllBySpec.FetchStrategy |
| 224 | + .Include(obj => obj.EmailAddresses); |
| 225 | + |
| 226 | + // NOTE: This line will erase my FetchStrategy from above |
| 227 | + if (null != specification) |
| 228 | + { |
| 229 | + findAllBySpec = findAllBySpec.And(specification); |
| 230 | + } |
| 231 | + |
| 232 | + var contact = repository.FindAll(findAllBySpec).First(); |
| 233 | + contact.Name.ShouldBe("Test User 1"); |
| 234 | + dbContext.QueryLog.Count(s => s.Contains("QUERY")).ShouldBe(1); // first query is count for total records |
| 235 | + contact.EmailAddresses.First().Email.ShouldBe("[email protected]"); |
| 236 | + dbContext.QueryLog.Count(s => s.Contains("QUERY")).ShouldBe(1); |
| 237 | + |
| 238 | + repository.FindAll(findAllBySpec).Count().ShouldBe(1); |
| 239 | + } |
| 240 | + } |
| 241 | +} |
0 commit comments