Files
sql-utilities/tests/Strata.SqlTools.SqlBreakdown.Tests/SqlBreakdownCollectionTests/SqlBreakdownCollectionCoreTests.cs
T
Thom LambandClaude Opus 4.7 505ada5017 chore(sonar): adopt Has.Count, Is.Empty, Does.Contain, char overloads in tests (NUnit2046, NUnit2011, CA1866)
- NUnit2046: `Assert.That(x.Count, Is.EqualTo(n))` → `Assert.That(x, Has.Count.EqualTo(n))` (or `Is.Empty` when n==0)
- NUnit2011: `Assert.That(s.Contains(x))` → `Assert.That(s, Does.Contain(x))` for richer failure messages
- CA1866: `.StartsWith("$"|"@"|":")` → `.StartsWith('$'|'@'|':')` char overload

Driven by `dotnet format analyzers --diagnostics NUnit2046 NUnit2011 CA1866`
for the cases the Roslyn fixer handles, plus a regex sweep for the remaining
`Count == n` (n>0) cases which the fixer doesn't address. CA1866 had no
associated code fix and was edited by hand (3 sites in 2 files). All tests
green (1180 passing).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 11:58:00 -05:00

470 lines
13 KiB
C#

using Strata.SqlTools.SqlBreakdown.Classes;
using Strata.SqlTools.SqlBreakdown.Interfaces;
namespace Strata.SqlTools.SqlBreakdown.Tests.SqlBreakdownCollectionTests;
/// <summary>
/// Unit tests for the SqlBreakdownCollection core functionality.
/// </summary>
[TestFixture]
public class SqlBreakdownCollectionCoreTests : SqlBreakdownCollectionTestBase
{
[Test]
public void Constructor_WithNoArguments_CreatesEmptyCollection()
{
// Arrange & Act
var collection = new SqlBreakdownCollection();
// Assert
Assert.That(collection, Is.Empty);
Assert.That(collection.IsEmpty, Is.True);
Assert.That(collection.Breakdowns, Is.Empty);
Assert.That(collection.RawStatements, Is.Empty);
}
[Test]
public void Constructor_WithBreakdownList_InitializesCollection()
{
// Arrange
var mockBreakdowns = new List<ISqlBreakdown>
{
new MockSqlBreakdown("SELECT * FROM Table1"),
new MockSqlBreakdown("SELECT * FROM Table2")
};
// Act
var collection = new SqlBreakdownCollection(mockBreakdowns);
// Assert
Assert.That(collection, Has.Count.EqualTo(2));
Assert.That(collection.IsEmpty, Is.False);
}
[Test]
public void Add_WithValidBreakdown_AddsToCollection()
{
// Arrange
var collection = new SqlBreakdownCollection();
var breakdown = new MockSqlBreakdown("SELECT * FROM Table1");
// Act
collection.Add(breakdown);
// Assert
Assert.That(collection, Has.Count.EqualTo(1));
Assert.That(collection.GetAt(0), Is.EqualTo(breakdown));
}
[Test]
public void Add_WithNullBreakdown_ThrowsArgumentNullException()
{
// Arrange
var collection = new SqlBreakdownCollection();
// Act & Assert
Assert.Throws<ArgumentNullException>(() => collection.Add(null!));
}
[Test]
public void AddRange_WithMultipleBreakdowns_AddsAllItems()
{
// Arrange
var collection = new SqlBreakdownCollection();
var breakdowns = new List<ISqlBreakdown>
{
new MockSqlBreakdown("SELECT * FROM Table1"),
new MockSqlBreakdown("SELECT * FROM Table2"),
new MockSqlBreakdown("SELECT * FROM Table3")
};
// Act
collection.AddRange(breakdowns);
// Assert
Assert.That(collection, Has.Count.EqualTo(3));
}
[Test]
public void AddRange_WithNullCollection_ThrowsArgumentNullException()
{
// Arrange
var collection = new SqlBreakdownCollection();
// Act & Assert
Assert.Throws<ArgumentNullException>(() => collection.AddRange(null!));
}
[Test]
public void Remove_WithExistingBreakdown_RemovesItem()
{
// Arrange
var breakdown = new MockSqlBreakdown("SELECT * FROM Table1");
var collection = new SqlBreakdownCollection(new[] { breakdown });
// Act
var result = collection.Remove(breakdown);
// Assert
Assert.That(result, Is.True);
Assert.That(collection, Is.Empty);
}
[Test]
public void Remove_WithNonExistingBreakdown_ReturnsFalse()
{
// Arrange
var collection = new SqlBreakdownCollection(new[] { new MockSqlBreakdown("SELECT * FROM Table1") });
var otherBreakdown = new MockSqlBreakdown("SELECT * FROM Table2");
// Act
var result = collection.Remove(otherBreakdown);
// Assert
Assert.That(result, Is.False);
Assert.That(collection, Has.Count.EqualTo(1));
}
[Test]
public void Clear_WithMultipleItems_RemovesAll()
{
// Arrange
var collection = new SqlBreakdownCollection(new[]
{
new MockSqlBreakdown("SELECT * FROM Table1"),
new MockSqlBreakdown("SELECT * FROM Table2")
});
// Act
collection.Clear();
// Assert
Assert.That(collection, Is.Empty);
Assert.That(collection.IsEmpty, Is.True);
}
[Test]
public void ParseBatch_WithGoSeparators_ParsesStatements()
{
// Arrange
var collection = new SqlBreakdownCollection();
var batchSql = @"
SELECT * FROM Table1
GO
SELECT * FROM Table2
GO
SELECT * FROM Table3
";
// Act
collection.ParseBatch(batchSql);
// Assert
Assert.That(collection.RawStatements, Has.Count.EqualTo(3));
Assert.That(collection.RawStatements[0], Does.Contain("Table1"));
Assert.That(collection.RawStatements[1], Does.Contain("Table2"));
Assert.That(collection.RawStatements[2], Does.Contain("Table3"));
}
[Test]
public void ParseBatch_WithMixedCaseGO_ParsesCorrectly()
{
// Arrange
var collection = new SqlBreakdownCollection();
var batchSql = @"
SELECT * FROM Table1
go
SELECT * FROM Table2
GO
SELECT * FROM Table3
";
// Act
collection.ParseBatch(batchSql);
// Assert
Assert.That(collection.RawStatements, Has.Count.EqualTo(3));
}
[Test]
public void ParseBatch_WithGoAndWhitespace_ParsesCorrectly()
{
// Arrange
var collection = new SqlBreakdownCollection();
var batchSql = @"
SELECT * FROM Table1
GO
SELECT * FROM Table2
GO
SELECT * FROM Table3
";
// Act
collection.ParseBatch(batchSql);
// Assert
Assert.That(collection.RawStatements, Has.Count.EqualTo(3));
}
[Test]
public void ParseBatch_WithEmptyStatements_FiltersOutEmptyValues()
{
// Arrange
var collection = new SqlBreakdownCollection();
var batchSql = @"
SELECT * FROM Table1
GO
GO
SELECT * FROM Table2
";
// Act
collection.ParseBatch(batchSql);
// Assert
Assert.That(collection.RawStatements, Has.Count.EqualTo(2));
Assert.That(collection.RawStatements[0], Does.Contain("Table1"));
Assert.That(collection.RawStatements[1], Does.Contain("Table2"));
}
[Test]
public void ParseBatch_WithNullInput_ThrowsArgumentNullException()
{
// Arrange
var collection = new SqlBreakdownCollection();
// Act & Assert
Assert.Throws<ArgumentNullException>(() => collection.ParseBatch(null!));
}
[Test]
public void GetCombinedSql_WithMultipleBreakdowns_CombinesSqlWithSeparator()
{
// Arrange
var breakdowns = new[]
{
new MockSqlBreakdown("SELECT * FROM Table1"),
new MockSqlBreakdown("SELECT * FROM Table2")
};
var collection = new SqlBreakdownCollection(breakdowns);
// Act
var result = collection.GetCombinedSql();
// Assert
Assert.That(result, Does.Contain("Table1"));
Assert.That(result, Does.Contain("Table2"));
Assert.That(result, Does.Contain("GO"));
}
[Test]
public void GetCombinedSql_WithEmptyCollection_ReturnsEmpty()
{
// Arrange
var collection = new SqlBreakdownCollection();
// Act
var result = collection.GetCombinedSql();
// Assert
Assert.That(result, Is.Empty);
}
[Test]
public void GetCombinedSql_WithCustomSeparator_UsesCustomSeparator()
{
// Arrange
var breakdowns = new[]
{
new MockSqlBreakdown("SELECT * FROM Table1"),
new MockSqlBreakdown("SELECT * FROM Table2")
};
var collection = new SqlBreakdownCollection(breakdowns);
// Act
var result = collection.GetCombinedSql(true, ";");
// Assert
Assert.That(result, Does.Contain(";"));
Assert.That(result, Does.Not.Contain("GO"));
}
[Test]
public void GetBatchSql_WithRawStatements_CombinesWithGO()
{
// Arrange
var collection = new SqlBreakdownCollection();
collection.ParseBatch(@"
SELECT * FROM Table1
GO
SELECT * FROM Table2
");
// Act
var result = collection.GetBatchSql();
// Assert
Assert.That(result, Does.Contain("Table1"));
Assert.That(result, Does.Contain("GO"));
Assert.That(result, Does.Contain("Table2"));
}
[Test]
public void GetBatchSql_WithEmptyCollection_ReturnsEmpty()
{
// Arrange
var collection = new SqlBreakdownCollection();
// Act
var result = collection.GetBatchSql();
// Assert
Assert.That(result, Is.Empty);
}
[Test]
public void Where_WithValidPredicate_FiltersBreakdowns()
{
// Arrange
var breakdowns = new[]
{
new MockSqlBreakdown("SELECT * FROM Table1"),
new MockSqlBreakdown("SELECT * FROM Table2"),
new MockSqlBreakdown("SELECT * FROM Table3")
};
var collection = new SqlBreakdownCollection(breakdowns);
// Act
var filtered = collection.Where(b => b.ToString()!.Contains("Table2")).ToList();
// Assert
Assert.That(filtered, Has.Count.EqualTo(1));
Assert.That(filtered[0].ToString()!, Does.Contain("Table2"));
}
[Test]
public void Select_WithValidSelector_ProjectsBreakdowns()
{
// Arrange
var breakdowns = new[]
{
new MockSqlBreakdown("SELECT * FROM Table1"),
new MockSqlBreakdown("SELECT * FROM Table2")
};
var collection = new SqlBreakdownCollection(breakdowns);
// Act
var projected = collection.Select(b => b.ToString()!.Length).ToList();
// Assert
Assert.That(projected, Has.Count.EqualTo(2));
Assert.That(projected[0], Is.GreaterThan(0));
}
[Test]
public void GetAt_WithValidIndex_ReturnsBreakdown()
{
// Arrange
var breakdown1 = new MockSqlBreakdown("SELECT * FROM Table1");
var breakdown2 = new MockSqlBreakdown("SELECT * FROM Table2");
var collection = new SqlBreakdownCollection(new[] { breakdown1, breakdown2 });
// Act
var result = collection.GetAt(1);
// Assert
Assert.That(result, Is.EqualTo(breakdown2));
}
[Test]
public void GetAt_WithInvalidIndex_ThrowsArgumentOutOfRangeException()
{
// Arrange
var collection = new SqlBreakdownCollection(new[] { new MockSqlBreakdown("SELECT * FROM Table1") });
// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>(() => collection.GetAt(5));
}
[Test]
public void FirstOrDefault_WithMatchingPredicate_ReturnsBreakdown()
{
// Arrange
var breakdowns = new[]
{
new MockSqlBreakdown("SELECT * FROM Table1"),
new MockSqlBreakdown("SELECT * FROM Table2")
};
var collection = new SqlBreakdownCollection(breakdowns);
// Act
var result = collection.FirstOrDefault(b => b.ToString()!.Contains("Table2"));
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result!.ToString(), Does.Contain("Table2"));
}
[Test]
public void FirstOrDefault_WithNoMatch_ReturnsNull()
{
// Arrange
var collection = new SqlBreakdownCollection(new[] { new MockSqlBreakdown("SELECT * FROM Table1") });
// Act
var result = collection.FirstOrDefault(b => b.ToString()!.Contains("NonExistent"));
// Assert
Assert.That(result, Is.Null);
}
[Test]
public void GetRawStatementAt_WithValidIndex_ReturnsStatement()
{
// Arrange
var collection = new SqlBreakdownCollection();
collection.ParseBatch(@"
SELECT * FROM Table1
GO
SELECT * FROM Table2
");
// Act
var result = collection.GetRawStatementAt(1);
// Assert
Assert.That(result, Does.Contain("Table2"));
}
[Test]
public void GetRawStatementAt_WithInvalidIndex_ThrowsArgumentOutOfRangeException()
{
// Arrange
var collection = new SqlBreakdownCollection();
collection.ParseBatch("SELECT * FROM Table1");
// Act & Assert
Assert.Throws<ArgumentOutOfRangeException>(() => collection.GetRawStatementAt(5));
}
[Test]
public void ToString_ReturnsFormattedSql()
{
// Arrange
var breakdowns = new[]
{
new MockSqlBreakdown("SELECT * FROM Table1"),
new MockSqlBreakdown("SELECT * FROM Table2")
};
var collection = new SqlBreakdownCollection(breakdowns);
// Act
var result = collection.ToString();
// Assert
Assert.That(result, Does.Contain("Table1"));
Assert.That(result, Does.Contain("Table2"));
Assert.That(result, Does.Contain("GO"));
}
}