• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentCommand / 8850661434

26 Apr 2024 03:30PM UTC coverage: 54.48% (+2.6%) from 51.881%
8850661434

push

github

pwelter34
update debug settings

1152 of 2717 branches covered (42.4%)

Branch coverage included in aggregate %.

3682 of 6156 relevant lines covered (59.81%)

701.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/FluentCommand.SqlServer/Import/ImportValidator.cs
1
using System.ComponentModel.DataAnnotations;
2
using System.Data;
3

4
using FluentCommand.Extensions;
5

6
using HashCode = FluentCommand.Internal.HashCode;
7

8
namespace FluentCommand.Import;
9

10
/// <summary>
11
/// An import data validator
12
/// </summary>
13
public class ImportValidator : IImportValidator
14
{
15
    private readonly HashSet<int> _duplicateHash;
16

17
    /// <summary>
18
    /// Initializes a new instance of the <see cref="ImportValidator"/> class.
19
    /// </summary>
20
    public ImportValidator()
×
21
    {
22
        _duplicateHash = new HashSet<int>();
×
23
    }
×
24

25

26
    /// <summary>
27
    /// Validates the specified <paramref name="targetRow" />.
28
    /// </summary>
29
    /// <param name="importDefinition">The import definition.</param>
30
    /// <param name="targetRow">The target row.</param>
31
    /// <returns></returns>
32
    public virtual Task ValidateRow(ImportDefinition importDefinition, DataRow targetRow)
33
    {
34
        // check for DBNull or null
35
        CheckForNull(importDefinition, targetRow);
×
36

37
        // look for duplicate key values
38
        CheckForDuplicate(importDefinition, targetRow);
×
39

40
        return Task.CompletedTask;
×
41
    }
42

43

44
    /// <summary>
45
    /// Checks specified <paramref name="targetRow" /> for null values.
46
    /// </summary>
47
    /// <param name="importDefinition">The import definition.</param>
48
    /// <param name="targetRow">The target row.</param>
49
    /// <exception cref="ValidationException">When a required field is null</exception>
50
    protected virtual void CheckForNull(ImportDefinition importDefinition, DataRow targetRow)
51
    {
52
        var requiredFields = importDefinition
×
53
            .Fields
×
54
            .Where(b => b.IsRequired || b.IsKey)
×
55
            .Select(b => b.Name)
×
56
            .ToList();
×
57

58
        // first null
59
        var invalidField = requiredFields
×
60
            .FirstOrDefault(targetRow.IsNull);
×
61

62
        if (!invalidField.HasValue())
×
63
            return;
×
64

65
        string message = "Field '{0}' can not be null. {1}"
×
66
            .FormatWith(invalidField, targetRow.ItemArray.ToDelimitedString());
×
67

68
        throw new ValidationException(message);
×
69
    }
70

71
    /// <summary>
72
    /// Checks specified <paramref name="targetRow" /> for key duplicates.
73
    /// </summary>
74
    /// <param name="importDefinition">The import definition.</param>
75
    /// <param name="targetRow">The target row.</param>
76
    /// <exception cref="ValidationException">When key is duplicate</exception>
77
    protected virtual void CheckForDuplicate(ImportDefinition importDefinition, DataRow targetRow)
78
    {
79
        var keyFields = importDefinition
×
80
            .Fields
×
81
            .Where(b => b.IsKey)
×
82
            .Select(b => b.Name)
×
83
            .ToList();
×
84

85
        var keyValues = keyFields
×
86
            .Select(k => targetRow[k])
×
87
            .ToList();
×
88

89
        // use a hash code of all the key values to check duplicate
90
        var hashCode = keyValues
×
91
            .Aggregate(HashCode.Seed, (hash, value) => hash.Combine(value));
×
92

93
        bool added = _duplicateHash.Add(hashCode);
×
94
        if (added)
×
95
            return;
×
96

97
        string message = "Duplicate key found.  Field: {0}, Value: {1}"
×
98
            .FormatWith(keyFields.ToDelimitedString(), keyValues.ToDelimitedString());
×
99

100
        throw new ValidationException(message);
×
101
    }
102
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc