-
Notifications
You must be signed in to change notification settings - Fork 331
Tests | SqlError, SqlErrorCollection #3869
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
44fd995
Move SqlErrorTest to unit test project
benrr101 773baa1
Start on moving SqlErrorCollectionTests
benrr101 0cc1164
Improve tests for SqlErrorCollection (part 2)
benrr101 db7726d
Fix issue with sqlerrorcollectiontests
benrr101 e9218b8
Remove old SqlErrorCollectionTest
benrr101 3bd4fff
Merge branch 'main' into dev/russellben/tests/sqlerror
benrr101 8cb28e2
comments
benrr101 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 0 additions & 71 deletions
71
src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlErrorTest.cs
This file was deleted.
Oops, something went wrong.
241 changes: 241 additions & 0 deletions
241
...rosoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlErrorCollectionTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.UnitTests.Microsoft.Data.SqlClient | ||
| { | ||
| public class SqlErrorCollectionTests | ||
| { | ||
| private const int ErrorsInTestCollection = 3; | ||
|
|
||
| [Fact] | ||
| public void Constructor_PropertiesInitialized() | ||
| { | ||
| // Act | ||
| SqlErrorCollection collection = new(); | ||
|
|
||
| // Assert | ||
| Assert.Empty(collection); | ||
|
|
||
| // - ICollection properties | ||
| ICollection collection2 = collection; | ||
| Assert.Same(collection2, collection2.SyncRoot); | ||
| Assert.False(collection2.IsSynchronized); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(1)] | ||
| [InlineData(2)] | ||
| [InlineData(10)] | ||
| public void Add(int itemsToAdd) | ||
| { | ||
| // Arrange | ||
| SqlErrorCollection collection = new(); | ||
| SqlError error = GetTestError(); | ||
|
|
||
| // Act | ||
| for (int i = 0; i < itemsToAdd; i++) | ||
| { | ||
| collection.Add(error); | ||
| } | ||
|
|
||
| // Assert | ||
| Assert.Equal(itemsToAdd, collection.Count); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(ErrorsInTestCollection, 0)] // Destination just right size | ||
| [InlineData(ErrorsInTestCollection + 10, 0)] // Null elements at end | ||
| [InlineData(ErrorsInTestCollection + 2, 2)] // Null elements at beginning | ||
| [InlineData(ErrorsInTestCollection + 10, 1)] // Null elements at beginning and end | ||
| public void CopyTo_SqlErrorArray_WithinRange(int destinationSize, int offset) | ||
| { | ||
| // Arrange | ||
| (SqlErrorCollection collection, SqlError[] errors) = GetTestErrorCollection(); | ||
| SqlError[] copyDestination = new SqlError[destinationSize]; | ||
|
|
||
| // Act | ||
| // - Uses SqlErrorCollection.CopyTo | ||
| collection.CopyTo(copyDestination, offset); | ||
|
|
||
| // Assert | ||
| AssertCopiedCollection(errors, copyDestination, offset); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(ErrorsInTestCollection, -1)] // Offset is negative | ||
| [InlineData(ErrorsInTestCollection - 1, 0)] // Destination is too small | ||
| [InlineData(ErrorsInTestCollection, 1)] // Destination is big enough, but offset pushes it over edge | ||
| public void CopyTo_SqlErrorArray_OutOfRange(int destinationSize, int offset) | ||
| { | ||
| // Arrange | ||
| (SqlErrorCollection collection, SqlError[] _) = GetTestErrorCollection(); | ||
| SqlError[] copyDestination = new SqlError[destinationSize]; | ||
|
|
||
| // Act | ||
| // - Uses ICollection.CopyTo | ||
|
benrr101 marked this conversation as resolved.
Outdated
|
||
| Action action = () => collection.CopyTo(copyDestination, offset); | ||
|
|
||
| // Assert | ||
| Assert.ThrowsAny<ArgumentException>(action); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(ErrorsInTestCollection, 0)] // Destination just right size | ||
| [InlineData(ErrorsInTestCollection + 10, 0)] // Null elements at end | ||
| [InlineData(ErrorsInTestCollection + 2, 2)] // Null elements at beginning | ||
| [InlineData(ErrorsInTestCollection + 10, 1)] // Null elements at beginning and end | ||
| public void CopyTo_ObjectArray_WithinRange(int destinationSize, int offset) | ||
| { | ||
| // Arrange | ||
| (SqlErrorCollection collection, SqlError[] errors) = GetTestErrorCollection(); | ||
| object[] copyDestination = new object[destinationSize]; | ||
|
|
||
| // Act | ||
| // - Uses ICollection.CopyTo | ||
| collection.CopyTo(copyDestination, offset); | ||
|
|
||
| // Assert | ||
| AssertCopiedCollection(errors, copyDestination, offset); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(ErrorsInTestCollection, -1)] // Offset is negative | ||
| [InlineData(ErrorsInTestCollection - 1, 0)] // Destination is too small | ||
| [InlineData(ErrorsInTestCollection, 1)] // Destination is big enough, but offset pushes it over edge | ||
| public void CopyTo_ObjectArray_OutOfRange(int destinationSize, int offset) | ||
| { | ||
| // Arrange | ||
| (SqlErrorCollection collection, SqlError[] _) = GetTestErrorCollection(); | ||
| SqlError[] copyDestination = new SqlError[destinationSize]; | ||
|
benrr101 marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Act | ||
| // - Uses ICollection.CopyTo | ||
| Action action = () => collection.CopyTo(copyDestination, offset); | ||
|
|
||
| // Assert | ||
| Assert.ThrowsAny<ArgumentException>(action); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CopyTo_ObjectArray_WrongType() | ||
| { | ||
| // Arrange | ||
| (SqlErrorCollection collection, SqlError[] errors) = GetTestErrorCollection(); | ||
| int[] destination = new int[errors.Length]; | ||
|
|
||
| // Act | ||
| Action action = () => collection.CopyTo(destination, 0); | ||
|
|
||
| // Assert | ||
| Assert.Throws<InvalidCastException>(action); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetEnumerator() | ||
| { | ||
| // Arrange | ||
| (SqlErrorCollection collection, SqlError[] errors) = GetTestErrorCollection(); | ||
| List<SqlError> output = new(); | ||
|
|
||
| // Act | ||
| foreach (SqlError error in collection) | ||
| { | ||
| output.Add(error); | ||
| } | ||
|
|
||
| // Assert | ||
| for (int i = 0; i < errors.Length; i++) | ||
| { | ||
| Assert.Same(errors[i], output[i]); | ||
| } | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(0)] | ||
| [InlineData(1)] | ||
| [InlineData(2)] | ||
| public void Indexer_InRange(int index) | ||
| { | ||
| // Arrange | ||
| (SqlErrorCollection collection, SqlError[] errors) = GetTestErrorCollection(); | ||
|
|
||
| // Act | ||
| SqlError result = collection[index]; | ||
|
|
||
| // Assert | ||
| Assert.Same(errors[index], result); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(-1)] | ||
| [InlineData(123)] | ||
| public void Indexer_OutOfRange(int index) | ||
| { | ||
| // Arrange | ||
| (SqlErrorCollection collection, _) = GetTestErrorCollection(); | ||
|
|
||
| // Act | ||
| Action action = () => _ = collection[index]; | ||
|
|
||
| // Assert | ||
| Assert.Throws<ArgumentOutOfRangeException>(action); | ||
| } | ||
|
|
||
| private static void AssertCopiedCollection(SqlError[] source, IReadOnlyList<object> destination, int offset) | ||
| { | ||
| for (int i = 0; i < destination.Count; i++) | ||
| { | ||
| if (i < offset) | ||
| { | ||
| // - Elements before the offset should be null | ||
| Assert.Null(destination[i]); | ||
| } | ||
| else if (i >= offset && i < source.Length + offset) | ||
| { | ||
| // - Elements after the offset but within the range of original elements should match | ||
| Assert.Same(source[i - offset], destination[i]); | ||
| } | ||
| else | ||
| { | ||
| // - Elements after the offset and original elements should be null | ||
| Assert.Null(destination[i]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static SqlError GetTestError() | ||
| { | ||
| return new SqlError( | ||
| infoNumber: 123, | ||
| errorState: 0x02, | ||
| errorClass: 0x03, | ||
| server: "foo", | ||
| errorMessage: "bar", | ||
| procedure: "baz", | ||
| lineNumber: 234, | ||
| exception: new Exception(), | ||
| batchIndex: 345); | ||
|
benrr101 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| private static (SqlErrorCollection collection, SqlError[] errors) GetTestErrorCollection() | ||
| { | ||
| SqlErrorCollection collection = new(); | ||
| SqlError[] errors = new SqlError[ErrorsInTestCollection]; | ||
|
|
||
| for (int i = 0; i < ErrorsInTestCollection; i++) | ||
| { | ||
| SqlError error = GetTestError(); | ||
| errors[i] = error; | ||
| collection.Add(error); | ||
| } | ||
|
|
||
| return (collection, errors); | ||
| } | ||
| } | ||
| } | ||
56 changes: 56 additions & 0 deletions
56
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlErrorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Runtime.Serialization; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.UnitTests.Microsoft.Data.SqlClient | ||
| { | ||
| public class SqlErrorTests | ||
| { | ||
| [Fact] | ||
| public void SerializationRoundTrip() | ||
| { | ||
| // Arrange | ||
| DataContractSerializer serializer = new(typeof(SqlError)); | ||
| MemoryStream stream = new(); | ||
|
benrr101 marked this conversation as resolved.
Outdated
|
||
|
|
||
| // - Create the test error | ||
| SqlError originalError = new( | ||
| infoNumber: 123, | ||
| errorState: 0x02, | ||
| errorClass: 0x03, | ||
| server: "foo", | ||
| errorMessage: "bar", | ||
| procedure: "baz", | ||
| lineNumber: 234, | ||
| exception: new Exception(), | ||
|
benrr101 marked this conversation as resolved.
|
||
| batchIndex: 345); | ||
|
|
||
| // Act - Serialize and deserialize | ||
| serializer.WriteObject(stream, originalError); | ||
| stream.Position = 0; | ||
| SqlError? actualError = serializer.ReadObject(stream) as SqlError; | ||
|
|
||
| // Assert | ||
| Assert.NotNull(actualError); | ||
| Assert.Equal(originalError.Source, actualError.Source); | ||
| Assert.Equal(originalError.Number, actualError.Number); | ||
| Assert.Equal(originalError.State, actualError.State); | ||
| Assert.Equal(originalError.Class, actualError.Class); | ||
| Assert.Equal(originalError.Server, actualError.Server); | ||
| Assert.Equal(originalError.Message, actualError.Message); | ||
| Assert.Equal(originalError.Procedure, actualError.Procedure); | ||
| Assert.Equal(originalError.LineNumber, actualError.LineNumber); | ||
| Assert.Equal(originalError.Win32ErrorCode, actualError.Win32ErrorCode); | ||
| Assert.Equal(originalError.BatchIndex, actualError.BatchIndex); | ||
|
|
||
| Assert.NotNull(actualError.Exception); | ||
| Assert.Equal(originalError.Exception.Message, actualError.Exception.Message); | ||
| Assert.Equal(originalError.Exception.HResult, actualError.Exception.HResult); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.