• 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

61.54
/src/FluentCommand.SqlServer/Import/ImportProcessContext.cs
1
namespace FluentCommand.Import;
2

3
/// <summary>
4
/// The shared data for the current import process
5
/// </summary>
6
public class ImportProcessContext
7
{
8
    private readonly Lazy<IReadOnlyList<ImportFieldMapping>> _mappedFields;
9
    private readonly Dictionary<Type, object> _serviceCache;
10
    private readonly ImportFactory _importFactory;
11

12
    /// <summary>
13
    /// Initializes a new instance of the <see cref="ImportProcessContext" /> class.
14
    /// </summary>
15
    /// <param name="definition">The definition.</param>
16
    /// <param name="importData">The import data.</param>
17
    /// <param name="userName">Name of the user.</param>
18
    /// <param name="importFactory">The import service factory.</param>
19
    public ImportProcessContext(ImportDefinition definition, ImportData importData, string userName, ImportFactory importFactory)
6✔
20
    {
21
        Definition = definition;
6✔
22
        _serviceCache = new Dictionary<Type, object>();
6✔
23
        ImportData = importData;
6✔
24
        UserName = userName;
6✔
25
        _importFactory = importFactory;
6✔
26
        Errors = new List<Exception>();
6✔
27

28
        _mappedFields = new Lazy<IReadOnlyList<ImportFieldMapping>>(GetMappedFields);
6✔
29
    }
6✔
30

31
    /// <summary>
32
    /// Gets the name of the user importing the data.
33
    /// </summary>
34
    /// <value>
35
    /// The name of the user.
36
    /// </value>
37
    public string UserName { get; }
×
38

39
    /// <summary>
40
    /// Gets the import definition.
41
    /// </summary>
42
    /// <value>
43
    /// The import definition.
44
    /// </value>
45
    public ImportDefinition Definition { get; }
21✔
46

47
    /// <summary>
48
    /// Gets the import data.
49
    /// </summary>
50
    /// <value>
51
    /// The import data.
52
    /// </value>
53
    public ImportData ImportData { get; }
48✔
54

55
    /// <summary>
56
    /// Gets the mapped fields.
57
    /// </summary>
58
    /// <value>
59
    /// The mapped fields.
60
    /// </value>
61
    public IReadOnlyList<ImportFieldMapping> MappedFields => _mappedFields.Value;
15✔
62

63

64
    /// <summary>
65
    /// Gets or sets the list errors that occurred.
66
    /// </summary>
67
    /// <value>
68
    /// The list errors that occurred.
69
    /// </value>
70
    public List<Exception> Errors { get; set; }
6✔
71

72

73
    /// <summary>
74
    /// Gets the service instance.
75
    /// </summary>
76
    /// <param name="type">The type of service.</param>
77
    /// <returns></returns>
78
    public object GetService(Type type)
79
    {
80
        if (_serviceCache.TryGetValue(type, out object instance))
×
81
            return instance;
×
82

83
        instance = _importFactory(type);
×
84
        _serviceCache.Add(type, instance);
×
85

86
        return instance;
×
87
    }
88

89

90
    private List<ImportFieldMapping> GetMappedFields()
91
    {
92
        var list = new List<ImportFieldMapping>();
6✔
93

94
        var firstRow = ImportData.Data.FirstOrDefault();
6✔
95
        var columns = firstRow?.Length ?? 0;
6!
96

97
        foreach (var field in Definition.Fields)
72✔
98
        {
99
            // if default value, include
100
            if (field.Default.HasValue)
30!
101
            {
102
                list.Add(new ImportFieldMapping(field));
×
103
                continue;
×
104
            }
105

106
            var name = field.Name;
30✔
107

108
            // if mapped to an index, include
109
            var mapping = ImportData.Mappings.FirstOrDefault(f => f.Name == name);
120✔
110
            if (mapping?.Index == null)
30!
111
            {
112
                if (field.IsRequired)
×
113
                    throw new InvalidOperationException($"Missing required field mapping for '{name}'");
×
114

115
                continue;
116
            }
117

118
            if (mapping.Index.Value >= columns)
30!
119
                throw new IndexOutOfRangeException(
×
120
                    $"The mapped index {mapping.Index.Value} for field '{name}' is out of range of {columns}");
×
121

122
            list.Add(new ImportFieldMapping(field, mapping));
30✔
123
        }
124

125
        return list;
6✔
126
    }
127
}
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