Skip to content

Commit

Permalink
Prefer new method for argument guarding for string params
Browse files Browse the repository at this point in the history
  • Loading branch information
sjp committed Sep 27, 2024
1 parent 6bac492 commit d958cde
Show file tree
Hide file tree
Showing 171 changed files with 544 additions and 751 deletions.
11 changes: 8 additions & 3 deletions src/SJP.Schematic.Core.Tests/DatabaseCheckConstraintTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ namespace SJP.Schematic.Core.Tests;
[TestFixture]
internal static class DatabaseCheckConstraintTests
{
[TestCase((string)null)]
[Test]
public static void Ctor_GivenNullDefinition_ThrowsArgumentNullException()
{
Assert.That(() => new DatabaseCheckConstraint(Option<Identifier>.Some("test_check"), null!, true), Throws.ArgumentNullException);
}

[TestCase("")]
[TestCase(" ")]
public static void Ctor_GivenNullOrWhiteSpaceDefinition_ThrowsArgumentNullException(string definition)
public static void Ctor_GivenEmptyOrWhiteSpaceDefinition_ThrowsArgumentException(string definition)
{
Assert.That(() => new DatabaseCheckConstraint(Option<Identifier>.Some("test_check"), definition, true), Throws.ArgumentNullException);
Assert.That(() => new DatabaseCheckConstraint(Option<Identifier>.Some("test_check"), definition, true), Throws.ArgumentException);
}

[Test]
Expand Down
13 changes: 10 additions & 3 deletions src/SJP.Schematic.Core.Tests/DatabaseIndexColumnTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ namespace SJP.Schematic.Core.Tests;
[TestFixture]
internal static class DatabaseIndexColumnTests
{
[TestCase((string)null)]
[Test]
public static void Ctor_GivenNullExpression_ThrowsArgumentNullException()
{
var column = Mock.Of<IDatabaseColumn>();

Assert.That(() => new DatabaseIndexColumn(null!, column, IndexColumnOrder.Ascending), Throws.ArgumentNullException);
}

[TestCase("")]
[TestCase(" ")]
public static void Ctor_GivenNullOrWhiteSpaceExpression_ThrowsArgumentNullException(string expression)
public static void Ctor_GivenEmptyOrWhiteSpaceExpression_ThrowsArgumentException(string expression)
{
var column = Mock.Of<IDatabaseColumn>();

Assert.That(() => new DatabaseIndexColumn(expression, column, IndexColumnOrder.Ascending), Throws.ArgumentNullException);
Assert.That(() => new DatabaseIndexColumn(expression, column, IndexColumnOrder.Ascending), Throws.ArgumentException);
}

[Test]
Expand Down
14 changes: 11 additions & 3 deletions src/SJP.Schematic.Core.Tests/DatabaseMaterializedViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ public static void Ctor_GivenNullName_ThrowsArgumentNullException()
Assert.That(() => new DatabaseMaterializedView(null, definition, columns), Throws.ArgumentNullException);
}

[TestCase((string)null)]
[Test]
public static void Ctor_GivenNullDefinition_ThrowsArgumentNullException()
{
Identifier viewName = "test_mat_view";
var columns = new[] { Mock.Of<IDatabaseColumn>() };

Assert.That(() => new DatabaseMaterializedView(viewName, null!, columns), Throws.ArgumentNullException);
}

[TestCase("")]
[TestCase(" ")]
public static void Ctor_GivenNullOrWhiteSpaceDefinition_ThrowsArgumentNullException(string definition)
public static void Ctor_GivenEmptyOrWhiteSpaceDefinition_ThrowsArgumentException(string definition)
{
Identifier viewName = "test_mat_view";
var columns = new[] { Mock.Of<IDatabaseColumn>() };

Assert.That(() => new DatabaseMaterializedView(viewName, definition, columns), Throws.ArgumentNullException);
Assert.That(() => new DatabaseMaterializedView(viewName, definition, columns), Throws.ArgumentException);
}

[Test]
Expand Down
13 changes: 10 additions & 3 deletions src/SJP.Schematic.Core.Tests/DatabaseRoutineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ public static void Ctor_GivenNullName_ThrowsArgumentNullException()
Assert.That(() => new DatabaseRoutine(null, definition), Throws.ArgumentNullException);
}

[TestCase((string)null)]
[Test]
public static void Ctor_GivenNullDefinition_ThrowsArgumentNullException()
{
Identifier routineName = "test_routine";

Assert.That(() => new DatabaseRoutine(routineName, null!), Throws.ArgumentNullException);
}

[TestCase("")]
[TestCase(" ")]
public static void Ctor_GivenNullOrWhiteSpaceDefinition_ThrowsArgumentNullException(string definition)
public static void Ctor_GivenEmptyOrWhiteSpaceDefinition_ThrowsArgumentException(string definition)
{
Identifier routineName = "test_routine";

Assert.That(() => new DatabaseRoutine(routineName, definition), Throws.ArgumentNullException);
Assert.That(() => new DatabaseRoutine(routineName, definition), Throws.ArgumentException);
}

[Test]
Expand Down
15 changes: 12 additions & 3 deletions src/SJP.Schematic.Core.Tests/DatabaseTriggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ public static void Ctor_GivenNullName_ThrowsArgumentNullException()
Assert.That(() => new DatabaseTrigger(null, definition, timing, events, true), Throws.ArgumentNullException);
}

[TestCase((string)null)]
[Test]
public static void Ctor_GivenNullDefinition_ThrowsArgumentNullException()
{
Identifier triggerName = "test_trigger";
const TriggerQueryTiming timing = TriggerQueryTiming.Before;
const TriggerEvent events = TriggerEvent.Update;

Assert.That(() => new DatabaseTrigger(triggerName, null!, timing, events, true), Throws.ArgumentNullException);
}

[TestCase("")]
[TestCase(" ")]
public static void Ctor_GivenNullOrWhiteSpaceDefinition_ThrowsArgumentNullException(string definition)
public static void Ctor_GivenEmptyOrWhiteSpaceDefinition_ThrowsArgumentException(string definition)
{
Identifier triggerName = "test_trigger";
const TriggerQueryTiming timing = TriggerQueryTiming.Before;
const TriggerEvent events = TriggerEvent.Update;

Assert.That(() => new DatabaseTrigger(triggerName, definition, timing, events, true), Throws.ArgumentNullException);
Assert.That(() => new DatabaseTrigger(triggerName, definition, timing, events, true), Throws.ArgumentException);
}

[Test]
Expand Down
14 changes: 11 additions & 3 deletions src/SJP.Schematic.Core.Tests/DatabaseViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ public static void Ctor_GivenNullName_ThrowsArgumentNullException()
Assert.That(() => new DatabaseView(null, definition, columns), Throws.ArgumentNullException);
}

[TestCase((string)null)]
[Test]
public static void Ctor_GivenNullDefinition_ThrowsArgumentNullException()
{
Identifier viewName = "test_view";
var columns = new[] { Mock.Of<IDatabaseColumn>() };

Assert.That(() => new DatabaseView(viewName, null!, columns), Throws.ArgumentNullException);
}

[TestCase("")]
[TestCase(" ")]
public static void Ctor_GivenNullOrWhiteSpaceDefinition_ThrowsArgumentNullException(string definition)
public static void Ctor_GivenEmptyOrWhiteSpaceDefinition_ThrowsArgumentException(string definition)
{
Identifier viewName = "test_view";
var columns = new[] { Mock.Of<IDatabaseColumn>() };

Assert.That(() => new DatabaseView(viewName, definition, columns), Throws.ArgumentNullException);
Assert.That(() => new DatabaseView(viewName, definition, columns), Throws.ArgumentException);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using System;
using System.Threading;
using Moq;
using NUnit.Framework;
using SJP.Schematic.Core.Extensions;
Expand All @@ -17,11 +18,11 @@ public static void QueryAsync_WithoutParamsGivenNullConnection_ThrowsArgumentNul
[TestCase((string)null)]
[TestCase("")]
[TestCase(" ")]
public static void QueryAsync_WithoutParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentNullException(string sql)
public static void QueryAsync_WithoutParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentException(string sql)
{
var connection = Mock.Of<IDbConnectionFactory>();

Assert.That(() => connection.QueryAsync<string>(sql, CancellationToken.None), Throws.ArgumentNullException);
Assert.That(() => connection.QueryAsync<string>(sql, CancellationToken.None), Throws.InstanceOf<ArgumentException>());
}

[Test]
Expand All @@ -35,12 +36,12 @@ public static void QueryAsync_WithParamsGivenNullConnection_ThrowsArgumentNullEx
[TestCase((string)null)]
[TestCase("")]
[TestCase(" ")]
public static void QueryAsync_WithParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentNullException(string sql)
public static void QueryAsync_WithParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentException(string sql)
{
var connection = Mock.Of<IDbConnectionFactory>();
var param = new TestQuery { Test = "test" };

Assert.That(() => connection.QueryAsync(sql, param, CancellationToken.None), Throws.ArgumentNullException);
Assert.That(() => connection.QueryAsync(sql, param, CancellationToken.None), Throws.InstanceOf<ArgumentException>());
}

[Test]
Expand All @@ -60,11 +61,11 @@ public static void QueryEnumerableAsync_WithoutParamsGivenNullConnection_ThrowsA
[TestCase((string)null)]
[TestCase("")]
[TestCase(" ")]
public static void QueryEnumerableAsync_WithoutParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentNullException(string sql)
public static void QueryEnumerableAsync_WithoutParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentException(string sql)
{
var connection = Mock.Of<IDbConnectionFactory>();

Assert.That(() => connection.QueryEnumerableAsync<string>(sql, CancellationToken.None), Throws.ArgumentNullException);
Assert.That(() => connection.QueryEnumerableAsync<string>(sql, CancellationToken.None), Throws.InstanceOf<ArgumentException>());
}

[Test]
Expand All @@ -78,12 +79,12 @@ public static void QueryEnumerableAsync_WithParamsGivenNullConnection_ThrowsArgu
[TestCase((string)null)]
[TestCase("")]
[TestCase(" ")]
public static void QueryEnumerableAsync_WithParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentNullException(string sql)
public static void QueryEnumerableAsync_WithParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentException(string sql)
{
var connection = Mock.Of<IDbConnectionFactory>();
var param = new TestQuery { Test = "test" };

Assert.That(() => connection.QueryEnumerableAsync(sql, param, CancellationToken.None), Throws.ArgumentNullException);
Assert.That(() => connection.QueryEnumerableAsync(sql, param, CancellationToken.None), Throws.InstanceOf<ArgumentException>());
}

[Test]
Expand All @@ -103,11 +104,11 @@ public static void ExecuteScalarAsync_WithoutParamsGivenNullConnection_ThrowsArg
[TestCase((string)null)]
[TestCase("")]
[TestCase(" ")]
public static void ExecuteScalarAsync_WithoutParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentNullException(string sql)
public static void ExecuteScalarAsync_WithoutParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentException(string sql)
{
var connection = Mock.Of<IDbConnectionFactory>();

Assert.That(() => connection.ExecuteScalarAsync<string>(sql, CancellationToken.None), Throws.ArgumentNullException);
Assert.That(() => connection.ExecuteScalarAsync<string>(sql, CancellationToken.None), Throws.InstanceOf<ArgumentException>());
}

[Test]
Expand All @@ -121,12 +122,12 @@ public static void ExecuteScalarAsync_WithParamsGivenNullConnection_ThrowsArgume
[TestCase((string)null)]
[TestCase("")]
[TestCase(" ")]
public static void ExecuteScalarAsync_WithParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentNullException(string sql)
public static void ExecuteScalarAsync_WithParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentException(string sql)
{
var connection = Mock.Of<IDbConnectionFactory>();
var param = new TestQuery { Test = "test" };

Assert.That(() => connection.ExecuteScalarAsync(sql, param, CancellationToken.None), Throws.ArgumentNullException);
Assert.That(() => connection.ExecuteScalarAsync(sql, param, CancellationToken.None), Throws.InstanceOf<ArgumentException>());
}

[Test]
Expand All @@ -146,11 +147,11 @@ public static void ExecuteAsync_WithoutParamsGivenNullConnection_ThrowsArgumentN
[TestCase((string)null)]
[TestCase("")]
[TestCase(" ")]
public static void ExecuteAsync_WithoutParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentNullException(string sql)
public static void ExecuteAsync_WithoutParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentException(string sql)
{
var connection = Mock.Of<IDbConnectionFactory>();

Assert.That(() => connection.ExecuteAsync(sql, CancellationToken.None), Throws.ArgumentNullException);
Assert.That(() => connection.ExecuteAsync(sql, CancellationToken.None), Throws.InstanceOf<ArgumentException>());
}

[Test]
Expand All @@ -164,12 +165,12 @@ public static void ExecuteAsync_WithParamsGivenNullConnection_ThrowsArgumentNull
[TestCase((string)null)]
[TestCase("")]
[TestCase(" ")]
public static void ExecuteAsync_WithParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentNullException(string sql)
public static void ExecuteAsync_WithParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentException(string sql)
{
var connection = Mock.Of<IDbConnectionFactory>();
var param = new { Test = "test" };

Assert.That(() => connection.ExecuteAsync(sql, param, CancellationToken.None), Throws.ArgumentNullException);
Assert.That(() => connection.ExecuteAsync(sql, param, CancellationToken.None), Throws.InstanceOf<ArgumentException>());
}

[Test]
Expand All @@ -189,11 +190,11 @@ public static void QueryFirstOrNone_WithoutParamsGivenNullConnection_ThrowsArgum
[TestCase((string)null)]
[TestCase("")]
[TestCase(" ")]
public static void QueryFirstOrNone_WithoutParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentNullException(string sql)
public static void QueryFirstOrNone_WithoutParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentException(string sql)
{
var connection = Mock.Of<IDbConnectionFactory>();

Assert.That(() => connection.QueryFirstOrNone<string>(sql, CancellationToken.None), Throws.ArgumentNullException);
Assert.That(() => connection.QueryFirstOrNone<string>(sql, CancellationToken.None), Throws.InstanceOf<ArgumentException>());
}

[Test]
Expand All @@ -207,12 +208,12 @@ public static void QueryFirstOrNone_WithParamsGivenNullConnection_ThrowsArgument
[TestCase((string)null)]
[TestCase("")]
[TestCase(" ")]
public static void QueryFirstOrNone_WithParamsGivenNullSql_ThrowsArgumentNullException(string sql)
public static void QueryFirstOrNone_WithParamsGivenNullSql_ThrowsArgumentException(string sql)
{
var connection = Mock.Of<IDbConnectionFactory>();
var param = new TestQuery { Test = "test" };

Assert.That(() => connection.QueryFirstOrNone(sql, param, CancellationToken.None), Throws.ArgumentNullException);
Assert.That(() => connection.QueryFirstOrNone(sql, param, CancellationToken.None), Throws.InstanceOf<ArgumentException>());
}

[Test]
Expand All @@ -232,11 +233,11 @@ public static void QuerySingleAsync_WithoutParamsGivenNullConnection_ThrowsArgum
[TestCase((string)null)]
[TestCase("")]
[TestCase(" ")]
public static void QuerySingleAsync_WithoutParamsGivenNullSql_ThrowsArgumentNullException(string sql)
public static void QuerySingleAsync_WithoutParamsGivenNullSql_ThrowsArgumentException(string sql)
{
var connection = Mock.Of<IDbConnectionFactory>();

Assert.That(() => connection.QuerySingleAsync<string>(sql, CancellationToken.None), Throws.ArgumentNullException);
Assert.That(() => connection.QuerySingleAsync<string>(sql, CancellationToken.None), Throws.InstanceOf<ArgumentException>());
}

[Test]
Expand All @@ -250,12 +251,12 @@ public static void QuerySingleAsync_WithParamsGivenNullConnection_ThrowsArgument
[TestCase((string)null)]
[TestCase("")]
[TestCase(" ")]
public static void QuerySingleAsync_WithParamsGivenNullSql_ThrowsArgumentNullException(string sql)
public static void QuerySingleAsync_WithParamsGivenNullSql_ThrowsArgumentException(string sql)
{
var connection = Mock.Of<IDbConnectionFactory>();
var param = new TestQuery { Test = "test" };

Assert.That(() => connection.QuerySingleAsync(sql, param, CancellationToken.None), Throws.ArgumentNullException);
Assert.That(() => connection.QuerySingleAsync(sql, param, CancellationToken.None), Throws.InstanceOf<ArgumentException>());
}

[Test]
Expand All @@ -275,11 +276,11 @@ public static void QuerySingleOrNone_WithoutParamsGivenNullConnection_ThrowsArgu
[TestCase((string)null)]
[TestCase("")]
[TestCase(" ")]
public static void QuerySingleOrNone_WithoutParamsGivenNullSql_ThrowsArgumentNullException(string sql)
public static void QuerySingleOrNone_WithoutParamsGivenNullSql_ThrowsArgumentException(string sql)
{
var connection = Mock.Of<IDbConnectionFactory>();

Assert.That(() => connection.QuerySingleOrNone<string>(sql, CancellationToken.None), Throws.ArgumentNullException);
Assert.That(() => connection.QuerySingleOrNone<string>(sql, CancellationToken.None), Throws.InstanceOf<ArgumentException>());
}

[Test]
Expand All @@ -293,12 +294,12 @@ public static void QuerySingleOrNone_WithParamsGivenNullConnection_ThrowsArgumen
[TestCase((string)null)]
[TestCase("")]
[TestCase(" ")]
public static void QuerySingleOrNone_WithParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentNullException(string sql)
public static void QuerySingleOrNone_WithParamsGivenNullOrWhiteSpaceSql_ThrowsArgumentException(string sql)
{
var connection = Mock.Of<IDbConnectionFactory>();
var param = new TestQuery { Test = "test" };

Assert.That(() => connection.QuerySingleOrNone(sql, param, CancellationToken.None), Throws.ArgumentNullException);
Assert.That(() => connection.QuerySingleOrNone(sql, param, CancellationToken.None), Throws.InstanceOf<ArgumentException>());
}

[Test]
Expand Down
Loading

0 comments on commit d958cde

Please sign in to comment.