Skip to content
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

Add a test for #23059 #24732

Merged
1 commit merged into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.TestUtilities;
Expand Down Expand Up @@ -151,6 +152,13 @@ public override void Optional_owned_with_converter_reading_non_nullable_column()
base.Optional_owned_with_converter_reading_non_nullable_column();
}

public override void Value_conversion_on_enum_collection_contains()
{
Assert.Contains(
CoreStrings.TranslationFailed("").Substring(47),
Assert.Throws<InvalidOperationException>(() => base.Value_conversion_on_enum_collection_contains()).Message);
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down
41 changes: 40 additions & 1 deletion test/EFCore.Specification.Tests/CustomConvertersTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public virtual void Can_query_and_update_with_nullable_converter_on_primary_key(
var principal = context.Add(
new NullablePrincipal
{
Id = 1, Dependents = new List<NonNullableDependent> { new() { Id = 1 } }
Id = 1,
Dependents = new List<NonNullableDependent> { new() { Id = 1 } }
})
.Entity;

Expand Down Expand Up @@ -572,6 +573,31 @@ public virtual void Value_conversion_with_property_named_value()
() => context.Set<EntityWithValueWrapper>().SingleOrDefault(e => e.Wrapper.Value == "foo"));
}

[ConditionalFact]
public virtual void Value_conversion_on_enum_collection_contains()
{
using var context = CreateContext();
var group = MessageGroup.SomeGroup;
var query = context.Set<User23059>()
.Where(x => !x.IsSoftDeleted && (x.MessageGroups.Contains(group) || x.MessageGroups.Contains(MessageGroup.All)))
.ToList();

Assert.Single(query);
}

protected class User23059
{
public int Id { get; set; }
public bool IsSoftDeleted { get; set; }
public List<MessageGroup> MessageGroups { get; set; }
}

protected enum MessageGroup
{
All,
SomeGroup
}

protected class Blog
{
private bool _indexerVisible;
Expand Down Expand Up @@ -1230,6 +1256,19 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con

b.HasData(new Book(new BookId(1)) { Value = "Book1" });
});

modelBuilder.Entity<User23059>(
b =>
{
b.Property(e => e.MessageGroups).HasConversion(
v => string.Join(',', v),
v => v.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(x => Enum.Parse<MessageGroup>(x)).ToList(),
new ValueComparer<List<MessageGroup>>(favorStructuralComparisons: true));

b.HasData(
new User23059 { Id = 1, IsSoftDeleted = true, MessageGroups = new List<MessageGroup> { MessageGroup.SomeGroup } },
new User23059 { Id = 2, IsSoftDeleted = false, MessageGroups = new List<MessageGroup> { MessageGroup.SomeGroup } });
});
}

private static class StringToDictionarySerializer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ public virtual void Columns_have_expected_data_types()
UnicodeDataTypes.StringUnicode ---> [nullable nvarchar] [MaxLength = -1]
User.Email ---> [nullable nvarchar] [MaxLength = -1]
User.Id ---> [uniqueidentifier]
User23059.Id ---> [int] [Precision = 10 Scale = 0]
User23059.IsSoftDeleted ---> [bit]
User23059.MessageGroups ---> [nullable nvarchar] [MaxLength = -1]
";

Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
Expand Down Expand Up @@ -289,6 +292,13 @@ FROM [Book] AS [b]
WHERE [b].[Id] = 1");
}

public override void Value_conversion_on_enum_collection_contains()
{
Assert.Contains(
CoreStrings.TranslationFailed("").Substring(47),
Assert.Throws<InvalidOperationException>(() => base.Value_conversion_on_enum_collection_contains()).Message);
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;

Expand Down Expand Up @@ -115,6 +116,13 @@ public override void Where_bool_gets_converted_to_equality_when_value_conversion
WHERE ""b"".""IndexerVisible"" = 'Nay'");
}

public override void Value_conversion_on_enum_collection_contains()
{
Assert.Contains(
CoreStrings.TranslationFailed("").Substring(47),
Assert.Throws<InvalidOperationException>(() => base.Value_conversion_on_enum_collection_contains()).Message);
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down