Пример #1
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"));
        }