generated from CodelyTV/csharp-basic-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
487 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
test/src/Backoffice/BackofficeContextInfrastructureTestCase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
test/src/Backoffice/Courses/BackofficeCoursesModuleInfrastructureTestCase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
test/src/Backoffice/Courses/Domain/BackofficeCourseCriteriaMother.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
test/src/Backoffice/Courses/Domain/BackofficeCourseMother.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")}"; | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...koffice/Courses/Infrastructure/Persistence/ElasticsearchBackofficeCourseRepositoryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
test/src/Backoffice/Shared/Infrastructure/Persistence/ElasticDatabaseCleaner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
Oops, something went wrong.