/// <summary> /// Registers an instance of <see cref="BOTestFactory"/> /// </summary> /// <typeparam name="TBO"></typeparam> /// <param name="boTestFactory"></param> public virtual void Register <TBO>(BOTestFactory boTestFactory) { lock (_lockProp) { var boType = typeof(TBO); this.ClearPreviousInstances(boType); this._boTestFactoryInstances.Add(boType, boTestFactory); } }
public void Test_CreateValidBusinessObject_ShouldReturnBOWithCompulsoryPropsPopulated() { //---------------Set up test pack------------------- //---------------Assert Precondition---------------- var factory = new BOTestFactory(typeof(FakeBO)); //---------------Execute Test ---------------------- var businessObject = factory.CreateValidBusinessObject() as FakeBO; //---------------Test Result ----------------------- Assert.IsNotNull(businessObject); Assert.IsInstanceOf<FakeBO>(businessObject); Assert.IsNotNull(businessObject.CompulsoryString); }
public void Test_SetDefaultValue_WhenRelationship_ShouldCreateWithValueSet() { //---------------Set up test pack------------------- var relatedBO = new RelatedFakeBo(); var boWithRelFactory = new BOTestFactory(typeof(FakeBOWithRelationship)); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- boWithRelFactory.SetValueFor("SingleRelationship", relatedBO); var boWithRelationship = boWithRelFactory.CreateValidBusinessObject() as FakeBOWithRelationship; //---------------Test Result ----------------------- Assert.IsNotNull(boWithRelationship); Assert.AreSame(relatedBO, boWithRelationship.SingleRelationship); }
public void Test_SetDefaultValue_WhenProperty_ShouldReturnSetValue() { //---------------Set up test pack------------------- const string expectedPropValue = "SomeValue"; const string propName = "SomeProp"; var boWithRelFactory = new BOTestFactory(typeof(FakeBO)); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- boWithRelFactory.SetValueFor(propName, expectedPropValue); var actualValue = boWithRelFactory.GetValidPropValue(typeof(FakeBOWithRelationship), propName); //---------------Test Result ----------------------- Assert.AreSame(expectedPropValue, actualValue); }
public void Test_GetValidRelationshipValue_ShouldRetValidValue() { //---------------Set up test pack------------------- var relationship = new BOTestFactory(typeof(FakeBO)).CreateValidBusinessObject().Relationships["NonCompulsoryRelationship"] as ISingleRelationship; var factory = new BOTestFactory(typeof(FakeBO)); //---------------Assert Precondition---------------- Assert.IsNotNull(relationship); Assert.IsNull(relationship.GetRelatedObject()); //---------------Execute Test ---------------------- var validRelationshipValue = factory.GetValidRelationshipValue(relationship.RelationshipDef as ISingleValueDef); //---------------Test Result ----------------------- Assert.IsNotNull(validRelationshipValue); Assert.AreSame(validRelationshipValue.ClassDef, relationship.RelatedObjectClassDef); }
public void Test_GetValidRelationshipValue_ShouldCreateRelatedBO() { //---------------Set up test pack------------------- var boTestFactory = new BOTestFactory(typeof(FakeBO)); var businessObject = (FakeBO) boTestFactory.CreateValidBusinessObject(); var relationship = businessObject.Relationships["NonCompulsoryRelationship"] as ISingleRelationship; //---------------Assert Precondition---------------- Assert.IsNotNull(relationship); Assert.IsNull(relationship.GetRelatedObject()); //---------------Execute Test ---------------------- IRelationshipDef relationshipDef = relationship.RelationshipDef; IBusinessObject validRelationshipValue = boTestFactory.GetValidRelationshipValue(relationshipDef as ISingleValueDef); //---------------Test Result ----------------------- Assert.IsNotNull(validRelationshipValue); Assert.IsTrue(validRelationshipValue.Status.IsNew); }
public static BOTestFactory <T> WithValueFor <T, TPropType>(this BOTestFactory <T> factory, Expression <Func <T, TPropType> > expression) where T : class, IBusinessObject { factory.SetValueFor(expression); return(factory); }
public void Test_GetValidValue_WithBO_WhenNotRelOrProp_ShouldRaiseError() { //---------------Set up test pack------------------- var factory = new BOTestFactory(typeof(FakeBO)); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- try { factory.GetValidValue(new FakeBO(), "InvalidProp"); Assert.Fail("Expected to throw an HabaneroApplicationException"); } //---------------Test Result ----------------------- catch (HabaneroDeveloperException ex) { string message = "The property 'InvalidProp' for the ClassDef"; StringAssert.Contains(message, ex.Message); message = "is not defined"; StringAssert.Contains(message, ex.Message); } }
public void Test_GetValidPropValue_WhenStringAndMaxLengthWhenPropName_ShouldRetValidValue() { //---------------Set up test pack------------------- var classDef = ClassDef.Get<FakeBO>(); var def = classDef.PropDefcol.FirstOrDefault(propDef => propDef.PropertyName == "CompulsoryString"); //---------------Assert Precondition---------------- Assert.IsNotNull(def); def.AddPropRule(CreatePropRuleString(3, 7)); var factory = new BOTestFactory(typeof(FakeBO)); Assert.AreSame(typeof(string), def.PropertyType); Assert.IsNotEmpty(def.PropRules.OfType<PropRuleString>().ToList()); var propRule = def.PropRules.OfType<PropRuleString>().First(); Assert.AreEqual(3, propRule.MinLength); Assert.AreEqual(7, propRule.MaxLength); //---------------Execute Test ---------------------- var validPropValue = factory.GetValidPropValue(typeof(FakeBO), "CompulsoryString").ToString(); //---------------Test Result ----------------------- Assert.IsNotNull(validPropValue); Assert.GreaterOrEqual(validPropValue.Length, 3); Assert.LessOrEqual(validPropValue.Length, 7); string errMessage = ""; Assert.IsTrue(def.IsValueValid(validPropValue, ref errMessage)); }
public void Test_SetRelationship_WhenHasValue_AndValueRegistered_ShouldSetToRegisteredValue() { //---------------Set up test pack------------------- var bo = new BOTestFactory(typeof(FakeBO)).CreateValidBusinessObject(); var relationship = bo.Relationships["NonCompulsoryRelationship"] as ISingleRelationship; var factory = new BOTestFactory<FakeBO>(); factory.SetRelationshipToValidValue(relationship); var expectedRelationshipValue = new RelatedFakeBo(); //---------------Assert Precondition---------------- Assert.IsNotNull(relationship); var origionalBO = relationship.GetRelatedObject(); Assert.IsNotNull(relationship.GetRelatedObject()); //---------------Execute Test ---------------------- factory.SetValueFor(fakeBO => fakeBO.NonCompulsoryRelationship, expectedRelationshipValue); factory.SetRelationshipToValidValue(relationship); var actualRelatedObject = relationship.GetRelatedObject(); //---------------Test Result ----------------------- Assert.IsNotNull(actualRelatedObject); Assert.AreSame(expectedRelationshipValue, actualRelatedObject); }
public void Test_GetValidPropValue_WhenNoPropDefForClassDef_ShouldRaiseError() { //---------------Set up test pack------------------- Type type = typeof(FakeBO); var factory = new BOTestFactory(typeof(FakeBO)); //---------------Assert Precondition---------------- Assert.IsTrue(ClassDef.ClassDefs.Contains(type)); //---------------Execute Test ---------------------- try { factory.GetValidPropValue(type, "InvalidProp"); Assert.Fail("Expected to throw an HabaneroDeveloperException"); } catch (HabaneroDeveloperException ex) { StringAssert.Contains(string.Format("The property '{0}' for the ClassDef for '{1}' is not defined", "InvalidProp", type), ex.Message); StringAssert.Contains("The BOTestFactory class is designed to be used in Testing so it is likely that your classdefs are not being loaded as part of your testing process.", ex.DeveloperMessage); } }
public void Test_GetValidPropValue_WhenStringAndMaxLength_ShouldRetValidValue() { //---------------Set up test pack------------------- IPropDef def = new PropDefFake { PropertyType = typeof(string) }; def.AddPropRule(CreatePropRuleString(3, 7)); IBOProp prop = new BOProp(def); var factory = new BOTestFactory(typeof(FakeBO)); //---------------Assert Precondition---------------- Assert.AreSame(typeof(string), prop.PropertyType); Assert.IsNotEmpty(def.PropRules.OfType<PropRuleString>().ToList()); var propRule = def.PropRules.OfType<PropRuleString>().First(); Assert.AreEqual(3, propRule.MinLength); Assert.AreEqual(7, propRule.MaxLength); //---------------Execute Test ---------------------- var validPropValue = factory.GetValidPropValue(prop); //---------------Test Result ----------------------- Assert.IsNotNull(validPropValue); Assert.GreaterOrEqual(validPropValue.ToString().Length, 3); Assert.LessOrEqual(validPropValue.ToString().Length, 7); }
public void Test_GetValidPropValue_WhenDateTimeAndMaxValue_WhenPropName_ShouldRetValidValue() { //---------------Set up test pack------------------- IPropDef def = new PropDefFake { PropertyType = typeof(DateTime) }; DateTime min = RandomValueGen.GetAbsoluteMin<DateTime>().AddDays(5555.0); DateTime max = RandomValueGen.GetAbsoluteMin<DateTime>().AddDays(5565.0); def.AddPropRule(TestUtilsFactory.CreatePropRuleDateTime(min, max)); var factory = new BOTestFactory(typeof(FakeBO)); //---------------Assert Precondition---------------- Assert.AreSame(typeof(DateTime), def.PropertyType); Assert.IsNotEmpty(def.PropRules.OfType<PropRuleDate>().ToList()); var propRule = def.PropRules.OfType<PropRuleDate>().First(); Assert.AreEqual(min, propRule.MinValue); Assert.AreEqual(max, propRule.MaxValue.Date); //---------------Execute Test ---------------------- var validPropValue = factory.GetValidPropValue(def); //---------------Test Result ----------------------- Assert.GreaterOrEqual((DateTime) validPropValue, min); Assert.LessOrEqual((DateTime) validPropValue, max); string errMessage = ""; Assert.IsTrue(def.IsValueValid(validPropValue, ref errMessage), errMessage); }
public void Test_GetValidPropValue_WhenClassNotInClassDefs_ShouldRaiseError() { //---------------Assert Precondition---------------- Type type = typeof(Unmapped); var factory = new BOTestFactory(typeof(FakeBO)); Assert.IsFalse(ClassDef.ClassDefs.Contains(type)); //---------------Execute Test ---------------------- try { factory.GetValidPropValue(type, "SomeProp"); Assert.Fail("Expected to throw an HabaneroDeveloperException"); } catch (HabaneroDeveloperException ex) { StringAssert.Contains(string.Format("The ClassDef for '{0}' does not have any classDefs Loaded", type), ex.Message); StringAssert.Contains("The BOTestFactory class is designed to be used in Testing so it is likely that your classdefs are not being loaded as part of your testing process", ex.DeveloperMessage); } }
public static BOTestFactory <T> WithValueFor <T, TPropType>(this BOTestFactory <T> factory, string propertyName) where T : class, IBusinessObject { factory.SetValueFor(propertyName); return(factory); }
public void Test_SetRelationship_WhenNull_ShouldSetValue() { //---------------Set up test pack------------------- var bo = new BOTestFactory(typeof(FakeBO)).CreateValidBusinessObject(); var relationship = bo.Relationships["NonCompulsoryRelationship"] as ISingleRelationship; var factory = new BOTestFactory<FakeBO>(); //---------------Assert Precondition---------------- Assert.IsNotNull(relationship); Assert.IsNull(relationship.GetRelatedObject()); //---------------Execute Test ---------------------- factory.SetRelationshipToValidValue(relationship); //---------------Test Result ----------------------- Assert.IsNotNull(relationship.GetRelatedObject()); }
public void Test_GetValidValueGenerator_WhenEnum_ShouldRetEnumGenerator() { //---------------Set up test pack------------------- IPropDef def = new PropDefFake { PropertyType = typeof(FakeEnum) }; //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var generator = new BOTestFactory<FakeBOWithRules>().GetValidValueGenerator(def); //---------------Test Result ----------------------- Assert.IsNotNull(generator); Assert.IsInstanceOf<ValidValueGeneratorEnum>(generator); }
public void Test_SetRelationship_WhenHasValue_ShouldNotChange() { //---------------Set up test pack------------------- var bo = new BOTestFactory(typeof(FakeBO)).CreateValidBusinessObject(); var relationship = bo.Relationships["NonCompulsoryRelationship"] as ISingleRelationship; var factory = new BOTestFactory<FakeBO>(); factory.SetRelationshipToValidValue(relationship); //---------------Assert Precondition---------------- Assert.IsNotNull(relationship); var origionalBO = relationship.GetRelatedObject(); Assert.IsNotNull(relationship.GetRelatedObject()); //---------------Execute Test ---------------------- factory.SetRelationshipToValidValue(relationship); var actualRelatedObject = relationship.GetRelatedObject(); //---------------Test Result ----------------------- Assert.IsNotNull(actualRelatedObject); Assert.AreSame(origionalBO, actualRelatedObject); }
public void Test_GetValidValue_WithBO_WhenIsRelationship_ShouldRetValidValue() { //---------------Set up test pack------------------- const string relName = "NonCompulsoryRelationship"; Type boType = typeof(FakeBO); var relationship = new BOTestFactory(boType).CreateValidBusinessObject().Relationships[relName] as ISingleRelationship; var factory = new BOTestFactory(boType); //---------------Assert Precondition---------------- Assert.IsNotNull(relationship); Assert.IsNull(relationship.GetRelatedObject()); //---------------Execute Test ---------------------- object validValue = factory.GetValidValue(new FakeBO(),relName); //---------------Test Result ----------------------- Assert.IsNotNull(validValue); Assert.IsInstanceOf<IBusinessObject>(validValue); var validRelationshipValue = validValue as IBusinessObject; Assert.IsNotNull(validRelationshipValue); Assert.AreSame(validRelationshipValue.ClassDef, relationship.RelatedObjectClassDef); }
public void Test_GetValidValue_WithBO_WhenIsProp_ShouldRetValidValue() { //---------------Set up test pack------------------- var classDef = ClassDef.Get<FakeBO>(); var factory = new BOTestFactory(typeof(FakeBO)); IPropDef def = classDef.PropDefcol.FirstOrDefault(propDef => propDef.PropertyName == "CompulsoryString"); //---------------Assert Precondition---------------- Assert.IsNotNull(def); def.AddPropRule(CreatePropRuleString(3, 7)); //---------------Execute Test ---------------------- var validPropValue = factory.GetValidValue(new FakeBO(), "CompulsoryString"); //---------------Test Result ----------------------- Assert.IsNotNull(validPropValue); string validPropStringValue = validPropValue.ToString(); Assert.GreaterOrEqual(validPropStringValue.Length, 3); Assert.LessOrEqual(validPropStringValue.Length, 7); string errMessage = ""; Assert.IsTrue(def.IsValueValid(validPropStringValue, ref errMessage)); }
public void Test_GetValidValueGenerator_WhenHasLookup_ShouldRetLookupGenerator() { //---------------Set up test pack------------------- IPropDef def = new PropDefFake { PropertyType = typeof(string), LookupList = MockRepository.GenerateStub<IBusinessObjectLookupList>() }; //---------------Assert Precondition---------------- Assert.IsTrue(def.HasLookupList()); //---------------Execute Test ---------------------- var generator = new BOTestFactory<FakeBOWithRules>().GetValidValueGenerator(def); //---------------Test Result ----------------------- Assert.IsNotNull(generator); Assert.IsInstanceOf<ValidValueGeneratorLookupList>(generator); }