public void CreateThrowsExceptionWhenNoCapabilityFoundForProperty() { var buildHistory = new BuildHistory(); var expected = new SlimModel(); var processor = Substitute.For <IBuildProcessor>(); var buildConfiguration = Substitute.For <IBuildConfiguration>(); var propertyResolver = Substitute.For <IPropertyResolver>(); var typeCapability = Substitute.For <IBuildCapability>(); typeCapability.AutoPopulate.Returns(true); typeCapability.SupportsCreate.Returns(true); typeCapability.SupportsPopulate.Returns(true); typeCapability.ImplementedByType.Returns(typeof(DummyTypeCreator)); var sut = new DefaultExecuteStrategy(buildHistory, _buildLog, processor); processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(), typeof(SlimModel)) .Returns(typeCapability); typeCapability.CreateType(sut, typeof(SlimModel), null !).Returns(expected); typeCapability.Populate(sut, expected).Returns(expected); buildConfiguration.PropertyResolver.Returns(propertyResolver); propertyResolver.GetOrderedProperties(buildConfiguration, typeof(SlimModel)) .Returns(typeof(SlimModel).GetProperties()); sut.Initialize(buildConfiguration); Action action = () => sut.Create(typeof(SlimModel)); action.Should().Throw <BuildException>(); }
public void BuildChainShouldBeEmptyAfterCreateCompleted() { var buildHistory = new BuildHistory(); var expected = new SlimModel(); var processor = Substitute.For <IBuildProcessor>(); var buildConfiguration = Substitute.For <IBuildConfiguration>(); var buildCapability = Substitute.For <IBuildCapability>(); buildCapability.AutoPopulate.Returns(true); buildCapability.SupportsCreate.Returns(true); buildCapability.ImplementedByType.Returns(typeof(DummyTypeCreator)); var sut = new DefaultExecuteStrategy(buildHistory, _buildLog, processor); processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(), typeof(SlimModel)).Returns(buildCapability); buildCapability.CreateType(sut, typeof(SlimModel), Arg.Any <object[]>()).Returns(expected); sut.Initialize(buildConfiguration); sut.BuildChain.Should().BeEmpty(); sut.Create(typeof(SlimModel)); sut.BuildChain.Should().BeEmpty(); }
public void CreatePopulatesWithProcessorWhenAutoPopulateDisabled() { var buildHistory = new BuildHistory(); var expected = new SlimModel(); var processor = Substitute.For <IBuildProcessor>(); var buildConfiguration = Substitute.For <IBuildConfiguration>(); var typeCapability = Substitute.For <IBuildCapability>(); typeCapability.SupportsPopulate.Returns(true); typeCapability.SupportsCreate.Returns(true); typeCapability.ImplementedByType.Returns(typeof(DummyTypeCreator)); var sut = new DefaultExecuteStrategy(buildHistory, _buildLog, processor); processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(), typeof(SlimModel)) .Returns(typeCapability); typeCapability.CreateType(sut, typeof(SlimModel), Arg.Any <object[]>()).Returns(expected); typeCapability.Populate(sut, expected).Returns(expected); sut.Initialize(buildConfiguration); var actual = (SlimModel)sut.Create(typeof(SlimModel)) !; actual.Should().Be(expected); actual.Value.Should().BeEmpty(); typeCapability.Received().Populate(sut, expected); }
public void PopulateThrowsExceptionWhenProcessorReturnsNull() { var model = new SlimModel(); var buildHistory = new BuildHistory(); var processor = Substitute.For <IBuildProcessor>(); var buildConfiguration = Substitute.For <IBuildConfiguration>(); var typeCapability = Substitute.For <IBuildCapability>(); typeCapability.SupportsCreate.Returns(true); typeCapability.SupportsPopulate.Returns(true); typeCapability.ImplementedByType.Returns(typeof(DummyTypeCreator)); var sut = new DefaultExecuteStrategy(buildHistory, _buildLog, processor); processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(), typeof(SlimModel)) .Returns(typeCapability); sut.Initialize(buildConfiguration); Action action = () => sut.Populate(model); action.Should().Throw <BuildException>(); }
protected override void ProcessModelAnnotations( Dictionary <string, object?> annotations, IModel model, SlimModel slimModel, bool runtime) { base.ProcessModelAnnotations(annotations, model, slimModel, runtime); if (runtime) { annotations[RelationalAnnotationNames.RelationalModel] = RelationalModel.Create(slimModel, RelationalDependencies.RelationalAnnotationProvider); } else { if (annotations.TryGetValue(RelationalAnnotationNames.DbFunctions, out var functions)) { var slimFunctions = new SortedDictionary <string, IDbFunction>(); foreach (var functionPair in (SortedDictionary <string, IDbFunction>)functions !) { var slimFunction = Create(functionPair.Value, slimModel); slimFunctions[functionPair.Key] = slimFunction; foreach (var parameter in functionPair.Value.Parameters) { var slimParameter = Create(parameter, slimFunction); CreateAnnotations(parameter, slimParameter, static (convention, annotations, source, target, runtime) =>
public void CreatePushesInstanceIntoBuildChain() { var testPassed = false; var buildHistory = new BuildHistory(); var expected = new SlimModel(); var value = Guid.NewGuid(); var processor = Substitute.For <IBuildProcessor>(); var buildConfiguration = Substitute.For <IBuildConfiguration>(); var propertyResolver = Substitute.For <IPropertyResolver>(); var typeCapability = Substitute.For <IBuildCapability>(); var valueCapability = Substitute.For <IBuildCapability>(); typeCapability.SupportsPopulate.Returns(true); typeCapability.AutoPopulate.Returns(true); typeCapability.SupportsCreate.Returns(true); typeCapability.ImplementedByType.Returns(typeof(DummyTypeCreator)); valueCapability.SupportsCreate.Returns(true); valueCapability.ImplementedByType.Returns(typeof(DummyTypeCreator)); var sut = new DefaultExecuteStrategy(buildHistory, _buildLog, processor); processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(), typeof(SlimModel)) .Returns(typeCapability); processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(), Arg.Any <PropertyInfo>()) .Returns(valueCapability); processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(), typeof(Guid)) .Returns(valueCapability); typeCapability.CreateType(sut, typeof(SlimModel), Arg.Any <object[]>()).Returns(expected); valueCapability.CreateProperty(sut, Arg.Any <PropertyInfo>(), Arg.Any <object[]>()).Returns(value); valueCapability.When( x => x.CreateProperty( Arg.Is <IExecuteStrategy>(y => y.BuildChain.Last == expected), Arg.Any <PropertyInfo>(), Arg.Any <object[]>())).Do( x => { sut.BuildChain.Should().HaveCount(1); sut.BuildChain.First().Should().BeOfType <SlimModel>(); testPassed = true; }); typeCapability.Populate(sut, expected).Returns(expected); buildConfiguration.PropertyResolver.Returns(propertyResolver); propertyResolver.GetOrderedProperties(buildConfiguration, typeof(SlimModel)) .Returns(typeof(SlimModel).GetProperties()); sut.Initialize(buildConfiguration); var actual = (SlimModel)sut.Create(typeof(SlimModel)) !; actual.Should().Be(expected); actual.Value.Should().Be(value); sut.BuildChain.Count.Should().Be(0); testPassed.Should().BeTrue(); }
public void CanPopulateExistingInstance() { var expected = new SlimModel(); var actual = Model.Populate(expected); actual.Should().BeSameAs(expected); actual.Value.Should().NotBeEmpty(); }
public void PopulatePropertyThrowsExceptionWithNullProperty() { var instance = new SlimModel(); var sut = new PopulatePropertyWrapper(); Action action = () => sut.RunTest(null !, instance); action.Should().Throw <ArgumentNullException>(); }
public void PopulateUsesDefaultExecuteStrategyToPopulateInstance() { var expected = new SlimModel(); var sut = new BuildConfiguration().UsingModule <DefaultConfigurationModule>(); var actual = sut.Populate(expected); actual.Should().Be(expected); actual.Value.Should().NotBeEmpty(); }
public void CreateDeterminesPropertiesToCreateByProvidingConstructorArgsForNestedType() { var buildHistory = new BuildHistory(); var model = new SlimModel(); var expected = new SimpleConstructor(model); var age = Environment.TickCount; var typeCapability = Substitute.For <IBuildCapability>(); var valueCapability = Substitute.For <IBuildCapability>(); var processor = Substitute.For <IBuildProcessor>(); var buildConfiguration = Substitute.For <IBuildConfiguration>(); var propertyResolver = Substitute.For <IPropertyResolver>(); typeCapability.SupportsPopulate.Returns(true); typeCapability.AutoPopulate.Returns(true); typeCapability.SupportsCreate.Returns(true); typeCapability.ImplementedByType.Returns(typeof(DummyTypeCreator)); valueCapability.SupportsCreate.Returns(true); valueCapability.ImplementedByType.Returns(typeof(DummyTypeCreator)); var sut = new DefaultExecuteStrategy(buildHistory, _buildLog, processor); processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(), typeof(SimpleConstructor)) .Returns(typeCapability); processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(), Arg.Any <PropertyInfo>()) .Returns(valueCapability); processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(), typeof(int)) .Returns(valueCapability); typeCapability.CreateType(sut, typeof(SimpleConstructor), Arg.Any <object[]>()).Returns(expected); valueCapability.CreateProperty(sut, Arg.Is <PropertyInfo>(x => x.Name == nameof(SimpleConstructor.Model)), Arg.Any <object[]>()).Returns(model); valueCapability.CreateProperty(sut, Arg.Is <PropertyInfo>(x => x.Name == nameof(SimpleConstructor.Age)), Arg.Any <object[]>()).Returns(age); typeCapability.Populate(sut, expected).Returns(expected); buildConfiguration.PropertyResolver.Returns(propertyResolver); propertyResolver.GetOrderedProperties(buildConfiguration, typeof(SimpleConstructor)) .Returns(typeof(SimpleConstructor).GetProperties()); sut.Initialize(buildConfiguration); var actual = (SimpleConstructor)sut.Create(typeof(SimpleConstructor), model) !; actual.Should().Be(expected); actual.Model.Should().Be(model); actual.Age.Should().Be(age); }
public void IsMatchThrowsExceptionWithNullType() { var context = new SlimModel(); var buildChain = new BuildHistory(); buildChain.Push(context); var sut = new Wrapper <string>(NameExpression.FirstName, typeof(string)); #pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type. Action action = () => sut.RunIsMatch(null !, "FirstName", buildChain); #pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type. action.Should().Throw <ArgumentNullException>(); }
public void PopulateDoesNotAssignIgnoredProperty() { var buildHistory = new BuildHistory(); var model = new SlimModel(); var processor = Substitute.For <IBuildProcessor>(); var buildConfiguration = Substitute.For <IBuildConfiguration>(); var propertyResolver = Substitute.For <IPropertyResolver>(); var typeCapability = Substitute.For <IBuildCapability>(); var valueCapability = Substitute.For <IBuildCapability>(); typeCapability.AutoPopulate.Returns(true); typeCapability.SupportsCreate.Returns(true); typeCapability.SupportsPopulate.Returns(true); typeCapability.ImplementedByType.Returns(typeof(DummyTypeCreator)); valueCapability.SupportsCreate.Returns(true); valueCapability.ImplementedByType.Returns(typeof(DummyTypeCreator)); var sut = new DefaultExecuteStrategy(buildHistory, _buildLog, processor); processor.GetBuildCapability(sut, BuildRequirement.Populate, typeof(SlimModel)) .Returns(typeCapability); buildConfiguration.PropertyResolver.Returns(propertyResolver); propertyResolver.GetOrderedProperties(buildConfiguration, typeof(SlimModel)) .Returns(typeof(SlimModel).GetProperties()); processor.GetBuildCapability(sut, BuildRequirement.Create, Arg.Is <PropertyInfo>(x => x.Name == nameof(SlimModel.Value))) .Returns(valueCapability); processor.GetBuildCapability(sut, BuildRequirement.Populate, typeof(Guid)) .Returns(valueCapability); typeCapability.Populate(sut, model).Returns(model); propertyResolver.IsIgnored( Arg.Any <IBuildConfiguration>(), Arg.Any <object>(), Arg.Any <PropertyInfo>(), Arg.Any <object[]>()).Returns(true); sut.Initialize(buildConfiguration); var actual = (SlimModel)sut.Populate(model); actual.Value.Should().BeEmpty(); typeCapability.DidNotReceive() .CreateProperty(sut, Arg.Is <PropertyInfo>(x => x.Name == nameof(SlimModel.Value)), null !); }
public void PopulateReturnsInstanceWhenNoBuildActionCapabilityFound() { var buildHistory = new BuildHistory(); var model = new SlimModel(); var processor = Substitute.For <IBuildProcessor>(); var buildConfiguration = Substitute.For <IBuildConfiguration>(); var sut = new DefaultExecuteStrategy(buildHistory, _buildLog, processor); sut.Initialize(buildConfiguration); var actual = sut.Populate(model); actual.Should().Be(model); }
public void GenerateReturnsValueWhenContextLacksNameAndGenderProperties() { var model = new SlimModel(); var buildChain = new BuildHistory(); var executeStrategy = Substitute.For <IExecuteStrategy>(); executeStrategy.BuildChain.Returns(buildChain); buildChain.Push(model); var sut = new Wrapper(); var actual = (string)sut.RunGenerate(typeof(string), "email", executeStrategy); actual.Should().NotBeNullOrWhiteSpace(); }
public void PopulateAssignsPropertyValuesToExistingInstance() { var buildHistory = new BuildHistory(); var model = new SlimModel(); var expected = Guid.NewGuid(); var properties = typeof(SlimModel).GetProperties(); var propertyInfo = properties.Single(); var processor = Substitute.For <IBuildProcessor>(); var buildConfiguration = Substitute.For <IBuildConfiguration>(); var propertyResolver = Substitute.For <IPropertyResolver>(); var typeCapability = Substitute.For <IBuildCapability>(); var valueCapability = Substitute.For <IBuildCapability>(); typeCapability.AutoPopulate.Returns(true); typeCapability.SupportsCreate.Returns(true); typeCapability.SupportsPopulate.Returns(true); typeCapability.ImplementedByType.Returns(typeof(DummyTypeCreator)); valueCapability.SupportsCreate.Returns(true); valueCapability.ImplementedByType.Returns(typeof(DummyTypeCreator)); var sut = new DefaultExecuteStrategy(buildHistory, _buildLog, processor); processor.GetBuildCapability(sut, BuildRequirement.Populate, typeof(SlimModel)) .Returns(typeCapability); buildConfiguration.PropertyResolver.Returns(propertyResolver); propertyResolver.GetOrderedProperties(buildConfiguration, typeof(SlimModel)) .Returns(properties); processor.GetBuildCapability(sut, BuildRequirement.Create, propertyInfo) .Returns(valueCapability); valueCapability.CreateProperty(sut, propertyInfo, Arg.Any <object?[]?>()) .Returns(expected); processor.GetBuildCapability(sut, BuildRequirement.Populate, typeof(Guid)) .Returns(valueCapability); typeCapability.Populate(sut, model).Returns(model); sut.Initialize(buildConfiguration); var actual = (SlimModel)sut.Populate(model); actual.Value.Should().Be(expected); }
public void CreateReturnsValueFromProcessorWithPropertyPopulation() { var buildHistory = new BuildHistory(); var expected = new SlimModel(); var value = Guid.NewGuid(); var processor = Substitute.For <IBuildProcessor>(); var buildConfiguration = Substitute.For <IBuildConfiguration>(); var propertyResolver = Substitute.For <IPropertyResolver>(); var typeCapability = Substitute.For <IBuildCapability>(); var valueCapability = Substitute.For <IBuildCapability>(); typeCapability.AutoPopulate.Returns(true); typeCapability.SupportsCreate.Returns(true); typeCapability.SupportsPopulate.Returns(true); typeCapability.ImplementedByType.Returns(typeof(DummyTypeCreator)); valueCapability.SupportsCreate.Returns(true); valueCapability.ImplementedByType.Returns(typeof(DummyTypeCreator)); var sut = new DefaultExecuteStrategy(buildHistory, _buildLog, processor); processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(), typeof(SlimModel)) .Returns(typeCapability); processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(), Arg.Any <PropertyInfo>()) .Returns(valueCapability); processor.GetBuildCapability(sut, Arg.Any <BuildRequirement>(), typeof(Guid)) .Returns(valueCapability); typeCapability.CreateType(sut, typeof(SlimModel), Arg.Any <object[]>()).Returns(expected); valueCapability.CreateProperty(sut, Arg.Any <PropertyInfo>(), Arg.Any <object[]>()).Returns(value); typeCapability.Populate(sut, expected).Returns(expected); buildConfiguration.PropertyResolver.Returns(propertyResolver); propertyResolver.GetOrderedProperties(buildConfiguration, typeof(SlimModel)) .Returns(typeof(SlimModel).GetProperties()); sut.Initialize(buildConfiguration); var actual = (SlimModel)sut.Create(typeof(SlimModel)) !; actual.Should().Be(expected); actual.Value.Should().Be(value); }