public void OptionalChildren_AreNot_Chosen_Over_Required_Children() { var reader = SetupDataReader( new Dictionary <string, object> { { "Id", 42 } }, new Dictionary <string, object> { { "ParentId", 42 }, { "Value", "Foo" } }); var toTest = new HierarchicalTypeRowFactory <OptionalCousins>(); var res = toTest.ParseRows(reader, new IDataTransformer[0], CancellationToken.None); res.Should().ContainSingle("because only one row was setup").Which .ShouldBeEquivalentTo( new OptionalCousins { Id = 42, Children = new[] { new Relative { ParentId = 42, Value = "Foo" } }, Cousins = new Relative[0] }); }
public void Builds_Hierarchy_When_Child_Has_Case_Insensitive_Key_Match() { using (GlobalSettings.UseTestInstance()) { var reader = SetupDataReader( new Dictionary <string, object> { { nameof(CaseInsensitiveParent.id), 42 } }, new Dictionary <string, object> { { nameof(CaseInsensitiveChild.caseinsensitiveparentid), 42 }, { nameof(CaseInsensitiveChild.Id), 18 } }); var toTest = new HierarchicalTypeRowFactory <CaseInsensitiveParent>(); var res = toTest.ParseRows(reader, new IDataTransformer[0], CancellationToken.None); res.Should().ContainSingle("because only one row was setup").Which .ShouldBeEquivalentTo( new CaseInsensitiveParent { id = 42, Children = new List <CaseInsensitiveChild> { new CaseInsensitiveChild { caseinsensitiveparentid = 42, Id = 18 } } }); } }
public void OptionalChildren_AreAdded_When_They_Are_Returned() { var reader = SetupDataReader( new Dictionary <string, object> { { "Id", 42 } }, new Dictionary <string, object> { { "ParentId", 42 }, { "Value", "Foo" } }); var toTest = new HierarchicalTypeRowFactory <OptionalChildren>(); var res = toTest.ParseRows(reader, new IDataTransformer[0], CancellationToken.None); res.Should().ContainSingle("because only one row was setup").Which .ShouldBeEquivalentTo( new OptionalChildren { Id = 42, Children = new[] { new Level4 { ParentId = 42, Value = "Foo" } } }); }
public void OptionalChildren_Still_Throws_When_Required_Children_Are_Not_Returned() { var reader = SetupDataReader( new Dictionary <string, object> { { "Id", 42 } }); var toTest = new HierarchicalTypeRowFactory <BothOptionalAndNonOptionalChildren>(); toTest.Invoking(f => f.ParseRows(reader, new IDataTransformer[0], CancellationToken.None)) .ShouldThrow <StoredProcedureException>("because a required result set was missing"); }
public void MultipleHierarchyWithCustomKeyAndForeignKey_AssociatesChildren() { var reader = SetupDataReader( new Dictionary <string, object> { { "Id", 0 }, { "Galaxy_Id", 0 }, { "Name", "Sol" } }, new Dictionary <string, object> { { "Galaxy_Id", 0 }, { "Name", "Milky Way" } }, new Dictionary <string, object> { { "Id", 3 }, { "SolarSystemId", 0 }, { "Name", "Earth" } }); var toTest = new HierarchicalTypeRowFactory <Galaxy>(); var res = toTest.ParseRows(reader, Enumerable.Empty <IDataTransformer>(), CancellationToken.None); res.Should().ContainSingle("because only one row was setup").Which .ShouldBeEquivalentTo( new Galaxy { GalaxyId = 0, Name = "Milky Way", SolarSystems = new[] { new SolarSystem { Id = 0, Galaxy_Id = 0, Name = "Sol", Planets = new[] { new Planet { Id = 3, SolarSystemId = 0, Name = "Earth" } } } } }); }
public void RequiredChildren_Of_OptionalChildren_Set_If_All_Result_Sets_Returned() { using (GlobalSettings.UseTestInstance()) { GlobalSettings.Instance.InterfaceMap.TryAdd(typeof(ILevel4), typeof(Level4)); var reader = SetupDataReader( new Dictionary <string, object> { { "Id", 42 } }, new Dictionary <string, object> { { "Level2Id", 42 }, { "Name", 15 } }, new Dictionary <string, object> { { "ParentId", 15 }, { "Value", "Bar" } }); var toTest = new HierarchicalTypeRowFactory <OptionalRequired>(); var res = toTest.ParseRows(reader, new IDataTransformer[0], CancellationToken.None); res.Should().ContainSingle("because only one row was setup").Which .ShouldBeEquivalentTo( new OptionalRequired { Id = 42, Children = new List <Level3> { new Level3 { Level2Id = 42, Name = 15, Level4s = new[] { new Level4 { ParentId = 15, Value = "Bar" } } } } }); } }
public void CancelsWhenTokenCanceledBeforeExecuting() { var reader = new Mock <IDataReader>(); var cts = new CancellationTokenSource(); cts.Cancel(); var toTest = new HierarchicalTypeRowFactory <SolarSystem>(); toTest.Invoking(f => f.ParseRows(reader.Object, Enumerable.Empty <IDataTransformer>(), cts.Token)) .ShouldThrow <OperationCanceledException>("the operation was cancelled"); reader.Verify(d => d.Read(), Times.Never); }
public void Throws_If_OptionalChildren_Returned_But_RequiredChildren_Of_OptionalChildren_AreNot() { using (GlobalSettings.UseTestInstance()) { GlobalSettings.Instance.InterfaceMap.TryAdd(typeof(ILevel4), typeof(Level4)); var reader = SetupDataReader( new Dictionary <string, object> { { "Id", 42 } }, new Dictionary <string, object> { { "Level2Id", 42 }, { "Name", 15 } }); var toTest = new HierarchicalTypeRowFactory <OptionalRequired>(); toTest.Invoking(t => t.ParseRows(reader, new IDataTransformer[0], CancellationToken.None)) .ShouldThrow <StoredProcedureException>("because a required result set was missing"); } }
public void DoesNotThrow_If_OptionalChildren_And_RequiredChildren_Of_OptionalChildren_NotReturned() { using (GlobalSettings.UseTestInstance()) { GlobalSettings.Instance.InterfaceMap.TryAdd(typeof(ILevel4), typeof(Level4)); var reader = SetupDataReader( new Dictionary <string, object> { { "Id", 42 } }); var toTest = new HierarchicalTypeRowFactory <OptionalRequired>(); var res = toTest.ParseRows(reader, new IDataTransformer[0], CancellationToken.None); res.Should().ContainSingle("because only one row was setup").Which .ShouldBeEquivalentTo( new OptionalRequired { Id = 42, Children = new List <Level3>() }); } }
public void Builds_Hierarchy_When_All_Items_Retrieved_By_Interface() { using (GlobalSettings.UseTestInstance()) { GlobalSettings.Instance.InterfaceMap.TryAdd(typeof(IParent), typeof(Parent)); GlobalSettings.Instance.InterfaceMap.TryAdd(typeof(IChild), typeof(Child)); var reader = SetupDataReader( new Dictionary <string, object> { { "ParentId", 42 } }, new Dictionary <string, object> { { "ParentId", 42 }, { "Name", "Foo" } }); var toTest = new HierarchicalTypeRowFactory <IParent>(); var res = toTest.ParseRows(reader, new IDataTransformer[0], CancellationToken.None); res.Should().ContainSingle("because only one row was setup").Which .ShouldBeEquivalentTo( new Parent { ParentId = 42, Children = new List <IChild> { new Child { ParentId = 42, Name = "Foo" } } }); } }
public void CompoundKeys_CanStillBeUsedToGenerateHierarchy() { var reader = SetupDataReader( new Dictionary <string, object> { { "Age", 42 }, { "Name", "The Answer" } }, new Dictionary <string, object> { { "Age", 16 }, { "Name", "Candles" }, { "ParentAge", 42 }, { "ParentName", "The Answer" } }); var toTest = new HierarchicalTypeRowFactory <UnmappedKeyParent>(); var res = toTest.ParseRows(reader, new IDataTransformer[0], CancellationToken.None); res.Should().ContainSingle("because only one row was setup").Which .ShouldBeEquivalentTo( new UnmappedKeyParent { Name = "The Answer", Age = 42, Children = new[] { new UnmappedKeyChild { Name = "Candles", Age = 16, ParentName = "The Answer", ParentAge = 42 } } }); }
public void CancelsWhenTokenCanceled() { var sema = new SemaphoreSlim(0, 1); var values = new Dictionary <string, object> { { "Id", 0 }, { "Galaxy_Id", 0 }, { "Name", "Sol" } }; var reader = new Mock <IDataReader>(); SetupDataReader(reader, values); var execCount = 0; reader.Setup(d => d.Read()) .Callback(() => { sema.Release(); Thread.Sleep(100); ++execCount; }) .Returns(() => execCount == 1); var cts = new CancellationTokenSource(); var toTest = new HierarchicalTypeRowFactory <SolarSystem>(); var task = Task.Factory.StartNew(() => toTest.Invoking(f => f.ParseRows(reader.Object, Enumerable.Empty <IDataTransformer>(), cts.Token)) .ShouldThrow <OperationCanceledException>("the operation was cancelled"), cts.Token); sema.Wait(TimeSpan.FromMilliseconds(250)); cts.Cancel(); task.Wait(TimeSpan.FromMilliseconds(250)).Should().BeTrue(); }
public void ChildrenResultsReturnedBeforeParents_AssociatesChildren() { var reader = SetupDataReader( new Dictionary <string, object> { { "Id", 4 }, { "SolarSystemId", 1 }, { "Name", "Mars" } }, new Dictionary <string, object> { { "Id", 1 }, { "Galaxy_Id", 0 }, { "Name", "Sol" } }); var toTest = new HierarchicalTypeRowFactory <SolarSystem>(); var res = toTest.ParseRows(reader, Enumerable.Empty <IDataTransformer>(), CancellationToken.None); res.Should().ContainSingle("because only one row was setup").Which .ShouldBeEquivalentTo(new SolarSystem { Id = 1, Galaxy_Id = 0, Name = "Sol", Planets = new[] { new Planet { Id = 4, SolarSystemId = 1, Name = "Mars" } } }); }
public void OrderSpecifiedInCtor_IsUsedToParse() { var reader = SetupDataReader( new Dictionary <string, object> { { "Key", 0 }, { "OtherKey", 1 } }, new Dictionary <string, object> { { "Key", 1 }, { "OtherKey", 0 } }); var toTest = new HierarchicalTypeRowFactory <Foo>(new Type[] { typeof(Foo), typeof(Bar) }); var res = toTest.ParseRows(reader, new IDataTransformer[0], CancellationToken.None); res.Should().ContainSingle("because only one row was setup").Which .ShouldBeEquivalentTo( new Foo { Key = 0, OtherKey = 1, Bars = new Bar[] { new Bar { Key = 1, OtherKey = 0 } } }); reader = SetupDataReader( new Dictionary <string, object> { { "Key", 2 }, { "OtherKey", 3 } }, new Dictionary <string, object> { { "Key", 3 }, { "OtherKey", 2 } }); toTest = new HierarchicalTypeRowFactory <Foo>(new Type[] { typeof(Bar), typeof(Foo) }); res = toTest.ParseRows(reader, new IDataTransformer[0], CancellationToken.None); res.Should().ContainSingle("because only one row was setup").Which .ShouldBeEquivalentTo( new Foo { Key = 3, OtherKey = 2, Bars = new Bar[] { new Bar { Key = 2, OtherKey = 3 } } }); }
public void MultipleHierarchyWithRenamedParentKey_WithDataTransformers_AssociatesChildren() { var reader = SetupDataReader( new Dictionary <string, object> { { "Id", 0 }, { "Galaxy_Id", 0 }, { "Name", "Sol" } }, new Dictionary <string, object> { { "Galaxy_Id", 0 }, { "Universe_Id", 0 }, { "Name", "Milky Way" } }, new Dictionary <string, object> { { "Id", 3 }, { "SolarSystemId", 0 }, { "Name", "Earth" } }, new Dictionary <string, object> { { "UniverseId", 0 }, { "Name", "The Known" } }); var toTest = new HierarchicalTypeRowFactory <Universe>(); var res = toTest.ParseRows(reader, new[] { Mock.Of <IDataTransformer>() }, CancellationToken.None); res.Should().ContainSingle("because only one row was setup").Which .ShouldBeEquivalentTo( new Universe { Name = "The Known", UniverseId = 0, Galaxies = new[] { new Galaxy { GalaxyId = 0, UniverseId = 0, Name = "Milky Way", SolarSystems = new[] { new SolarSystem { Id = 0, Galaxy_Id = 0, Name = "Sol", Planets = new[] { new Planet { Id = 3, SolarSystemId = 0, Name = "Earth" } } } } } } }); }