public async Task UnderpostingTest() { // Arrange JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService()); MemoryStream stream = new MemoryStream(); EntityFrameworkMaterializer materializer = new EntityFrameworkMaterializer(context); string underpost = @"{""posts"":{""id"":""" + p.Id.ToString() + @""",""title"":""Not at all linkbait!""}}"; stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(underpost)); int previousCommentsCount = p.Comments.Count; // Act Post pUpdated; pUpdated = (Post)await formatter.ReadFromStreamAsync(typeof(Post), stream, (System.Net.Http.HttpContent) null, (System.Net.Http.Formatting.IFormatterLogger) null); pUpdated = await materializer.MaterializeUpdateAsync <Post>(pUpdated); // Assert Assert.AreEqual(previousCommentsCount, pUpdated.Comments.Count, "Comments were wiped out!"); Assert.AreEqual("Not at all linkbait!", pUpdated.Title, "Title was not updated."); }
public async Task DeserializePostIntegrationTest() { // Arrange JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService()); MemoryStream stream = new MemoryStream(); EntityFrameworkMaterializer materializer = new EntityFrameworkMaterializer(context); // Serialize a post and change the JSON... Not "unit" at all, I know, but a good integration test I think... formatter.WriteToStreamAsync(typeof(Post), p, stream, (System.Net.Http.HttpContent)null, (System.Net.TransportContext)null); string serializedPost = System.Text.Encoding.ASCII.GetString(stream.ToArray()); // Change the post title (a scalar value) serializedPost = serializedPost.Replace("Linkbait!", "Not at all linkbait!"); // Remove a comment (Note that order is undefined/not deterministic!) serializedPost = Regex.Replace(serializedPost, String.Format(@"(""comments""\s*:\s*\[[^]]*)(,""{0}""|""{0}"",)", c3.Id), @"$1"); // Reread the serialized JSON... stream.Dispose(); stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(serializedPost)); // Act Post pUpdated; pUpdated = (Post)await formatter.ReadFromStreamAsync(typeof(Post), stream, (System.Net.Http.HttpContent) null, (System.Net.Http.Formatting.IFormatterLogger) null); pUpdated = await materializer.MaterializeUpdateAsync <Post>(pUpdated); // Assert Assert.AreEqual(a, pUpdated.Author); Assert.AreEqual("Not at all linkbait!", pUpdated.Title); Assert.AreEqual(2, pUpdated.Comments.Count()); Assert.IsFalse(pUpdated.Comments.Contains(c3)); //Debug.WriteLine(sw.ToString()); }
protected override JSONAPI.Core.IMaterializer MaterializerFactory() { if (_materializer == null) { DbContext context = (DbContext)Activator.CreateInstance(typeof(TC)); _materializer = new JSONAPI.EntityFramework.EntityFrameworkMaterializer(context); } return(_materializer); }
protected override void Dispose(bool disposing) { //FIXME: Unsure what to do with the "disposing" parameter here...what does it mean?? if (_materializer != null) { _materializer.DbContext.Dispose(); } _materializer = null; base.Dispose(disposing); }
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"); }
public void GetKeyNamesNotAnEntityTest() { // Arrange var materializer = new EntityFrameworkMaterializer(context); // Act Action action = () => { materializer.GetKeyNames(typeof(NotAnEntity)); }; action.ShouldThrow <ArgumentException>().Which.Message.Should().Be("The Type NotAnEntity was not found in the DbContext with Type TestDbContext"); }
public async Task Includes_many_to_many_navigation_properties_that_were_serialized() { using (var conn = TestHelpers.GetEffortConnection("Acceptance/Data")) { using (var context = new TestDbContext(conn, false)) { var metadataManager = new Mock<IMetadataManager>(); var materializer = new EntityFrameworkMaterializer(context, metadataManager.Object); var ephemeral = new Post { Id = "201" }; var material = await materializer.MaterializeAsync(ephemeral); material.Tags.Should().NotBeNull(); material.Tags.Count.Should().Be(2); } } }