Beispiel #1
0
        public void AutoMapper_projectto_expression_transformed_to_single_intermediate_type()
        {
            // configure automapper
            AutoMapper.Mapper.CreateMap <TestData, FormattedName>()
            .ForMember(f => f.DisplayName, m => m.MapFrom(t => string.Format("{0} {1}", t.FirstName, t.LastName)))
            .ForMember(f => f.IndexedName, m => m.MapFrom(t => string.Format("{0}, {1}", t.LastName.ToUpper(), t.FirstName)));
            AutoMapper.Mapper.AssertConfigurationIsValid();

            var query = TestData.Sample.AsQueryable()
                        .Project().To <FormattedName>();

            // build queryable provider stub to monitor what's passed through to underlying provider
            var stubProvider  = new StubQueryProviderProxy(query.Provider);
            var mockQueryable = MockQueryableHelper.CreateMockQueryable(query, stubProvider);

            // act: apply LinqDefer to the mocked query/provider stub
            var queryWithDefer = mockQueryable.Object.ThenDoDeferred();
            var results        = queryWithDefer.ToArray();

            // assert: the underlying mocked provider was only asked for our intermediate type
            Assert.AreEqual(1, stubProvider.CreateQueryCalledTypes.Count);
            Assert.IsTrue(stubProvider.CreateQueryCalledTypes[0].Assembly.FullName == IntermediateTypeCache.AssemblyFullName);

            var expectedResults =
                TestData.Sample.Select(AutoMapper.Mapper.Map <TestData, FormattedName>).ToArray();

            AssertExtensions.AreCollectionsEquivalent(results, expectedResults);
        }
        public void Passes_through_entirety_if_just_source_expression()
        {
            // arrange: analyser which says underlying provider should only handle getting the
            // Now property value, and stub provider to record which type is requested to be
            // retrieved by that provider
            Mock <IExpressionAnalyser> mockAnalyser;
            var config = MockQueryableHelper.CreateConfigWithMockRejectingAnalyser(out mockAnalyser);

            mockAnalyser.Setup(a =>
                               a.ShouldProviderHandleExpression(It.Is <ExpressionNodePath>(e =>
                                                                                           e.Current.Expression is MemberExpression && ((MemberExpression)e.Current.Expression).Member.Name == "Now")))
            .Returns(true);

            var query = TestData.Sample.AsQueryable()
                        .Select(d => DateTime.Now);

            var stubProvider  = new StubQueryProviderProxy(query.Provider);
            var mockQueryable = MockQueryableHelper.CreateMockQueryable(query, stubProvider);

            // act: apply LinqDefer to the mocked query/provider stub
            var queryWithDefer = mockQueryable.Object.ThenDoDeferred(config);
            var results        = queryWithDefer.ToArray();

            // assert: the underlying mocked provider was asked for one field precisely, a DateTime one
            Assert.AreEqual(1, stubProvider.CreateQueryCalledTypes.Count);
            var members = stubProvider.CreateQueryCalledTypes[0].GetFields();

            Assert.AreEqual(1, members.Length);
            Assert.IsTrue(members[0].FieldType == typeof(DateTime));
        }
        public void Throws_exception_if_select_not_final_clause()
        {
            // arrange: analyser which says underlying provider shouldn't handle any expressions,
            // stub provider to record which type is requested to be retrieved by that provider
            Mock <IExpressionAnalyser> mockAnalyser;
            var config = MockQueryableHelper.CreateConfigWithMockRejectingAnalyser(out mockAnalyser);

            var query = TestData.Sample.AsQueryable()
                        .Select(d => DateTime.Now)
                        .Where(d => (d.Ticks % 10) == 0);

            var stubProvider  = new StubQueryProviderProxy(query.Provider);
            var mockQueryable = MockQueryableHelper.CreateMockQueryable(query, stubProvider);

            // act: apply LinqDefer to the mocked query/provider stub
            mockQueryable.Object.ThenDoDeferred(config);
        }
Beispiel #4
0
        public void Deferred_method_calls_occur_after_main_query()
        {
            // arrange: analyser which says underlying provider should only handle getting the GetNextA
            // method call to our test type, and stub provider to record which type is requested to be
            // retrieved by that provider
            Mock <IExpressionAnalyser> mockAnalyser;
            var config      = MockQueryableHelper.CreateConfigWithMockRejectingAnalyser(out mockAnalyser);
            var sequenceGen = new SimpleSequenceGenerator();

            mockAnalyser.Setup(a =>
                               a.ShouldProviderHandleExpression(It.Is <ExpressionNodePath>(e =>
                                                                                           e.Current.Expression is MethodCallExpression && ((MethodCallExpression)e.Current.Expression).Method.Name == "GetNextA")))
            .Returns(true);

            var query = TestData.Sample.AsQueryable()
                        .Select(d => new ThreePropertyClass <int>
            {
                PropOne   = sequenceGen.GetNextA(),
                PropTwo   = sequenceGen.GetNextB(),
                PropThree = sequenceGen.GetNextA()
            });

            var stubProvider  = new StubQueryProviderProxy(query.Provider);
            var mockQueryable = MockQueryableHelper.CreateMockQueryable(query, stubProvider);

            // act: apply LinqDefer to the mocked query/provider stub
            var queryWithDefer = mockQueryable.Object.ThenDoDeferred(config);
            var results        = queryWithDefer.ToArray();

            // assert: the underlying mocked provider was asked for two fields precisely, both int ones;
            // also that the Two member of each result is greater than One and Three, indicating it was
            // executed after the GetNextA calls passed to the underlying provider
            Assert.AreEqual(1, stubProvider.CreateQueryCalledTypes.Count);
            var members = stubProvider.CreateQueryCalledTypes[0].GetFields();

            Assert.AreEqual(2, members.Length);
            Assert.IsTrue(members.All(m => m.FieldType == typeof(int)));
            foreach (var result in results)
            {
                Assert.IsTrue(result.PropTwo > result.PropOne);
                Assert.IsTrue(result.PropTwo > result.PropThree);
            }
        }
Beispiel #5
0
        public void Exception_in_post_process_throws_LinqDeferException()
        {
            // arrange: analyser which says underlying provider should do nothing (everything is deferred),
            // and query to cause exception to be thrown
            Mock <IExpressionAnalyser> mockAnalyser;
            var config = MockQueryableHelper.CreateConfigWithMockRejectingAnalyser(out mockAnalyser);

            var query = TestData.Sample.AsQueryable()
                        .Select(d => ExceptionThrower.Throw());

            var stubProvider  = new StubQueryProviderProxy(query.Provider);
            var mockQueryable = MockQueryableHelper.CreateMockQueryable(query, stubProvider);

            // act: apply LinqDefer to the mocked query/provider stub
            var queryWithDefer = mockQueryable.Object.ThenDoDeferred(config);

            // final act & assert
            var results = queryWithDefer.ToArray();
        }
        public void Passes_through_nothing_if_no_source_expressions()
        {
            // arrange: analyser which says underlying provider shouldn't handle any expressions,
            // stub provider to record which type is requested to be retrieved by that provider
            Mock <IExpressionAnalyser> mockAnalyser;
            var config = MockQueryableHelper.CreateConfigWithMockRejectingAnalyser(out mockAnalyser);

            var query = TestData.Sample.AsQueryable()
                        .Select(d => DateTime.Now);

            var stubProvider  = new StubQueryProviderProxy(query.Provider);
            var mockQueryable = MockQueryableHelper.CreateMockQueryable(query, stubProvider);

            // act: apply LinqDefer to the mocked query/provider stub
            var queryWithDefer = mockQueryable.Object.ThenDoDeferred(config);
            var results        = queryWithDefer.ToArray();

            // assert: the underlying mocked provider wasn't asked for any data to be retrieved
            Assert.AreEqual(1, stubProvider.CreateQueryCalledTypes.Count);
            Assert.AreEqual(0, stubProvider.CreateQueryCalledTypes[0].GetFields().Length);
        }