Skip to content

Commit 952099b

Browse files
committed
Improve the DataRowCollectionEquivalency code coverage
1 parent dadb0bc commit 952099b

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

Tests/FluentAssertions.Equivalency.Specs/DataTableSpecs.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Data;
34
using System.Globalization;
45
using System.Linq;
@@ -32,6 +33,112 @@ public void When_data_tables_are_both_null_equivalence_test_should_succeed()
3233
((DataTable)null).Should().BeEquivalentTo(null);
3334
}
3435

36+
[Fact]
37+
public void When_row_match_mode_is_invalid_it_should_fail()
38+
{
39+
// Arrange
40+
var typedDataSet = CreateDummyDataSet<TypedDataSetSubclass>();
41+
42+
var subject = typedDataSet.ToUntypedDataSet().Tables["TypedDataTable1"];
43+
var expectation = typedDataSet.ToUntypedDataSet().Tables["TypedDataTable1"];
44+
45+
// Act
46+
Action action = () => subject.Should().BeEquivalentTo(expectation, options => options.UsingRowMatchMode((RowMatchMode)2));
47+
48+
// Assert
49+
action.Should().Throw<XunitException>().WithMessage(
50+
"Unknown RowMatchMode *when trying to compare *");
51+
}
52+
53+
[Theory]
54+
[MemberData(nameof(EmptyPrimaryKeys))]
55+
public void When_row_match_mode_is_primary_key_without_primary_key_it_should_fail(DataColumn[] emptyPrimaryKey)
56+
{
57+
// Arrange
58+
var typedDataSet = CreateDummyDataSet<TypedDataSetSubclass>(includeRelation: false);
59+
60+
var subject = typedDataSet.ToUntypedDataSet().Tables["TypedDataTable1"];
61+
var expectation = typedDataSet.ToUntypedDataSet().Tables["TypedDataTable1"];
62+
63+
subject.PrimaryKey = emptyPrimaryKey;
64+
65+
// Act
66+
Action action = () =>
67+
subject.Should().BeEquivalentTo(expectation, options => options.UsingRowMatchMode(RowMatchMode.PrimaryKey));
68+
69+
// Assert
70+
action.Should().Throw<XunitException>().WithMessage(
71+
"*Table *containing *does not have a primary key. RowMatchMode.PrimaryKey cannot be applied.*");
72+
}
73+
74+
public static IEnumerable<object[]> EmptyPrimaryKeys => new List<object[]>
75+
{
76+
new object[] { null },
77+
new object[] { new DataColumn[] { } }
78+
};
79+
80+
[Fact]
81+
public void When_primary_key_types_do_not_match_it_should_throw()
82+
{
83+
// Arrange
84+
var typedDataSetSubject = CreateDummyDataSet<TypedDataSetSubclass>(includeDummyData: false, includeRelation: false);
85+
var typedDataSetExpectation = new TypedDataSetSubclass(typedDataSetSubject);
86+
87+
var subject = typedDataSetSubject.ToUntypedDataSet().Tables["TypedDataTable1"];
88+
var expectation = typedDataSetExpectation.ToUntypedDataSet().Tables["TypedDataTable1"];
89+
90+
subject.PrimaryKey[0].DataType = typeof(long);
91+
subject.Rows.Add(1L);
92+
subject.AcceptChanges();
93+
expectation.Rows.Add(1);
94+
expectation.AcceptChanges();
95+
96+
// Act
97+
Action action = () =>
98+
subject.Should().BeEquivalentTo(expectation, options => options.UsingRowMatchMode(RowMatchMode.PrimaryKey));
99+
100+
// Assert
101+
action.Should().Throw<XunitException>().WithMessage(
102+
"*Subject and expectation primary keys of table containing *do not have the same schema and cannot be compared. " +
103+
"RowMatchMode.PrimaryKey cannot be applied.*");
104+
}
105+
106+
[Theory]
107+
[InlineData(1)]
108+
[InlineData(3)]
109+
public void When_primary_key_of_rows_differ_it_should_fail(int nbrRowsToEdit)
110+
{
111+
// Arrange
112+
var typedDataSetSubject = CreateDummyDataSet<TypedDataSetSubclass>();
113+
var typedDataSetExpectation = new TypedDataSetSubclass(typedDataSetSubject);
114+
115+
var subject = typedDataSetSubject.ToUntypedDataSet().Tables["TypedDataTable1"];
116+
var expectation = typedDataSetExpectation.ToUntypedDataSet().Tables["TypedDataTable1"];
117+
118+
for (int i = 0; i < nbrRowsToEdit; i++)
119+
{
120+
expectation.Rows[i].SetField(expectation.PrimaryKey[0], i);
121+
}
122+
123+
expectation.AcceptChanges();
124+
125+
// Act
126+
Action action = () =>
127+
subject.Should().BeEquivalentTo(expectation, options => options.UsingRowMatchMode(RowMatchMode.PrimaryKey));
128+
129+
// Assert
130+
if (nbrRowsToEdit == 1)
131+
{
132+
action.Should().Throw<XunitException>().WithMessage(
133+
"Found unexpected row in *with key *Expected to find a row with key *in *, but no such row was found*");
134+
}
135+
else
136+
{
137+
action.Should().Throw<XunitException>().WithMessage(
138+
"Found unexpected row in *with key * rows were expected in *and not found*");
139+
}
140+
}
141+
35142
[Fact]
36143
public void When_data_table_is_null_and_isnt_expected_to_be_equivalence_test_should_fail()
37144
{

0 commit comments

Comments
 (0)