< Summary - Core.Tests

Information
Class: Common.Core.Models.Company
Assembly: Common.Core
File(s): D:\a\NuGetPackages\NuGetPackages\src\Common\Core\Models\Company.cs
Tag: 3_8508158812
Line coverage
100%
Covered lines: 158
Uncovered lines: 0
Coverable lines: 158
Total lines: 327
Line coverage: 100%
Branch coverage
98%
Covered branches: 61
Total branches: 62
Branch coverage: 98.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
get_Id()100%11100%
set_Id(...)100%22100%
get_Name()100%22100%
set_Name(...)100%22100%
get_Address()100%11100%
set_Address(...)100%22100%
get_GovernmentNumber()100%11100%
set_GovernmentNumber(...)100%22100%
get_PrimaryPhone()100%11100%
set_PrimaryPhone(...)100%22100%
get_SecondaryPhone()100%11100%
set_SecondaryPhone(...)100%22100%
get_Email()100%11100%
set_Email(...)100%22100%
get_NaicsCode()100%11100%
set_NaicsCode(...)100%22100%
get_Private()100%11100%
set_Private(...)100%22100%
get_DepositsCount()100%11100%
set_DepositsCount(...)100%22100%
get_DepositsBal()100%11100%
set_DepositsBal(...)100%22100%
Clone()50%22100%
Equals(...)100%44100%
Update(...)100%66100%
GetSerializerOptions()100%11100%
Read(...)100%11100%
UpdateSQL(...)100%1212100%
UpdateOthers(...)100%1414100%

File(s)

D:\a\NuGetPackages\NuGetPackages\src\Common\Core\Models\Company.cs

#LineLine coverage
 1// Ignore Spelling: Naics Bal
 2
 3using System.ComponentModel.DataAnnotations;
 4using System.ComponentModel.DataAnnotations.Schema;
 5using System.Data;
 6using System.Text.Json;
 7using System.Text.Json.Serialization;
 8using Common.Core.Classes;
 9using Common.Core.Converters;
 10using Common.Core.Interfaces;
 11
 12namespace Common.Core.Models;
 13
 14/// <summary>This class contains details of a Company.</summary>
 15public class Company : ModelEdit, ICompany
 16{
 17  /// <summary>Default name of the Company data file.</summary>
 18  public const string cDefaultFile = "Company.json";
 19
 20  #region Private Variables
 21
 22  private int _id;
 23  private string? _name;
 2924  private Address _address = new();
 25  private string? _governmentNumber;
 26  private string? _primaryPhone;
 27  private string? _secondaryPhone;
 28  private string? _email;
 29  private string? _naicsCode;
 30  private bool? _private;
 31  private int? _depositsCount;
 32  private decimal? _depositsBal;
 33
 34  #endregion
 35
 36  #region ICompany Properties
 37
 38  /// <inheritdoc/>
 39  public override int Id
 40  {
 5641    get => _id;
 42    set
 3043    {
 3044      if( value != _id )
 2945      {
 2946        _id = value;
 2947        OnPropertyChanged( nameof( Id ) );
 2948      }
 3049    }
 50  }
 51
 52  /// <inheritdoc/>
 53  [Required]
 54  [MaxLength( 100 )]
 55  public string Name
 56  {
 5457    get => ( _name is not null ) ? _name : string.Empty;
 58    set
 3059    {
 3060      if( !value.Equals( _name ) )
 3061      {
 3062        _name = value;
 3063        OnPropertyChanged( nameof( Name ) );
 3064      }
 3065    }
 66  }
 67
 68  /// <inheritdoc/>
 69  public Address Address
 70  {
 4471    get => _address;
 72    set
 2773    {
 2774      if( value != _address )
 2775      {
 2776        _address = value;
 2777        OnPropertyChanged( nameof( Address ) );
 2778      }
 2779    }
 80  }
 81
 82  /// <inheritdoc/>
 83  [MaxLength( 50 )]
 84  [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
 85  public string? GovernmentNumber
 86  {
 2387    get => _governmentNumber;
 88    set
 3089    {
 3090      if( value != _governmentNumber )
 3091      {
 3092        _governmentNumber = SetNullString( value );
 3093        OnPropertyChanged( nameof( GovernmentNumber ) );
 3094      }
 3095    }
 96  }
 97
 98  /// <inheritdoc/>
 99  [MaxLength( 50 )]
 100  [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
 101  public string? PrimaryPhone
 102  {
 23103    get => _primaryPhone;
 104    set
 30105    {
 30106      if( value != _primaryPhone )
 30107      {
 30108        _primaryPhone = SetNullString( value );
 30109        OnPropertyChanged( nameof( PrimaryPhone ) );
 30110      }
 30111    }
 112  }
 113
 114  /// <inheritdoc/>
 115  [MaxLength( 50 )]
 116  [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
 117  public string? SecondaryPhone
 118  {
 23119    get => _secondaryPhone;
 120    set
 30121    {
 30122      if( value != _secondaryPhone )
 30123      {
 30124        _secondaryPhone = SetNullString( value );
 30125        OnPropertyChanged( nameof( SecondaryPhone ) );
 30126      }
 30127    }
 128  }
 129
 130  /// <inheritdoc/>
 131  [MaxLength( 50 )]
 132  [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
 133  public string? Email
 134  {
 22135    get => _email;
 136    set
 28137    {
 28138      if( value != _email )
 28139      {
 28140        _email = SetNullString( value );
 28141        OnPropertyChanged( nameof( Email ) );
 28142      }
 28143    }
 144  }
 145
 146  /// <inheritdoc/>
 147  [MaxLength( 20 )]
 148  [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
 149  public string? NaicsCode
 150  {
 23151    get => _naicsCode;
 152    set
 30153    {
 30154      if( value != _naicsCode )
 30155      {
 30156        _naicsCode = SetNullString( value );
 30157        OnPropertyChanged( nameof( NaicsCode ) );
 30158      }
 30159    }
 160  }
 161
 162  /// <inheritdoc/>
 163  [Column( TypeName = "char(1)" )]
 164  [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
 165  [JsonConverter( typeof( JsonBooleanString ) )]
 166  public bool? Private
 167  {
 30168    get => _private;
 169    set
 34170    {
 34171      if( value != _private )
 33172      {
 33173        _private = value;
 33174        OnPropertyChanged( nameof( Private ) );
 33175      }
 34176    }
 177  }
 178
 179  /// <inheritdoc/>
 180  [Range( 0, 100 )]
 181  [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
 182  [JsonConverter( typeof( JsonIntegerString ) )]
 183  public int? DepositsCount
 184  {
 23185    get => _depositsCount;
 186    set
 30187    {
 30188      if( value != _depositsCount )
 30189      {
 30190        _depositsCount = value;
 30191        OnPropertyChanged( nameof( DepositsCount ) );
 30192      }
 30193    }
 194  }
 195
 196  /// <inheritdoc/>
 197  [Column( TypeName = "decimal(18,2)" )]
 198  [JsonIgnore( Condition = JsonIgnoreCondition.WhenWritingNull )]
 199  [JsonConverter( typeof( JsonDecimalString ) )]
 200  public decimal? DepositsBal
 201  {
 24202    get => _depositsBal;
 203    set
 31204    {
 31205      if( value != _depositsBal )
 24206      {
 24207        _depositsBal = value;
 24208        OnPropertyChanged( nameof( DepositsBal ) );
 24209      }
 31210    }
 211  }
 212
 213  #endregion
 214
 215  #region Public Methods
 216
 217  /// <inheritdoc/>
 218  public override object Clone()
 2219  {
 2220    return ReflectionHelper.CreateDeepCopy( this ) as Company ?? new Company();
 2221  }
 222
 223  /// <inheritdoc/>
 224  /// <param name="obj">An object implementing the ICompany interface to compare with this object.</param>
 225  public override bool Equals( object? obj )
 19226  {
 23227    if( obj is null || obj is not ICompany other ) { return false; }
 17228    return ReflectionHelper.IsEqual( this, other );
 19229  }
 230
 231  /// <inheritdoc/>
 232  /// <param name="obj">An object implementing the ICompany interface with the changed values.</param>
 233  public override void Update( object? obj )
 3234  {
 7235    if( obj is null || obj is not ICompany other || other is not Company company ) { return; }
 236
 1237        ReflectionHelper.ApplyChanges( company, this );
 3238  }
 239
 240  /// <summary>Gets the Json serializer options for Company objects.</summary>
 241  /// <returns>A JsonSerializerOptions object.</returns>
 242  public static JsonSerializerOptions GetSerializerOptions()
 1243  {
 1244    var rtn = JsonHelper.DefaultSerializerOptions();
 1245    rtn.Converters.Add( new InterfaceFactory( typeof( Company ), typeof( ICompany ) ) );
 1246    rtn.NumberHandling = JsonNumberHandling.AllowReadingFromString;
 247
 1248    return rtn;
 1249  }
 250
 251  /// <summary>Builds a Company object from a database table row.</summary>
 252  /// <param name="row">Database row containing the Company columns.</param>
 253  /// <param name="addPrefix">Table column prefix for Address fields if required.</param>
 254  /// <returns>Company object containing the database values.</returns>
 255  /// <remarks>This method assumes that the table column names are the same as the property names.</remarks>
 256  public static Company Read( DataRow row, string addPrefix = "" )
 10257  {
 10258    return new Company()
 10259    {
 10260      Id = row.Field<int>( nameof( Id ) ),
 10261      Name = row.Field<string>( nameof( Name ) )!,
 10262      Address = Address.BuildAddress( row, addPrefix ),
 10263      GovernmentNumber = row.Field<string?>( nameof( GovernmentNumber ) ),
 10264      PrimaryPhone = row.Field<string?>( nameof( PrimaryPhone ) ),
 10265      SecondaryPhone = row.Field<string?>( nameof( SecondaryPhone ) ),
 10266      Email = row.Field<string?>( nameof( Email ) ),
 10267      NaicsCode = row.Field<string?>( nameof( NaicsCode ) ),
 10268      Private = Generic.CharToBool( row[nameof( Private )] ),
 10269      DepositsCount = row.Field<int?>( nameof( DepositsCount ) ),
 10270      DepositsBal = row.Field<decimal?>( nameof( DepositsBal ) ),
 10271    };
 10272  }
 273
 274  /// <summary>Builds the SQL script for any value changes.</summary>
 275  /// <param name="row">Database row containing the current Company data.</param>
 276  /// <param name="obj">ICompany object containing the original values.</param>
 277  /// <param name="mod">ICompany object containing the modified values.</param>
 278  /// <param name="addPrefix">Table column name prefix for Address fields (if required).</param>
 279  /// <returns>An empty string is returned if no changes were found.</returns>
 280  /// <remarks>This method assumes that the table column names are the same as the property names.</remarks>
 281  public static string UpdateSQL( DataRow row, ICompany obj, ICompany mod, string addPrefix = "" )
 6282  {
 6283    IList<string> sql = new List<string>();
 6284    Company cur = Read( row, addPrefix );
 8285    if( cur.Id != mod.Id ) { return string.Empty; }
 286
 5287    if( !cur.Equals( obj ) )
 1288    {
 1289      mod.Name = cur.Name;
 1290      mod.Address = cur.Address;
 1291      mod.GovernmentNumber = cur.GovernmentNumber;
 1292      mod.PrimaryPhone = cur.PrimaryPhone;
 1293      mod.SecondaryPhone = cur.SecondaryPhone;
 1294      mod.Email = cur.Email;
 1295      mod.NaicsCode = cur.NaicsCode;
 1296      mod.DepositsCount = cur.DepositsCount;
 1297      mod.DepositsBal = cur.DepositsBal;
 1298      mod.Private = cur.Private;
 1299      return string.Empty;
 300    }
 301
 16302    if( obj.Name != mod.Name ) { SetSQLColumn( nameof( Name ), mod.Name, sql ); }
 4303    _ = Address.UpdateAddress( obj.Address, mod.Address, cur.Address, sql, addPrefix );
 4304    UpdateOthers( obj, mod, sql );
 4305    if( obj.Private != mod.Private )
 4306    {
 307      // Special handling for boolean as char
 4308      char? val = mod.Private is null ? null : mod.Private.Value ? 'Y' : 'N';
 4309      SetSQLColumn( nameof( Private ), val, sql );
 4310    }
 311
 4312    return string.Join( ", ", sql );
 6313  }
 314
 315  #endregion
 316
 317  private static void UpdateOthers( ICompany obj, ICompany mod, IList<string> sql )
 4318  {
 16319    if( obj.GovernmentNumber != mod.GovernmentNumber ) { SetSQLColumn( nameof( GovernmentNumber ), mod.GovernmentNumber,
 16320    if( obj.PrimaryPhone != mod.PrimaryPhone ) { SetSQLColumn( nameof( PrimaryPhone ), mod.PrimaryPhone, sql ); }
 16321    if( obj.SecondaryPhone != mod.SecondaryPhone ) { SetSQLColumn( nameof( SecondaryPhone ), mod.SecondaryPhone, sql ); 
 16322    if( obj.Email != mod.Email ) { SetSQLColumn( nameof( Email ), mod.Email, sql ); }
 16323    if( obj.NaicsCode != mod.NaicsCode ) { SetSQLColumn( nameof( NaicsCode ), mod.NaicsCode, sql ); }
 16324    if( obj.DepositsCount != mod.DepositsCount ) { SetSQLColumn( nameof( DepositsCount ), mod.DepositsCount.ToString(), 
 16325    if( obj.DepositsBal != mod.DepositsBal ) { SetSQLColumn( nameof( DepositsBal ), mod.DepositsBal.ToString(), sql ); }
 4326  }
 327}