Class SitecoreQueryMapper
상속: AbstractDataMapper
        public void ReadOnly_ReturnsTrue()
        {
            //Assign
            var mapper = new SitecoreQueryMapper(null);

            //Act
            var result = mapper.ReadOnly;

            //Assert
            Assert.IsTrue(result);
        }
        public void CanHandle_CorrectConfigIEnumerableNotMappedClass_ReturnsTrueOnDemand()
        {
            //Assign
            var mapper = new SitecoreQueryMapper(null);
            var config = new SitecoreQueryConfiguration();
            config.PropertyInfo = new FakePropertyInfo(typeof(IEnumerable<StubNotMapped>));
            var context = Context.Create(Utilities.CreateStandardResolver());
            context.Load(new SitecoreAttributeConfigurationLoader("Glass.Mapper.Sc.Integration"));


            //Act
            var result = mapper.CanHandle(config, context);

            //Assert
            Assert.IsTrue(result);
        }
        public void CanHandle_CorrectConfigIEnumerableNotMappedClass_ReturnsTrueOnDemand()
        {
            //Assign
            var mapper = new SitecoreQueryMapper(null);
            var config = new SitecoreQueryConfiguration();
            config.PropertyInfo = typeof(StubClass).GetProperty("StubNotMappeds");

            var context = Context.Create(Utilities.CreateStandardResolver());
            context.Load(new OnDemandLoader<SitecoreTypeConfiguration>(typeof(StubClass)));



            //Act
            var result = mapper.CanHandle(config, context);

            //Assert
            Assert.IsTrue(result);
        }
        public void MapToProperty_AbsoluteQueryWithParameter_ReturnsResults()
        {
            //Assign
            //Assign
            var config = new SitecoreQueryConfiguration();
            config.PropertyInfo = typeof (StubClass).GetProperty("StubMappeds");
            config.Query = "{path}/../Results/*";
            config.IsRelative = false;
            config.UseQueryContext = true;

            var context = Context.Create(Utilities.CreateStandardResolver());
            context.Load(new SitecoreAttributeConfigurationLoader("Glass.Mapper.Sc.Integration"));

            var mapper = new SitecoreQueryMapper(null);
            mapper.Setup(new DataMapperResolverArgs(context, config));

            var source = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreQueryMapper/Source");
            var service = new SitecoreService(Database, context);

            var result1 = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreQueryMapper/Results/Result1");
            var result2 = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreQueryMapper/Results/Result2");

            //Act
            var results =
                mapper.MapToProperty(new SitecoreDataMappingContext(null, source, service)) as IEnumerable<StubMapped>;

            //Assert
            Assert.AreEqual(2, results.Count());
            Assert.IsTrue(results.Any(x => x.Id == result1.ID.Guid));
            Assert.IsTrue(results.Any(x => x.Id == result2.ID.Guid));

        }
        public void MapToProperty_AbsoluteQuery_ReturnsSingleResults()
        {
            //Assign
            //Assign
            var config = new SitecoreQueryConfiguration();
            config.PropertyInfo = typeof (StubClass).GetProperty("StubMapped");
            config.Query = "/sitecore/content/Tests/DataMappers/SitecoreQueryMapper/Results/Result1";
            config.IsRelative = false;

            var context = Context.Create(Utilities.CreateStandardResolver());
            context.Load(new SitecoreAttributeConfigurationLoader("Glass.Mapper.Sc.Integration"));

            var mapper = new SitecoreQueryMapper(null);
            mapper.Setup(new DataMapperResolverArgs(context, config));

            var source = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreQueryMapper/Source");
            var service = new SitecoreService(Database, context);

            var result1 = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreQueryMapper/Results/Result1");

            //Act
            var result =
                mapper.MapToProperty(new SitecoreDataMappingContext(null, source, service)) as StubMapped;

            //Assert
            Assert.AreEqual(result1.ID.Guid, result.Id);

        }
        public void MapToProperty_RelativeQuery_ReturnsNoResults()
        {
            //Assign
            //Assign
            var config = new SitecoreQueryConfiguration();
            config.PropertyInfo = typeof (StubClass).GetProperty("StubMappeds");

            config.Query = "../Results/DoesNotExist/*";
            config.IsRelative = true;

            var context = Context.Create(Utilities.CreateStandardResolver());
            context.Load(new SitecoreAttributeConfigurationLoader("Glass.Mapper.Sc.Integration"));

            var mapper = new SitecoreQueryMapper(null);
            mapper.Setup(new DataMapperResolverArgs(context, config));

            var source = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreQueryMapper/Source");
            var service = new SitecoreService(Database, context);

            var result1 = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreQueryMapper/Results/Result1");
            var result2 = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreQueryMapper/Results/Result2");

            //Act
            var results =
                mapper.MapToProperty(new SitecoreDataMappingContext(null, source, service)) as IEnumerable<StubMapped>;

            //Assert
            Assert.AreEqual(0, results.Count());
        }
        public void CanHandle_IncorrectConfigMappedClass_ReturnsFalse()
        {
            //Assign
            var mapper = new SitecoreQueryMapper(null);
            var config = new SitecoreFieldConfiguration();
            config.PropertyInfo = typeof (StubClass).GetProperty("StubMapped");

            var context = Context.Create(Utilities.CreateStandardResolver());
            context.Load(new SitecoreAttributeConfigurationLoader("Glass.Mapper.Sc.Integration"));


            //Act
            var result = mapper.CanHandle(config, context);

            //Assert
            Assert.IsFalse(result);
        }
        public void MapToProperty_AbsoluteQueryWithParameter_ReturnsResults()
        {
            //Assign
            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("Target")
                {
                    new Sitecore.FakeDb.DbItem("Child1"),
                    new Sitecore.FakeDb.DbItem("Child2")
                }
            })
            {
                var config = new SitecoreQueryConfiguration();
                config.PropertyInfo = typeof(StubClass).GetProperty("StubMappeds");
                config.Query = "{path}/../Results/*";
                config.IsRelative = false;
                config.UseQueryContext = true;

                var context = Context.Create(Utilities.CreateStandardResolver());
                context.Load(new OnDemandLoader<SitecoreTypeConfiguration>(typeof(StubMapped)));

                var mapper = new SitecoreQueryMapper(new[] {new ItemPathParameter()});
                mapper.Setup(new DataMapperResolverArgs(context, config));

                var source = database.GetItem("/sitecore/content/Target");
                var service = new SitecoreService(database.Database, context);

                var result1 = database.GetItem("/sitecore/content/Target/Child1");
                var result2 = database.GetItem("/sitecore/content/Target/Child2");

                //Act
                var results =
                    mapper.MapToProperty(new SitecoreDataMappingContext(null, source, service)) as
                        IEnumerable<StubMapped>;

                //Assert
                Assert.AreEqual(2, results.Count());
                Assert.IsTrue(results.Any(x => x.Id == result1.ID.Guid));
                Assert.IsTrue(results.Any(x => x.Id == result2.ID.Guid));

            }
        }
        public void MapToProperty_AbsoluteQuery_ReturnsSingleResults()
        {
            //Assign
            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("Target")
                {
                    new Sitecore.FakeDb.DbItem("Child1"),
                    new Sitecore.FakeDb.DbItem("Child2")
                }
            })
            {
                var config = new SitecoreQueryConfiguration();
                config.PropertyInfo = typeof(StubClass).GetProperty("StubMapped");
                config.Query = "/sitecore/content/Target/Child1";
                config.IsRelative = false;

                var context = Context.Create(Utilities.CreateStandardResolver());
                context.Load(new OnDemandLoader<SitecoreTypeConfiguration>(typeof(StubMapped)));

                var mapper = new SitecoreQueryMapper(null);
                mapper.Setup(new DataMapperResolverArgs(context, config));

                var source = database.GetItem("/sitecore/content/Target");
                var service = new SitecoreService(database.Database, context);

                var result1 = database.GetItem("/sitecore/content/Target/Child1");

                //Act
                var result =
                    mapper.MapToProperty(new SitecoreDataMappingContext(null, source, service)) as StubMapped;

                //Assert
                Assert.AreEqual(result1.ID.Guid, result.Id);

            }
        }
        public void MapToProperty_RelativeQuery_ReturnsNoResults()
        {
            //Assign
            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("Target")
                {
                     new Sitecore.FakeDb.DbItem("Child1"),
                     new Sitecore.FakeDb.DbItem("Child2")
                }
            })
            {

                var config = new SitecoreQueryConfiguration();
                config.PropertyInfo = typeof(StubClass).GetProperty("StubMappeds");

                config.Query = "../Target/DoesNotExist/*";
                config.IsRelative = true;

                var context = Context.Create(Utilities.CreateStandardResolver());
                context.Load(new OnDemandLoader<SitecoreTypeConfiguration>(typeof(StubMapped)));

                var mapper = new SitecoreQueryMapper(null);
                mapper.Setup(new DataMapperResolverArgs(context, config));

                var source = database.GetItem("/sitecore/content/Target");
                var service = new SitecoreService(database.Database, context);

                //Act
                var results =
                    mapper.MapToProperty(new SitecoreDataMappingContext(null, source, service)) as
                        IEnumerable<StubMapped>;

                //Assert
                Assert.AreEqual(0, results.Count());
            }
        }