// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace ProductivityApiUnitTests
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Objects.DataClasses;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Internal;
using System.Data.Entity.ModelConfiguration.Configuration.Types;
using System.Data.Entity.Resources;
using System.Linq;
using System.Reflection;
using Moq;
using Xunit;
public class FakeEntity1
{
}
public class FakeEntity2
{
}
public class FakeEntity3
{
}
public class FakeEntity4
{
}
public class FakeEntity5
{
}
public class FakeEntity6
{
}
public class FakeEntity7
{
}
public class FakeEntity8
{
}
public class FakeEntity9
{
}
public class FakeEntity10
{
}
public class FakeEntity11
{
}
public class FakeEntity12
{
}
public class FakeEntity13
{
}
public class FakeEntity14
{
}
public class FakeEntity15
{
}
public class FakeEntity16
{
}
public class FakeEntity17
{
}
public class FakeEntity18
{
}
///
/// Unit tests for the DbSet/ObjectSet discovery service.
///
public class SetDiscoveryTests : TestBase
{
#region Positive DbContext discovery and initialization tests
private class FakeDbContextWithDbSets : DbContext
{
public FakeDbContextWithDbSets()
: base("this=is=not=valid")
{
}
// Should be detected: DbSets with no modifiers
public DbSet PublicGetSet { get; set; }
protected IDbSet ProtectedGetSet { get; set; }
internal DbSet InternalGetSet { get; set; }
protected internal IDbSet InternalProtectedGetSet { get; set; }
private DbSet PrivateGetSet { get; set; }
// Should be detected: Public DbSets setter modifiers
public IDbSet PrivateSet { get; private set; }
public DbSet ProtectedSet { get; protected set; }
public IDbSet InternalSet { get; internal set; }
public DbSet InternalProtectedSet { get; protected internal set; }
// Should be detected: Public DbSets getter modifiers
public DbSet PrivateGet { private get; set; }
public IDbSet ProtectedGet { protected get; set; }
public DbSet InternalGet { internal get; set; }
public IDbSet InternalProtectedGet { protected internal get; set; }
// Should be detected: DbSets with no setters
public DbSet PublicGetNoSet
{
get { return null; }
}
protected IDbSet ProtectedGetNoSet
{
get { return null; }
}
internal IDbSet InternalGetNoSet
{
get { return null; }
}
protected internal DbSet InternalProtectedGetNoSet
{
get { return null; }
}
private IDbSet PrivateGetNoSet
{
get { return null; }
}
}
[Fact]
public void DbSet_and_IDbSet_properties_on_derived_DbContext_are_discovered()
{
var expected = new[]
{
"PublicGetSet", "ProtectedGetSet", "InternalGetSet", "InternalProtectedGetSet", "PrivateGetSet",
"PrivateSet", "ProtectedSet", "InternalSet", "InternalProtectedSet",
"PrivateGet", "ProtectedGet", "InternalGet", "InternalProtectedGet",
"PublicGetNoSet", "ProtectedGetNoSet", "InternalGetNoSet", "InternalProtectedGetNoSet", "PrivateGetNoSet"
,
};
AssertExpectedSetsDiscovered(new FakeDbContextWithDbSets(), expected);
}
private class FakeDbContextWithNonSets : DbContext
{
public FakeDbContextWithNonSets()
: base("this=is=not=valid")
{
}
public DbSet Control { get; set; }
public IQueryable QueryableT { get; set; }
public IQueryable Queryable { get; set; }
public IEnumerable EnumerableT { get; set; }
public IEnumerable Enumerable { get; set; }
public DbQuery DbQuery { get; set; }
}
[Fact]
public void IQueryable_and_DbQuery_properties_on_derived_DbContext_are_ignored()
{
AssertExpectedSetsDiscovered(new FakeDbContextWithNonSets(), new[] { "Control" });
}
[Fact]
public void DbSet_and_IDbSet_properties_with_public_setters_are_initialized()
{
using (var context = new FakeDbContextWithDbSets())
{
var contextType = typeof(FakeDbContextWithDbSets);
const BindingFlags binding = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
Assert.NotNull(contextType.GetProperty("PublicGetSet", binding).GetValue(context, null));
Assert.Null(contextType.GetProperty("ProtectedGetSet", binding).GetValue(context, null));
Assert.Null(contextType.GetProperty("InternalGetSet", binding).GetValue(context, null));
Assert.Null(contextType.GetProperty("InternalProtectedGetSet", binding).GetValue(context, null));
Assert.Null(contextType.GetProperty("PrivateGetSet", binding).GetValue(context, null));
Assert.Null(contextType.GetProperty("PrivateSet", binding).GetValue(context, null));
Assert.Null(contextType.GetProperty("ProtectedSet", binding).GetValue(context, null));
Assert.Null(contextType.GetProperty("InternalSet", binding).GetValue(context, null));
Assert.Null(contextType.GetProperty("InternalProtectedSet", binding).GetValue(context, null));
Assert.NotNull(contextType.GetProperty("PrivateGet", binding).GetValue(context, null));
Assert.NotNull(contextType.GetProperty("ProtectedGet", binding).GetValue(context, null));
Assert.NotNull(contextType.GetProperty("InternalGet", binding).GetValue(context, null));
Assert.NotNull(contextType.GetProperty("InternalProtectedGet", binding).GetValue(context, null));
Assert.Null(contextType.GetProperty("PublicGetNoSet", binding).GetValue(context, null));
Assert.Null(contextType.GetProperty("ProtectedGetNoSet", binding).GetValue(context, null));
Assert.Null(contextType.GetProperty("InternalGetNoSet", binding).GetValue(context, null));
Assert.Null(contextType.GetProperty("InternalProtectedGetNoSet", binding).GetValue(context, null));
Assert.Null(contextType.GetProperty("PrivateGetNoSet", binding).GetValue(context, null));
}
}
[Fact]
public void IQueryable_and_DbQuery_properties_with_public_setters_are_not_initialized()
{
using (var context = new FakeDbContextWithNonSets())
{
Assert.NotNull(context.Control);
Assert.Null(context.DbQuery);
Assert.Null(context.Enumerable);
Assert.Null(context.EnumerableT);
Assert.Null(context.Queryable);
Assert.Null(context.QueryableT);
}
}
#endregion
#region Negative entity types
private class FakeDbContextWithInterfaceDbSet : DbContext
{
public DbSet SetProp { get; set; }
}
[Fact]
public void Discovery_of_DbSet_of_interface_type_throws()
{
Assert.Equal(
Strings.InvalidEntityType(typeof(IList)),
Assert.Throws(
() => new DbSetDiscoveryService(new FakeDbContextWithInterfaceDbSet()).InitializeSets()).Message);
}
private class FakeDbContextWithGenericDbSet : DbContext
{
public DbSet> SetProp { get; set; }
}
[Fact]
public void Discovery_of_DbSet_of_generic_type_throws()
{
Assert.Equal(
Strings.InvalidEntityType(typeof(List)),
Assert.Throws(
() => new DbSetDiscoveryService(new FakeDbContextWithGenericDbSet()).InitializeSets()).Message);
}
private class FakeDbContextWithNestedDbSet : DbContext
{
public DbSet SetProp { get; set; }
}
[Fact]
public void Discovery_of_DbSet_of_nested_type_throws()
{
Assert.Equal(
Strings.InvalidEntityType(typeof(FakeDbContextWithDbSets)),
Assert.Throws(
() => new DbSetDiscoveryService(new FakeDbContextWithNestedDbSet()).InitializeSets()).Message);
}
private class FakeDbContextWithObjectDbSet : DbContext
{
public DbSet