-
Notifications
You must be signed in to change notification settings - Fork 23
EF materializer nonstandard id bug #52
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 all commits
Commits
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
88 changes: 88 additions & 0 deletions
88
JSONAPI.EntityFramework.Tests/EntityFrameworkMaterializerTests.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,88 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using JSONAPI.EntityFramework.Tests.Models; | ||
| using FluentAssertions; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace JSONAPI.EntityFramework.Tests | ||
| { | ||
| [TestClass] | ||
| public class EntityFrameworkMaterializerTests | ||
| { | ||
| private class NotAnEntity | ||
| { | ||
| public string Id { get; set; } | ||
| public string Temporary { get; set; } | ||
| } | ||
|
|
||
| private TestEntities context; | ||
| private Backlink b1, b2; | ||
|
|
||
| [TestInitialize] | ||
| public void SetupEntities() | ||
| { | ||
| //- See http://stackoverflow.com/a/19130718/489116 | ||
| var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance; | ||
| //- | ||
|
|
||
| context = new TestEntities(); | ||
| //JSONAPI.EntityFramework.Json.ContractResolver.ObjectContext = context; | ||
|
|
||
|
|
||
| // Clear it out! | ||
| foreach (Backlink o in context.Backlinks) context.Backlinks.Remove(o); | ||
| context.SaveChanges(); | ||
|
|
||
| b1 = new Backlink | ||
| { | ||
| Url = "http://www.google.com/", | ||
| Snippet = "1 Results" | ||
| }; | ||
|
|
||
| context.SaveChanges(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetKeyNamesStandardIdTest() | ||
| { | ||
| // Arrange | ||
| var materializer = new EntityFrameworkMaterializer(context); | ||
|
|
||
| // Act | ||
| IEnumerable<string> keyNames = materializer.GetKeyNames(typeof(Post)); | ||
|
|
||
| // Assert | ||
| keyNames.Count().Should().Be(1); | ||
| keyNames.First().Should().Be("Id"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetKeyNamesNonStandardIdTest() | ||
| { | ||
| // Arrange | ||
| var materializer = new EntityFrameworkMaterializer(context); | ||
|
|
||
| // Act | ||
| IEnumerable<string> keyNames = materializer.GetKeyNames(typeof(Backlink)); | ||
|
|
||
| // Assert | ||
| keyNames.Count().Should().Be(1); | ||
| keyNames.First().Should().Be("Url"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [ExpectedException(typeof(System.ArgumentException))] | ||
| public void GetKeyNamesNotAnEntityTest() | ||
| { | ||
| // Arrange | ||
| var materializer = new EntityFrameworkMaterializer(context); | ||
|
|
||
| // Act | ||
| IEnumerable<string> keyNames = materializer.GetKeyNames(typeof(NotAnEntity)); | ||
|
|
||
| // Assert | ||
| Assert.Fail("A System.ArgumentException should be thrown, this assertion should be unreachable!"); | ||
| } | ||
| } | ||
| } | ||
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
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,18 @@ | ||
| using JSONAPI.Attributes; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
|
|
||
| namespace JSONAPI.EntityFramework.Tests.Models | ||
| { | ||
| public class Backlink | ||
| { | ||
| [UseAsId] | ||
| [System.ComponentModel.DataAnnotations.Key] | ||
| public string Url { get; set; } | ||
|
|
||
| public Post Post { get; set; } | ||
| public string Snippet { get; set; } | ||
| } | ||
| } |
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like FluentAssertions for this sort of thing. You can do:
which I think is clearer than adding an attribute on the method. You can also add additional checks against the exception object itself, which you can't do with the MSTest approach.