From 03036c79b5643c6b0b6cc48c4fef4e0cfbbe4b85 Mon Sep 17 00:00:00 2001 From: Blake Stephens Date: Wed, 24 Apr 2024 13:02:36 -0400 Subject: [PATCH 1/2] Reworked file structure. Switched to bogus. Reworked so DapperAutoData exists in dll Added functionality for automatically registering customizations. --- DapperAutoData/AutoMoqDataAttribute.cs | 49 ++++++ .../ContentFiles/AutoMoqDataAttribute.cs | 46 ------ .../ContentFiles/DataGeneratorInstaller.cs | 16 -- DapperAutoData/DapperAutoData.csproj | 17 +- DapperAutoData/DapperAutoData.nuspec | 10 +- DapperAutoData/DataGeneratorInstaller.cs | 42 +++++ DapperAutoData/DefaultCustomizations.cs | 17 ++ .../{Lib => }/Generators/BooleanGenerators.cs | 2 +- .../Generators/DateTimeGenerator.cs | 148 ++++++++++++++++++ DapperAutoData/Generators/NumberGenerator.cs | 32 ++++ DapperAutoData/Generators/StringGenerator.cs | 50 ++++++ DapperAutoData/IDapperProjectCustomization.cs | 11 ++ DapperAutoData/IDataGenerator.cs | 11 ++ DapperAutoData/Lib/DefaultCustomizations.cs | 11 -- DapperAutoData/Lib/Generators/CompanyName.cs | 23 --- .../Lib/Generators/NegativeNumber.cs | 30 ---- .../Lib/Generators/PersonFirstName.cs | 31 ---- .../Lib/Generators/PersonFullName.cs | 31 ---- .../Lib/Generators/PersonLastName.cs | 31 ---- .../Lib/Generators/PositiveNumber.cs | 28 ---- DapperAutoData/Lib/Generators/TestEmail.cs | 32 ---- .../Generators/Time/FutureDateTimeOffset.cs | 23 --- .../Lib/Generators/Time/PastDateTimeOffset.cs | 23 --- DapperAutoData/Lib/IDataGenerator.cs | 8 - README.md | 28 ++++ 25 files changed, 405 insertions(+), 345 deletions(-) create mode 100644 DapperAutoData/AutoMoqDataAttribute.cs delete mode 100644 DapperAutoData/ContentFiles/AutoMoqDataAttribute.cs delete mode 100644 DapperAutoData/ContentFiles/DataGeneratorInstaller.cs create mode 100644 DapperAutoData/DataGeneratorInstaller.cs create mode 100644 DapperAutoData/DefaultCustomizations.cs rename DapperAutoData/{Lib => }/Generators/BooleanGenerators.cs (83%) create mode 100644 DapperAutoData/Generators/DateTimeGenerator.cs create mode 100644 DapperAutoData/Generators/NumberGenerator.cs create mode 100644 DapperAutoData/Generators/StringGenerator.cs create mode 100644 DapperAutoData/IDapperProjectCustomization.cs create mode 100644 DapperAutoData/IDataGenerator.cs delete mode 100644 DapperAutoData/Lib/DefaultCustomizations.cs delete mode 100644 DapperAutoData/Lib/Generators/CompanyName.cs delete mode 100644 DapperAutoData/Lib/Generators/NegativeNumber.cs delete mode 100644 DapperAutoData/Lib/Generators/PersonFirstName.cs delete mode 100644 DapperAutoData/Lib/Generators/PersonFullName.cs delete mode 100644 DapperAutoData/Lib/Generators/PersonLastName.cs delete mode 100644 DapperAutoData/Lib/Generators/PositiveNumber.cs delete mode 100644 DapperAutoData/Lib/Generators/TestEmail.cs delete mode 100644 DapperAutoData/Lib/Generators/Time/FutureDateTimeOffset.cs delete mode 100644 DapperAutoData/Lib/Generators/Time/PastDateTimeOffset.cs delete mode 100644 DapperAutoData/Lib/IDataGenerator.cs diff --git a/DapperAutoData/AutoMoqDataAttribute.cs b/DapperAutoData/AutoMoqDataAttribute.cs new file mode 100644 index 0000000..62efaed --- /dev/null +++ b/DapperAutoData/AutoMoqDataAttribute.cs @@ -0,0 +1,49 @@ +using System.Reflection; +using AutoFixture; +using AutoFixture.AutoMoq; +using AutoFixture.Xunit2; + +namespace DapperAutoData.ContentFiles; + +/// +/// Attribute used to mark a test method for AutoFixture data generation. +/// +public class DapperAutoDataAttribute : InlineAutoDataAttribute +{ + public DapperAutoDataAttribute(params object[] values) + : base(new AutoFixtureMoqDataAttribute(), values) + { + } + + public class AutoFixtureMoqDataAttribute : AutoDataAttribute + { + public AutoFixtureMoqDataAttribute() + : base(() => + { + var fixture = new Fixture().Customize(new DefaultCustomizations()); + + var customizationsTypes = Assembly.GetExecutingAssembly() + .GetTypes() + .Where(t => typeof(IDapperProjectCustomization).IsAssignableFrom(t) && !t.IsInterface); + + foreach (var customizationsType in customizationsTypes) + { + var customizations = (IDapperProjectCustomization)Activator.CreateInstance(customizationsType); + fixture.Customize(customizations); + } + + return fixture; + }) + { + } + } + + + [AttributeUsage(AttributeTargets.Method)] +public class AutoDomainDataAttribute : AutoDataAttribute +{ + public AutoDomainDataAttribute() + : base(() => new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true })) + { + } +} \ No newline at end of file diff --git a/DapperAutoData/ContentFiles/AutoMoqDataAttribute.cs b/DapperAutoData/ContentFiles/AutoMoqDataAttribute.cs deleted file mode 100644 index 177a655..0000000 --- a/DapperAutoData/ContentFiles/AutoMoqDataAttribute.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using AutoFixture; -using AutoFixture.AutoMoq; -using AutoFixture.Xunit2; - -namespace DapperAutoData; - -public class DapperAutoDataAttribute : InlineAutoDataAttribute -{ - public DapperAutoDataAttribute(params object[] values) - : base(new AutoFixtureMoqDataAttribute(), values) - { - } - - public class AutoFixtureMoqDataAttribute : AutoDataAttribute - { - public AutoFixtureMoqDataAttribute() - : base(() => new Fixture() - .Customize(new DefaultCustomizations()) - .Customize(new ProjectCustomizations()) - ) - { - } - } - - internal class ProjectCustomizations : ICustomization - { - public void Customize(IFixture fixture) - { - fixture.RepeatCount = 3; - fixture.Customize(new AutoMoqCustomization() { ConfigureMembers = true }); - fixture.Behaviors.Remove(new ThrowingRecursionBehavior()); - fixture.Behaviors.Add(new OmitOnRecursionBehavior()); - DataGeneratorInstaller.Run(fixture); - } - } -} - -[AttributeUsage(AttributeTargets.Method)] -public class AutoDomainDataAttribute : AutoDataAttribute -{ - public AutoDomainDataAttribute() - : base(() => new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true })) - { - } -} \ No newline at end of file diff --git a/DapperAutoData/ContentFiles/DataGeneratorInstaller.cs b/DapperAutoData/ContentFiles/DataGeneratorInstaller.cs deleted file mode 100644 index 5cb879d..0000000 --- a/DapperAutoData/ContentFiles/DataGeneratorInstaller.cs +++ /dev/null @@ -1,16 +0,0 @@ -using AutoFixture; - -namespace DapperAutoData; - -public static class DataGeneratorInstaller -{ - public static void Run(IFixture fixture) - { - var list = System.Reflection.Assembly.GetCallingAssembly() - .GetTypes() - .Where(type => typeof(IDataGenerator).IsAssignableFrom(type) && !type.IsInterface).ToList(); - list.ForEach(type => - (Activator.CreateInstance(type) as IDataGenerator)?.RegisterGenerators(fixture)); - } - -} \ No newline at end of file diff --git a/DapperAutoData/DapperAutoData.csproj b/DapperAutoData/DapperAutoData.csproj index 1160605..9bf5777 100644 --- a/DapperAutoData/DapperAutoData.csproj +++ b/DapperAutoData/DapperAutoData.csproj @@ -23,23 +23,22 @@ - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + @@ -54,8 +53,8 @@ - - + + diff --git a/DapperAutoData/DapperAutoData.nuspec b/DapperAutoData/DapperAutoData.nuspec index 449dcf8..84eab74 100644 --- a/DapperAutoData/DapperAutoData.nuspec +++ b/DapperAutoData/DapperAutoData.nuspec @@ -17,22 +17,28 @@ https://opensource.org/licenses/MIT - + + + + - + + + + diff --git a/DapperAutoData/DataGeneratorInstaller.cs b/DapperAutoData/DataGeneratorInstaller.cs new file mode 100644 index 0000000..7ecbc02 --- /dev/null +++ b/DapperAutoData/DataGeneratorInstaller.cs @@ -0,0 +1,42 @@ +using AutoFixture; +using DapperAutoData.Generators; + +namespace DapperAutoData; + +/// +/// This class is responsible for registering all the data generators in the assembly. +/// It uses reflection to find all the classes that implement the IDataGenerator interface and registers them. +/// It also filters out the DateTimeGenerator and DateTimeOffsetGenerator classes as they are generic and should not be registered. +/// +public static class DataGeneratorInstaller +{ + public static void Run(IFixture fixture) + { + var list = System.Reflection.Assembly.GetCallingAssembly() + .GetTypes() + .Where(type => typeof(IDataGenerator).IsAssignableFrom(type) + && !type.IsInterface + && !type.IsAbstract + && !type.ContainsGenericParameters + && !IsSubclassOfRawGeneric(typeof(DateTimeGenerator<>), type) + && !IsSubclassOfRawGeneric(typeof(DateTimeOffsetGenerator<>), type)) + .ToList(); + list.ForEach(type => + (Activator.CreateInstance(type) as IDataGenerator)?.RegisterGenerators(fixture)); + } + + private static bool IsSubclassOfRawGeneric(Type generic, Type toCheck) + { + while (toCheck != null && toCheck != typeof(object)) + { + var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck; + if (generic == cur) + { + return true; + } + toCheck = toCheck.BaseType; + } + return false; + } + +} \ No newline at end of file diff --git a/DapperAutoData/DefaultCustomizations.cs b/DapperAutoData/DefaultCustomizations.cs new file mode 100644 index 0000000..0660c0d --- /dev/null +++ b/DapperAutoData/DefaultCustomizations.cs @@ -0,0 +1,17 @@ +using AutoFixture; +using AutoFixture.AutoMoq; + +namespace DapperAutoData; + +/// +/// Class that registers the default customizations for the AutoFixture instance. +/// Sets up AutoMoqCustomization and registers the data generators. +/// +public class DefaultCustomizations : ICustomization +{ + public void Customize(IFixture fixture) + { + fixture.Customize(new AutoMoqCustomization() { ConfigureMembers = true }); + DataGeneratorInstaller.Run(fixture); + } +} \ No newline at end of file diff --git a/DapperAutoData/Lib/Generators/BooleanGenerators.cs b/DapperAutoData/Generators/BooleanGenerators.cs similarity index 83% rename from DapperAutoData/Lib/Generators/BooleanGenerators.cs rename to DapperAutoData/Generators/BooleanGenerators.cs index e22d397..35693ab 100644 --- a/DapperAutoData/Lib/Generators/BooleanGenerators.cs +++ b/DapperAutoData/Generators/BooleanGenerators.cs @@ -1,6 +1,6 @@ using AutoFixture; -namespace DapperAutoData.Lib.Generators; +namespace DapperAutoData.Generators; public class BooleanGenerators : IDataGenerator { diff --git a/DapperAutoData/Generators/DateTimeGenerator.cs b/DapperAutoData/Generators/DateTimeGenerator.cs new file mode 100644 index 0000000..c5773cc --- /dev/null +++ b/DapperAutoData/Generators/DateTimeGenerator.cs @@ -0,0 +1,148 @@ +using AutoFixture; +using Bogus; + +namespace DapperAutoData.Generators; + +/// +/// Set of classes that generate DateTime and DateTimeOffset values. +/// +/// +public class DateTimeGenerator : IDataGenerator +{ + public DateTime Value { get; } + + private readonly Func _generator = null!; + + public DateTimeGenerator(DateTime value) + { + Value = value; + } + + public DateTimeGenerator(Func generator) + { + _generator = generator; + Value = generator(new Faker()); + } + + public static implicit operator DateTimeGenerator(DateTime y) => new(y); + public static implicit operator DateTime(DateTimeGenerator x) => x.Value; + + public static DateTimeGenerator Generate(IFixture fixture, Func generator) => new(generator); + + public override string? ToString() + { + return Value.ToString(); + } + + public void RegisterGenerators(IFixture fixture) => fixture.Register(() => Generate(fixture, _generator)); +} + +public class DateTimeOffsetGenerator : IDataGenerator +{ + public DateTimeOffset Value { get; } + + private readonly Func _generator; + + public DateTimeOffsetGenerator(DateTimeOffset value) + { + Value = value; + } + + public DateTimeOffsetGenerator(Func generator) + { + _generator = generator; + Value = generator(new Faker()); + } + + public static implicit operator DateTimeOffsetGenerator(DateTimeOffset y) => new(y); + public static implicit operator DateTimeOffset(DateTimeOffsetGenerator x) => x.Value; + + public static DateTimeOffsetGenerator Generate(IFixture fixture, Func generator) => new(generator); + + public override string? ToString() + { + return Value.ToString(); + } + + public void RegisterGenerators(IFixture fixture) => fixture.Register(() => Generate(fixture, _generator)); +} + +public class DateTimeFuture : DateTimeGenerator +{ + public DateTimeFuture() : base(faker => faker.Date.Future()) { } +} + +public class DateTimeOffsetFuture : DateTimeOffsetGenerator +{ + public DateTimeOffsetFuture() : base(faker => faker.Date.FutureOffset()) { } +} + +public class DateTimeOfBirth : DateTimeGenerator +{ + public DateTimeOfBirth() : base(faker => faker.Date.Between(DateTime.Now.AddYears(-80), DateTime.Now.AddYears(-18))) { } +} + +public class DateTimeOffsetOfBirth : DateTimeOffsetGenerator +{ + public DateTimeOffsetOfBirth() : base(faker => faker.Date.BetweenOffset(DateTime.Now.AddYears(-80), DateTime.Now.AddYears(-18))) { } +} + +public class DateTimePast : DateTimeGenerator +{ + public DateTimePast() : base(faker => faker.Date.Past()) { } +} + +public class DateTimeOffsetPast : DateTimeOffsetGenerator +{ + public DateTimeOffsetPast() : base(faker => faker.Date.PastOffset()) { } +} + +public class CurrentDate : DateTimeGenerator +{ + public CurrentDate() : base(DateTime.Now) { } +} + +public class DateSpecialEvent : DateTimeGenerator +{ + public DateSpecialEvent() : base(new DateTime(2023, 12, 25)) { } // Example: Christmas +} + +public class DateWeekday : DateTimeGenerator +{ + public DateWeekday() : base(GenerateWeekdayDate) { } + + private static DateTime GenerateWeekdayDate(Faker faker) + { + DateTime date; + do + { + date = faker.Date.Future(); + } while (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday); + return date; + } +} + +public class DateWeekend : DateTimeGenerator +{ + public DateWeekend() : base(GenerateWeekendDate) { } + + private static DateTime GenerateWeekendDate(Faker faker) + { + DateTime date; + do + { + date = faker.Date.Future(); + } while (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday); + return date; + } +} + +public class DateLeapYear : DateTimeGenerator +{ + public DateLeapYear() : base(new DateTime(2024, 2, 29)) { } // Example: Leap year date +} + +public class DateCustomFormat : DateTimeGenerator +{ + public DateCustomFormat() : base(faker => DateTime.Parse(faker.Date.Recent(7).ToShortDateString())) { } +} \ No newline at end of file diff --git a/DapperAutoData/Generators/NumberGenerator.cs b/DapperAutoData/Generators/NumberGenerator.cs new file mode 100644 index 0000000..173ad5d --- /dev/null +++ b/DapperAutoData/Generators/NumberGenerator.cs @@ -0,0 +1,32 @@ +using AutoFixture; +using Bogus; + +namespace DapperAutoData.Generators; + +/// +/// Set of classes that generate numbers. +/// +/// +public class NumberGenerator : IDataGenerator +{ + public decimal Value { get; } + private readonly Func _generator; + + public NumberGenerator(decimal value) => (Value, _generator) = (value, _ => value); + public NumberGenerator(Func generator) => (Value, _generator) = (generator(new Faker()), generator); + + public static implicit operator NumberGenerator(decimal y) => new(y); + public static implicit operator decimal(NumberGenerator x) => x.Value; + public static implicit operator int(NumberGenerator x) => (int)x.Value; + public static implicit operator float(NumberGenerator x) => (float)x.Value; + + public static NumberGenerator Generate(IFixture fixture, Func generator) => new(generator); + + public override string? ToString() => Value.ToString(); + public void RegisterGenerators(IFixture fixture) => fixture.Register(() => Generate(fixture, _generator)); +} + +public class NumberPositive : NumberGenerator { public NumberPositive() : base(faker => faker.Random.Decimal(1, decimal.MaxValue)) { } } +public class NumberNegative : NumberGenerator { public NumberNegative() : base(faker => faker.Random.Decimal(decimal.MinValue, -1)) { } } +public class NumberMoney : NumberGenerator { public NumberMoney() : base(GenerateFakeMoney) { } private static decimal GenerateFakeMoney(Faker faker) => faker.Random.Decimal(1, 1000) + faker.Random.Decimal(0.01m, 0.99m); } +public class NumberFraction : NumberGenerator { public NumberFraction() : base(faker => Math.Round(faker.Random.Decimal(0.01m, 0.99m), 2)) { } } \ No newline at end of file diff --git a/DapperAutoData/Generators/StringGenerator.cs b/DapperAutoData/Generators/StringGenerator.cs new file mode 100644 index 0000000..ebaddcd --- /dev/null +++ b/DapperAutoData/Generators/StringGenerator.cs @@ -0,0 +1,50 @@ +using AutoFixture; +using Bogus; + +namespace DapperAutoData.Generators; + +/// +/// Set of data generators for various string types. These generators are based on the Bogus library. +/// +/// +public class StringGenerator : IDataGenerator +{ + private readonly Func _generator; + public string Value { get; } + + public StringGenerator(string value = "") => (Value, _generator) = (value, faker => value); + public StringGenerator(Func generator) => (Value, _generator) = (generator(new Faker()), generator); + + public static implicit operator string(StringGenerator x) => x.Value; + public static implicit operator StringGenerator(string y) => new(y); + + public void RegisterGenerators(IFixture fixture) => fixture.Register(() => new StringGenerator(_generator)); +} + +public class StringParagraph : StringGenerator { public StringParagraph() : base(faker => faker.Lorem.Paragraph()) { } } +public class StringPersonFullName : StringGenerator { public StringPersonFullName() : base(faker => faker.Name.FullName()) { } } +public class StringPhoneNumber : StringGenerator { public StringPhoneNumber() : base(faker => faker.Phone.PhoneNumber()) { } } +public class StringSentence : StringGenerator { public StringSentence() : base(faker => faker.Lorem.Sentence()) { } } +public class StringSsn : StringGenerator { public StringSsn() : base(faker => $"{faker.Random.Number(100, 999)}-{faker.Random.Number(10, 99)}-{faker.Random.Number(1000, 9999)}") { } } +public class StringWord : StringGenerator { public StringWord() : base(faker => faker.Lorem.Word()) { } } +public class StringCompanyName : StringGenerator { public StringCompanyName() : base(faker => faker.Company.CompanyName()) { } } +public class StringEmailTest : StringGenerator { public StringEmailTest() : base(faker => $"{faker.Internet.DomainName()}@FakeEmailAddress.com") { } } +public class StringFirstName : StringGenerator { public StringFirstName() : base(faker => faker.Name.FirstName()) { } } +public class StringInternetUrl : StringGenerator { public StringInternetUrl() : base(faker => faker.Internet.Url()) { } } +public class StringInternetUsername : StringGenerator { public StringInternetUsername() : base(faker => faker.Internet.UserName()) { } } + +public class StringCityName : StringGenerator { public StringCityName() : base(faker => faker.Address.City()) { } } +public class StringCountryName : StringGenerator { public StringCountryName() : base(faker => faker.Address.Country()) { } } +public class StringPostalCode : StringGenerator { public StringPostalCode() : base(faker => faker.Address.ZipCode()) { } } +public class StringStateAbbreviation : StringGenerator { public StringStateAbbreviation() : base(faker => faker.Address.StateAbbr()) { } } +public class StringStateFullName : StringGenerator { public StringStateFullName() : base(faker => faker.Address.State()) { } } +public class StringStreetAddress : StringGenerator { public StringStreetAddress() : base(faker => faker.Address.StreetAddress()) { } } +public class StringJobTitle : StringGenerator { public StringJobTitle() : base(faker => faker.Name.JobTitle()) { } } +public class StringProductCategory : StringGenerator { public StringProductCategory() : base(faker => faker.Commerce.Categories(1)[0]) { } } +public class StringProductDescription : StringGenerator { public StringProductDescription() : base(faker => faker.Commerce.ProductDescription()) { } } +public class StringCurrency : StringGenerator { public StringCurrency() : base(faker => faker.Finance.Currency().Code) { } } // Extracting the currency code as a string +public class StringFileExtension : StringGenerator { public StringFileExtension() : base(faker => faker.System.CommonFileExt()) { } } +public class StringIPAddress : StringGenerator { public StringIPAddress() : base(faker => faker.Internet.Ip()) { } } +public class StringHtmlTag : StringGenerator { public StringHtmlTag() : base(faker => faker.Random.AlphaNumeric(5)) { } } +public class StringPassword : StringGenerator { public StringPassword() : base(faker => faker.Internet.Password()) { } } +public class StringGuid : StringGenerator { public StringGuid() : base(faker => faker.Random.Guid().ToString()) { } } \ No newline at end of file diff --git a/DapperAutoData/IDapperProjectCustomization.cs b/DapperAutoData/IDapperProjectCustomization.cs new file mode 100644 index 0000000..867ba6e --- /dev/null +++ b/DapperAutoData/IDapperProjectCustomization.cs @@ -0,0 +1,11 @@ +using AutoFixture; + +namespace DapperAutoData; + +/// +/// Marker interface for project customizations. Any class implementing this interface will be registered as a project customization.# +/// +public interface IDapperProjectCustomization : ICustomization +{ + +} \ No newline at end of file diff --git a/DapperAutoData/IDataGenerator.cs b/DapperAutoData/IDataGenerator.cs new file mode 100644 index 0000000..4375235 --- /dev/null +++ b/DapperAutoData/IDataGenerator.cs @@ -0,0 +1,11 @@ +using AutoFixture; + +namespace DapperAutoData; + +/// +/// Marker interface for data generators. Any class implementing this interface will be registered as a data generator. +/// +public interface IDataGenerator +{ + public void RegisterGenerators(IFixture fixture); +} \ No newline at end of file diff --git a/DapperAutoData/Lib/DefaultCustomizations.cs b/DapperAutoData/Lib/DefaultCustomizations.cs deleted file mode 100644 index fabea86..0000000 --- a/DapperAutoData/Lib/DefaultCustomizations.cs +++ /dev/null @@ -1,11 +0,0 @@ -using AutoFixture; - -namespace DapperAutoData; - -public class DefaultCustomizations : ICustomization -{ - public void Customize(IFixture fixture) - { - DataGeneratorInstaller.Run(fixture); - } -} \ No newline at end of file diff --git a/DapperAutoData/Lib/Generators/CompanyName.cs b/DapperAutoData/Lib/Generators/CompanyName.cs deleted file mode 100644 index 76dfe82..0000000 --- a/DapperAutoData/Lib/Generators/CompanyName.cs +++ /dev/null @@ -1,23 +0,0 @@ -using AutoFixture; - -namespace DapperAutoData.Lib.Generators; - -public class CompanyName : IDataGenerator -{ - public static string Generate() => Faker.Company.Name(); - - public CompanyName(string value) - { - Value = value; - } - - public CompanyName() - { - Value = ""; - } - public string Value; - public static implicit operator string(CompanyName x) => x.Value; - public static implicit operator CompanyName(string y) => new CompanyName(y); - public void RegisterGenerators(IFixture fixture) => fixture.Register(() => Generate()); - -} \ No newline at end of file diff --git a/DapperAutoData/Lib/Generators/NegativeNumber.cs b/DapperAutoData/Lib/Generators/NegativeNumber.cs deleted file mode 100644 index 2caf0b7..0000000 --- a/DapperAutoData/Lib/Generators/NegativeNumber.cs +++ /dev/null @@ -1,30 +0,0 @@ -using AutoFixture; - -namespace DapperAutoData.Lib.Generators; - -public class NegativeNumber : IDataGenerator -{ - public int Value; - - public NegativeNumber(int value) - { - Value = value; - } - - public NegativeNumber() - { - } - - public static implicit operator NegativeNumber(int y) => new NegativeNumber(y); - public static implicit operator int(NegativeNumber x) => x.Value; - public static implicit operator double(NegativeNumber x) => x.Value; - - public static NegativeNumber Generate(IFixture fixture) => fixture.Create() * -1; - - public override string? ToString() - { - return Value.ToString(); - } - public void RegisterGenerators(IFixture fixture) => fixture.Register(() => Generate(fixture)); - -} \ No newline at end of file diff --git a/DapperAutoData/Lib/Generators/PersonFirstName.cs b/DapperAutoData/Lib/Generators/PersonFirstName.cs deleted file mode 100644 index e47b484..0000000 --- a/DapperAutoData/Lib/Generators/PersonFirstName.cs +++ /dev/null @@ -1,31 +0,0 @@ -using AutoFixture; - -namespace DapperAutoData.Lib.Generators; - -public class PersonFirstName : IDataGenerator -{ - public PersonFirstName() - { - Value = "test"; - } - - public PersonFirstName(string value) - { - Value = value; - } - - public string Value; - public static string Generate(IFixture fixture) => $"{Faker.Name.First()}"; - public static implicit operator string(PersonFirstName x) => x.Value; - public static implicit operator PersonFirstName(string y) => new(y); - - public override string ToString() - { - return Value; - } - - public void RegisterGenerators(IFixture fixture) - { - fixture.Register(() => Generate(fixture)); - } -} \ No newline at end of file diff --git a/DapperAutoData/Lib/Generators/PersonFullName.cs b/DapperAutoData/Lib/Generators/PersonFullName.cs deleted file mode 100644 index d5d07aa..0000000 --- a/DapperAutoData/Lib/Generators/PersonFullName.cs +++ /dev/null @@ -1,31 +0,0 @@ -using AutoFixture; - -namespace DapperAutoData.Lib.Generators; - -public class PersonFullName : IDataGenerator -{ - public PersonFullName() - { - Value = "test"; - } - - public PersonFullName(string value) - { - Value = value; - } - - public string Value; - public static string Generate(IFixture fixture) => $"{Faker.Name.FullName()}"; - public static implicit operator string(PersonFullName x) => x.Value; - public static implicit operator PersonFullName(string y) => new(y); - - public override string ToString() - { - return Value; - } - - public void RegisterGenerators(IFixture fixture) - { - fixture.Register(() => Generate(fixture)); - } -} \ No newline at end of file diff --git a/DapperAutoData/Lib/Generators/PersonLastName.cs b/DapperAutoData/Lib/Generators/PersonLastName.cs deleted file mode 100644 index 733aa0f..0000000 --- a/DapperAutoData/Lib/Generators/PersonLastName.cs +++ /dev/null @@ -1,31 +0,0 @@ -using AutoFixture; - -namespace DapperAutoData.Lib.Generators; - -public class PersonLastName : IDataGenerator -{ - public PersonLastName() - { - Value = "test"; - } - - public PersonLastName(string value) - { - Value = value; - } - - public string Value; - public static string Generate(IFixture fixture) => $"{Faker.Name.Last()}"; - public static implicit operator string(PersonLastName x) => x.Value; - public static implicit operator PersonLastName(string y) => new(y); - - public override string ToString() - { - return Value; - } - - public void RegisterGenerators(IFixture fixture) - { - fixture.Register(() => Generate(fixture)); - } -} \ No newline at end of file diff --git a/DapperAutoData/Lib/Generators/PositiveNumber.cs b/DapperAutoData/Lib/Generators/PositiveNumber.cs deleted file mode 100644 index 4cdc81b..0000000 --- a/DapperAutoData/Lib/Generators/PositiveNumber.cs +++ /dev/null @@ -1,28 +0,0 @@ -using AutoFixture; - -namespace DapperAutoData.Lib.Generators; - -public class PositiveNumber : IDataGenerator -{ - public int Value; - - public PositiveNumber(int value) - { - Value = value; - } - - public PositiveNumber() - { - } - - public static implicit operator PositiveNumber(int y) => new PositiveNumber(y); - public static implicit operator int(PositiveNumber x) => x.Value; - public static implicit operator double(PositiveNumber x) => x.Value; - public static PositiveNumber Generate(IFixture fixture) => fixture.Create>().Where(x => x > 0).Distinct().First(); - public override string? ToString() - { - return Value.ToString(); - } - public void RegisterGenerators(IFixture fixture) => fixture.Register(() => Generate(fixture)); - -} \ No newline at end of file diff --git a/DapperAutoData/Lib/Generators/TestEmail.cs b/DapperAutoData/Lib/Generators/TestEmail.cs deleted file mode 100644 index a4a9cef..0000000 --- a/DapperAutoData/Lib/Generators/TestEmail.cs +++ /dev/null @@ -1,32 +0,0 @@ -using AutoFixture; -using Faker; - -namespace DapperAutoData.Lib.Generators; - -public class TestEmail : IDataGenerator -{ - public TestEmail() - { - Value = "test"; - } - - public TestEmail(string value) - { - Value = value; - } - - public string Value; - public static string Generate(IFixture fixture) => $"{Internet.DomainName()}@FakeEmailAddress.com"; - public static implicit operator string(TestEmail x) => x.Value; - public static implicit operator TestEmail(string y) => new(y); - - public override string ToString() - { - return Value; - } - - public void RegisterGenerators(IFixture fixture) - { - fixture.Register(() => Generate(fixture)); - } -} \ No newline at end of file diff --git a/DapperAutoData/Lib/Generators/Time/FutureDateTimeOffset.cs b/DapperAutoData/Lib/Generators/Time/FutureDateTimeOffset.cs deleted file mode 100644 index 6b080a3..0000000 --- a/DapperAutoData/Lib/Generators/Time/FutureDateTimeOffset.cs +++ /dev/null @@ -1,23 +0,0 @@ -using AutoFixture; - -namespace DapperAutoData.Lib.Generators.Time; - -public class FutureDateTimeOffset : IDataGenerator -{ - public FutureDateTimeOffset() - { - } - - public FutureDateTimeOffset(DateTimeOffset value) - { - Value = value; - } - - public DateTimeOffset Value; - public static DateTimeOffset Generate(IFixture fixture) => - fixture.Create>().Where(x => x > DateTimeOffset.Now).Distinct().First().AddDays(1); - public static implicit operator DateTimeOffset(FutureDateTimeOffset x) => x.Value; - public static implicit operator FutureDateTimeOffset(DateTimeOffset y) => new FutureDateTimeOffset(y); - public void RegisterGenerators(IFixture fixture) => fixture.Register(() => Generate(fixture)); - -} \ No newline at end of file diff --git a/DapperAutoData/Lib/Generators/Time/PastDateTimeOffset.cs b/DapperAutoData/Lib/Generators/Time/PastDateTimeOffset.cs deleted file mode 100644 index d8d4f1a..0000000 --- a/DapperAutoData/Lib/Generators/Time/PastDateTimeOffset.cs +++ /dev/null @@ -1,23 +0,0 @@ -using AutoFixture; - -namespace DapperAutoData.Lib.Generators.Time; - -public class PastDateTimeOffset : IDataGenerator -{ - public PastDateTimeOffset() - { - } - - public PastDateTimeOffset(DateTimeOffset value) - { - Value = value; - } - - public DateTimeOffset Value; - public static DateTimeOffset Generate(IFixture fixture) => - fixture.Create>().Where(x => x < DateTimeOffset.Now).Distinct().First(); - public static implicit operator DateTimeOffset(PastDateTimeOffset x) => x.Value; - public static implicit operator PastDateTimeOffset(DateTimeOffset y) => new PastDateTimeOffset(y); - public void RegisterGenerators(IFixture fixture) => fixture.Register(() => Generate(fixture)); - -} \ No newline at end of file diff --git a/DapperAutoData/Lib/IDataGenerator.cs b/DapperAutoData/Lib/IDataGenerator.cs deleted file mode 100644 index bff59a3..0000000 --- a/DapperAutoData/Lib/IDataGenerator.cs +++ /dev/null @@ -1,8 +0,0 @@ -using AutoFixture; - -namespace DapperAutoData; - -public interface IDataGenerator -{ - public void RegisterGenerators(IFixture fixture); -} \ No newline at end of file diff --git a/README.md b/README.md index e5f6f9a..8ea0458 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,34 @@ The DataGeneratorInstaller will attempt to install any data generators in the a ## How to Use +``` +public class DapperDataCustomizations : IDapperProjectCustomization +{ + public void Customize(IFixture fixture) + { + fixture.RepeatCount = 3; + fixture.Behaviors.Remove(new ThrowingRecursionBehavior()); + fixture.Behaviors.Add(new OmitOnRecursionBehavior()); + } +} +``` + +## Customizing Data Generation + +In some cases, you may want to customize how data is generated for your tests. You can do this by creating a class that implements the `IDapperProjectCustomization` interface and customizing the `IFixture` instance in the `Customize` method. + +Here's an example of a class that customizes data generation: + +In this example, `DapperDataCustomizations` customizes the `IFixture` instance in several ways: + +- It sets `RepeatCount` to 3, which means that collections generated by AutoFixture will contain 3 items. +- It configures AutoMoq to set up all members on a mock to return a value. +- It removes the `ThrowingRecursionBehavior` and adds the `OmitOnRecursionBehavior`, which changes how AutoFixture handles recursive structures. +- It runs the `DataGeneratorInstaller`, which installs any data generators in the assembly. + +You can add this class to your test project and it will automatically be used by the `DapperAutoDataAttribute` to customize data generation for your tests. + + ### Creating a Data Generator `public class AssistantGenerator : IDataGenerator From 45ae36bc0cf67ab9adf5fab95dcbf917d24a6547 Mon Sep 17 00:00:00 2001 From: Blake Stephens Date: Thu, 25 Apr 2024 11:31:55 -0400 Subject: [PATCH 2/2] Fixed missing brace. --- DapperAutoData/AutoMoqDataAttribute.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DapperAutoData/AutoMoqDataAttribute.cs b/DapperAutoData/AutoMoqDataAttribute.cs index 62efaed..77b7426 100644 --- a/DapperAutoData/AutoMoqDataAttribute.cs +++ b/DapperAutoData/AutoMoqDataAttribute.cs @@ -38,8 +38,9 @@ public AutoFixtureMoqDataAttribute() } } +} - [AttributeUsage(AttributeTargets.Method)] +[AttributeUsage(AttributeTargets.Method)] public class AutoDomainDataAttribute : AutoDataAttribute { public AutoDomainDataAttribute()