-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from dpaquette/ExceptionHandling
Exception handling
- Loading branch information
Showing
10 changed files
with
158 additions
and
14 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 5 additions & 0 deletions
5
src/EntityFramework.Seeder.EF6.Tests/CountriesFileWithErrors.csv
This file contains 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,5 @@ | ||
CodeNotRight,NameAlsoNotRight | ||
AT,Austria | ||
AU,Australia | ||
CA,Canada | ||
US,United States |
This file contains 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
74 changes: 74 additions & 0 deletions
74
src/EntityFramework.Seeder.EF6.Tests/when_an_error_occurs_while_seeding_a_dbset.cs
This file contains 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,74 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using EntityFramework.Seeder.EF6.Tests.Domain; | ||
using Given.Common; | ||
using Given.NUnit; | ||
using NUnit.Framework; | ||
|
||
namespace EntityFramework.Seeder.EF6.Tests | ||
{ | ||
[Story(AsA = "developer", | ||
IWant = "see exception details", | ||
SoThat = "I can diagnose errors that might happen while seeding a dbset")] | ||
public class while_an_error_occurs_while_seeding_a_dbset : Scenario | ||
{ | ||
public static CountryContext _context; | ||
|
||
given an_empty_context = () => | ||
{ | ||
_context = new CountryContext(); | ||
_context.DeleteAll(); | ||
}; | ||
|
||
|
||
[then] | ||
[ExpectedException(typeof(Exception))] | ||
public void when_seeding_from_a_file_with_unmapped_columns() | ||
{ | ||
_context.Countries.SeedFromFile("CountriesFileWithErrors.csv", c => c.Code); | ||
} | ||
|
||
[then] | ||
public void the_exception_should_be_serializable() | ||
{ | ||
Exception exception = null; | ||
try | ||
{ | ||
_context.Countries.SeedFromFile("CountriesFileWithErrors.csv", c => c.Code); | ||
} | ||
catch (Exception ex) | ||
{ | ||
exception = ex; | ||
} | ||
|
||
Assert.IsNotNull(exception); | ||
|
||
string exceptionToString = exception.ToString(); | ||
|
||
// Round-trip the exception: Serialize and de-serialize with a BinaryFormatter | ||
BinaryFormatter bf = new BinaryFormatter(); | ||
using (MemoryStream ms = new MemoryStream()) | ||
{ | ||
// "Save" object state | ||
bf.Serialize(ms, exception); | ||
|
||
// Re-use the same stream for de-serialization | ||
ms.Seek(0, 0); | ||
|
||
// Replace the original exception with de-serialized one | ||
exception = (Exception)bf.Deserialize(ms); | ||
} | ||
|
||
// Double-check that the exception message and stack trace (owned by the base Exception) are preserved | ||
Assert.AreEqual(exceptionToString, exception.ToString(), "ex.ToString()"); | ||
} | ||
|
||
[TestFixtureTearDown] | ||
public void TearDown() | ||
{ | ||
_context.Dispose(); | ||
} | ||
} | ||
} |
This file contains 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,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.Serialization; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace EntityFramework.Seeder | ||
{ | ||
/// <summary> | ||
/// Represents an exception that has occurred while seeding a dbset. | ||
/// Wraps the underlying exception and is serializable so that the exception message can be | ||
/// properly displayed in the package manager console in Visual Studio | ||
/// </summary> | ||
[Serializable] | ||
public class EfSeederException : Exception, ISerializable | ||
{ | ||
public EfSeederException() | ||
{ | ||
} | ||
|
||
public EfSeederException(string message) : base(message) | ||
{ | ||
} | ||
|
||
public EfSeederException(string message, Exception innerException) : base(message, innerException) | ||
{ | ||
} | ||
|
||
protected EfSeederException(SerializationInfo info, StreamingContext context) : base(info, context) | ||
{ | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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