Exemple #1
0
        public void ShouldNotEagerLoad_WhenNavigationIsNotAllowedOnTheRootEntity()
        {
            var attribute = new EagerLoadAttribute(notOnRoot: true);

            var navigationHelperMock = new Mock <NavigationHelper>();

            navigationHelperMock.Setup(helper => helper.GetTypeForNavigation(It.IsAny <INavigation>()))
            .Returns(typeof(Book));

            var strategy = new EagerLoadAttributeIncludeStrategy(navigationHelperMock.Object);
            var context  = new EagerLoadContext(Mock.Of <DbContext>(), strategy);

            var firstNavigationMock = SetupGenericNavigationMock();

            EagerLoadAttributeIncludeStrategy.EagerLoadAttributeCache.TryAdd(firstNavigationMock.Object, attribute);

            context.SetCurrentNavigation(firstNavigationMock.Object);
            Assert.False(strategy.ShouldIncludeCurrentNavigation(context));

            var secondNavigationMock = SetupGenericNavigationMock();

            EagerLoadAttributeIncludeStrategy.EagerLoadAttributeCache.TryAdd(secondNavigationMock.Object, new EagerLoadAttribute());

            context.SetCurrentNavigation(secondNavigationMock.Object);

            context.SetCurrentNavigation(firstNavigationMock.Object);
            Assert.True(strategy.ShouldIncludeCurrentNavigation(context));
        }
        public void ShouldDisplayCorrect_CurrentIncludePath_WhenRemovingNavigation()
        {
            var context = new EagerLoadContext(Mock.Of <DbContext>(), Mock.Of <IIncludeStrategy>());

            var bookNavigationMock = new Mock <INavigation>();

            bookNavigationMock.Setup(nav => nav.Name).Returns(nameof(Book));
            var authorNavigationMock = new Mock <INavigation>();

            authorNavigationMock.Setup(nav => nav.Name).Returns(nameof(Author));
            var publisherNavigationMock = new Mock <INavigation>();

            publisherNavigationMock.Setup(nav => nav.Name).Returns(nameof(Publisher));

            context.SetCurrentNavigation(bookNavigationMock.Object);
            context.SetCurrentNavigation(authorNavigationMock.Object);
            context.SetCurrentNavigation(publisherNavigationMock.Object);

            Assert.Equal($"{nameof(Book)}.{nameof(Author)}.{nameof(Publisher)}", context.CurrentIncludePath);

            context.RemoveCurrentNavigation();
            Assert.Equal($"{nameof(Book)}.{nameof(Author)}", context.CurrentIncludePath);

            context.RemoveCurrentNavigation();
            Assert.Equal($"{nameof(Book)}", context.CurrentIncludePath);

            context.RemoveCurrentNavigation();
            Assert.Equal(string.Empty, context.CurrentIncludePath);

            context.RemoveCurrentNavigation();
            Assert.Equal(string.Empty, context.CurrentIncludePath);
        }
Exemple #3
0
        public void ShouldNotEagerLoad_WhenAttributeDoesNotExistOnNavigation()
        {
            var strategy = new EagerLoadAttributeIncludeStrategy();
            var context  = new EagerLoadContext(Mock.Of <DbContext>(), strategy);

            var navigationMock = SetupGenericNavigationMock();

            context.SetCurrentNavigation(navigationMock.Object);

            Assert.False(strategy.ShouldIncludeCurrentNavigation(context));
        }
        public void ShouldEagerLoad_ForAllOffRoot()
        {
            var strategy       = new AllNavigationsIncludeStrategy();
            var context        = new EagerLoadContext(new Mock <DbContext>().Object, strategy);
            var navigationMock = new Mock <INavigation>();

            navigationMock.Setup(nav => nav.Name).Returns(nameof(Book));
            context.SetCurrentNavigation(navigationMock.Object);

            Assert.True(strategy.ShouldIncludeCurrentNavigation(context));
        }
        public void ShouldDisplayCorrect_CurrentIncludePath_WhenAddingFirstNavigation()
        {
            var context = new EagerLoadContext(Mock.Of <DbContext>(), Mock.Of <IIncludeStrategy>());

            var navigationMock = new Mock <INavigation>();

            navigationMock.Setup(nav => nav.Name).Returns(nameof(Book));

            context.SetCurrentNavigation(navigationMock.Object);

            Assert.Equal(nameof(Book), context.CurrentIncludePath);
        }
        public void ShouldDoNothing_WhenNavigationAddedIsNull()
        {
            var context = new EagerLoadContext(Mock.Of <DbContext>(), Mock.Of <IIncludeStrategy>());

            Assert.Equal(string.Empty, context.CurrentIncludePath);
            Assert.Null(context.CurrentNavigation);

            context.SetCurrentNavigation(null);

            Assert.Equal(string.Empty, context.CurrentIncludePath);
            Assert.Null(context.CurrentNavigation);
        }
Exemple #7
0
        public void ShouldNotEagerLoad_WhenNavigationIsNotValid()
        {
            var strategy = new EagerLoadAttributeIncludeStrategy();
            var context  = new EagerLoadContext(Mock.Of <DbContext>(), strategy);

            var navigationMock = new Mock <INavigation>();

            navigationMock.Setup(nav => nav.Name).Returns(nameof(Book));
            context.SetCurrentNavigation(navigationMock.Object);

            Assert.False(strategy.ShouldIncludeCurrentNavigation(context));
        }
        public void ShouldNotEagerLoad_IfIncludePathToConsiderShouldBeIgnored()
        {
            var strategy = new AllNavigationsIncludeStrategy();
            var context  = new EagerLoadContext(new Mock <DbContext>().Object, strategy);

            context.IncludePathsToIgnore.Add(nameof(INavigation));
            //context.ParentIncludePath = "";
            var navigationMock = new Mock <INavigation>();

            navigationMock.Setup(nav => nav.Name).Returns(nameof(INavigation));
            context.SetCurrentNavigation(navigationMock.Object);

            Assert.False(strategy.ShouldIncludeCurrentNavigation(context));
        }
Exemple #9
0
        public void ShouldNotEagerLoad_WhenOverTheTypeCount()
        {
            var attribute = new EagerLoadAttribute(maxTypeCount: 1);

            var navigationHelperMock = new Mock <NavigationHelper>();

            var strategy = new EagerLoadAttributeIncludeStrategy(navigationHelperMock.Object);
            var context  = new EagerLoadContext(Mock.Of <DbContext>(), strategy, rootType: typeof(Publisher));

            var firstNavigationMock = SetupGenericNavigationMock();

            EagerLoadAttributeIncludeStrategy.EagerLoadAttributeCache.TryAdd(firstNavigationMock.Object, attribute);
            navigationHelperMock.Setup(helper => helper.GetTypeForNavigation(It.IsAny <INavigation>()))
            .Returns(typeof(Book));

            context.SetCurrentNavigation(firstNavigationMock.Object);
            Assert.True(strategy.ShouldIncludeCurrentNavigation(context));

            var secondNavigationMock = SetupGenericNavigationMock();

            EagerLoadAttributeIncludeStrategy.EagerLoadAttributeCache.TryAdd(secondNavigationMock.Object,
                                                                             new EagerLoadAttribute());
            navigationHelperMock.Setup(helper => helper.GetTypeForNavigation(It.IsAny <INavigation>()))
            .Returns(typeof(Author));

            context.SetCurrentNavigation(secondNavigationMock.Object);
            Assert.True(strategy.ShouldIncludeCurrentNavigation(context));

            var thirdNavigationMock = SetupGenericNavigationMock();

            EagerLoadAttributeIncludeStrategy.EagerLoadAttributeCache.TryAdd(thirdNavigationMock.Object, attribute);
            navigationHelperMock.Setup(helper => helper.GetTypeForNavigation(It.IsAny <INavigation>()))
            .Returns(typeof(Book));

            context.SetCurrentNavigation(thirdNavigationMock.Object);
            Assert.False(strategy.ShouldIncludeCurrentNavigation(context));
        }
Exemple #10
0
        public void ShouldNotEagerLoad_WhenAttributeSetToNever()
        {
            var strategy = new EagerLoadAttributeIncludeStrategy();
            var context  = new EagerLoadContext(Mock.Of <DbContext>(), strategy);

            var navigationMock = SetupGenericNavigationMock();
            var navigation     = navigationMock.Object;

            context.SetCurrentNavigation(navigation);
            var attribute = new EagerLoadAttribute(never: true);

            EagerLoadAttributeIncludeStrategy.EagerLoadAttributeCache.TryAdd(navigation, attribute);

            Assert.False(strategy.ShouldIncludeCurrentNavigation(context));
        }
Exemple #11
0
        public void ShouldEagerLoad_WhenAttributeExist()
        {
            var strategy       = new AttributeExistsIncludeStrategy <EagerLoadAttribute>();
            var context        = new EagerLoadContext(new Mock <DbContext>().Object, strategy);
            var navigationMock = new Mock <INavigation>();

            navigationMock.Setup(nav => nav.Name).Returns(nameof(Book));

            context.SetCurrentNavigation(navigationMock.Object);

            navigationMock.Setup(nav => nav.PropertyInfo)
            .Returns(GetType().GetProperty(nameof(HasEagerLoadAttribute)));
            Assert.True(strategy.ShouldIncludeCurrentNavigation(context));

            navigationMock.Setup(nav => nav.PropertyInfo)
            .Returns(GetType().GetProperty(nameof(HasManyIncludingEagerLoadAttribute)));
            Assert.True(strategy.ShouldIncludeCurrentNavigation(context));
        }
        public void ShouldUseSuppliedPathPredicate_ToTestIfTheNavigationShouldBeIncluded()
        {
            var pathChecked = string.Empty;
            var strategy    = new PredicateIncludeStrategy(path =>
            {
                pathChecked = path;
                return(true);
            });

            var context        = new EagerLoadContext(Mock.Of <DbContext>(), strategy);
            var navigationMock = new Mock <INavigation>();

            navigationMock.Setup(nav => nav.Name).Returns(nameof(Book));

            context.SetCurrentNavigation(navigationMock.Object);

            Assert.True(strategy.ShouldIncludeCurrentNavigation(context));

            Assert.Equal(nameof(Book), pathChecked);
        }
Exemple #13
0
        private void BuildIncludesForEagerLoadContext(EagerLoadContext context)
        {
            BuildIncludesForEagerLoadContext();

            void BuildIncludesForEagerLoadContext()
            {
                var navigationsToConsider = _navigationFinder.GetNavigationsForType(context, context.CurrentType ?? context.RootType);

                foreach (var navigation in navigationsToConsider)
                {
                    context.SetCurrentNavigation(navigation);

                    if (context.IncludeStrategy.ShouldIncludeCurrentNavigation(context))
                    {
                        context.IncludePathsToInclude.Add(context.CurrentIncludePath);
                        BuildIncludesForEagerLoadContext();
                    }

                    context.RemoveCurrentNavigation();
                }
            }
        }
        public void ShouldUseSuppliedContextPredicate_ToTestIfTheNavigationShouldBeIncluded()
        {
            var testCount = 0;
            var strategy  = new PredicateIncludeStrategy((EagerLoadContext contextInput) =>
            {
                var result = (testCount == 1);
                testCount++;
                return(result);
            });

            var context        = new EagerLoadContext(Mock.Of <DbContext>(), strategy);
            var navigationMock = new Mock <INavigation>();

            navigationMock.Setup(nav => nav.Name).Returns(nameof(Book));

            context.SetCurrentNavigation(navigationMock.Object);

            Assert.False(strategy.ShouldIncludeCurrentNavigation(context));

            Assert.True(strategy.ShouldIncludeCurrentNavigation(context));

            Assert.Equal(2, testCount);
        }