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 include tests with no tracking configuration #21052

Merged
merged 1 commit into from
May 29, 2020
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
@@ -0,0 +1,25 @@
// 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.

using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.EntityFrameworkCore.Query
{
public class NorthwindIncludeNoTrackingQueryInMemoryTest : NorthwindIncludeNoTrackingQueryTestBase<NorthwindQueryInMemoryFixture<NoopModelCustomizer>>
{
public NorthwindIncludeNoTrackingQueryInMemoryTest(NorthwindQueryInMemoryFixture<NoopModelCustomizer> fixture, ITestOutputHelper testOutputHelper)
: base(fixture)
{
//TestLoggerFactory.TestOutputHelper = testOutputHelper;
}

[ConditionalTheory(Skip = "Issue#17386")]
public override Task Include_collection_with_last_no_orderby(bool async)
{
return base.Include_collection_with_last_no_orderby(async);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
// 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.

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.TestModels.Northwind;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;

// ReSharper disable InconsistentNaming
// ReSharper disable StringStartsWithIsCultureSpecific

#pragma warning disable RCS1202 // Avoid NullReferenceException.

namespace Microsoft.EntityFrameworkCore.Query
{
public abstract class NorthwindIncludeNoTrackingQueryTestBase<TFixture> : NorthwindIncludeQueryTestBase<TFixture>
where TFixture : NorthwindQueryFixtureBase<NoopModelCustomizer>, new()
{
private static readonly MethodInfo _asNoTrackingMethodInfo
= typeof(EntityFrameworkQueryableExtensions)
.GetTypeInfo().GetDeclaredMethod(nameof(EntityFrameworkQueryableExtensions.AsNoTracking));

protected NorthwindIncludeNoTrackingQueryTestBase(TFixture fixture)
: base(fixture)
{
}

// Include with cycles are not allowed in no tracking query.
public override async Task Include_multi_level_reference_and_collection_predicate(bool async)
{
Assert.Equal(
CoreStrings.IncludeWithCycle("Customer", "Orders"),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => base.Include_multi_level_reference_and_collection_predicate(async))).Message);
}

public override async Task Include_multi_level_reference_then_include_collection_predicate(bool async)
{
Assert.Equal(
CoreStrings.IncludeWithCycle("Customer", "Orders"),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => base.Include_multi_level_reference_then_include_collection_predicate(async))).Message);
}

public override async Task Include_multiple_references_and_collection_multi_level(bool async)
{
Assert.Equal(
CoreStrings.IncludeWithCycle("Customer", "Orders"),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => base.Include_multiple_references_and_collection_multi_level(async))).Message);
}

public override async Task Include_multiple_references_and_collection_multi_level_reverse(bool async)
{
Assert.Equal(
CoreStrings.IncludeWithCycle("Customer", "Orders"),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => base.Include_multiple_references_and_collection_multi_level_reverse(async))).Message);
}

public override async Task Include_multiple_references_then_include_collection_multi_level(bool async)
{
Assert.Equal(
CoreStrings.IncludeWithCycle("Customer", "Orders"),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => base.Include_multiple_references_then_include_collection_multi_level(async))).Message);
}

public override async Task Include_multiple_references_then_include_collection_multi_level_reverse(bool async)
{
Assert.Equal(
CoreStrings.IncludeWithCycle("Customer", "Orders"),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => base.Include_multiple_references_then_include_collection_multi_level_reverse(async))).Message);
}

public override async Task Include_reference_and_collection_order_by(bool async)
{
Assert.Equal(
CoreStrings.IncludeWithCycle("Customer", "Orders"),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => base.Include_reference_and_collection_order_by(async))).Message);
}

public override async Task Include_references_and_collection_multi_level(bool async)
{
Assert.Equal(
CoreStrings.IncludeWithCycle("Customer", "Orders"),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => base.Include_references_and_collection_multi_level(async))).Message);
}

public override async Task Include_references_and_collection_multi_level_predicate(bool async)
{
Assert.Equal(
CoreStrings.IncludeWithCycle("Customer", "Orders"),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => base.Include_references_and_collection_multi_level_predicate(async))).Message);
}

public override async Task Include_references_then_include_collection(bool async)
{
Assert.Equal(
CoreStrings.IncludeWithCycle("Customer", "Orders"),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => base.Include_references_then_include_collection(async))).Message);
}

public override async Task Include_references_then_include_collection_multi_level(bool async)
{
Assert.Equal(
CoreStrings.IncludeWithCycle("Customer", "Orders"),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => base.Include_references_then_include_collection_multi_level(async))).Message);
}

public override async Task Include_references_then_include_collection_multi_level_predicate(bool async)
{
Assert.Equal(
CoreStrings.IncludeWithCycle("Customer", "Orders"),
(await Assert.ThrowsAsync<InvalidOperationException>(
() => base.Include_references_then_include_collection_multi_level_predicate(async))).Message);
}

public override async Task Include_closes_reader(bool async)
{
using var context = CreateContext();
if (async)
{
Assert.NotNull(await context.Set<Customer>().Include(c => c.Orders).AsNoTracking().FirstOrDefaultAsync());
Assert.NotNull(await context.Set<Product>().AsNoTracking().ToListAsync());
}
else
{
Assert.NotNull(context.Set<Customer>().Include(c => c.Orders).AsNoTracking().FirstOrDefault());
Assert.NotNull(context.Set<Product>().AsNoTracking().ToList());
}
}

public override async Task Include_collection_dependent_already_tracked(bool async)
{
using var context = CreateContext();
var orders = context.Set<Order>().Where(o => o.CustomerID == "ALFKI").ToList();
Assert.Equal(6, context.ChangeTracker.Entries().Count());
Assert.True(orders.All(o => o.Customer == null));

var customer
= async
? await context.Set<Customer>()
.Include(c => c.Orders)
.AsNoTracking()
.SingleAsync(c => c.CustomerID == "ALFKI")
: context.Set<Customer>()
.Include(c => c.Orders)
.AsNoTracking()
.Single(c => c.CustomerID == "ALFKI");

Assert.NotEqual(orders, customer.Orders, LegacyReferenceEqualityComparer.Instance);
Assert.Equal(6, customer.Orders.Count);
Assert.True(customer.Orders.All(e => ReferenceEquals(e.Customer, customer)));

Assert.Equal(6, context.ChangeTracker.Entries().Count());
Assert.True(orders.All(o => o.Customer == null));
}

public override async Task Include_collection_principal_already_tracked(bool async)
{
using var context = CreateContext();
var customer1 = context.Set<Customer>().Single(c => c.CustomerID == "ALFKI");
Assert.Single(context.ChangeTracker.Entries());

var customer2
= async
? await context.Set<Customer>()
.Include(c => c.Orders)
.AsNoTracking()
.SingleAsync(c => c.CustomerID == "ALFKI")
: context.Set<Customer>()
.Include(c => c.Orders)
.AsNoTracking()
.Single(c => c.CustomerID == "ALFKI");

Assert.NotSame(customer1, customer2);
Assert.Equal(6, customer2.Orders.Count);
Assert.True(customer2.Orders.All(o => o.Customer != null));
Assert.True(customer2.Orders.All(o => !ReferenceEquals(o.Customer, customer1)));
Assert.True(customer2.Orders.All(o => ReferenceEquals(o.Customer, customer2)));

Assert.Single(context.ChangeTracker.Entries());
}

public override async Task Include_reference_dependent_already_tracked(bool async)
{
using var context = CreateContext();
var customer = context.Set<Customer>().Single(o => o.CustomerID == "ALFKI");
Assert.Single(context.ChangeTracker.Entries());

var orders
= async
? await context.Set<Order>().Include(o => o.Customer).AsNoTracking().Where(o => o.CustomerID == "ALFKI").ToListAsync()
: context.Set<Order>().Include(o => o.Customer).AsNoTracking().Where(o => o.CustomerID == "ALFKI").ToList();

Assert.Equal(6, orders.Count);
Assert.True(orders.All(o => !ReferenceEquals(o.Customer, customer)));
Assert.True(orders.All(o => o.Customer != null));
Assert.Single(context.ChangeTracker.Entries());
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task NoTracking_Include_with_cycles_does_not_throw_when_performing_identity_resolution(bool async)
{
return AssertQuery(
async,
ss => (from i in ss.Set<Order>().Include(o => o.Customer.Orders)
where i.OrderID < 10800
select i)
.PerformIdentityResolution());
}

protected override bool IgnoreEntryCount => true;

protected override Expression RewriteServerQueryExpression(Expression serverQueryExpression)
{
serverQueryExpression = base.RewriteServerQueryExpression(serverQueryExpression);

return Expression.Call(
_asNoTrackingMethodInfo.MakeGenericMethod(serverQueryExpression.Type.TryGetSequenceType()),
serverQueryExpression);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ var orders
? await context.Set<Order>().Include(o => o.Customer).Where(o => o.CustomerID == "ALFKI").ToListAsync()
: context.Set<Order>().Include(o => o.Customer).Where(o => o.CustomerID == "ALFKI").ToList();

Assert.Equal(6, orders.Count);
Assert.True(orders.All(o => ReferenceEquals(o.Customer, customer)));
Assert.Equal(7, context.ChangeTracker.Entries().Count());
}
Expand Down Expand Up @@ -1568,56 +1569,6 @@ orderby c.CustomerID
entryCount: 14);
}

// TODO: move to notracking test base
//[ConditionalTheory]
//[MemberData(nameof(IsAsyncData))]
//public virtual async Task NoTracking_Include_with_cycles_throws(bool async)
//{
// return AssertQuery(
//async,
//ss => ,
//entryCount: 0);

// var query = (from o in ss.Set<Order>().Include(o => o.Customer.Orders)
// where o.OrderID < 10800
// select o)
// .AsNoTracking();

// Assert.Equal(
// CoreStrings.IncludeWithCycle("Customer", "Orders"),
// async
// ? (await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToListAsync())).Message
// : Assert.Throws<InvalidOperationException>(() => query.ToList()).Message);
//}

//[ConditionalTheory]
//[MemberData(nameof(IsAsyncData))]
//public virtual async Task NoTracking_Include_with_cycles_does_not_throw_when_performing_identity_resolution(bool async)
//{
// return AssertQuery(
//async,
//ss => ,
//entryCount: 0);

// var query = (from o in (async
// ? ss.Set<Order>().Include("Customer.Orders")
// : ss.Set<Order>().Include(o => o.Customer.Orders))
// where o.OrderID < 10800
// select o)
// .PerformIdentityResolution();

// var result = async
// ? await query.ToListAsync()
// : query.ToList();

// Assert.Empty(ss.ChangeTracker.Entries());
// foreach (var order in result)
// {
// Assert.NotNull(order.Customer);
// Assert.Same(order, order.Customer.Orders.First(o => o.OrderID == order.OrderID));
// }
//}

protected virtual void ClearLog()
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

using Microsoft.EntityFrameworkCore.TestUtilities;

namespace Microsoft.EntityFrameworkCore.Query
{
public class NorthwindIncludeNoTrackingQuerySqlServerTest : NorthwindIncludeNoTrackingQueryTestBase<NorthwindQuerySqlServerFixture<NoopModelCustomizer>>
{
// ReSharper disable once UnusedParameter.Local
public NorthwindIncludeNoTrackingQuerySqlServerTest(NorthwindQuerySqlServerFixture<NoopModelCustomizer> fixture)
: base(fixture)
{
Fixture.TestSqlLoggerFactory.Clear();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.

using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit.Abstractions;

namespace Microsoft.EntityFrameworkCore.Query
{
public class NorthwindIncludeNoTrackingQuerySqliteTest : NorthwindIncludeNoTrackingQueryTestBase<NorthwindQuerySqliteFixture<NoopModelCustomizer>>
{
public NorthwindIncludeNoTrackingQuerySqliteTest(NorthwindQuerySqliteFixture<NoopModelCustomizer> fixture, ITestOutputHelper testOutputHelper)
: base(fixture)
{
//TestSqlLoggerFactory.CaptureOutput(testOutputHelper);
}

// Sqlite does not support Apply operations
public override Task Include_collection_with_cross_apply_with_filter(bool async) => Task.CompletedTask;

public override Task Include_collection_with_outer_apply_with_filter(bool async) => Task.CompletedTask;
}
}