public void TwoFieldsWithSameNameShouldNotFail()
        {
            // 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",
                    "two"
                };

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

                var actual = sut.Document;

                //Assert
                Assert.AreEqual("one", (string)actual["keywords"][0]);
                Assert.AreEqual("two", (string)actual["keywords"][1]);
            }
        }
        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"]);
            }
        }
        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"]);
            }
        }
        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"]);
            }
        }
        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();
            }
        }
        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");
            }
        }
        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);
            }
        }
        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);
            }
        }