public void Should_Use_Double_Range_Data_Annotation_As_Convention() { var testModel = NAuto.AutoBuild <TestAnnotationModelIntegrationTests>() .ClearConventions() .Construct() .Build(); // Assert testModel.RangeDoubleTest.ShouldBeGreaterThanOrEqualTo(1.2); testModel.RangeDoubleTest.ShouldBeLessThanOrEqualTo(10.2); }
public void Should_Return_Model_With_Updated_Random_Settings_For_2_Levels_Deep_Property_In_Graph() { // Act var testModel = NAuto.AutoBuild <TestModel>() .Construct() .With(x => x.SubTestModel.SubString, 7, CharacterSetType.Anything, Spaces.Middle, Casing.Lowered) .Build(); // Assert testModel.SubTestModel.SubString.Length.ShouldEqual(7); }
/// <summary> /// Override model string property. /// </summary> /// <param name="expression">The expression.</param> /// <param name="minLength">The minimum length.</param> /// <param name="maxLength">The maximum length.</param> /// <param name="characterSetType">Type of the character set.</param> /// <param name="spaces">The spaces.</param> /// <param name="casing">The casing.</param> /// <returns>Returns this.</returns> public IAutoBuilderOverrides <TModel> With( Expression <Func <TModel, string> > expression, int minLength, int maxLength, CharacterSetType characterSetType, Spaces spaces, Casing casing) { this.Actions.Add(() => SetStringPropertyUsingNewRandomizerSetting(() => NAuto.GetRandomString(minLength, minLength, characterSetType, spaces, casing), expression)); return(this); }
public void Should_Return_Model_With_Updated_Random_Settings_For_Top_Level_Integer_Property_In_Graph() { // Act var testModel = NAuto.AutoBuild <TestModel>() .Construct() .With(x => x.FavouriteInteger, 10, 13) .Build(); // Assert testModel.FavouriteInteger.ShouldBeGreaterThanOrEqualTo(10); testModel.FavouriteInteger.ShouldBeLessThanOrEqualTo(13); }
public void Should_Create_Empty_Instance_Of_Model_In_Json() { const string Email = "*****@*****.**"; var testModel = NAuto.AutoBuild <TestModel>() .Empty() .With(x => x.Email = Email) .ToJson(); testModel.ShouldNotBeNull(); testModel.ShouldContain(Email); }
public void Should_Return_Model_With_Updated_Random_Settings_For_3rd_Level_Double_Property_In_Graph() { // Act var testModel = NAuto.AutoBuild <TestModel>() .Construct() .With(x => x.SubTestModel.SubSubTestModel.SubSubDouble, 100, 103) .Build(); // Assert testModel.SubTestModel.SubSubTestModel.SubSubDouble.ShouldBeGreaterThanOrEqualTo(100); testModel.SubTestModel.SubSubTestModel.SubSubDouble.ShouldBeLessThanOrEqualTo(103); }
public void Should_Generate_List_With_Specified_Number_Of_Arguments() { var config = new AutoBuilderConfiguration(defaultLanguage: Language.Chinese); var testModel = NAuto.AutoBuild <TestModel>(config) .Configure(x => x.DefaultLanguage = Language.Chinese) .Construct() .With(x => x.FavouriteStringList = NAuto.GetRandomList <string>(5, autoBuilderConfiguration: config)) .Build(); testModel.ShouldNotBeNull(); }
public void Should_Create_Empty_Instance_Of_Model() { const string Email = "*****@*****.**"; var testModel = NAuto.AutoBuild <TestModel>() .Empty() .With(x => x.Email = Email) .Build(); testModel.ShouldNotBeNull(); testModel.Email.ShouldEqual(Email); }
public static string GetRandomTelephoneNumber(Language language = Language.English) { var firstPart = NAuto.GetRandomString(4, CharacterSetType.Numeric, Spaces.None, Casing.Any, language); var secondPart = NAuto.GetRandomString(3, CharacterSetType.Numeric, Spaces.None, Casing.Any, language); var thirdPart = NAuto.GetRandomString(3, CharacterSetType.Numeric, Spaces.None, Casing.Any, language); if (language != Language.Chinese) { return(string.Format("0{0} {1} {2}", firstPart, secondPart, thirdPart)); } return(string.Format("{0} {1} {2}", firstPart, secondPart, thirdPart)); }
public void Should_Return_Sequenced_Random_List_Using_Specified_Increment() { // Act var list = NAuto.GetRandomList <ClassForSequencing>(x => x.Id, 5, 10, 2); // Assert list.Count.ShouldEqual(5); list[0].Id.ShouldEqual(10); list[1].Id.ShouldEqual(12); list[2].Id.ShouldEqual(14); list[3].Id.ShouldEqual(16); list[4].Id.ShouldEqual(18); }
public void Should_Return_Sequenced_Random_List_Starting_From_Correct_Seed_Number() { // Act var list = NAuto.GetRandomList <ClassForSequencing>(x => x.Id, 5, 10); // Assert list.Count.ShouldEqual(5); list[0].Id.ShouldEqual(10); list[1].Id.ShouldEqual(11); list[2].Id.ShouldEqual(12); list[3].Id.ShouldEqual(13); list[4].Id.ShouldEqual(14); }
public void Should_Return_A_Random_Integer_Which_Is_Less_Than_Or_Equal_To_Max() { // Arrange var max = random.Next(123456); // Act var result1 = NAuto.GetRandomInteger(max); var result2 = NAuto.GetRandomInteger(max); // Assert result1.ShouldNotEqual(result2); result1.ShouldBeLessThanOrEqualTo(max); result2.ShouldBeLessThanOrEqualTo(max); }
public void Should_Return_A_Fixed_Length_Random_String_Which_Can_Include_Any_Characters_And_Casing() { // Arrange var length = random.Next(100, 10000); // Act var result1 = NAuto.GetRandomString(length, CharacterSetType.Anything); var result2 = NAuto.GetRandomString(length, CharacterSetType.Anything); // Assert result1.Length.ShouldEqual(length); result2.Length.ShouldEqual(length); result1.ShouldNotEqual(result2); }
public void Should_Override_Property_With_Custom_Convention() { var autoTestBuilderConfiguration = new AutoBuilderConfiguration(); autoTestBuilderConfiguration.Conventions.Add(new ConventionMap(ConventionFilterType.Contains, "PetName", typeof(string), config => "Rex")); var conventionsTestModel = NAuto.AutoBuild <ConventionsModel>(autoTestBuilderConfiguration) .Construct() .Build(); // Assert conventionsTestModel.PetName.ShouldEqual("Rex"); conventionsTestModel.SubConventionsModel.PetName.ShouldEqual("Rex"); }
public void Should_Override_Property_With_Random_Email() { // Act var testModel = NAuto.AutoBuild <TestModel>() .Construct() .With(x => x.Email, PropertyType.Email) .With(x => x.SubTestModel.SubEmail, PropertyType.Email) .With(x => x.SubTestModel.SubSubTestModel.SubSubEmail, PropertyType.Email) .Build(); // Assert testModel.Email.ShouldContain("@"); testModel.SubTestModel.SubEmail.ShouldContain("@"); testModel.SubTestModel.SubSubTestModel.SubSubEmail.ShouldContain("@"); }
private static int GenerateRandomIntFromDataAnnotations(PropertyInfo propertyInfo, AutoBuilderConfiguration autoBuilderConfiguration) { var min = autoBuilderConfiguration.IntMinimum; var max = autoBuilderConfiguration.IntMaximum; var rangeAttribute = propertyInfo.GetCustomAttributes(typeof(RangeAttribute), false).FirstOrDefault(); if (rangeAttribute != null) { min = (int)((RangeAttribute)rangeAttribute).Minimum; max = (int)((RangeAttribute)rangeAttribute).Maximum; } return(NAuto.GetRandomInteger(min, max)); }
public static List <TModel> Get <TModel>(int numberOfItems = 2, AutoBuilderConfiguration configuration = null, Language language = Language.English) where TModel : class { var autoBuilder = configuration != null?NAuto.AutoBuild <List <TModel> >(configuration) : NAuto.AutoBuild <List <TModel> >(); if (language == Language.English && configuration != null) { language = configuration.DefaultLanguage; } return(autoBuilder .Configure(x => x.DefaultCollectionItemCount = numberOfItems) .Configure(x => x.DefaultLanguage = language) .Construct() .Build()); }
public void Should_Return_A_Random_String_Between_MinLength_And_MaxLength() { // Arrange var minLength = random.Next(5); var maxLength = random.Next(6, 10000); // Act var result1 = NAuto.GetRandomString(minLength, maxLength, CharacterSetType.Anything, Spaces.Any); var result2 = NAuto.GetRandomString(minLength, maxLength, CharacterSetType.Anything, Spaces.Any); // Assert result1.Length.ShouldBeGreaterThanOrEqualTo(minLength); result2.Length.ShouldBeLessThanOrEqualTo(maxLength); result1.ShouldNotEqual(result2); }
private static double GenerateRandomDoubleFromDataAnnotations(PropertyInfo propertyInfo, AutoBuilderConfiguration autoBuilderConfiguration) { var min = autoBuilderConfiguration.DoubleMinimum; var max = autoBuilderConfiguration.DoubleMaximum; var rangeAttribute = propertyInfo.GetCustomAttributes(typeof(RangeAttribute), false).FirstOrDefault(); if (rangeAttribute != null) { min = double.Parse(((RangeAttribute)rangeAttribute).Minimum.ToString()); max = double.Parse(((RangeAttribute)rangeAttribute).Maximum.ToString()); } return(NAuto.GetRandomDouble(min, max)); }
public void Should_Override_Properties_Data_Generation() { // Arrange const int minLength = 5; const int maxLength = 50; var config = new AutoBuilderConfiguration(stringMinLength: minLength, stringMaxLength: maxLength); // Act var testModel = NAuto.AutoBuild <TestModel>(config) .Construct() .Build(); // Assert testModel.SubTestModel.SubString.Length.ShouldBeGreaterThanOrEqualTo(minLength); testModel.SubTestModel.SubString.Length.ShouldBeLessThanOrEqualTo(maxLength); }
public void Should_Construct_List_Based_Top_Level_Models_With_Parameters() { // Arrange var testListParameter = new List <TestModel>(); testListParameter.Add(new TestModel { FirstName = "Sean" }); // Act var testList = NAuto.AutoBuild <List <TestModel> >() .Construct(testListParameter) .Build(); // Assert testList.ShouldNotBeEmpty(); }
private double?GetDoubleValue(string propertyName, PropertyInfo propertyInfo) { if (AutoBuilderConfiguration.Conventions.MatchesConvention(propertyName, typeof(double?))) { return((double?)AutoBuilderConfiguration.Conventions.GetConventionResult(propertyName, typeof(double?), AutoBuilderConfiguration)); } var annotatedType = dataAnnotationConventionMapper.TryGetValue(typeof(double), propertyInfo, AutoBuilderConfiguration); if (annotatedType != null) { return((double)annotatedType); } return(NAuto.GetRandomDouble(AutoBuilderConfiguration.DoubleMinimum, AutoBuilderConfiguration.DoubleMaximum)); }
public void Should_Return_A_Random_String_Which_Should_End_With_A_Space() { // Arrange var length = random.Next(20, 10000); // Act var result1 = NAuto.GetRandomString(length, CharacterSetType.AlphaNumeric, Spaces.End, Casing.Any); var result2 = NAuto.GetRandomString(length, CharacterSetType.AlphaNumeric, Spaces.End, Casing.Any); // Assert result1.Length.ShouldEqual(length); result2.Length.ShouldEqual(length); result1.ShouldNotEqual(result2); result1[length - 1].ShouldEqual(' '); result2[length - 1].ShouldEqual(' '); }
private int?GetIntValue(string propertyName, PropertyInfo propertyInfo) { if (AutoBuilderConfiguration.Conventions.MatchesConvention(propertyName, typeof(int?))) { return((int)AutoBuilderConfiguration.Conventions.GetConventionResult(propertyName, typeof(int?), AutoBuilderConfiguration)); } var annotatedType = dataAnnotationConventionMapper.TryGetValue(typeof(int), propertyInfo, AutoBuilderConfiguration); if (annotatedType != null) { return((int)annotatedType); } return(NAuto.GetRandomInteger(AutoBuilderConfiguration.IntMinimum, AutoBuilderConfiguration.IntMaximum)); }
public void Should_Return_A_Random_Integer_Which_Is_Between_Min_And_Max() { // Arrange var min = random.Next(100); var max = random.Next(101, 123456); // Act var result1 = NAuto.GetRandomInteger(min, max); var result2 = NAuto.GetRandomInteger(min, max); // Assert result1.ShouldNotEqual(result2); result1.ShouldBeGreaterThanOrEqualTo(min); result1.ShouldBeLessThanOrEqualTo(max); result2.ShouldBeGreaterThanOrEqualTo(min); result2.ShouldBeLessThanOrEqualTo(max); }
public void Should_Return_A_Random_String_Which_Can_Contain_Spaces() { // Arrange var length = random.Next(20, 10000); var regexForTrue = new Regex("^[a-zA-Z0-9 ]*$"); // Act var result1 = NAuto.GetRandomString(length, CharacterSetType.AlphaNumeric, Spaces.Any, Casing.Any); var result2 = NAuto.GetRandomString(length, CharacterSetType.AlphaNumeric, Spaces.Any, Casing.Any); // Assert result1.Length.ShouldEqual(length); result2.Length.ShouldEqual(length); result1.ShouldNotEqual(result2); regexForTrue.IsMatch(result1).ShouldBeTrue("Can contain spaces " + result1); regexForTrue.IsMatch(result2).ShouldBeTrue("Can contain spaces " + result2); }
public void Should_Return_A_Random_Double_Which_Is_Between_Min_And_Max() { // Arrange const double min = 123456.123456; const double max = 456789.789456; // Act var result1 = NAuto.GetRandomDouble(min, max); var result2 = NAuto.GetRandomDouble(min, max); // Assert result1.ShouldNotEqual(result2); result1.ShouldBeGreaterThanOrEqualTo(min); result1.ShouldBeLessThanOrEqualTo(max); result2.ShouldBeGreaterThanOrEqualTo(min); result2.ShouldBeLessThanOrEqualTo(max); }
public object TryGetValue(Type type, PropertyInfo propertyInfo, AutoBuilderConfiguration autoBuilderConfiguration) { if (propertyInfo != null) { var dataTypeAttribute = propertyInfo.GetCustomAttributes(typeof(DataTypeAttribute), true).FirstOrDefault(); if (dataTypeAttribute != null) { var dataType = ((DataTypeAttribute)dataTypeAttribute).DataType; if (type == typeof(string)) { if (dataType == DataType.EmailAddress) { return(NAuto.GetRandomPropertyType(PropertyType.Email)); } if (dataType == DataType.PhoneNumber) { return(NAuto.GetRandomPropertyType(PropertyType.TelephoneNumber)); } if (dataType == DataType.Url) { return(NAuto.GetRandomPropertyType(PropertyType.Url)); } } } if (type == typeof(string)) { return(GenerateRandomStringFromDataAnnotations(propertyInfo, autoBuilderConfiguration)); } if (type == typeof(int)) { return(GenerateRandomIntFromDataAnnotations(propertyInfo, autoBuilderConfiguration)); } if (type == typeof(double)) { return(GenerateRandomDoubleFromDataAnnotations(propertyInfo, autoBuilderConfiguration)); } } return(null); }
public void Should_Return_A_Fixed_Length_Random_String_Which_Can_Contain_Numbers_Only() { // Arrange var length = random.Next(20, 10000); var regexForFalse = new Regex("^[a-zA-Z_]*$"); var regexForTrue = new Regex("^[0-9 ]*$"); // Act var result1 = NAuto.GetRandomString(length, CharacterSetType.Numeric); var result2 = NAuto.GetRandomString(length, CharacterSetType.Numeric); // Assert result1.Length.ShouldEqual(length); result2.Length.ShouldEqual(length); result1.ShouldNotEqual(result2); regexForFalse.IsMatch(result1).ShouldBeFalse("Should only contain numbers " + result1); regexForFalse.IsMatch(result2).ShouldBeFalse("Should only contain numbers " + result2); regexForTrue.IsMatch(result1).ShouldBeTrue("Should only contain numbers " + result1); regexForTrue.IsMatch(result2).ShouldBeTrue("Should only contain numbers " + result2); }
public void Should_Return_A_Fixed_Length_Random_String_Which_Can_Contain_Upper_Case_Only() { // Arrange var length = random.Next(20, 10000); var regexForFalse = new Regex("^[a-z0-9_]*$"); var regexForTrue = new Regex("^[A-Z ]*$"); // Act var result1 = NAuto.GetRandomString(length, CharacterSetType.Alpha, Spaces.Any, Casing.Uppered); var result2 = NAuto.GetRandomString(length, CharacterSetType.Alpha, Spaces.Any, Casing.Uppered); // Assert result1.Length.ShouldEqual(length); result2.Length.ShouldEqual(length); result1.ShouldNotEqual(result2); regexForFalse.IsMatch(result1).ShouldBeFalse("Should only contain upper case letters " + result1); regexForFalse.IsMatch(result2).ShouldBeFalse("Should only contain upper case letters " + result2); regexForTrue.IsMatch(result1).ShouldBeTrue("Should only contain upper case letters " + result1); regexForTrue.IsMatch(result2).ShouldBeTrue("Should only contain upper case letters " + result2); }