using Strata.SqlTools.SqlBreakdown.Classes;
using Strata.SqlTools.SqlBreakdown.Interfaces;
namespace Strata.SqlTools.SqlBreakdown.Tests.SqlBreakdownCollectionTests;
///
/// Unit tests for the SqlBreakdownCollection core functionality.
///
[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
{
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(() => collection.Add(null!));
}
[Test]
public void AddRange_WithMultipleBreakdowns_AddsAllItems()
{
// Arrange
var collection = new SqlBreakdownCollection();
var breakdowns = new List
{
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(() => 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(() => 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(() => 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(() => 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"));
}
}