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

Information
Class: ZhEaIsNsAaBn.APPSettings.EFConfigurationWriter<T1, T2>
Assembly: ZhEaIsNsAaBn.APPSettings
File(s): /home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/DataBasePackages/ZhEaIsNsAaBn.APPSettings/Services/EFConfigurationWriter.cs
Line coverage
100%
Covered lines: 44
Uncovered lines: 0
Coverable lines: 44
Total lines: 114
Line coverage: 100%
Branch coverage
95%
Covered branches: 19
Total branches: 20
Branch coverage: 95%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
InsertAsync()100%22100%
InsertFromJsonAsync()87.5%88100%
FlattenJson(...)100%22100%
FlattenElement(...)100%88100%

File(s)

/home/runner/work/ZhEaIsNsAaBn.Libraries.V1/ZhEaIsNsAaBn.Libraries.V1/src/DataBasePackages/ZhEaIsNsAaBn.APPSettings/Services/EFConfigurationWriter.cs

#LineLine coverage
 1namespace ZhEaIsNsAaBn.APPSettings;
 2
 3/// <summary>Default writer: updates the row with the key (among rows matching the query) or adds one.</summary>
 4internal sealed class EFConfigurationWriter< TSettingsTable, TDbContext > : IEFConfigurationWriter<TSettingsTable, TDbCo
 5    where TSettingsTable : class, IEFConfigurationValue, new()
 6    where TDbContext : DbContext
 7{
 8    private readonly EFConfigurationProviderSettings<TSettingsTable, TDbContext> _settings;
 9
 10
 4811    public EFConfigurationWriter( EFConfigurationProviderSettings<TSettingsTable, TDbContext> settings ) => _settings = 
 12
 13
 14    /// <inheritdoc />
 15    public async Task InsertAsync( string key, string value, CancellationToken cancellationToken = default )
 16    {
 817        ArgumentException.ThrowIfNullOrWhiteSpace( key );
 818        ArgumentNullException.ThrowIfNull( value );
 19
 20
 821        var context = await _settings.GetRequiredDbContextFactory().CreateDbContextAsync( cancellationToken ).ConfigureA
 22
 823        await using ( context.ConfigureAwait( false ) )
 24        {
 825            var set      = context.Set<TSettingsTable>();
 826            var existing = await set.Where( _settings.Query ).FirstOrDefaultAsync( e => e.Key == key, cancellationToken 
 27
 28
 1229            if ( existing is null ) { set.Add( new TSettingsTable { Key = key, Value = value } ); }
 430            else { existing.Value = value; }
 31
 32
 833            await context.SaveChangesAsync( cancellationToken ).ConfigureAwait( false );
 834        }
 835    }
 36
 37    /// <inheritdoc />
 38    public async Task InsertFromJsonAsync( string json, CancellationToken cancellationToken = default )
 39    {
 1240        ArgumentNullException.ThrowIfNull( json );
 41
 42
 1243        var values = FlattenJson( json );
 44
 445        if ( values.Count == 0 ) { return; }
 46
 47
 448        var context = await _settings.GetRequiredDbContextFactory().CreateDbContextAsync( cancellationToken ).ConfigureA
 49
 450        await using ( context.ConfigureAwait( false ) )
 51        {
 452            var set      = context.Set<TSettingsTable>();
 453            var keys     = values.Keys.ToList();
 454            var existing = new Dictionary<string, TSettingsTable>( StringComparer.OrdinalIgnoreCase );
 55
 456            var rows = await set.Where( _settings.Query )
 457                                .Where( e => keys.Contains( e.Key ) )
 458                                .ToListAsync( cancellationToken )
 459                                .ConfigureAwait( false );
 60
 61
 2062            foreach ( var row in rows ) { existing.TryAdd( row.Key, row ); }
 63
 64
 4065            foreach ( var (key, value) in values )
 66            {
 2067                if ( existing.TryGetValue( key, out var row ) ) { row.Value = value; }
 1268                else { set.Add( new TSettingsTable { Key = key, Value = value } ); }
 69            }
 70
 71
 472            await context.SaveChangesAsync( cancellationToken ).ConfigureAwait( false );
 473        }
 474    }
 75
 76
 77    private static Dictionary<string, string> FlattenJson( string json )
 78    {
 1279        using var document = JsonDocument.Parse( json );
 80
 1281        if ( document.RootElement.ValueKind != JsonValueKind.Object ) { throw new ArgumentException( "The JSON root must
 82
 83
 484        var values = new Dictionary<string, string>( StringComparer.OrdinalIgnoreCase );
 85
 486        FlattenElement( document.RootElement, string.Empty, values );
 87
 488        return values;
 489    }
 90
 91    private static void FlattenElement( JsonElement element, string prefix, Dictionary<string, string> values )
 92    {
 2493        switch ( element.ValueKind )
 94        {
 95            case JsonValueKind.Object:
 5696                foreach ( var property in element.EnumerateObject() )
 97                {
 2098                    FlattenElement( property.Value, prefix.Length == 0 ? property.Name : $"{prefix}:{property.Name}", va
 99                }
 100
 101                break;
 102
 103            case JsonValueKind.Array:
 4104                values[ prefix ] = element.GetRawText();
 105
 4106                break;
 107
 108            default:
 12109                values[ prefix ] = element.ToString();
 110
 111                break;
 112        }
 20113    }
 114}