Пример #1
0
        public void Create_Dynamic_Search_With_Results()
        {
            AttributeTypeRegistry.SetCurrent(new CmsAttributeTypeRegistry());

            // Ensure parent exists for this test
            Hive.AutoCommitTo <IContentStore>(x => x.Repositories.Schemas.AddOrUpdate(new ContentRootSchema()));

            // Create schema
            var schema1 = Hive.Cms().NewContentType <EntitySchema, IContentStore>("homePage")
                          .Define("pageTitle", type => type.UseExistingType("singleLineTextBox"), FixedGroupDefinitions.GeneralGroup)
                          .Define("pageContent", type => type.UseExistingType("singleLineTextBox"), FixedGroupDefinitions.GeneralGroup)
                          .Commit();
            var schema2 = Hive.Cms().NewContentType <EntitySchema, IContentStore>("contentPage")
                          .Define("pageTitle", type => type.UseExistingType("singleLineTextBox"), FixedGroupDefinitions.GeneralGroup)
                          .Define("pageContent", type => type.UseExistingType("singleLineTextBox"), FixedGroupDefinitions.GeneralGroup)
                          .Commit();

            Assert.True(schema1.Success);
            Assert.True(schema2.Success);

            var item1 = new Content();

            item1.SetupFromSchema(schema1.Item);
            item1.Id             = new HiveId(Guid.NewGuid());
            item1["pageTitle"]   = "Hello There";
            item1["pageContent"] = "some page content";

            var item2 = new Content();

            item2.SetupFromSchema(schema2.Item);
            item2.Id             = new HiveId(Guid.NewGuid());
            item2["pageTitle"]   = "Title 1";
            item2["pageContent"] = "this is some page content. hi, hello, goodbye.";

            var item3 = new Content();

            item3.SetupFromSchema(schema1.Item);
            item3.Id             = new HiveId(Guid.NewGuid());
            item3["pageTitle"]   = "Another page";
            item3["pageContent"] = "Say hello to my little friend.";

            var writerResult = Hive.AutoCommitTo <IContentStore>(x =>
            {
                x.Repositories.AddOrUpdate(item1);
                x.Repositories.AddOrUpdate(item2);
                x.Repositories.AddOrUpdate(item3);
            });

            Assert.True(writerResult.WasCommitted);
            AddRevision(item1, FixedStatusTypes.Published);
            AddRevision(item2, FixedStatusTypes.Published);
            AddRevision(item3, FixedStatusTypes.Published);

            // Check can get the items normally
            using (var uow = Hive.OpenReader <IContentStore>())
            {
                Assert.True(uow.Repositories.Exists <Content>(item1.Id));
                Assert.True(uow.Repositories.Exists <Content>(item2.Id));
                Assert.True(uow.Repositories.Exists <Content>(item3.Id));
            }

            //Create a dynamic search

            var docTypes   = new[] { "homePage", "contentPage" };
            var fields     = new[] { "pageTitle", "pageContent" };
            var searchTerm = "hello";

            using (var uow = GroupUnitFactory.Create())
            {
                var predicate = ExpressionExtensions.False <TypedEntity>();
                var query     = uow.Repositories.Query();
                foreach (var d in docTypes)
                {
                    //this will add an 'AND' clause, not an 'OR' clause
                    //query = query.Where(x => x.EntitySchema.Alias == d);
                    var d1 = d;
                    predicate = predicate.Or(x => x.EntitySchema.Alias == d1);
                }
                foreach (var f in fields)
                {
                    //this will add an 'AND' clause, not an 'OR' clause
                    //query = query.Where(x => x.Attribute<string>(f) == searchTerm);
                    var f1 = f;
                    predicate = predicate.Or(x => x.Attribute <string>(f1) == searchTerm);
                }

                var result = query.Where(predicate).ToArray();

                Assert.AreEqual(3, result.Count());
            }
        }
Пример #2
0
        public void WhenTypedEntity_QueriedWithStringEquals_AndOrderBy_ResultsAreOrdered()
        {
            var item1Id  = Guid.NewGuid();
            var item2Id  = Guid.NewGuid();
            var item3Id  = Guid.NewGuid();
            var parentId = Guid.NewGuid();

            AttributeTypeRegistry.SetCurrent(new CmsAttributeTypeRegistry());

            // Ensure parent exists for this test
            Hive.AutoCommitTo <IContentStore>(x => x.Repositories.Schemas.AddOrUpdate(new ContentRootSchema()));

            // Create schema
            var schema = Hive.Cms().NewContentType <EntitySchema, IContentStore>("withTitle")
                         .Define("title", type => type.UseExistingType("singleLineTextBox"), FixedGroupDefinitions.GeneralGroup)
                         .Define("random", type => type.UseExistingType("singleLineTextBox"), FixedGroupDefinitions.GeneralGroup)
                         .Define("tag", type => type.UseExistingType("singleLineTextBox"), FixedGroupDefinitions.GeneralGroup)
                         .Define("bodyText", type => type.UseExistingType("richTextEditor"), FixedGroupDefinitions.GeneralGroup)
                         .Commit();

            Assert.True(schema.Success);

            var item1 = new Content();

            item1.SetupFromSchema(schema.Item);
            item1.Id        = new HiveId(item1Id);
            item1["title"]  = "Item1";
            item1["random"] = "Random3";
            item1["tag"]    = "apple";

            var item2 = new Content();

            item2.SetupFromSchema(schema.Item);
            item2.Id        = new HiveId(item2Id);
            item2["title"]  = "Item2";
            item2["random"] = "Random1";
            item2["tag"]    = "blueberry";

            var item3 = new Content();

            item3.SetupFromSchema(schema.Item);
            item3.Id        = new HiveId(item3Id);
            item3["title"]  = "Item3";
            item3["random"] = "Random2";
            item3["tag"]    = "apple";

            var writerResult = Hive.AutoCommitTo <IContentStore>(x =>
            {
                x.Repositories.AddOrUpdate(item1);
                x.Repositories.AddOrUpdate(item2);
                x.Repositories.AddOrUpdate(item3);
            });

            Assert.True(writerResult.WasCommitted);

            // Check can get the items normally
            using (var uow = Hive.OpenReader <IContentStore>())
            {
                Assert.True(uow.Repositories.Exists <Content>(item1.Id));
                Assert.True(uow.Repositories.Exists <Content>(item2.Id));
                Assert.True(uow.Repositories.Exists <Content>(item3.Id));
            }

            // query all with sortorder - first check is actually order of insertion anyway
            var allQuery_NaturalSort = Hive.QueryContent().OrderBy(x => x.Attribute <string>("title")).ToArray();

            Assert.That(allQuery_NaturalSort.Any());
            Assert.That(allQuery_NaturalSort[0]["title"], Is.EqualTo("Item1"));
            Assert.That(allQuery_NaturalSort[1]["title"], Is.EqualTo("Item2"));
            Assert.That(allQuery_NaturalSort[2]["title"], Is.EqualTo("Item3"));

            var allQuerySortByTag = Hive.QueryContent().OrderBy(x => x.Attribute <string>("tag")).ToArray();

            Assert.That(allQuerySortByTag.Any());
            Assert.That(allQuerySortByTag[0]["tag"], Is.EqualTo("apple"));
            Assert.That(allQuerySortByTag[0]["random"], Is.EqualTo("Random3").Or.EqualTo("Random2"));
            Assert.That(allQuerySortByTag[1]["tag"], Is.EqualTo("apple"));
            Assert.That(allQuerySortByTag[1]["random"], Is.EqualTo("Random3").Or.EqualTo("Random2"));
            Assert.That(allQuerySortByTag[2]["tag"], Is.EqualTo("blueberry"));
            Assert.That(allQuerySortByTag[2]["random"], Is.EqualTo("Random1"));

            var allQuerySortByTagThenRandom = Hive.QueryContent().OrderBy(x => x.Attribute <string>("tag")).ThenBy(x => x.Attribute <string>("random")).ToArray();

            Assert.That(allQuerySortByTagThenRandom.Any());
            Assert.That(allQuerySortByTagThenRandom[0]["tag"], Is.EqualTo("apple"));
            Assert.That(allQuerySortByTagThenRandom[0]["random"], Is.EqualTo("Random2"));
            Assert.That(allQuerySortByTagThenRandom[1]["tag"], Is.EqualTo("apple"));
            Assert.That(allQuerySortByTagThenRandom[1]["random"], Is.EqualTo("Random3"));
            Assert.That(allQuerySortByTagThenRandom[2]["tag"], Is.EqualTo("blueberry"));
            Assert.That(allQuerySortByTagThenRandom[2]["random"], Is.EqualTo("Random1"));

            // query invoking the executesingle methods
            var firstByTagDescending = Hive.QueryContent().OrderByDescending(x => x.Attribute <string>("tag")).FirstOrDefault();

            Assert.NotNull(firstByTagDescending);
            Assert.That(firstByTagDescending["tag"], Is.EqualTo("blueberry"));

            var singleByTagDescending = Hive.QueryContent().OrderByDescending(x => x.Attribute <string>("tag")).SingleOrDefault(x => x.Attribute <string>("random") == "Random2");

            Assert.NotNull(singleByTagDescending);
            Assert.That(singleByTagDescending["tag"], Is.EqualTo("apple"));
        }