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

Information
Line coverage
100%
Covered lines: 24
Uncovered lines: 0
Coverable lines: 24
Total lines: 117
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
get_Entities()100%11100%
SystemUser(...)100%11100%
Entity()100%11100%
ApplyConfiguration(...)100%11100%
ApplyConfigurationsFromAssembly(...)100%44100%
Settings(...)100%22100%

File(s)

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

#LineLine coverage
 1#region Usings
 2using ZhEaIsNsAaBn.DbSeeder.Builders;
 3#endregion
 4
 5namespace ZhEaIsNsAaBn.DbSeeder;
 6
 7/// <summary>Settings of one context's seed migrations, filled in <c>AddDbSeeder&lt;TDbContext&gt;().Configure( options 
 8public sealed class SeedOptions
 9{
 71010    private readonly Dictionary<Type, SeedEntitySettings> _entities = [];
 11
 12
 13    /// <summary>The clock for audit fields and history rows (local time). Default: <see cref="TimeProvider.System"/>.</
 14    public TimeProvider TimeProvider { get; set; } = TimeProvider.System;
 15
 16    /// <summary>How long a standalone run waits for the database seed lock. Default: 60 seconds.</summary>
 71017    public TimeSpan LockTimeout { get; set; } = TimeSpan.FromSeconds( 60 );
 18
 19    /// <summary>
 20    /// The folder of the migrations inside the consumer project, as embedded (<c>DbSeederMigrationsFolder</c> in MSBuil
 21    /// Default: <c>SeedMigrations</c>.
 22    /// </summary>
 23    public string MigrationsFolder { get; set; } = "SeedMigrations";
 24
 25
 26    /// <summary>
 27    /// Databases the design tools never force-delete in (<c>down --force --execute</c> refuses), compared case-insensit
 28    /// </summary>
 71029    public ISet<string> ProtectedDatabases { get; } = new HashSet<string>( StringComparer.OrdinalIgnoreCase );
 30
 31
 32    internal object? SystemUserId { get; private set; }
 33
 34    internal string? SystemUserName { get; private set; }
 35
 279836    internal IReadOnlyDictionary<Type, SeedEntitySettings> Entities => _entities;
 37
 38
 39    /// <summary>The user written to the created-by, updated-by and (on soft delete) deleted-by fields of seed rows.</su
 40    /// <typeparam name="TId">The user key type; must match the entities' <c>*ById</c> properties.</typeparam>
 41    /// <param name="id">The user key.</param>
 42    /// <param name="name">The user name.</param>
 43    /// <returns>These options.</returns>
 44    /// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/> or white space.</exception
 45    public SeedOptions SystemUser< TId >( TId id, string name )
 46        where TId : struct, IEquatable<TId>
 47    {
 58448        ArgumentException.ThrowIfNullOrWhiteSpace( name );
 49
 58450        SystemUserId   = id;
 58451        SystemUserName = name;
 52
 58453        return this;
 54    }
 55
 56    /// <summary>Configures one entity type inline.</summary>
 57    /// <typeparam name="TEntity">The entity type.</typeparam>
 58    /// <returns>The entity's seed builder.</returns>
 59    public SeedEntityBuilder<TEntity> Entity< TEntity >()
 60        where TEntity : class
 4361        => new( Settings( typeof( TEntity ) ) );
 62
 63    /// <summary>Applies one configuration class.</summary>
 64    /// <typeparam name="TEntity">The entity type.</typeparam>
 65    /// <param name="configuration">The configuration.</param>
 66    /// <returns>These options.</returns>
 67    /// <exception cref="ArgumentNullException"><paramref name="configuration"/> is <see langword="null"/>.</exception>
 68    public SeedOptions ApplyConfiguration< TEntity >( ISeedEntityConfiguration<TEntity> configuration )
 69        where TEntity : class
 70    {
 671        ArgumentNullException.ThrowIfNull( configuration );
 72
 673        configuration.Configure( Entity<TEntity>() );
 74
 675        return this;
 76    }
 77
 78    /// <summary>
 79    /// Applies every non-abstract <see cref="ISeedEntityConfiguration{TEntity}"/> in <paramref name="assembly"/> that h
 80    /// parameterless constructor, in type-name order. Only the assembly given here is scanned.
 81    /// </summary>
 82    /// <param name="assembly">The assembly with the configuration classes.</param>
 83    /// <returns>These options.</returns>
 84    /// <exception cref="ArgumentNullException"><paramref name="assembly"/> is <see langword="null"/>.</exception>
 85    public SeedOptions ApplyConfigurationsFromAssembly( Assembly assembly )
 86    {
 687        ArgumentNullException.ThrowIfNull( assembly );
 88
 689        var apply = typeof( SeedOptions ).GetMethod( nameof( ApplyConfiguration ) )!;
 90
 91
 49892        foreach ( var type in assembly.GetTypes()
 693                                      .Where( t => t is { IsAbstract: false, IsGenericTypeDefinition: false } && t.GetCo
 694                                      .OrderBy( t => t.FullName, StringComparer.Ordinal ) )
 95        {
 49896            foreach ( var contract in type.GetInterfaces().Where( i => i.IsGenericType && i.GetGenericTypeDefinition() =
 97            {
 698                apply.MakeGenericMethod( contract.GenericTypeArguments[ 0 ] ).Invoke( this, [ Activator.CreateInstance( 
 99            }
 100        }
 101
 102
 6103        return this;
 104    }
 105
 106
 107    private SeedEntitySettings Settings( Type type )
 108    {
 43109        if ( !_entities.TryGetValue( type, out var settings ) )
 110        {
 34111            settings          = new SeedEntitySettings( type );
 34112            _entities[ type ] = settings;
 113        }
 114
 43115        return settings;
 116    }
 117}