Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Validate UpdateDataExpressions: enforce that they either specify AllR…

…ows or a nonempty Where (but not both)
  • Loading branch information...
commit a91e140fcc37124f463ebf5c7b9f7973333131af 1 parent 6924cba
Lauri Kotilainen authored
3  src/FluentMigrator.Tests/FluentMigrator.Tests.csproj
View
@@ -218,6 +218,7 @@
<Compile Include="Unit\Builders\Schema\SchemaExpressionRootTest.cs" />
<Compile Include="Unit\Definitions\IndexDefinitionTests.cs" />
<Compile Include="Unit\Expressions\DeleteDefaultConstraintExpressionTests.cs" />
+ <Compile Include="Unit\Expressions\UpdateDataExpressionTests.cs" />
<Compile Include="Unit\Generators\GenericGenerator\GenericGeneratorTests.cs" />
<Compile Include="Unit\Generators\Firebird\FirebirdGeneratorTests.cs" />
<Compile Include="Unit\Generators\Postgres\PostgresDataTests.cs" />
@@ -384,4 +385,4 @@
<Target Name="AfterBuild">
</Target>
-->
-</Project>
+</Project>
68 src/FluentMigrator.Tests/Unit/Expressions/UpdateDataExpressionTests.cs
View
@@ -0,0 +1,68 @@
+using System.Collections.Generic;
+using FluentMigrator.Expressions;
+using FluentMigrator.Infrastructure;
+using FluentMigrator.Tests.Helpers;
+using NUnit.Framework;
+using NUnit.Should;
+
+namespace FluentMigrator.Tests.Unit.Expressions
+{
+ [TestFixture]
+ public class UpdateDataExpressionTests {
+ private UpdateDataExpression expression;
+
+ [SetUp]
+ public void Initialize()
+ {
+ expression =
+ new UpdateDataExpression()
+ {
+ TableName = "ExampleTable",
+ Set = new List<KeyValuePair<string, object>>
+ {
+ new KeyValuePair<string, object>("Column", "value")
+ },
+ IsAllRows = false
+ };
+ }
+
+ [Test]
+ public void NullUpdateTargetCausesErrorMessage()
+ {
+ // null is the default value, but it might not always be, so I'm codifying it here anyway
+ expression.Where = null;
+
+ var errors = ValidationHelper.CollectErrors(expression);
+ errors.ShouldContain(ErrorMessages.UpdateDataExpressionMustSpecifyWhereClauseOrAllRows);
+ }
+
+ [Test]
+ public void EmptyUpdateTargetCausesErrorMessage()
+ {
+ // The same should be true for an empty list
+ expression.Where = new List<KeyValuePair<string, object>>();
+
+ var errors = ValidationHelper.CollectErrors(expression);
+ errors.ShouldContain(ErrorMessages.UpdateDataExpressionMustSpecifyWhereClauseOrAllRows);
+ }
+
+ [Test]
+ public void DoesNotRequireWhereConditionWhenIsAllRowsIsSet()
+ {
+ expression.IsAllRows = true;
+
+ var errors = ValidationHelper.CollectErrors(expression);
+ errors.ShouldNotContain(ErrorMessages.UpdateDataExpressionMustSpecifyWhereClauseOrAllRows);
+ }
+
+ [Test]
+ public void DoesNotAllowWhereConditionWhenIsAllRowsIsSet()
+ {
+ expression.IsAllRows = true;
+ expression.Where = new List<KeyValuePair<string, object>> {new KeyValuePair<string, object>("key", "value")};
+
+ var errors = ValidationHelper.CollectErrors(expression);
+ errors.ShouldContain(ErrorMessages.UpdateDataExpressionMustNotSpecifyBothWhereClauseAndAllRows);
+ }
+ }
+}
6 src/FluentMigrator/Expressions/UpdateDataExpression.cs
View
@@ -35,6 +35,12 @@ public override void CollectValidationErrors(ICollection<string> errors)
{
if (String.IsNullOrEmpty(TableName))
errors.Add(ErrorMessages.TableNameCannotBeNullOrEmpty);
+
+ if (!IsAllRows && (Where == null || Where.Count == 0))
+ errors.Add(ErrorMessages.UpdateDataExpressionMustSpecifyWhereClauseOrAllRows);
+
+ if (IsAllRows && Where != null && Where.Count > 0)
+ errors.Add(ErrorMessages.UpdateDataExpressionMustNotSpecifyBothWhereClauseAndAllRows);
}
public override void ExecuteWith(IMigrationProcessor processor)
2  src/FluentMigrator/Infrastructure/ErrorMessages.cs
View
@@ -43,5 +43,7 @@ public static class ErrorMessages
public const string OperationCannotBeNull = "The operation to be performed using the database connection cannot be null";
public const string DestinationSchemaCannotBeNull = "The destination schema's name cannot be null or an empty string";
public const string SequenceNameCannotBeNullOrEmpty = "The sequence's name cannot be null or an empty string";
+ public const string UpdateDataExpressionMustSpecifyWhereClauseOrAllRows = "Update statement is missing a condition. Specify one by calling .Where() or target all rows by calling .AllRows().";
+ public const string UpdateDataExpressionMustNotSpecifyBothWhereClauseAndAllRows = "Update statement specifies both a .Where() condition and that .AllRows() should be targeted. Specify one or the other, but not both.";
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.