Skip to content

Commit

Permalink
Add elasticsearch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Leanwit committed Nov 20, 2020
1 parent a026eaa commit 6917270
Show file tree
Hide file tree
Showing 23 changed files with 487 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CsharpDDDSkeleton.sln
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Backoffice", "src\Backoffic
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Backend", "apps\Backoffice\Backend\Backend.csproj", "{8E36A0E1-97AA-498A-961A-FDCBE6B64A0E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Backoffice", "test\src\Backoffice\Backoffice.csproj", "{C32371FC-C56B-418D-854E-3B90C2C725E1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -83,6 +85,10 @@ Global
{8E36A0E1-97AA-498A-961A-FDCBE6B64A0E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E36A0E1-97AA-498A-961A-FDCBE6B64A0E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E36A0E1-97AA-498A-961A-FDCBE6B64A0E}.Release|Any CPU.Build.0 = Release|Any CPU
{C32371FC-C56B-418D-854E-3B90C2C725E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C32371FC-C56B-418D-854E-3B90C2C725E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C32371FC-C56B-418D-854E-3B90C2C725E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C32371FC-C56B-418D-854E-3B90C2C725E1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -102,6 +108,7 @@ Global
{A0AC8A46-A162-4771-8983-CD8AE83F1B04} = {EA77673C-C96C-4C6A-AF13-D14EB5BBE779}
{8E379E5B-1D79-470F-9ACA-51A159ABC9E4} = {ABF745D7-F9BF-4CEE-8A12-DC03FAB68C9A}
{8E36A0E1-97AA-498A-961A-FDCBE6B64A0E} = {EA77673C-C96C-4C6A-AF13-D14EB5BBE779}
{C32371FC-C56B-418D-854E-3B90C2C725E1} = {73884FEF-DAD3-43E0-AA56-8B75AF6E6568}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B97A4396-5C75-4848-848F-949B7BE081E2}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static void AddElasticsearch(this IServiceCollection services, IConfigura

GenerateIndexIfNotExist(elastic.IndexFor(nameof(BackofficeCourse)), elastic);

services.AddSingleton(client);
services.AddSingleton(elastic);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Shared/Domain/FiltersByCriteria/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ public static Order FromValues(string orderBy, string orderType)
? Enum.Parse<OrderType>(orderType.ToUpperInvariant())
: OrderType.NONE);
}

public static Order None()
{
return new Order(new OrderBy(string.Empty), OrderType.NONE);
}
}
}
Empty file removed test/src/Backoffice/.gitkeep
Empty file.
23 changes: 23 additions & 0 deletions test/src/Backoffice/Backoffice.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
<AssemblyName>CodelyTv.Test.Backoffice</AssemblyName>
<RootNamespace>CodelyTv.Test.Backoffice</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\apps\Backoffice\Frontend\Frontend.csproj" />
<ProjectReference Include="..\..\..\src\Backoffice\Backoffice.csproj" />
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions test/src/Backoffice/BackofficeContextInfrastructureTestCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Linq;
using CodelyTv.Apps.Backoffice.Frontend;
using CodelyTv.Backoffice.Courses.Domain;
using CodelyTv.Backoffice.Courses.Infrastructure.Persistence.Elasticsearch;
using CodelyTv.Backoffice.Shared.Infrastructure.Persistence.Elasticsearch;
using CodelyTv.Backoffice.Shared.Infrastructure.Persistence.EntityFramework;
using CodelyTv.Test.Backoffice.Shared.Infrastructure.Persistence;
using CodelyTv.Test.Shared.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nest;

namespace CodelyTv.Test.Backoffice
{
public class BackofficeContextInfrastructureTestCase : InfrastructureTestCase<Startup>, IDisposable
{
protected override Action<IServiceCollection> Services()
{
return services =>
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<BackofficeContext>));
if (descriptor != null)
services.Remove(descriptor);

var configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();

services.AddScoped<BackofficeCourseRepository, ElasticsearchBackofficeCourseRepository>();
services.AddElasticsearch(configuration);
};
}

protected override void Setup()
{
var cleaner = new ElasticDatabaseCleaner(GetService<ElasticClient>());
cleaner.Execute();
}

public void Dispose()
{
Finish();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using CodelyTv.Backoffice.Courses.Domain;

namespace CodelyTv.Test.Backoffice.Courses
{
public class BackofficeCoursesModuleInfrastructureTestCase : BackofficeContextInfrastructureTestCase
{
protected BackofficeCourseRepository ElasticRepository => GetService<BackofficeCourseRepository>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using CodelyTv.Shared.Domain.FiltersByCriteria;
using CodelyTv.Test.Shared.Domain.Criterias;

namespace CodelyTv.Test.Backoffice.Courses.Domain
{
public static class BackofficeCourseCriteriaMother
{
public static Criteria NameContains(string text)
{
return CriteriaMother.Create(
FiltersMother.CreateOne(
FilterMother.FromValues(new Dictionary<string, string>
{
{"field", "name"},
{"operator", "CONTAINS"},
{"value", text}
})
), Order.None()
);
}
}
}
34 changes: 34 additions & 0 deletions test/src/Backoffice/Courses/Domain/BackofficeCourseMother.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using CodelyTv.Backoffice.Courses.Domain;
using CodelyTv.Mooc.Courses.Application.Create;
using CodelyTv.Test.Shared.Domain;

namespace CodelyTv.Test.Backoffice.Courses.Domain
{
public static class BackofficeCourseMother
{
public static BackofficeCourse Create(string id, string name, string duration)
{
return new BackofficeCourse(id, name, duration);
}

public static BackofficeCourse FromRequest(CreateCourseCommand request)
{
return Create(request.Id, request.Name, request.Duration);
}

public static BackofficeCourse Random()
{
return Create( UuidMother.Random(), WordMother.Random(), RandomDuration());
}

public static BackofficeCourse WithName(string name)
{
return Create(UuidMother.Random(), name, RandomDuration());
}

private static string RandomDuration()
{
return $"{IntegerMother.Random()} {RandomElementPicker.From("months", "years", "days", "hours", "minutes", "seconds")}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CodelyTv.Backoffice.Courses.Domain;
using CodelyTv.Test.Backoffice.Courses.Domain;
using CodelyTv.Test.Shared.Domain.Criterias;
using Xunit;

namespace CodelyTv.Test.Backoffice.Courses.Infrastructure.Persistence
{
public class ElasticsearchBackofficeCourseRepositoryTest : BackofficeCoursesModuleInfrastructureTestCase
{
[Fact]
public void ItShouldSaveAValidCourse()
{
ElasticRepository.Save(BackofficeCourseMother.Random());
}

[Fact]
public async Task ItShouldSearchAllExistingCourses()
{
var existingCourse = BackofficeCourseMother.Random();
var anotherExistingCourse = BackofficeCourseMother.Random();

var existingCourses = new List<BackofficeCourse>
{
existingCourse, anotherExistingCourse
};

await ElasticRepository.Save(existingCourse);
await ElasticRepository.Save(anotherExistingCourse);

await WaitFor(async () => (await ElasticRepository.SearchAll()).Any());
Assert.Equal(existingCourses, (await ElasticRepository.SearchAll()).ToList());
}

[Fact]
public async Task ItShouldSearchAllExistingCoursesWithAnEmptyCriteria()
{
var existingCourse = BackofficeCourseMother.Random();
var anotherExistingCourse = BackofficeCourseMother.Random();

var existingCourses = new List<BackofficeCourse>
{
existingCourse, anotherExistingCourse
};

await ElasticRepository.Save(existingCourse);
await ElasticRepository.Save(anotherExistingCourse);

await WaitFor(async () => (await ElasticRepository.SearchAll()).Any());
Assert.Equal(existingCourses, (await ElasticRepository.Matching(CriteriaMother.Empty())).ToList());
}

[Fact]
public async Task ItShouldFilterByCriteria()
{
var dddInPhpCourse = BackofficeCourseMother.WithName("DDD en PHP");
var dddInCSharpCourse = BackofficeCourseMother.WithName("DDD en C#");
var exprimiendoIntellij = BackofficeCourseMother.WithName("Exprimiendo Intellij");

var courses = new List<BackofficeCourse>()
{
dddInPhpCourse, dddInCSharpCourse
};

var nameContainsDddCriteria = BackofficeCourseCriteriaMother.NameContains("DDD");

await ElasticRepository.Save(dddInPhpCourse);
await ElasticRepository.Save(dddInCSharpCourse);
await ElasticRepository.Save(exprimiendoIntellij);

await WaitFor(async () => (await ElasticRepository.Matching(nameContainsDddCriteria)).Any());

Assert.Equal(courses, (await ElasticRepository.Matching(nameContainsDddCriteria)).ToList());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Nest;

namespace CodelyTv.Test.Backoffice.Shared.Infrastructure.Persistence
{
public class ElasticDatabaseCleaner
{
private ElasticClient _client;

public ElasticDatabaseCleaner(ElasticClient client)
{
_client = client;
}

public void Execute()
{
var request = _client.Indices.Get(new GetIndexRequest(Indices.All));

foreach (var index in request.Indices)
{
_client.Indices.Delete(index.Key);
}
}
}
}
11 changes: 10 additions & 1 deletion test/src/Mooc/MoocContextInfrastructureTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@

namespace CodelyTv.Test.Mooc
{
public class MoocContextInfrastructureTestCase : InfrastructureTestCase<Startup>
public class MoocContextInfrastructureTestCase : InfrastructureTestCase<Startup>, IDisposable
{
protected override void Setup()
{
}

protected override Action<IServiceCollection> Services()
{
return services =>
Expand Down Expand Up @@ -56,5 +60,10 @@ protected override Action<IServiceCollection> Services()
services.Configure<RabbitMqConfig>(configuration.GetSection("RabbitMq"));
};
}

public void Dispose()
{
Finish();
}
}
}
22 changes: 22 additions & 0 deletions test/src/Shared/Domain/Criterias/CriteriaMother.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CodelyTv.Shared.Domain.FiltersByCriteria;

namespace CodelyTv.Test.Shared.Domain.Criterias
{
public static class CriteriaMother
{
public static Criteria Create(Filters filters, Order order, int? limit = null, int? offset = null)
{
return new Criteria(filters, order, offset, limit);
}

public static Criteria Empty()
{
return Create(FiltersMother.Blank(), Order.None());
}

public static Criteria Random()
{
return Create(FiltersMother.Random(), OrderMother.Random(), IntegerMother.Random(), IntegerMother.Random());
}
}
}
17 changes: 17 additions & 0 deletions test/src/Shared/Domain/Criterias/FilterFieldMother.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using CodelyTv.Shared.Domain.FiltersByCriteria;

namespace CodelyTv.Test.Shared.Domain.Criterias
{
public static class FilterFieldMother
{
public static FilterField Create(string fieldName)
{
return new FilterField(fieldName);
}

public static FilterField Random()
{
return new FilterField(WordMother.Random());
}
}
}
23 changes: 23 additions & 0 deletions test/src/Shared/Domain/Criterias/FilterMother.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using CodelyTv.Shared.Domain.FiltersByCriteria;

namespace CodelyTv.Test.Shared.Domain.Criterias
{
public static class FilterMother
{
public static Filter Create(FilterField field, FilterOperator @operator, FilterValue value)
{
return new Filter(field, @operator, value);
}

public static Filter FromValues(Dictionary<string, string> values)
{
return Filter.FromValues(values);
}

public static Filter Random()
{
return Create(FilterFieldMother.Random(), FilterOperatorMother.Random(), FilterValueMother.Random());
}
}
}
15 changes: 15 additions & 0 deletions test/src/Shared/Domain/Criterias/FilterOperatorMother.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using CodelyTv.Shared.Domain.FiltersByCriteria;

namespace CodelyTv.Test.Shared.Domain.Criterias
{
public static class FilterOperatorMother
{
public static FilterOperator Random()
{
Array values = Enum.GetValues(typeof(FilterOperator));
Random random = new Random();
return (FilterOperator)values.GetValue(random.Next(values.Length));
}
}
}
Loading

0 comments on commit 6917270

Please sign in to comment.