Skip to content

Commit

Permalink
Restructured tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel0024 committed Jul 11, 2024
1 parent 00f92cf commit 911d100
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
</ItemGroup>

<ItemGroup>
<None Update="ExtractListAndPaginate\Responses\page-2.html">
<None Update="Scenarios\ExtractListAndPaginate\Responses\page-2.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="ExtractListAndPaginate\Responses\main-page.html">
<None Update="Scenarios\ExtractListAndPaginate\Responses\main-page.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
using CocoCrawler.Builders;
using CocoCrawler.Scheduler;
using FluentAssertions;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

namespace CocoCrawler.IntegrationTests.Engine;

[Collection(nameof(BrowserCollection))]
public class CookiesTest
public class CookiesTests
{
private readonly WireMockServer _wireMockServer = WireMockServer.Start();

[Fact]
public async Task Cookies_Should_Be_Send_To_The_Client()
{
// Arange
_wireMockServer.Given(Request.Create().WithUrl($"{_wireMockServer.Url}/cookies"))
.RespondWith(Response.Create().WithSuccess());
_wireMockServer.ReturnSuccessFor($"{_wireMockServer.Url}/cookies");

var crawlerEngine = await new CrawlerEngineBuilder()
.AddPage($"{_wireMockServer.Url}/cookies", options => options.ExtractObject([new("No exist", "div.test")]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace CocoCrawler.IntegrationTests.Engine;

[Collection(nameof(BrowserCollection))]
public class ThrowOnBuilderExceptions
public class ThrowOnBuilderExceptionsTests
{
private readonly WireMockServer _wireMockServer = WireMockServer.Start();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using CocoCrawler.Builders;
using CocoCrawler.Scheduler;
using FluentAssertions;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

namespace CocoCrawler.IntegrationTests.Engine;
Expand All @@ -16,8 +14,7 @@ public class UserAgentTests
public async Task UserAgent_Should_Be_Overwritten()
{
// Arange
_wireMockServer.Given(Request.Create().WithUrl($"{_wireMockServer.Url}/useragent"))
.RespondWith(Response.Create().WithSuccess());
_wireMockServer.ReturnSuccessFor($"{_wireMockServer.Url}/useragent");

var crawlerEngine = await new CrawlerEngineBuilder()
.AddPage($"{_wireMockServer.Url}/useragent", options => options.ExtractObject([new("No exist", "div.test")]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace CocoCrawler.IntegrationTests.ConfigureEngine;

[Collection(nameof(BrowserCollection))]
public class VisitedLinks
public class VisitedLinksTests
{
private readonly WireMockServer _wireMockServer = WireMockServer.Start();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
using CocoCrawler.Builders;
using CocoCrawler.Scheduler;
using FluentAssertions;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

namespace CocoCrawler.IntegrationTests.Outputs;

[Collection(nameof(BrowserCollection))]
public class CsvOutput
public class CsvOutputTests
{
private readonly WireMockServer _wireMockServer = WireMockServer.Start();

[Fact]
public async Task CsvOutput_ShouldCreateFile_OnCleanOnStartup()
public async Task CsvOutput_ShouldCreateFile_WhenCleanOnStartup()
{
// Arange
_wireMockServer.Given(Request.Create().WithUrl($"{_wireMockServer.Url}/main-page"))
.RespondWith(Response.Create()
.WithHeader("Content-Type", "text/xml; charset=utf-8")
.WithBody(GetPage()));
_wireMockServer.ReturnSuccessWithPage($"{_wireMockServer.Url}/main-page", GetPage());

var outputPath = Path.Combine("Outputs", "Really", "Deep", "Path", "resultstest1.csv");

Expand Down Expand Up @@ -53,10 +48,7 @@ public async Task CsvOutput_ShouldCreateFile_OnCleanOnStartup()
public async Task CsvOutput_ShouldNotCreateFile_OnCleanOnStartupFalse()
{
// Arange
_wireMockServer.Given(Request.Create().WithUrl($"{_wireMockServer.Url}/main-page"))
.RespondWith(Response.Create()
.WithHeader("Content-Type", "text/xml; charset=utf-8")
.WithBody(GetPage()));
_wireMockServer.ReturnSuccessWithPage($"{_wireMockServer.Url}/main-page", GetPage());

var outputPath = Path.Combine("Outputs", "Really", "Deep", "Path", $"results-dont-clean-{Random.Shared.Next(0,100)}.csv");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using CocoCrawler.Builders;
using CocoCrawler.Scheduler;
using FluentAssertions;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

namespace CocoCrawler.IntegrationTests.ExtractListAndPaginate;
namespace CocoCrawler.IntegrationTests.Scenarios.ExtractListAndPaginate;

[Collection(nameof(BrowserCollection))]
public class ExtractObjectAndPaginateTests
Expand All @@ -16,15 +14,8 @@ public class ExtractObjectAndPaginateTests
public async Task ExtractListAndPaginate_ShouldHaveDetailsInFile_OnHappyFlow()
{
// Arange
_wireMockServer.Given(Request.Create().WithUrl("http://localhost:9090/main-page"))
.RespondWith(Response.Create()
.WithHeader("Content-Type", "text/xml; charset=utf-8")
.WithBodyFromFile("ExtractListAndPaginate\\Responses\\main-page.html"));

_wireMockServer.Given(Request.Create().WithUrl("http://localhost:9090/page-2"))
.RespondWith(Response.Create()
.WithHeader("Content-Type", "text/xml; charset=utf-8")
.WithBodyFromFile("ExtractListAndPaginate\\Responses\\page-2.html"));
_wireMockServer.ReturnSuccessWithBodyFromFile("http://localhost:9090/main-page", "Scenarios\\ExtractListAndPaginate\\Responses\\main-page.html");
_wireMockServer.ReturnSuccessWithBodyFromFile("http://localhost:9090/page-2", "Scenarios\\ExtractListAndPaginate\\Responses\\page-2.html");

var crawlerEngine = await new CrawlerEngineBuilder()
.AddPage("http://localhost:9090/main-page", options => options
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
using CocoCrawler.Builders;
using CocoCrawler.Scheduler;
using FluentAssertions;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

namespace CocoCrawler.IntegrationTests.ExtractListAndPaginate;
namespace CocoCrawler.IntegrationTests.Scenarios.OpenLinksExtractObjectAndPaginate;

[Collection(nameof(BrowserCollection))]
public class OpenLinksExtractObjectAndPaginate
public class OpenLinksExtractObjectAndPaginateTests
{
private readonly WireMockServer _wireMockServer = WireMockServer.Start(port: 9010);

[Fact]
public async Task ExtractListAndPaginate_ShouldHaveDetailsInFile_OnHappyFlow()
public async Task OpenLinksExtractObjAndPaginate_ShouldHaveDetailsInFile_OnHappyFlow()
{
// Arange
foreach (var index in Enumerable.Range(1, 10))
{
_wireMockServer.Given(Request.Create().WithUrl($"http://localhost:9010/main-page-{index}"))
.RespondWith(Response.Create()
.WithHeader("Content-Type", "text/xml; charset=utf-8")
.WithBody(GetListingHtmlPages(index)));
_wireMockServer.ReturnSuccessWithPage($"{_wireMockServer.Url}/main-page-{index}", GetListingHtmlPages(index));
}

foreach (var index in Enumerable.Range(1, 10 * 3))
foreach (var index in Enumerable.Range(1, 10 * 3)) // 3 listing per page
{
_wireMockServer.Given(Request.Create().WithUrl($"http://localhost:9010/content-page-{index}"))
.RespondWith(Response.Create()
.WithHeader("Content-Type", "text/xml; charset=utf-8")
.WithBody(GetContentPage(index)));
_wireMockServer.ReturnSuccessWithPage($"{_wireMockServer.Url}/content-page-{index}", GetContentPage(index));
}

var outputFile = Path.Combine("Scenarios", "OpenLinksExtractObjectAndPaginate", "Results", "resultstest1.csv");

var crawlerEngine = await new CrawlerEngineBuilder()
.AddPage("http://localhost:9010/main-page-1", options => options
.AddPage($"{_wireMockServer.Url}/main-page-1", options => options
.OpenLinks("div.content.test a.link", newPage => newPage
.ExtractObject([
new("Title", "div.content.test div.title"),
Expand All @@ -42,9 +36,9 @@ public async Task ExtractListAndPaginate_ShouldHaveDetailsInFile_OnHappyFlow()
new("Link", "a", "href")
]))
.AddPagination("div.pagination a:nth-last-child(1)")
.AddOutputToCsvFile("OpenLinksExtractObjectAndPaginate\\Results\\resultstest1.csv"))
.AddOutputToCsvFile(outputFile))
.ConfigureEngine(e => e
.WithScheduler(new InMemoryScheduler(totalSecondsTimeoutAfterJob: 5))
.WithScheduler(new InMemoryScheduler(totalSecondsTimeoutAfterJob: 2))
.WithIgnoreUrls(["http://localhost:9010/content-page-6"])
.DisableParallelism())
.BuildAsync();
Expand All @@ -53,7 +47,7 @@ public async Task ExtractListAndPaginate_ShouldHaveDetailsInFile_OnHappyFlow()
await crawlerEngine.RunAsync();

// Assert
var outputContents = File.ReadAllText("OpenLinksExtractObjectAndPaginate\\Results\\resultstest1.csv");
var outputContents = File.ReadAllText(outputFile);

var expect = @"Url,Title,Description,Amount,Link
http://localhost:9010/content-page-1,Title 1,Description1,Amount 10,link1
Expand Down Expand Up @@ -88,6 +82,8 @@ public async Task ExtractListAndPaginate_ShouldHaveDetailsInFile_OnHappyFlow()
";

outputContents.Should().BeEquivalentTo(expect);

File.Delete(outputFile);
}

private static string GetContentPage(int index)
Expand Down
42 changes: 42 additions & 0 deletions Tests/CocoCrawler.IntegrationTests/WireMockExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

namespace CocoCrawler.IntegrationTests;

internal static class WireMockExtensions
{
internal static void ReturnSuccessFor(this WireMockServer wireMockServer, string url)
{
wireMockServer
.Given(Request.Create()
.UsingGet()
.WithUrl(url))
.RespondWith(Response.Create()
.WithSuccess());
}

internal static void ReturnSuccessWithBodyFromFile(this WireMockServer wireMockServer, string url, string filePath)
{
wireMockServer
.Given(Request.Create()
.UsingGet()
.WithUrl(url))
.RespondWith(Response.Create()
.WithHeader("Content-Type", "text/xml; charset=utf-8")
.WithSuccess()
.WithBodyFromFile(filePath));
}

internal static void ReturnSuccessWithPage(this WireMockServer wireMockServer, string url, string page)
{
wireMockServer
.Given(Request.Create()
.UsingGet()
.WithUrl(url))
.RespondWith(Response.Create()
.WithHeader("Content-Type", "text/xml; charset=utf-8")
.WithSuccess()
.WithBody(page));
}
}

0 comments on commit 911d100

Please sign in to comment.