コード例 #1
0
        public void ParentsShouldBeAdded()
        {
            // arrange
            using (var db = new Db {
                new ItemBuilder().AddSubItem().Build()
            })
            {
                var item      = db.GetItem("/sitecore/content/source/subitem");
                var indexable = new SitecoreIndexableItem(item);

                var context = new Mock <IProviderUpdateContext>();
                var index   = new IndexBuilder().WithParentsComputedField("parents")
                              .Build();
                context.Setup(t => t.Index).Returns(index);
                var sut = new AlgoliaDocumentBuilder(indexable, context.Object);

                //Act
                sut.AddComputedIndexFields();

                //Assert
                var doc     = sut.Document;
                var parents = (JArray)doc["parents"];
                parents.Count.Should().Be(1);
                ((string)parents.First).Should().Be(TestData.TestItemId.ToString());
            }
        }
コード例 #2
0
        public void StandardValueTest(string defaultValue)
        {
            var templateId = new TemplateID(ID.NewID);

            using (var db = new Db
            {
                new DbTemplate("Sample", templateId)
                {
                    { "Title", defaultValue }
                }
            })
            {
                var contentRoot = db.GetItem(ItemIDs.ContentRoot);
                var item        = contentRoot.Add("Home", templateId);

                var indexable = new SitecoreIndexableItem(item);

                var context = new Mock <IProviderUpdateContext>();
                var index   = new IndexBuilder()
                              .WithSimpleFieldTypeMap("text")
                              .Build();
                context.Setup(t => t.Index).Returns(index);
                var sut = new AlgoliaDocumentBuilder(indexable, context.Object);

                var field = new SitecoreItemDataField(item.Fields["Title"]);

                //Act
                sut.AddField(field);

                //Assert
                JObject doc = sut.Document;
                Assert.AreEqual("Home", (string)doc["title"]);
            }
        }
コード例 #3
0
        public void StringValueShouldBeTrimmed()
        {
            // arrange
            using (var db = new Db {
                new ItemBuilder().WithDisplayName("  test  ").Build()
            })
            {
                var item      = db.GetItem("/sitecore/content/source");
                var indexable = new SitecoreIndexableItem(item);


                var context = new Mock <IProviderUpdateContext>();
                var index   = new IndexBuilder()
                              .WithSimpleFieldTypeMap("text")
                              .Build();
                context.Setup(t => t.Index).Returns(index);
                var sut = new AlgoliaDocumentBuilder(indexable, context.Object);

                var field = new SitecoreItemDataField(item.Fields[ItemBuilder.DispalyNameFieldName]);

                //Act
                sut.AddField(field);

                //Assert
                JObject doc = sut.Document;
                Assert.AreEqual("test", (string)doc["display name"]);
            }
        }
コード例 #4
0
        public void EmptyArrayShuldNotBeAdded()
        {
            // arrange
            using (var db = new Db {
                new ItemBuilder().Build()
            })
            {
                var item      = db.GetItem("/sitecore/content/source");
                var indexable = new SitecoreIndexableItem(item);

                var context = new Mock <IProviderUpdateContext>();
                var index   = new IndexBuilder()
                              .Build();
                context.Setup(t => t.Index).Returns(index);
                var sut   = new AlgoliaDocumentBuilder(indexable, context.Object);
                var value = new List <string>
                {
                };

                //Act
                sut.AddField("keywords", value);

                var actual = sut.Document;

                //Assert
                actual["keywords"].Should().BeNull();
            }
        }
コード例 #5
0
        public void ArrayWithOneValueShuldBeAddedAsArray()
        {
            // arrange
            using (var db = new Db {
                new ItemBuilder().Build()
            })
            {
                var item      = db.GetItem("/sitecore/content/source");
                var indexable = new SitecoreIndexableItem(item);

                var context = new Mock <IProviderUpdateContext>();
                var index   = new IndexBuilder()
                              .Build();
                context.Setup(t => t.Index).Returns(index);
                var sut   = new AlgoliaDocumentBuilder(indexable, context.Object);
                var value = new List <string>
                {
                    "one"
                };

                //Act
                sut.AddField("keywords", value);

                var actual = sut.Document;

                //Assert
                Assert.AreEqual("one", (string)actual["keywords"][0]);
            }
        }
コード例 #6
0
        public void AddDictionaryTest()
        {
            // arrange
            using (var db = new Db {
                new ItemBuilder().Build()
            })
            {
                var item      = db.GetItem("/sitecore/content/source");
                var indexable = new SitecoreIndexableItem(item);

                var context = new Mock <IProviderUpdateContext>();
                var index   = new IndexBuilder()
                              .Build();
                context.Setup(t => t.Index).Returns(index);
                var sut = new AlgoliaDocumentBuilder(indexable, context.Object);

                var value = new Dictionary <string, object>
                {
                    { "Price", "$0-$6" },
                    { "Blade Count", "2 Blades" }
                };

                //Act
                sut.AddField("any name", value);

                //Assert
                JObject doc = sut.Document;
                ((string)doc["Price"]).Should().Be("$0-$6");
                ((string)doc["Blade Count"]).Should().Be("2 Blades");
            }
        }
コード例 #7
0
        public void AddSimpleDoubleFieldTest()
        {
            // arrange
            using (var db = new Db {
                new ItemBuilder().WithPrice(123.456).Build()
            })
            {
                var item      = db.GetItem("/sitecore/content/source");
                var indexable = new SitecoreIndexableItem(item);

                var context = new Mock <IProviderUpdateContext>();
                var index   = new IndexBuilder()
                              .WithSimpleFieldTypeMap("number")
                              .WithNumericFieldReader("number")
                              .Build();
                context.Setup(t => t.Index).Returns(index);
                var sut = new AlgoliaDocumentBuilder(indexable, context.Object);

                var field = new SitecoreItemDataField(item.Fields[ItemBuilder.PriceFieldName]);

                //Act
                sut.AddField(field);

                //Assert
                JObject doc = sut.Document;
                Assert.AreEqual(123.456, (double)doc["price"]);
                Assert.AreEqual(JTokenType.Float, doc["price"].Type);
            }
        }
コード例 #8
0
        public void DuplicatedFieldShouldNotFail()
        {
            // arrange
            using (var db = new Db {
                new ItemBuilder().WithHardcodedDate().Build()
            })
            {
                var item      = db.GetItem("/sitecore/content/source");
                var indexable = new SitecoreIndexableItem(item);


                var context = new Mock <IProviderUpdateContext>();
                var index   = new IndexBuilder()
                              .Build();
                context.Setup(t => t.Index).Returns(index);
                var sut = new AlgoliaDocumentBuilder(indexable, context.Object);

                var value = JObject.Parse(@"{'_geoloc': {
        'lat': 33.7489954,
        'lng': -84.3879824
      }}");
                //Act
                sut.AddField(ItemBuilder.LocationFieldName, value);
                sut.AddField(ItemBuilder.LocationFieldName, value);

                //Assert
                JObject doc = sut.Document;
                Assert.AreEqual(33.7489954, (double)doc["_geoloc"]["lat"]);
                Assert.AreEqual(-84.3879824, (double)doc["_geoloc"]["lng"]);
            }
        }
コード例 #9
0
        public void AddDateFieldTest()
        {
            // arrange
            using (var db = new Db {
                new ItemBuilder().WithHardcodedDate().Build()
            })
            {
                var item      = db.GetItem("/sitecore/content/source");
                var indexable = new SitecoreIndexableItem(item);

                var context = new Mock <IProviderUpdateContext>();
                var index   = new IndexBuilder()
                              .WithSimpleFieldTypeMap("datetime")
                              .WithDateFieldReader("datetime")
                              .Build();
                context.Setup(t => t.Index).Returns(index);
                var sut   = new AlgoliaDocumentBuilder(indexable, context.Object);
                var field = new SitecoreItemDataField(item.Fields[ItemBuilder.DateFieldName]);

                //Act
                sut.AddField(field);

                //Assert
                JObject doc = sut.Document;
                Assert.AreEqual(1418787000, (int)doc["date"]);
                Assert.AreEqual(JTokenType.Integer, doc["date"].Type);
            }
        }
コード例 #10
0
        public void WithIndexAllFieldsShouldIncludeFields()
        {
            // arrange
            using (var db = new Db {
                new ItemBuilder().Build()
            })
            {
                var item      = db.GetItem("/sitecore/content/source");
                var indexable = new SitecoreIndexableItem(item);

                var context = new Mock <IProviderUpdateContext>();
                var index   = new IndexBuilder().Build();
                context.Setup(t => t.Index).Returns(index);
                var sut = new AlgoliaDocumentBuilder(indexable, context.Object);
                sut.Options.IndexAllFields = true;

                //Act
                sut.AddItemFields();

                //Assert
                JObject doc = sut.Document;
                doc.Properties().Count().Should().BeGreaterThan(4);
            }
        }