< Summary - ZhEaIsNsAaBn.Libraries coverage (affected: 23 packages)

Information
Class: ZhEaIsNsAaBn.DbSeeder.Builders.SeedEntityBuilder<T>
Assembly: ZhEaIsNsAaBn.DbSeeder
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/DataBasePackages/ZhEaIsNsAaBn.DbSeeder/Builders/SeedEntityBuilder.cs
Line coverage
69%
Covered lines: 18
Uncovered lines: 8
Coverable lines: 26
Total lines: 140
Line coverage: 69.2%
Branch coverage
85%
Covered branches: 12
Total branches: 14
Branch coverage: 85.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
MatchBy(...)100%22100%
SoftDeleteFlag(...)100%210%
DeletedDate(...)100%210%
DeletedById(...)100%210%
DeletedByUser(...)100%210%
CreatedDate(...)100%11100%
CreatedById(...)100%210%
CreatedByUser(...)100%210%
UpdatedDate(...)100%11100%
UpdatedById(...)100%210%
UpdatedByUser(...)100%210%
Ignore(...)100%11100%
DeleteMode(...)100%11100%
Role(...)100%11100%
PropertyName(...)83.33%1212100%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/DataBasePackages/ZhEaIsNsAaBn.DbSeeder/Builders/SeedEntityBuilder.cs

#LineLine coverage
 1namespace ZhEaIsNsAaBn.DbSeeder.Builders;
 2
 3/// <summary>
 4/// Seed settings for one entity type, from <c>options.Entity&lt;TEntity&gt;()</c> or an <see cref="ISeedEntityConfigura
 5/// These win over the Domain attributes, which win over the Domain interfaces. Setting the same thing twice: the last o
 6/// CLI warns). Every method returns the builder.
 7/// </summary>
 8/// <typeparam name="TEntity">The entity type.</typeparam>
 9public sealed class SeedEntityBuilder< TEntity >
 10    where TEntity : class
 11{
 12    private readonly SeedEntitySettings _settings;
 13
 14
 8615    internal SeedEntityBuilder( SeedEntitySettings settings ) => _settings = settings;
 16
 17
 18    /// <summary>Identifies rows by a business key instead of the primary key. It must be backed by a unique index.</sum
 19    /// <param name="properties">The key properties, e.g. <c>x =&gt; x.IsoCode</c>.</param>
 20    /// <returns>This builder.</returns>
 21    /// <exception cref="ArgumentException">No property, or an expression that isn't a simple property access.</exceptio
 22    public SeedEntityBuilder<TEntity> MatchBy( params Expression<Func<TEntity, object?>>[] properties )
 23    {
 2724        ArgumentNullException.ThrowIfNull( properties );
 25
 3026        if ( properties.Length == 0 ) { throw new ArgumentException( "MatchBy needs at least one property.", nameof( pro
 27
 28
 2429        _settings.SetMatchBy( properties.Select( PropertyName ).ToList() );
 30
 2131        return this;
 32    }
 33
 34    /// <summary>Marks the soft-delete flag (<see langword="true"/> = deleted).</summary>
 35    /// <param name="property">The <see cref="bool"/> property.</param>
 36    /// <returns>This builder.</returns>
 37    /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception>
 038    public SeedEntityBuilder<TEntity> SoftDeleteFlag( Expression<Func<TEntity, bool>> property ) => Role( SeedRole.SoftD
 39
 40    /// <summary>Marks the property that holds when the row was soft-deleted.</summary>
 41    /// <typeparam name="TProperty">The property type (<see cref="DateTime"/> or <see cref="Nullable{DateTime}"/>).</typ
 42    /// <param name="property">The property.</param>
 43    /// <returns>This builder.</returns>
 44    /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception>
 045    public SeedEntityBuilder<TEntity> DeletedDate< TProperty >( Expression<Func<TEntity, TProperty>> property ) => Role(
 46
 47    /// <summary>Marks the property that holds the key of the user who soft-deleted the row.</summary>
 48    /// <typeparam name="TProperty">The property type (the system user's key type, or its nullable form).</typeparam>
 49    /// <param name="property">The property.</param>
 50    /// <returns>This builder.</returns>
 51    /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception>
 052    public SeedEntityBuilder<TEntity> DeletedById< TProperty >( Expression<Func<TEntity, TProperty>> property ) => Role(
 53
 54    /// <summary>Marks the property that holds the name of the user who soft-deleted the row.</summary>
 55    /// <param name="property">The <see cref="string"/> property.</param>
 56    /// <returns>This builder.</returns>
 57    /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception>
 058    public SeedEntityBuilder<TEntity> DeletedByUser( Expression<Func<TEntity, string?>> property ) => Role( SeedRole.Del
 59
 60    /// <summary>Marks the property that holds when the row was created.</summary>
 61    /// <typeparam name="TProperty">The property type (<see cref="DateTime"/> or <see cref="Nullable{DateTime}"/>).</typ
 62    /// <param name="property">The property.</param>
 63    /// <returns>This builder.</returns>
 64    /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception>
 1265    public SeedEntityBuilder<TEntity> CreatedDate< TProperty >( Expression<Func<TEntity, TProperty>> property ) => Role(
 66
 67    /// <summary>Marks the property that holds the key of the user who created the row.</summary>
 68    /// <typeparam name="TProperty">The property type (the system user's key type).</typeparam>
 69    /// <param name="property">The property.</param>
 70    /// <returns>This builder.</returns>
 71    /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception>
 072    public SeedEntityBuilder<TEntity> CreatedById< TProperty >( Expression<Func<TEntity, TProperty>> property ) => Role(
 73
 74    /// <summary>Marks the property that holds the name of the user who created the row.</summary>
 75    /// <param name="property">The <see cref="string"/> property.</param>
 76    /// <returns>This builder.</returns>
 77    /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception>
 078    public SeedEntityBuilder<TEntity> CreatedByUser( Expression<Func<TEntity, string?>> property ) => Role( SeedRole.Cre
 79
 80    /// <summary>Marks the property that holds when the row was last updated.</summary>
 81    /// <typeparam name="TProperty">The property type (<see cref="DateTime"/> or <see cref="Nullable{DateTime}"/>).</typ
 82    /// <param name="property">The property.</param>
 83    /// <returns>This builder.</returns>
 84    /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception>
 385    public SeedEntityBuilder<TEntity> UpdatedDate< TProperty >( Expression<Func<TEntity, TProperty>> property ) => Role(
 86
 87    /// <summary>Marks the property that holds the key of the user who last updated the row.</summary>
 88    /// <typeparam name="TProperty">The property type (the system user's key type, or its nullable form).</typeparam>
 89    /// <param name="property">The property.</param>
 90    /// <returns>This builder.</returns>
 91    /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception>
 092    public SeedEntityBuilder<TEntity> UpdatedById< TProperty >( Expression<Func<TEntity, TProperty>> property ) => Role(
 93
 94    /// <summary>Marks the property that holds the name of the user who last updated the row.</summary>
 95    /// <param name="property">The <see cref="string"/> property.</param>
 96    /// <returns>This builder.</returns>
 97    /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception>
 098    public SeedEntityBuilder<TEntity> UpdatedByUser( Expression<Func<TEntity, string?>> property ) => Role( SeedRole.Upd
 99
 100    /// <summary>Leaves a property out of seed data: never exported, compared or written (like <c>[SeedIgnore]</c>).</su
 101    /// <typeparam name="TProperty">The property type.</typeparam>
 102    /// <param name="property">The property.</param>
 103    /// <returns>This builder.</returns>
 104    /// <exception cref="ArgumentException">The expression isn't a simple property access.</exception>
 105    public SeedEntityBuilder<TEntity> Ignore< TProperty >( Expression<Func<TEntity, TProperty>> property )
 106    {
 3107        _settings.Ignore( PropertyName( property ) );
 108
 3109        return this;
 110    }
 111
 112    /// <summary>How rows of this entity are removed. Default: <see cref="SeedDeleteMode.Auto"/>; a run can override it.
 113    /// <param name="mode">The mode.</param>
 114    /// <returns>This builder.</returns>
 115    public SeedEntityBuilder<TEntity> DeleteMode( SeedDeleteMode mode )
 116    {
 22117        _settings.SetDeleteMode( mode );
 118
 22119        return this;
 120    }
 121
 122
 123    private SeedEntityBuilder<TEntity> Role( SeedRole role, LambdaExpression property )
 124    {
 15125        _settings.SetRole( role, PropertyName( property ) );
 126
 15127        return this;
 128    }
 129
 130    private static string PropertyName( LambdaExpression expression )
 131    {
 42132        ArgumentNullException.ThrowIfNull( expression );
 133
 42134        var body = expression.Body is UnaryExpression { NodeType: ExpressionType.Convert or ExpressionType.ConvertChecke
 135
 42136        return body is MemberExpression { Member: PropertyInfo property } member && member.Expression == expression.Para
 42137                   ? property.Name
 42138                   : throw new ArgumentException( $"'{expression}' must be a property of the entity, e.g. x => x.Name.",
 139    }
 140}