Skip to content

Commit

Permalink
Query: Add regression test for #21602 (#25792)
Browse files Browse the repository at this point in the history
Resolves #21602
  • Loading branch information
smitpatel authored Aug 31, 2021
1 parent 7538900 commit 2248cb3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
69 changes: 69 additions & 0 deletions test/EFCore.Specification.Tests/CustomConvertersTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Diagnostics;
Expand Down Expand Up @@ -767,6 +768,41 @@ public override int GetHashCode()
=> Id.GetHashCode();
}

[ConditionalFact]
public virtual void Composition_over_collection_of_complex_mapped_as_scalar()
{
using var context = CreateContext();
Assert.Equal(
CoreStrings.TranslationFailed(
@"l => new { H = l.Height, W = l.Width }"),
Assert.Throws<InvalidOperationException>(
() => context.Set<Dashboard>().AsNoTracking().Select(d => new
{
Id = d.Id,
Name = d.Name,
Layouts = d.Layouts.Select(l => new { H = l.Height, W = l.Width }).ToList()
}).ToList())
.Message.Replace("\r", "").Replace("\n", ""));
}

public class Dashboard
{
public Dashboard()
{
Layouts = new List<Layout>();
}

public int Id { get; set; }
public string Name { get; set; }
public List<Layout> Layouts { get; set; }
}

public class Layout
{
public int Width { get; set; }
public int Height { get; set; }
}

public abstract class CustomConvertersFixtureBase : BuiltInDataTypesFixtureBase
{
protected override string StoreName { get; } = "CustomConverters";
Expand Down Expand Up @@ -1269,6 +1305,15 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
new User23059 { Id = 1, IsSoftDeleted = true, MessageGroups = new List<MessageGroup> { MessageGroup.SomeGroup } },
new User23059 { Id = 2, IsSoftDeleted = false, MessageGroups = new List<MessageGroup> { MessageGroup.SomeGroup } });
});

modelBuilder.Entity<Dashboard>()
.Property(e => e.Layouts).HasConversion(
v => LayoutsToStringSerializer.Serialize(v),
v => LayoutsToStringSerializer.Deserialize(v),
new ValueComparer<List<Layout>>(
(v1, v2) => v1.SequenceEqual(v2),
v => v.GetHashCode(),
v => new List<Layout>(v)));
}

private static class StringToDictionarySerializer
Expand All @@ -1291,6 +1336,30 @@ public static IDictionary<string, string> Deserialize(string s)
return dictionary;
}
}
private static class LayoutsToStringSerializer
{
public static string Serialize(List<Layout> layouts)
{
return string.Join(Environment.NewLine, layouts.Select(layout => $"({layout.Height},{layout.Width})"));
}

public static List<Layout> Deserialize(string s)
{
var list = new List<Layout>();
var keyValuePairs = s.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
foreach (var keyValuePair in keyValuePairs)
{
var parts = keyValuePair[1..^1].Split(",");
list.Add(new Layout
{
Height = int.Parse(parts[0]),
Width = int.Parse(parts[1]),
});
}

return list;
}
}

private class OrderIdEntityFrameworkValueConverter : ValueConverter<OrderId, string>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ public virtual void Columns_have_expected_data_types()
CollectionEnum.Roles ---> [nullable nvarchar] [MaxLength = -1]
CollectionScalar.Id ---> [int] [Precision = 10 Scale = 0]
CollectionScalar.Tags ---> [nullable nvarchar] [MaxLength = -1]
Dashboard.Id ---> [int] [Precision = 10 Scale = 0]
Dashboard.Layouts ---> [nullable nvarchar] [MaxLength = -1]
Dashboard.Name ---> [nullable nvarchar] [MaxLength = -1]
DateTimeEnclosure.DateTimeOffset ---> [nullable datetimeoffset] [Precision = 7]
DateTimeEnclosure.Id ---> [int] [Precision = 10 Scale = 0]
EmailTemplate.Id ---> [uniqueidentifier]
Expand Down

0 comments on commit 2248cb3

Please sign in to comment.