Handling tests logging and fixing tests CI

This commit is contained in:
Zoe Roux 2021-06-27 21:45:57 +02:00
parent 6f87139815
commit b5f31eba02
10 changed files with 57 additions and 29 deletions

View File

@ -5,6 +5,17 @@ on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
container: mcr.microsoft.com/dotnet/sdk:5.0
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v2
- name: Setup .NET
@ -17,3 +28,7 @@ jobs:
run: dotnet build --no-restore '-p:SkipWebApp=true;SkipTranscoder=true'
- name: Test
run: dotnet test --no-build
env:
POSTGRES_HOST: postgres
POSTGRES_USERNAME: postgres
POSTGRES_PASSWORD: postgres

View File

@ -29,7 +29,6 @@ namespace Kyoo.Models
if (value == null)
throw new ArgumentNullException(nameof(value));
Console.WriteLine(value);
Match match = Regex.Match(value, @"(?<show>.+)-s(?<season>\d+)e(?<episode>\d+)");
if (match.Success)

View File

@ -1,3 +0,0 @@
FROM mcr.microsoft.com/dotnet/sdk:5.0
COPY ../ .
RUN dotnet tests '-p:SkipWebApp=true;SkipTranscoder=true'

View File

@ -14,6 +14,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Divergic.Logging.Xunit" Version="3.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">

View File

@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using Kyoo.Controllers;
using Xunit.Abstractions;
namespace Kyoo.Tests
{
@ -12,11 +13,11 @@ namespace Kyoo.Tests
private readonly DatabaseContext _database;
public RepositoryActivator(PostgresFixture postgres = null)
public RepositoryActivator(ITestOutputHelper output, PostgresFixture postgres = null)
{
Context = postgres == null
? new SqLiteTestContext()
: new PostgresTestContext(postgres);
? new SqLiteTestContext(output)
: new PostgresTestContext(postgres, output);
_database = Context.New();
ProviderRepository provider = new(_database);

View File

@ -2,6 +2,7 @@ using System.Threading.Tasks;
using Kyoo.Controllers;
using Kyoo.Models;
using Xunit;
using Xunit.Abstractions;
namespace Kyoo.Tests.Library
{
@ -9,8 +10,8 @@ namespace Kyoo.Tests.Library
{
public class EpisodeTests : AEpisodeTests
{
public EpisodeTests()
: base(new RepositoryActivator()) { }
public EpisodeTests(ITestOutputHelper output)
: base(new RepositoryActivator(output)) { }
}
}
@ -20,8 +21,8 @@ namespace Kyoo.Tests.Library
[Collection(nameof(Postgresql))]
public class EpisodeTests : AEpisodeTests
{
public EpisodeTests(PostgresFixture postgres)
: base(new RepositoryActivator(postgres)) { }
public EpisodeTests(PostgresFixture postgres, ITestOutputHelper output)
: base(new RepositoryActivator(output, postgres)) { }
}
}
@ -165,5 +166,7 @@ namespace Kyoo.Tests.Library
episode = await _repository.Get(1);
Assert.Equal($"{TestSample.Get<Show>().Slug}-12", episode.Slug);
}
// TODO add movies tests.
}
}

View File

@ -3,6 +3,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Kyoo.Models;
using Xunit;
using Xunit.Abstractions;
namespace Kyoo.Tests.Library
{
@ -10,9 +11,9 @@ namespace Kyoo.Tests.Library
{
private readonly RepositoryActivator _repositories;
public GlobalTests()
public GlobalTests(ITestOutputHelper output)
{
_repositories = new RepositoryActivator();
_repositories = new RepositoryActivator(output);
}
[Fact]

View File

@ -2,6 +2,7 @@ using System.Threading.Tasks;
using Kyoo.Controllers;
using Kyoo.Models;
using Xunit;
using Xunit.Abstractions;
namespace Kyoo.Tests.Library
{
@ -9,8 +10,8 @@ namespace Kyoo.Tests.Library
{
public class SeasonTests : ASeasonTests
{
public SeasonTests()
: base(new RepositoryActivator()) { }
public SeasonTests(ITestOutputHelper output)
: base(new RepositoryActivator(output)) { }
}
}
@ -20,8 +21,8 @@ namespace Kyoo.Tests.Library
[Collection(nameof(Postgresql))]
public class SeasonTests : ASeasonTests
{
public SeasonTests(PostgresFixture postgres)
: base(new RepositoryActivator(postgres)) { }
public SeasonTests(PostgresFixture postgres, ITestOutputHelper output)
: base(new RepositoryActivator(output, postgres)) { }
}
}

View File

@ -6,6 +6,7 @@ using Kyoo.Controllers;
using Kyoo.Models;
using Microsoft.EntityFrameworkCore;
using Xunit;
using Xunit.Abstractions;
namespace Kyoo.Tests.Library
{
@ -13,8 +14,8 @@ namespace Kyoo.Tests.Library
{
public class ShowTests : AShowTests
{
public ShowTests()
: base(new RepositoryActivator()) { }
public ShowTests(ITestOutputHelper output)
: base(new RepositoryActivator(output)) { }
}
}
@ -23,8 +24,8 @@ namespace Kyoo.Tests.Library
[Collection(nameof(Postgresql))]
public class ShowTests : AShowTests
{
public ShowTests(PostgresFixture postgres)
: base(new RepositoryActivator(postgres)) { }
public ShowTests(PostgresFixture postgres, ITestOutputHelper output)
: base(new RepositoryActivator(output, postgres)) { }
}
}

View File

@ -7,6 +7,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Npgsql;
using Xunit;
using Xunit.Abstractions;
namespace Kyoo.Tests
{
@ -22,14 +23,18 @@ namespace Kyoo.Tests
/// </summary>
private readonly DbContextOptions<DatabaseContext> _context;
public SqLiteTestContext()
public SqLiteTestContext(ITestOutputHelper output)
{
_connection = new SqliteConnection("DataSource=:memory:");
_connection.Open();
_context = new DbContextOptionsBuilder<DatabaseContext>()
.UseSqlite(_connection)
.UseLoggerFactory(LoggerFactory.Create(x => x.AddConsole()))
.UseLoggerFactory(LoggerFactory.Create(x =>
{
x.ClearProviders();
x.AddXunit(output);
}))
.EnableSensitiveDataLogging()
.EnableDetailedErrors()
.Options;
@ -101,7 +106,7 @@ namespace Kyoo.Tests
private readonly NpgsqlConnection _connection;
private readonly DbContextOptions<DatabaseContext> _context;
public PostgresTestContext(PostgresFixture template)
public PostgresTestContext(PostgresFixture template, ITestOutputHelper output)
{
string id = Guid.NewGuid().ToString().Replace('-', '_');
string database = $"kyoo_test_{id}";
@ -118,7 +123,11 @@ namespace Kyoo.Tests
_context = new DbContextOptionsBuilder<DatabaseContext>()
.UseNpgsql(_connection)
.UseLoggerFactory(LoggerFactory.Create(x => x.AddConsole()))
.UseLoggerFactory(LoggerFactory.Create(x =>
{
x.ClearProviders();
x.AddXunit(output);
}))
.EnableSensitiveDataLogging()
.EnableDetailedErrors()
.Options;
@ -126,10 +135,10 @@ namespace Kyoo.Tests
public static string GetConnectionString(string database)
{
string server = Environment.GetEnvironmentVariable("SERVER") ?? "127.0.0.1";
string port = Environment.GetEnvironmentVariable("PORT") ?? "5432";
string username = Environment.GetEnvironmentVariable("USERNAME") ?? "kyoo";
string password = Environment.GetEnvironmentVariable("PASSWORD") ?? "kyooPassword";
string server = Environment.GetEnvironmentVariable("POSTGRES_HOST") ?? "127.0.0.1";
string port = Environment.GetEnvironmentVariable("POSTGRES_PORT") ?? "5432";
string username = Environment.GetEnvironmentVariable("POSTGRES_USERNAME") ?? "kyoo";
string password = Environment.GetEnvironmentVariable("POSTGRES_PASSWORD") ?? "kyooPassword";
return $"Server={server};Port={port};Database={database};User ID={username};Password={password};Include Error Detail=true";
}