public void LazyLoaded_Implicit_Conversion_When_Not_IsLoad_Should_Call_DB_Once() { // Arrange StubResultSet rsOffices = new StubResultSet("Number"); rsOffices.AddRow(1); rsOffices.AddRow(2); var db = CreateDB_ForQuery(rsOffices); Building building = new Building(); int calls = 0; var lazyProxy = new LazyLoaded <Building, List <Office> >((d, b) => { calls++; return(d.Query <Office>().ToList()); }, RelationshipTypes.Many); lazyProxy.Prepare(() => db, building, "Offices"); building._offices = lazyProxy; // Act int count = building.Offices.Count; // Assert Assert.AreEqual(2, count); Assert.AreEqual(1, calls); // Act again (should not hit db) count = building.Offices.Count; // Assert Assert.AreEqual(2, count); Assert.AreEqual(1, calls); }
public void LazyLoaded_Implicit_Conversion_When_Not_IsLoad_Should_Call_DB_Once() { // Arrange StubResultSet rsOffices = new StubResultSet("Number"); rsOffices.AddRow(1); rsOffices.AddRow(2); var db = CreateDB_ForQuery(rsOffices); Building building = new Building(); int calls = 0; var lazyProxy = new LazyLoaded<Building, List<Office>>((d, b) => { calls++; return d.Query<Office>().ToList(); }, RelationshipTypes.Many); lazyProxy.Prepare(() => db, building, "Offices"); building._offices = lazyProxy; // Act int count = building.Offices.Count; // Assert Assert.AreEqual(2, count); Assert.AreEqual(1, calls); // Act again (should not hit db) count = building.Offices.Count; // Assert Assert.AreEqual(2, count); Assert.AreEqual(1, calls); }
public void LazyLoaded_Implicit_Conversion_When_Not_IsLoad_Should_Call_DB_Once() { // Arrange IDataMapper db = MockRepository.GenerateMock<IDataMapper>(); Office office1 = new Office { Number = 1 }; Office office2 = new Office { Number = 2 }; List<Office> offices = new List<Office> { office1, office2 }; db.Expect(d => d.Query<Office>().ToList()).Return(offices); Building building = new Building(); var lazyProxy = new LazyLoaded<Building, List<Office>>((d,b) => d.Query<Office>().ToList()); lazyProxy.Prepare(() => db, building); building._offices = lazyProxy; // Act int count = building.Offices.Count; // Assert Assert.IsTrue(count == 2); // Act count = building.Offices.Count; // Assert db.AssertWasCalled(d => d.Query<Office>(), o => o.Repeat.Once()); // Should hit DB once }
public void Setup() { LanguageProfile _profile = new LazyLoaded <LanguageProfile> (new LanguageProfile { Languages = LanguageFixture.GetDefaultLanguages(Language.English, Language.Spanish), Cutoff = Language.Spanish }); _remoteEpisode = new RemoteEpisode { ParsedEpisodeInfo = new ParsedEpisodeInfo { Language = Language.English }, Series = new Series { LanguageProfile = _profile } }; }
public void LazyLoadedException_ShouldThrowDataMappingException() { // Arrange StubResultSet rsOffices = new StubResultSet("Number"); rsOffices.AddRow(1); rsOffices.AddRow(2); var db = CreateDB_ForQuery(rsOffices); Building building = new Building(); var lazyProxy = new LazyLoaded<Building, List<Office>>((d, b) => { throw new Exception("Oops!"); //return d.Query<Office>().ToList(); }, RelationshipTypes.Many); lazyProxy.Prepare(() => db, building, "Offices"); building._offices = lazyProxy; // Act int count = building.Offices.Count; }
public Type GetLazyLoadedEntityType() { if (!IsLazyLoaded) { return(RelationshipInfo.EntityType); } // Generic type: LazyLoaded<TParent, TChild> var genericType = LazyLoaded.GetType(); var tChildArg = genericType.GetGenericArguments()[1]; if (typeof(System.Collections.ICollection).IsAssignableFrom(tChildArg)) { // Is a one-to-many (list) return(tChildArg.GetGenericArguments()[0]); } else { // Is a one-to-one (single entity) return(tChildArg); } }
public void LazyLoadedException_ShouldThrowDataMappingException() { // Arrange StubResultSet rsOffices = new StubResultSet("Number"); rsOffices.AddRow(1); rsOffices.AddRow(2); var db = CreateDB_ForQuery(rsOffices); Building building = new Building(); var lazyProxy = new LazyLoaded <Building, List <Office> >((d, b) => { throw new Exception("Oops!"); //return d.Query<Office>().ToList(); }, RelationshipTypes.Many); lazyProxy.Prepare(() => db, building, "Offices"); building._offices = lazyProxy; // Act int count = building.Offices.Count; }