Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
riplikash committed Apr 26, 2024
1 parent 42a5b90 commit b48b3fe
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 131 deletions.
47 changes: 16 additions & 31 deletions DapperAutoData/AutoMoqDataAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,33 @@
using AutoFixture.AutoMoq;
using AutoFixture.Xunit2;

namespace DapperAutoData.ContentFiles;
namespace DapperAutoData;

/// <summary>
/// Attribute used to mark a test method for AutoFixture data generation.
/// </summary>
public class DapperAutoDataAttribute : InlineAutoDataAttribute
public class DapperAutoDataAttribute(params object[] values)
: InlineAutoDataAttribute(new AutoFixtureMoqDataAttribute(), values)
{
public DapperAutoDataAttribute(params object[] values)
: base(new AutoFixtureMoqDataAttribute(), values)
public class AutoFixtureMoqDataAttribute() : AutoDataAttribute(() =>
{
}
var fixture = new Fixture().Customize(new DefaultCustomizations());

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);
}
var customizationsTypes = Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => typeof(IDapperProjectCustomization).IsAssignableFrom(t) && !t.IsInterface);

return fixture;
})
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 }))
{
}
}
public class AutoDomainDataAttribute()
: AutoDataAttribute(() => new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true }));
9 changes: 2 additions & 7 deletions DapperAutoData/DapperAutoData.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Library</OutputType>
Expand All @@ -14,7 +14,7 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>xunit;autofixture;c#;.net;unittesting</PackageTags>
<PackageId>DapperAutoData</PackageId>
<Version>1.0.7</Version>
<Version>1.0.8</Version>
</PropertyGroup>

<ItemGroup>
Expand All @@ -36,12 +36,7 @@
<PackageReference Include="AutoFixture.AutoMoq" Version="4.18.1" />
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.1" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 1 addition & 3 deletions DapperAutoData/DataGeneratorInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ 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
&& type is { IsInterface: false, IsAbstract: false, ContainsGenericParameters: false }
&& !IsSubclassOfRawGeneric(typeof(DateTimeGenerator<>), type)
&& !IsSubclassOfRawGeneric(typeof(DateTimeOffsetGenerator<>), type))
.ToList();
Expand Down
83 changes: 23 additions & 60 deletions DapperAutoData/Generators/DateTimeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,15 @@ namespace DapperAutoData.Generators;
/// Set of classes that generate DateTime and DateTimeOffset values.
/// </summary>
/// <typeparam name="T"></typeparam>
public class DateTimeGenerator<T> : IDataGenerator
public class DateTimeGenerator<T>(DateTime value) : IDataGenerator
{
public DateTime Value { get; }
public DateTime Value { get; } = value;

private readonly Func<Faker, DateTime> _generator = null!;

public DateTimeGenerator(DateTime value)
{
Value = value;
}

public DateTimeGenerator(Func<Faker, DateTime> generator)
public DateTimeGenerator(Func<Faker, DateTime> generator) : this(generator(new Faker()))
{
_generator = generator;
Value = generator(new Faker());
}

public static implicit operator DateTimeGenerator<T>(DateTime y) => new(y);
Expand All @@ -37,21 +31,15 @@ public DateTimeGenerator(Func<Faker, DateTime> generator)
public void RegisterGenerators(IFixture fixture) => fixture.Register(() => Generate(fixture, _generator));
}

public class DateTimeOffsetGenerator<T> : IDataGenerator
public class DateTimeOffsetGenerator<T>(DateTimeOffset value) : IDataGenerator
{
public DateTimeOffset Value { get; }
public DateTimeOffset Value { get; } = value;

private readonly Func<Faker, DateTimeOffset> _generator;

public DateTimeOffsetGenerator(DateTimeOffset value)
{
Value = value;
}

public DateTimeOffsetGenerator(Func<Faker, DateTimeOffset> generator)
public DateTimeOffsetGenerator(Func<Faker, DateTimeOffset> generator) : this(generator(new Faker()))
{
_generator = generator;
Value = generator(new Faker());
}

public static implicit operator DateTimeOffsetGenerator<T>(DateTimeOffset y) => new(y);
Expand All @@ -67,50 +55,29 @@ public DateTimeOffsetGenerator(Func<Faker, DateTimeOffset> generator)
public void RegisterGenerators(IFixture fixture) => fixture.Register(() => Generate(fixture, _generator));
}

public class DateTimeFuture : DateTimeGenerator<DateTimeFuture>
{
public DateTimeFuture() : base(faker => faker.Date.Future()) { }
}
public class DateTimeFuture() : DateTimeGenerator<DateTimeFuture>(faker => faker.Date.Future());

public class DateTimeOffsetFuture : DateTimeOffsetGenerator<DateTimeOffsetFuture>
{
public DateTimeOffsetFuture() : base(faker => faker.Date.FutureOffset()) { }
}
public class DateTimeOffsetFuture() : DateTimeOffsetGenerator<DateTimeOffsetFuture>(faker => faker.Date.FutureOffset());

public class DateTimeOfBirth : DateTimeGenerator<DateTimeOfBirth>
{
public DateTimeOfBirth() : base(faker => faker.Date.Between(DateTime.Now.AddYears(-80), DateTime.Now.AddYears(-18))) { }
}
public class DateTimeOfBirth() : DateTimeGenerator<DateTimeOfBirth>(faker =>
faker.Date.Between(DateTime.Now.AddYears(-80), DateTime.Now.AddYears(-18)));

public class DateTimeOffsetOfBirth : DateTimeOffsetGenerator<DateTimeOffsetOfBirth>
{
public DateTimeOffsetOfBirth() : base(faker => faker.Date.BetweenOffset(DateTime.Now.AddYears(-80), DateTime.Now.AddYears(-18))) { }
}
public class DateTimeOffsetOfBirth() : DateTimeOffsetGenerator<DateTimeOffsetOfBirth>(faker =>
faker.Date.BetweenOffset(DateTime.Now.AddYears(-80), DateTime.Now.AddYears(-18)));

public class DateTimePast : DateTimeGenerator<DateTimePast>
{
public DateTimePast() : base(faker => faker.Date.Past()) { }
}
public class DateTimePast() : DateTimeGenerator<DateTimePast>(faker => faker.Date.Past());

public class DateTimeOffsetPast : DateTimeOffsetGenerator<DateTimeOffsetPast>
{
public DateTimeOffsetPast() : base(faker => faker.Date.PastOffset()) { }
}
public class DateTimeOffsetPast() : DateTimeOffsetGenerator<DateTimeOffsetPast>(faker => faker.Date.PastOffset());

public class CurrentDate : DateTimeGenerator<CurrentDate>
{
public CurrentDate() : base(DateTime.Now) { }
}
public class CurrentDate() : DateTimeGenerator<CurrentDate>(DateTime.Now);

public class DateSpecialEvent : DateTimeGenerator<DateSpecialEvent>
public class DateSpecialEvent() : DateTimeGenerator<DateSpecialEvent>(new DateTime(2023, 12, 25))
{
public DateSpecialEvent() : base(new DateTime(2023, 12, 25)) { } // Example: Christmas
// Example: Christmas
}

public class DateWeekday : DateTimeGenerator<DateWeekday>
public class DateWeekday() : DateTimeGenerator<DateWeekday>(GenerateWeekdayDate)
{
public DateWeekday() : base(GenerateWeekdayDate) { }

private static DateTime GenerateWeekdayDate(Faker faker)
{
DateTime date;
Expand All @@ -122,10 +89,8 @@ private static DateTime GenerateWeekdayDate(Faker faker)
}
}

public class DateWeekend : DateTimeGenerator<DateWeekend>
public class DateWeekend() : DateTimeGenerator<DateWeekend>(GenerateWeekendDate)
{
public DateWeekend() : base(GenerateWeekendDate) { }

private static DateTime GenerateWeekendDate(Faker faker)
{
DateTime date;
Expand All @@ -137,12 +102,10 @@ private static DateTime GenerateWeekendDate(Faker faker)
}
}

public class DateLeapYear : DateTimeGenerator<DateLeapYear>
public class DateLeapYear() : DateTimeGenerator<DateLeapYear>(new DateTime(2024, 2, 29))
{
public DateLeapYear() : base(new DateTime(2024, 2, 29)) { } // Example: Leap year date
// Example: Leap year date
}

public class DateCustomFormat : DateTimeGenerator<DateCustomFormat>
{
public DateCustomFormat() : base(faker => DateTime.Parse(faker.Date.Recent(7).ToShortDateString())) { }
}
public class DateCustomFormat()
: DateTimeGenerator<DateCustomFormat>(faker => DateTime.Parse(faker.Date.Recent(7).ToShortDateString()));
10 changes: 6 additions & 4 deletions DapperAutoData/Generators/NumberGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public class NumberGenerator<T> : IDataGenerator
public void RegisterGenerators(IFixture fixture) => fixture.Register(() => Generate(fixture, _generator));
}

public class NumberPositive : NumberGenerator<NumberPositive> { public NumberPositive() : base(faker => faker.Random.Decimal(1, decimal.MaxValue)) { } }
public class NumberNegative : NumberGenerator<NumberNegative> { public NumberNegative() : base(faker => faker.Random.Decimal(decimal.MinValue, -1)) { } }
public class NumberMoney : NumberGenerator<NumberMoney> { 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<NumberFraction> { public NumberFraction() : base(faker => Math.Round(faker.Random.Decimal(0.01m, 0.99m), 2)) { } }
public class NumberPositive() : NumberGenerator<NumberPositive>(faker => faker.Random.Decimal(1, decimal.MaxValue));
public class NumberNegative() : NumberGenerator<NumberNegative>(faker => faker.Random.Decimal(decimal.MinValue, -1));
public class NumberMoney() : NumberGenerator<NumberMoney>(GenerateFakeMoney)
{ private static decimal GenerateFakeMoney(Faker faker) => faker.Random.Decimal(1, 1000) + faker.Random.Decimal(0.01m, 0.99m); }
public class NumberFraction()
: NumberGenerator<NumberFraction>(faker => Math.Round(faker.Random.Decimal(0.01m, 0.99m), 2));
52 changes: 26 additions & 26 deletions DapperAutoData/Generators/StringGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,30 @@ public class StringGenerator<T> : IDataGenerator
public void RegisterGenerators(IFixture fixture) => fixture.Register(() => new StringGenerator<T>(_generator));
}

public class StringParagraph : StringGenerator<StringParagraph> { public StringParagraph() : base(faker => faker.Lorem.Paragraph()) { } }
public class StringPersonFullName : StringGenerator<StringPersonFullName> { public StringPersonFullName() : base(faker => faker.Name.FullName()) { } }
public class StringPhoneNumber : StringGenerator<StringPhoneNumber> { public StringPhoneNumber() : base(faker => faker.Phone.PhoneNumber()) { } }
public class StringSentence : StringGenerator<StringSentence> { public StringSentence() : base(faker => faker.Lorem.Sentence()) { } }
public class StringSsn : StringGenerator<StringSsn> { public StringSsn() : base(faker => $"{faker.Random.Number(100, 999)}-{faker.Random.Number(10, 99)}-{faker.Random.Number(1000, 9999)}") { } }
public class StringWord : StringGenerator<StringWord> { public StringWord() : base(faker => faker.Lorem.Word()) { } }
public class StringCompanyName : StringGenerator<StringCompanyName> { public StringCompanyName() : base(faker => faker.Company.CompanyName()) { } }
public class StringEmailTest : StringGenerator<StringEmailTest> { public StringEmailTest() : base(faker => $"{faker.Internet.DomainName()}@FakeEmailAddress.com") { } }
public class StringFirstName : StringGenerator<StringFirstName> { public StringFirstName() : base(faker => faker.Name.FirstName()) { } }
public class StringInternetUrl : StringGenerator<StringInternetUrl> { public StringInternetUrl() : base(faker => faker.Internet.Url()) { } }
public class StringInternetUsername : StringGenerator<StringInternetUsername> { public StringInternetUsername() : base(faker => faker.Internet.UserName()) { } }
public class StringParagraph() : StringGenerator<StringParagraph>(faker => faker.Lorem.Paragraph());
public class StringPersonFullName() : StringGenerator<StringPersonFullName>(faker => faker.Name.FullName());
public class StringPhoneNumber() : StringGenerator<StringPhoneNumber>(faker => faker.Phone.PhoneNumber());
public class StringSentence() : StringGenerator<StringSentence>(faker => faker.Lorem.Sentence());
public class StringSsn() : StringGenerator<StringSsn>(faker => $"{faker.Random.Number(100, 999)}-{faker.Random.Number(10, 99)}-{faker.Random.Number(1000, 9999)}");
public class StringWord() : StringGenerator<StringWord>(faker => faker.Lorem.Word());
public class StringCompanyName() : StringGenerator<StringCompanyName>(faker => faker.Company.CompanyName());
public class StringEmailTest() : StringGenerator<StringEmailTest>(faker => $"{faker.Internet.DomainName()}@FakeEmailAddress.com");
public class StringFirstName() : StringGenerator<StringFirstName>(faker => faker.Name.FirstName());
public class StringInternetUrl() : StringGenerator<StringInternetUrl>(faker => faker.Internet.Url());
public class StringInternetUsername() : StringGenerator<StringInternetUsername>(faker => faker.Internet.UserName());

public class StringCityName : StringGenerator<StringCityName> { public StringCityName() : base(faker => faker.Address.City()) { } }
public class StringCountryName : StringGenerator<StringCountryName> { public StringCountryName() : base(faker => faker.Address.Country()) { } }
public class StringPostalCode : StringGenerator<StringPostalCode> { public StringPostalCode() : base(faker => faker.Address.ZipCode()) { } }
public class StringStateAbbreviation : StringGenerator<StringStateAbbreviation> { public StringStateAbbreviation() : base(faker => faker.Address.StateAbbr()) { } }
public class StringStateFullName : StringGenerator<StringStateFullName> { public StringStateFullName() : base(faker => faker.Address.State()) { } }
public class StringStreetAddress : StringGenerator<StringStreetAddress> { public StringStreetAddress() : base(faker => faker.Address.StreetAddress()) { } }
public class StringJobTitle : StringGenerator<StringJobTitle> { public StringJobTitle() : base(faker => faker.Name.JobTitle()) { } }
public class StringProductCategory : StringGenerator<StringProductCategory> { public StringProductCategory() : base(faker => faker.Commerce.Categories(1)[0]) { } }
public class StringProductDescription : StringGenerator<StringProductDescription> { public StringProductDescription() : base(faker => faker.Commerce.ProductDescription()) { } }
public class StringCurrency : StringGenerator<StringCurrency> { public StringCurrency() : base(faker => faker.Finance.Currency().Code) { } } // Extracting the currency code as a string
public class StringFileExtension : StringGenerator<StringFileExtension> { public StringFileExtension() : base(faker => faker.System.CommonFileExt()) { } }
public class StringIPAddress : StringGenerator<StringIPAddress> { public StringIPAddress() : base(faker => faker.Internet.Ip()) { } }
public class StringHtmlTag : StringGenerator<StringHtmlTag> { public StringHtmlTag() : base(faker => faker.Random.AlphaNumeric(5)) { } }
public class StringPassword : StringGenerator<StringPassword> { public StringPassword() : base(faker => faker.Internet.Password()) { } }
public class StringGuid : StringGenerator<StringGuid> { public StringGuid() : base(faker => faker.Random.Guid().ToString()) { } }
public class StringCityName() : StringGenerator<StringCityName>(faker => faker.Address.City());
public class StringCountryName() : StringGenerator<StringCountryName>(faker => faker.Address.Country());
public class StringPostalCode() : StringGenerator<StringPostalCode>(faker => faker.Address.ZipCode());
public class StringStateAbbreviation() : StringGenerator<StringStateAbbreviation>(faker => faker.Address.StateAbbr());
public class StringStateFullName() : StringGenerator<StringStateFullName>(faker => faker.Address.State());
public class StringStreetAddress() : StringGenerator<StringStreetAddress>(faker => faker.Address.StreetAddress());
public class StringJobTitle() : StringGenerator<StringJobTitle>(faker => faker.Name.JobTitle());
public class StringProductCategory() : StringGenerator<StringProductCategory>(faker => faker.Commerce.Categories(1)[0]);
public class StringProductDescription() : StringGenerator<StringProductDescription>(faker => faker.Commerce.ProductDescription());
public class StringCurrency() : StringGenerator<StringCurrency>(faker => faker.Finance.Currency().Code); // Extracting the currency code as a string
public class StringFileExtension() : StringGenerator<StringFileExtension>(faker => faker.System.CommonFileExt());
public class StringIPAddress() : StringGenerator<StringIPAddress>(faker => faker.Internet.Ip());
public class StringHtmlTag() : StringGenerator<StringHtmlTag>(faker => faker.Random.AlphaNumeric(5));
public class StringPassword() : StringGenerator<StringPassword>(faker => faker.Internet.Password());
public class StringGuid() : StringGenerator<StringGuid>(faker => faker.Random.Guid().ToString());

0 comments on commit b48b3fe

Please sign in to comment.