Skip to content

Commit e586c14

Browse files
authored
Changed OltActionRule null dbTransaction check (#104)
* Changed OltActionRule null dbTransaction check closes #103 * add unit test for OltRuleMissingTransactionException * fixed code smell
1 parent cd31e12 commit e586c14

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

src/OLT.Core.Rules/RuleBuilders/OltActionRule.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,13 @@ public abstract class OltActionRule<T> : OltRule, IOltActionRule
1616
protected Dictionary<string, OltDependentRule> DependentRules = new Dictionary<string, OltDependentRule>();
1717

1818

19-
protected virtual bool HasTransaction { get; set; }
2019
protected abstract Task RunRuleAsync();
2120

2221
#region [ Execute ]
2322

2423
public virtual Task<List<OltRuleCanRunException>> CanExecuteAsync()
2524
{
2625
var list = new List<OltRuleCanRunException>();
27-
if (!HasTransaction)
28-
{
29-
list.Add(new OltRuleMissingTransactionException(this));
30-
}
3126
return Task.FromResult(list);
3227
}
3328

@@ -83,9 +78,12 @@ public virtual Task ExecuteAsync<TContext>(TContext context)
8378
/// <exception cref="OltRuleCanRunException"></exception>
8479
public virtual async Task ExecuteAsync(IDbContextTransaction dbTransaction)
8580
{
86-
HasTransaction = dbTransaction != null;
8781

8882
var errors = await CanExecuteAsync();
83+
if (dbTransaction == null)
84+
{
85+
errors.Add(new OltRuleMissingTransactionException(this));
86+
}
8987
if (errors.Any())
9088
{
9189
throw new AggregateException(errors);

tests/OLT.Core.Rules.Tests/ExceptionTests.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ public class ExceptionTests : BaseUnitTests
1313
{
1414

1515
[Fact]
16-
public void MissingTransactionExceptionTest()
16+
public async Task MissingTransactionExceptionTest()
1717
{
1818
var rule1 = new Test1Rule();
1919
var ex = new OltRuleMissingTransactionException(rule1);
2020
var expectedMsg = $"{rule1.RuleName} requires {nameof(IDbContextTransaction)}";
21-
Assert.Equal(expectedMsg, ex.Message); }
21+
Assert.Equal(expectedMsg, ex.Message);
22+
23+
Func<Task> func = () => rule1.ExecuteAsync(null);
24+
(await func.Should().ThrowAsync<AggregateException>()).WithInnerException<OltRuleMissingTransactionException>();
25+
}
26+
27+
28+
2229

2330
[Fact]
2431
public async Task MissingServiceExceptionTest()

0 commit comments

Comments
 (0)