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

fluentassertions / fluentassertions.datasets / 13117535442

03 Feb 2025 03:58PM UTC coverage: 94.089% (-0.3%) from 94.362%
13117535442

Pull #21

github

web-flow
Bump FluentAssertions from 8.0.0 to 8.0.1

Bumps [FluentAssertions](https://github.com/fluentassertions/fluentassertions) from 8.0.0 to 8.0.1.
- [Release notes](https://github.com/fluentassertions/fluentassertions/releases)
- [Changelog](https://github.com/fluentassertions/fluentassertions/blob/main/AcceptApiChanges.ps1)
- [Commits](https://github.com/fluentassertions/fluentassertions/compare/8.0.0...8.0.1)

---
updated-dependencies:
- dependency-name: FluentAssertions
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #21: Bump FluentAssertions from 8.0.0 to 8.0.1

458 of 498 branches covered (91.97%)

Branch coverage included in aggregate %.

1261 of 1329 relevant lines covered (94.88%)

2324.26 hits per line

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

96.05
/Src/FluentAssertions.DataSets/Equivalency/DataRowCollectionEquivalencyStep.cs
1
using System;
2
using System.Data;
3
using System.Linq;
4
using FluentAssertions.DataSets.Common;
5
using FluentAssertions.Equivalency;
6
using FluentAssertions.Execution;
7

8
namespace FluentAssertions.DataSets.Equivalency;
9

10
public class DataRowCollectionEquivalencyStep : EquivalencyStep<DataRowCollection>
11
{
12
    protected override EquivalencyResult OnHandle(Comparands comparands, IEquivalencyValidationContext context,
13
        IValidateChildNodeEquivalency nestedValidator)
14
    {
15
        var assertionChain = AssertionChain.GetOrCreate().For(context);
424✔
16

17
        if (comparands.Subject is not DataRowCollection)
424✔
18
        {
19
            assertionChain
2✔
20
                .FailWith("Expected {context:value} to be of type DataRowCollection, but found {0}",
2✔
21
                    comparands.Subject.GetType());
2✔
22
        }
23
        else
24
        {
25
            RowMatchMode rowMatchMode = context.Options switch
422!
26
            {
422✔
27
                DataEquivalencyAssertionOptions<DataSet> dataSetConfig => dataSetConfig.RowMatchMode,
200✔
28
                DataEquivalencyAssertionOptions<DataTable> dataTableConfig => dataTableConfig.RowMatchMode,
222✔
29
                _ => RowMatchMode.Index
×
30
            };
422✔
31

32
            var subject = (DataRowCollection)comparands.Subject;
422✔
33
            var expectation = (DataRowCollection)comparands.Expectation;
422✔
34

35
            assertionChain
422✔
36
                .ForCondition(subject.Count == expectation.Count)
422✔
37
                .FailWith("Expected {context:DataRowCollection} to contain {0} row(s){reason}, but found {1}",
422✔
38
                    expectation.Count, subject.Count);
422✔
39

40
            if (assertionChain.Succeeded)
422✔
41
            {
42
                switch (rowMatchMode)
43
                {
44
                    case RowMatchMode.Index:
45
                        MatchRowsByIndexAndCompare(context, nestedValidator, subject, expectation);
390✔
46
                        break;
390✔
47

48
                    case RowMatchMode.PrimaryKey:
49
                        MatchRowsByPrimaryKeyAndCompare(nestedValidator, context, subject, expectation, assertionChain);
14✔
50
                        break;
14✔
51

52
                    default:
53
                        assertionChain.FailWith(
2✔
54
                            "Unknown RowMatchMode {0} when trying to compare {context:DataRowCollection}", rowMatchMode);
2✔
55

56
                        break;
57
                }
58
            }
59
        }
60

61
        return EquivalencyResult.EquivalencyProven;
424✔
62
    }
63

64
    private static void MatchRowsByIndexAndCompare(IEquivalencyValidationContext context, IValidateChildNodeEquivalency parent,
65
        DataRowCollection subject, DataRowCollection expectation)
66
    {
67
        for (int index = 0; index < expectation.Count; index++)
8,580✔
68
        {
69
            IEquivalencyValidationContext nestedContext = context.AsCollectionItem<DataRow>(index);
3,900✔
70
            parent.AssertEquivalencyOf(new Comparands(subject[index], expectation[index], typeof(DataRow)), nestedContext);
3,900✔
71
        }
72
    }
390✔
73

74
    private static void MatchRowsByPrimaryKeyAndCompare(IValidateChildNodeEquivalency parent,
75
        IEquivalencyValidationContext context,
76
        DataRowCollection subject, DataRowCollection expectation, AssertionChain assertionChain)
77
    {
78
        Type[] subjectPrimaryKeyTypes = null;
14✔
79
        Type[] expectationPrimaryKeyTypes = null;
14✔
80

81
        if (subject.Count > 0)
14✔
82
        {
83
            subjectPrimaryKeyTypes = GatherPrimaryKeyColumnTypes(subject[0].Table, "subject", assertionChain);
14✔
84
        }
85

86
        if (expectation.Count > 0)
14✔
87
        {
88
            expectationPrimaryKeyTypes = GatherPrimaryKeyColumnTypes(expectation[0].Table, "expectation", assertionChain);
14✔
89
        }
90

91
        bool matchingTypes = ComparePrimaryKeyTypes(subjectPrimaryKeyTypes, expectationPrimaryKeyTypes, assertionChain);
14✔
92

93
        if (matchingTypes)
14✔
94
        {
95
            GatherRowsByPrimaryKeyAndCompareData(parent, context, subject, expectation, assertionChain);
8✔
96
        }
97
    }
14✔
98

99
    private static Type[] GatherPrimaryKeyColumnTypes(DataTable table, string comparisonTerm, AssertionChain assertionChain)
100
    {
101
        Type[] primaryKeyTypes = null;
28✔
102

103
        if (table.PrimaryKey is null or { Length: 0 })
28✔
104
        {
105
            assertionChain
4✔
106
                .FailWith(
4✔
107
                    "Table {0} containing {1} {context:DataRowCollection} does not have a primary key. RowMatchMode.PrimaryKey cannot be applied.",
4✔
108
                    table.TableName, comparisonTerm);
4✔
109
        }
110
        else
111
        {
112
            primaryKeyTypes = new Type[table.PrimaryKey.Length];
24✔
113

114
            for (int i = 0; i < table.PrimaryKey.Length; i++)
96✔
115
            {
116
                primaryKeyTypes[i] = table.PrimaryKey[i].DataType;
24✔
117
            }
118
        }
119

120
        return primaryKeyTypes;
28✔
121
    }
122

123
    private static bool ComparePrimaryKeyTypes(Type[] subjectPrimaryKeyTypes, Type[] expectationPrimaryKeyTypes, AssertionChain assertionChain)
124
    {
125
        bool matchingTypes = false;
14✔
126

127
        if (subjectPrimaryKeyTypes is not null && expectationPrimaryKeyTypes is not null)
14✔
128
        {
129
            matchingTypes = subjectPrimaryKeyTypes.Length == expectationPrimaryKeyTypes.Length;
10✔
130

131
            for (int i = 0; matchingTypes && i < subjectPrimaryKeyTypes.Length; i++)
40✔
132
            {
133
                if (subjectPrimaryKeyTypes[i] != expectationPrimaryKeyTypes[i])
10✔
134
                {
135
                    matchingTypes = false;
2✔
136
                }
137
            }
138

139
            if (!matchingTypes)
10✔
140
            {
141
                assertionChain
2✔
142
                    .FailWith(
2✔
143
                        "Subject and expectation primary keys of table containing {context:DataRowCollection} do not have the same schema and cannot be compared. RowMatchMode.PrimaryKey cannot be applied.");
2✔
144
            }
145
        }
146

147
        return matchingTypes;
14✔
148
    }
149

150
    private static void GatherRowsByPrimaryKeyAndCompareData(IValidateChildNodeEquivalency parent, IEquivalencyValidationContext context,
151
        DataRowCollection subject, DataRowCollection expectation, AssertionChain assertionChain)
152
    {
153
        var expectationRowByKey = expectation.Cast<DataRow>()
8✔
154
            .ToDictionary(row => ExtractPrimaryKey(row));
88✔
155

156
        foreach (DataRow subjectRow in subject.Cast<DataRow>())
176✔
157
        {
158
            CompoundKey key = ExtractPrimaryKey(subjectRow);
80✔
159

160
            if (!expectationRowByKey.TryGetValue(key, out DataRow expectationRow))
80✔
161
            {
162
                assertionChain
8✔
163
                    .FailWith("Found unexpected row in {context:DataRowCollection} with key {0}", key);
8✔
164
            }
165
            else
166
            {
167
                expectationRowByKey.Remove(key);
72✔
168

169
                IEquivalencyValidationContext nestedContext = context.AsCollectionItem<DataRow>(key.ToString());
72✔
170
                parent.AssertEquivalencyOf(new Comparands(subjectRow, expectationRow, typeof(DataRow)), nestedContext);
72✔
171
            }
172
        }
173

174
        if (expectationRowByKey.Count > 0)
8✔
175
        {
176
            if (expectationRowByKey.Count > 1)
4✔
177
            {
178
                assertionChain
2✔
179
                    .FailWith("{0} rows were expected in {context:DataRowCollection} and not found", expectationRowByKey.Count);
2✔
180
            }
181
            else
182
            {
183
                assertionChain
2✔
184
                    .FailWith(
2✔
185
                        "Expected to find a row with key {0} in {context:DataRowCollection}{reason}, but no such row was found",
2✔
186
                        expectationRowByKey.Keys.Single());
2✔
187
            }
188
        }
189
    }
6✔
190

191
    private sealed class CompoundKey : IEquatable<CompoundKey>
192
    {
193
        private readonly object[] values;
194

195
        public CompoundKey(params object[] values)
160✔
196
        {
197
            this.values = values;
160✔
198
        }
160✔
199

200
        public bool Equals(CompoundKey other)
201
        {
202
            if (other is null)
144!
203
            {
204
                return false;
×
205
            }
206

207
            return values.Length == other.values.Length && values.SequenceEqual(other.values);
144!
208
        }
209

210
        public override bool Equals(object obj) => Equals(obj as CompoundKey);
×
211

212
        public override int GetHashCode()
213
        {
214
            int hash = 0;
232✔
215

216
            foreach (var value in values)
928✔
217
            {
218
                hash = hash * 389 ^ value.GetHashCode();
232✔
219
            }
220

221
            return hash;
232✔
222
        }
223

224
        public override string ToString()
225
        {
226
            return "{ " + string.Join(", ", values) + " }";
80✔
227
        }
228
    }
229

230
    private static CompoundKey ExtractPrimaryKey(DataRow row)
231
    {
232
        DataColumn[] primaryKey = row.Table.PrimaryKey;
160✔
233

234
        var values = new object[primaryKey.Length];
160✔
235

236
        for (int i = 0; i < values.Length; i++)
640✔
237
        {
238
            values[i] = row[primaryKey[i]];
160✔
239
        }
240

241
        return new CompoundKey(values);
160✔
242
    }
243
}
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

© 2025 Coveralls, Inc