public void CallingContentfulShouldReturnCorrectNumberOfDocuments() { //Arrange var mockClient = new Mock <IContentfulClient>(); mockClient.Setup(c => c.GetSpace(default(CancellationToken))) .ReturnsAsync( new Space() { SystemProperties = new SystemProperties { Id = "467" }, Locales = new List <Locale>() { new Locale() { Code = "en-US", Default = true } } }); var collection = new ContentfulCollection <JObject>() { Items = new List <JObject>() { JObject.FromObject(new { sys = new { id = "123" } }), JObject.FromObject(new { sys = new { id = "3456" } }), JObject.FromObject(new { sys = new { id = "62365" } }), JObject.FromObject(new { sys = new { id = "tw635" } }), JObject.FromObject(new { sys = new { id = "uer46" } }), JObject.FromObject(new { sys = new { id = "jy456" } }), }, IncludedAssets = new List <Asset>(), IncludedEntries = new List <Entry <dynamic> >() }; mockClient.Setup(c => c.GetEntries(It.IsAny <QueryBuilder <JObject> >(), default(CancellationToken))) .ReturnsAsync(collection); var mockContext = new Mock <IExecutionContext>(); var contentful = new Contentful(mockClient.Object).WithContentField("body"); //Act var res = contentful.Execute(new List <IDocument>(), mockContext.Object); //Assert Assert.Equal(6, res.Count()); }
internal static Engine GetTestEngine <TContent>(Contentful <TContent> module, Func <IReadOnlyList <IDocument>, Task> finalModule) where TContent : class { var engine = new Engine(); var pipeline = new Pipeline() { InputModules = { module, new TestModule(finalModule) } }; engine.Pipelines.Add(TestPipeline, pipeline); return(engine); }
public async Task It_should_copy_all_system_fields_into_the_document() { // Arrange var content = new BlogPost { Sys = new SystemProperties { Id = "xyz", FirstPublishedAt = new DateTime(2020, 12, 31), }, Title = "Blog post", Image = new Asset { Title = "Image title", } }; var contentfulClient = A.Fake <IContentfulClient>() .WithFakeContent(content); var sut = new Contentful <BlogPost>(contentfulClient, "test_ct"); // Act var engine = EngineHelper.GetTestEngine(sut, docs => { docs.Should().NotBeNull(); return(Task.CompletedTask); }); await engine.ExecuteAsync(); // Assert var outputs = engine.Outputs.FromPipeline(EngineHelper.TestPipeline); outputs.Should().HaveCount(1); var metadata = outputs[0]; // TODO: Maybe add more fields? metadata.Get <string>(ContentfulKeys.System.Id).Should().Be("xyz"); metadata.Get <DateTime>(ContentfulKeys.System.FirstPublishedAt).Should().Be(new DateTime(2020, 12, 31)); }
public async Task It_should_set_the_default_content() { // Arrange var content = new BlogPost { Title = "Hello world!" }; var contentfulClient = A.Fake <IContentfulClient>() .WithFakeContent(content); var sut = new Contentful <BlogPost>(contentfulClient, "test_ct") .WithContent(item => item.Title); // Set up the Assert var engine = EngineHelper.GetTestEngine(sut, async docs => { var content = await docs[0].GetContentStringAsync(); content.Should().Contain("world"); }); // Act & Assert await engine.ExecuteAsync(); }
public void CallingContentfulRecursivelyShouldReturnCorrectNumberOfDocuments() { //Arrange var mockClient = new Mock <IContentfulClient>(); mockClient.Setup(c => c.GetSpaceAsync(default(CancellationToken))) .ReturnsAsync( new Space() { SystemProperties = new SystemProperties { Id = "467" }, Locales = new List <Locale>() { new Locale() { Code = "en-US", Default = true } } }); var collection = new ContentfulCollection <Entry <dynamic> >() { Items = new List <Entry <dynamic> >() { new Entry <dynamic>() { Fields = new JObject(), SystemProperties = new SystemProperties() { Id = "123" } }, new Entry <dynamic>() { Fields = new JObject(), SystemProperties = new SystemProperties() { Id = "3456" } }, new Entry <dynamic>() { Fields = new JObject(), SystemProperties = new SystemProperties() { Id = "62365" } }, new Entry <dynamic>() { Fields = new JObject(), SystemProperties = new SystemProperties() { Id = "tw635" } }, new Entry <dynamic>() { Fields = new JObject(), SystemProperties = new SystemProperties() { Id = "uer46" } }, new Entry <dynamic>() { Fields = new JObject(), SystemProperties = new SystemProperties() { Id = "jy456" } }, }, IncludedAssets = new List <Asset>(), IncludedEntries = new List <Entry <dynamic> >(), Total = 24 }; var callCount = 0; mockClient.Setup(c => c.GetEntriesAsync(It.IsAny <QueryBuilder <Entry <dynamic> > >(), default(CancellationToken))) .ReturnsAsync(() => { if (callCount == 4) { return(new ContentfulCollection <Entry <dynamic> >() { Items = new List <Entry <dynamic> >(), IncludedAssets = new List <Asset>(), IncludedEntries = new List <Entry <dynamic> >(), Total = 24 }); } callCount++; return(collection); }); var mockContext = new Mock <IExecutionContext>(); var contentful = new Contentful(mockClient.Object).WithContentField("body").WithRecursiveCalling().WithLimit(6); //Act var res = contentful.Execute(new List <IDocument>(), mockContext.Object); //Assert Assert.Equal(24, res.Count()); mockClient.Verify(c => c.GetEntriesAsync(It.IsAny <QueryBuilder <Entry <dynamic> > >(), default(CancellationToken)), Times.Exactly(5)); }
public static Contentful <TContentModel> WithSerializedContent <TContentModel>(this Contentful <TContentModel> module) where TContentModel : class { module.GetContent = x => JsonConvert.SerializeObject(x, Formatting.Indented); return(module); }