|
| 1 | +using System.Data.Common; |
| 2 | +using DbExceptionClassifier.Common; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace EntityFramework.Exceptions.Tests; |
| 6 | + |
| 7 | +public class CompositeExceptionClassifierTests |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void ReturnsTrueWhenAnyClassifierMatches() |
| 11 | + { |
| 12 | + var classifier = new CompositeExceptionClassifier( |
| 13 | + new StubClassifier(), |
| 14 | + new StubClassifier(uniqueConstraint: true) |
| 15 | + ); |
| 16 | + |
| 17 | + Assert.True(classifier.IsUniqueConstraintError(new StubDbException())); |
| 18 | + } |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public void ReturnsFalseWhenNoClassifierMatches() |
| 22 | + { |
| 23 | + var classifier = new CompositeExceptionClassifier( |
| 24 | + new StubClassifier(), |
| 25 | + new StubClassifier() |
| 26 | + ); |
| 27 | + |
| 28 | + Assert.False(classifier.IsUniqueConstraintError(new StubDbException())); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void ReturnsFalseWhenEmpty() |
| 33 | + { |
| 34 | + var classifier = new CompositeExceptionClassifier(); |
| 35 | + |
| 36 | + Assert.False(classifier.IsUniqueConstraintError(new StubDbException())); |
| 37 | + Assert.False(classifier.IsReferenceConstraintError(new StubDbException())); |
| 38 | + Assert.False(classifier.IsCannotInsertNullError(new StubDbException())); |
| 39 | + Assert.False(classifier.IsMaxLengthExceededError(new StubDbException())); |
| 40 | + Assert.False(classifier.IsNumericOverflowError(new StubDbException())); |
| 41 | + Assert.False(classifier.IsDeadlockError(new StubDbException())); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void DelegatesAllMethodsCorrectly() |
| 46 | + { |
| 47 | + var classifier = new CompositeExceptionClassifier(new StubClassifier( |
| 48 | + uniqueConstraint: true, |
| 49 | + referenceConstraint: true, |
| 50 | + cannotInsertNull: true, |
| 51 | + maxLengthExceeded: true, |
| 52 | + numericOverflow: true, |
| 53 | + deadlock: true |
| 54 | + )); |
| 55 | + |
| 56 | + var exception = new StubDbException(); |
| 57 | + |
| 58 | + Assert.True(classifier.IsUniqueConstraintError(exception)); |
| 59 | + Assert.True(classifier.IsReferenceConstraintError(exception)); |
| 60 | + Assert.True(classifier.IsCannotInsertNullError(exception)); |
| 61 | + Assert.True(classifier.IsMaxLengthExceededError(exception)); |
| 62 | + Assert.True(classifier.IsNumericOverflowError(exception)); |
| 63 | + Assert.True(classifier.IsDeadlockError(exception)); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void MatchesCorrectErrorTypeAcrossClassifiers() |
| 68 | + { |
| 69 | + var classifier = new CompositeExceptionClassifier( |
| 70 | + new StubClassifier(uniqueConstraint: true), |
| 71 | + new StubClassifier(referenceConstraint: true) |
| 72 | + ); |
| 73 | + |
| 74 | + var exception = new StubDbException(); |
| 75 | + |
| 76 | + Assert.True(classifier.IsUniqueConstraintError(exception)); |
| 77 | + Assert.True(classifier.IsReferenceConstraintError(exception)); |
| 78 | + Assert.False(classifier.IsDeadlockError(exception)); |
| 79 | + } |
| 80 | + |
| 81 | + private class StubDbException : DbException; |
| 82 | + |
| 83 | + private class StubClassifier( |
| 84 | + bool uniqueConstraint = false, |
| 85 | + bool referenceConstraint = false, |
| 86 | + bool cannotInsertNull = false, |
| 87 | + bool maxLengthExceeded = false, |
| 88 | + bool numericOverflow = false, |
| 89 | + bool deadlock = false) : IDbExceptionClassifier |
| 90 | + { |
| 91 | + public bool IsUniqueConstraintError(DbException exception) => uniqueConstraint; |
| 92 | + public bool IsReferenceConstraintError(DbException exception) => referenceConstraint; |
| 93 | + public bool IsCannotInsertNullError(DbException exception) => cannotInsertNull; |
| 94 | + public bool IsMaxLengthExceededError(DbException exception) => maxLengthExceeded; |
| 95 | + public bool IsNumericOverflowError(DbException exception) => numericOverflow; |
| 96 | + public bool IsDeadlockError(DbException exception) => deadlock; |
| 97 | + } |
| 98 | +} |
0 commit comments