|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 4 | +using JSONAPI.EntityFramework.Tests.Models; |
| 5 | +using FluentAssertions; |
| 6 | +using System.Collections.Generic; |
| 7 | + |
| 8 | +namespace JSONAPI.EntityFramework.Tests |
| 9 | +{ |
| 10 | + [TestClass] |
| 11 | + public class EntityFrameworkMaterializerTests |
| 12 | + { |
| 13 | + private class NotAnEntity |
| 14 | + { |
| 15 | + public string Id { get; set; } |
| 16 | + public string Temporary { get; set; } |
| 17 | + } |
| 18 | + |
| 19 | + private TestEntities context; |
| 20 | + private Backlink b1, b2; |
| 21 | + |
| 22 | + [TestInitialize] |
| 23 | + public void SetupEntities() |
| 24 | + { |
| 25 | + //- See http://stackoverflow.com/a/19130718/489116 |
| 26 | + var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance; |
| 27 | + //- |
| 28 | + |
| 29 | + context = new TestEntities(); |
| 30 | + //JSONAPI.EntityFramework.Json.ContractResolver.ObjectContext = context; |
| 31 | + |
| 32 | + |
| 33 | + // Clear it out! |
| 34 | + foreach (Backlink o in context.Backlinks) context.Backlinks.Remove(o); |
| 35 | + context.SaveChanges(); |
| 36 | + |
| 37 | + b1 = new Backlink |
| 38 | + { |
| 39 | + Url = "http://www.google.com/", |
| 40 | + Snippet = "1 Results" |
| 41 | + }; |
| 42 | + |
| 43 | + context.SaveChanges(); |
| 44 | + } |
| 45 | + |
| 46 | + [TestMethod] |
| 47 | + public void GetKeyNamesStandardIdTest() |
| 48 | + { |
| 49 | + // Arrange |
| 50 | + var materializer = new EntityFrameworkMaterializer(context); |
| 51 | + |
| 52 | + // Act |
| 53 | + IEnumerable<string> keyNames = materializer.GetKeyNames(typeof(Post)); |
| 54 | + |
| 55 | + // Assert |
| 56 | + keyNames.Count().Should().Be(1); |
| 57 | + keyNames.First().Should().Be("Id"); |
| 58 | + } |
| 59 | + |
| 60 | + [TestMethod] |
| 61 | + public void GetKeyNamesNonStandardIdTest() |
| 62 | + { |
| 63 | + // Arrange |
| 64 | + var materializer = new EntityFrameworkMaterializer(context); |
| 65 | + |
| 66 | + // Act |
| 67 | + IEnumerable<string> keyNames = materializer.GetKeyNames(typeof(Backlink)); |
| 68 | + |
| 69 | + // Assert |
| 70 | + keyNames.Count().Should().Be(1); |
| 71 | + keyNames.First().Should().Be("Url"); |
| 72 | + } |
| 73 | + |
| 74 | + [TestMethod] |
| 75 | + [ExpectedException(typeof(System.ArgumentException))] |
| 76 | + public void GetKeyNamesNotAnEntityTest() |
| 77 | + { |
| 78 | + // Arrange |
| 79 | + var materializer = new EntityFrameworkMaterializer(context); |
| 80 | + |
| 81 | + // Act |
| 82 | + IEnumerable<string> keyNames = materializer.GetKeyNames(typeof(NotAnEntity)); |
| 83 | + |
| 84 | + // Assert |
| 85 | + Assert.Fail("A System.ArgumentException should be thrown, this assertion should be unreachable!"); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments