Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions Tests/FluentAssertions.Equivalency.Specs/DataTableSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -32,6 +33,125 @@ public void When_data_tables_are_both_null_equivalence_test_should_succeed()
((DataTable)null).Should().BeEquivalentTo(null);
}

[Fact]
public void When_row_match_mode_is_invalid_it_should_fail()
{
// Arrange
var typedDataSet = CreateDummyDataSet<TypedDataSetSubclass>();

var subject = typedDataSet.ToUntypedDataSet().Tables["TypedDataTable1"];
var expectation = typedDataSet.ToUntypedDataSet().Tables["TypedDataTable1"];

// Act
Action action = () => subject.Should().BeEquivalentTo(expectation, options => options.UsingRowMatchMode((RowMatchMode)2));

// Assert
action.Should().Throw<XunitException>().WithMessage(
"Unknown RowMatchMode *when trying to compare *");
}

[Theory]
[MemberData(nameof(EmptyPrimaryKeys))]
public void When_row_match_mode_is_primary_key_without_primary_key_it_should_fail(DataColumn[] emptyPrimaryKey)
{
// Arrange
var typedDataSet = CreateDummyDataSet<TypedDataSetSubclass>(includeRelation: false);

var subject = typedDataSet.ToUntypedDataSet().Tables["TypedDataTable1"];
var expectation = typedDataSet.ToUntypedDataSet().Tables["TypedDataTable1"];

subject.PrimaryKey = emptyPrimaryKey;

// Act
Action action = () =>
subject.Should().BeEquivalentTo(expectation, options => options.UsingRowMatchMode(RowMatchMode.PrimaryKey));

// Assert
action.Should().Throw<XunitException>().WithMessage(
"*Table *containing *does not have a primary key. RowMatchMode.PrimaryKey cannot be applied.*");
}

public static TheoryData<DataColumn[]> EmptyPrimaryKeys => new()
{
null,
new DataColumn[] { }
};

[Fact]
public void When_primary_key_types_do_not_match_it_should_throw()
{
// Arrange
var typedDataSetSubject = CreateDummyDataSet<TypedDataSetSubclass>(includeDummyData: false, includeRelation: false);
var typedDataSetExpectation = new TypedDataSetSubclass(typedDataSetSubject);

var subject = typedDataSetSubject.ToUntypedDataSet().Tables["TypedDataTable1"];
var expectation = typedDataSetExpectation.ToUntypedDataSet().Tables["TypedDataTable1"];

subject.PrimaryKey[0].DataType = typeof(long);
subject.Rows.Add(1L);
subject.AcceptChanges();
expectation.Rows.Add(1);
expectation.AcceptChanges();

// Act
Action action = () =>
subject.Should().BeEquivalentTo(expectation, options => options.UsingRowMatchMode(RowMatchMode.PrimaryKey));

// Assert
action.Should().Throw<XunitException>().WithMessage(
"*Subject and expectation primary keys of table containing *do not have the same schema and cannot be compared. " +
"RowMatchMode.PrimaryKey cannot be applied.*");
}

[Fact]
public void When_primary_key_of_one_rows_differ_it_should_fail()
{
// Arrange
var typedDataSetSubject = CreateDummyDataSet<TypedDataSetSubclass>();
var typedDataSetExpectation = new TypedDataSetSubclass(typedDataSetSubject);

var subject = typedDataSetSubject.ToUntypedDataSet().Tables["TypedDataTable1"];
var expectation = typedDataSetExpectation.ToUntypedDataSet().Tables["TypedDataTable1"];

expectation.Rows[0].SetField(expectation.PrimaryKey[0], 0);

expectation.AcceptChanges();

// Act
Action action = () =>
subject.Should().BeEquivalentTo(expectation, options => options.UsingRowMatchMode(RowMatchMode.PrimaryKey));

// Assert
action.Should().Throw<XunitException>().WithMessage(
"Found unexpected row in *with key *Expected to find a row with key *in *, but no such row was found*");
}

[Fact]
public void When_primary_key_of_multiple_rows_differ_it_should_fail()
{
// Arrange
var typedDataSetSubject = CreateDummyDataSet<TypedDataSetSubclass>();
var typedDataSetExpectation = new TypedDataSetSubclass(typedDataSetSubject);

var subject = typedDataSetSubject.ToUntypedDataSet().Tables["TypedDataTable1"];
var expectation = typedDataSetExpectation.ToUntypedDataSet().Tables["TypedDataTable1"];

for (int i = 0; i < 3; i++)
{
expectation.Rows[i].SetField(expectation.PrimaryKey[0], i);
}

expectation.AcceptChanges();

// Act
Action action = () =>
subject.Should().BeEquivalentTo(expectation, options => options.UsingRowMatchMode(RowMatchMode.PrimaryKey));

// Assert
action.Should().Throw<XunitException>().WithMessage(
"Found unexpected row in *with key * rows were expected in *and not found*");
}

[Fact]
public void When_data_table_is_null_and_isnt_expected_to_be_equivalence_test_should_fail()
{
Expand Down