Skip to content

Commit

Permalink
Move JSON serialization tests out of query tests (#21594)
Browse files Browse the repository at this point in the history
Fixes #21538

Also add regression test for serialization of proxies

Fixes #19403
  • Loading branch information
ajcvickers authored Jul 15, 2020
1 parent 6334381 commit ca2a793
Show file tree
Hide file tree
Showing 8 changed files with 349 additions and 247 deletions.
13 changes: 13 additions & 0 deletions test/EFCore.InMemory.FunctionalTests/SerializationInMemoryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.EntityFrameworkCore
{
public class SerializationInMemoryTest : SerializationTestBase<F1InMemoryFixture>
{
public SerializationInMemoryTest(F1InMemoryFixture fixture)
: base(fixture)
{
}
}
}
152 changes: 152 additions & 0 deletions test/EFCore.Specification.Tests/LazyLoadProxyTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
Expand Down Expand Up @@ -2030,6 +2033,155 @@ public virtual async Task Load_collection(EntityState state, bool async)
Assert.Equal(3, context.ChangeTracker.Entries().Count());
}

[ConditionalFact]
public virtual void Can_serialize_proxies_to_JSON()
{
using var context = CreateContext(lazyLoadingEnabled: true);

var blogs = context.Set<Blog>().OrderBy(e => e.Host.HostName).ToList();

VerifyBlogs(blogs);
foreach (var blog in blogs)
{
Assert.IsNotType<Blog>(blog);
}

var serialized = Newtonsoft.Json.JsonConvert.SerializeObject(
blogs, new Newtonsoft.Json.JsonSerializerSettings
{
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
Formatting = Newtonsoft.Json.Formatting.Indented
});

Assert.Equal(@"[
{
""Writer"": {
""FirstName"": ""firstNameWriter0"",
""LastName"": ""lastNameWriter0""
},
""Reader"": {
""FirstName"": ""firstNameReader0"",
""LastName"": ""lastNameReader0""
},
""Host"": {
""HostName"": ""127.0.0.1""
},
""Id"": 1
},
{
""Writer"": {
""FirstName"": ""firstNameWriter1"",
""LastName"": ""lastNameWriter1""
},
""Reader"": {
""FirstName"": ""firstNameReader1"",
""LastName"": ""lastNameReader1""
},
""Host"": {
""HostName"": ""127.0.0.2""
},
""Id"": 2
},
{
""Writer"": {
""FirstName"": ""firstNameWriter2"",
""LastName"": ""lastNameWriter2""
},
""Reader"": {
""FirstName"": ""firstNameReader2"",
""LastName"": ""lastNameReader2""
},
""Host"": {
""HostName"": ""127.0.0.3""
},
""Id"": 3
}
]", serialized, ignoreLineEndingDifferences: true);

var newBlogs = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Blog>>(serialized);

VerifyBlogs(newBlogs);
foreach (var blog in newBlogs)
{
Assert.IsType<Blog>(blog);
}

#if NETCOREAPP5_0
var options = new JsonSerializerOptions { ReferenceHandler = ReferenceHandler.Preserve, WriteIndented = true };

serialized = JsonSerializer.Serialize(blogs, options);

Assert.Equal(@"{
""$id"": ""1"",
""$values"": [
{
""$id"": ""2"",
""Id"": 1,
""Writer"": {
""$id"": ""3"",
""FirstName"": ""firstNameWriter0"",
""LastName"": ""lastNameWriter0""
},
""Reader"": {
""$id"": ""4"",
""FirstName"": ""firstNameReader0"",
""LastName"": ""lastNameReader0""
},
""Host"": {
""$id"": ""5"",
""HostName"": ""127.0.0.1""
}
},
{
""$id"": ""6"",
""Id"": 2,
""Writer"": {
""$id"": ""7"",
""FirstName"": ""firstNameWriter1"",
""LastName"": ""lastNameWriter1""
},
""Reader"": {
""$id"": ""8"",
""FirstName"": ""firstNameReader1"",
""LastName"": ""lastNameReader1""
},
""Host"": {
""$id"": ""9"",
""HostName"": ""127.0.0.2""
}
},
{
""$id"": ""10"",
""Id"": 3,
""Writer"": {
""$id"": ""11"",
""FirstName"": ""firstNameWriter2"",
""LastName"": ""lastNameWriter2""
},
""Reader"": {
""$id"": ""12"",
""FirstName"": ""firstNameReader2"",
""LastName"": ""lastNameReader2""
},
""Host"": {
""$id"": ""13"",
""HostName"": ""127.0.0.3""
}
}
]
}", serialized, ignoreLineEndingDifferences: true);

newBlogs = JsonSerializer.Deserialize<List<Blog>>(serialized, options);
Assert.IsType<List<Blog>>(newBlogs);

foreach (var blog in newBlogs)
{
Assert.IsType<Blog>(blog);
}
VerifyBlogs(newBlogs);
#endif
}

[ConditionalFact]
public virtual void Lazy_loading_finds_correct_entity_type_with_already_loaded_owned_types()
{
Expand Down
Loading

0 comments on commit ca2a793

Please sign in to comment.