Fixing test database fill

This commit is contained in:
Zoe Roux 2021-06-20 13:54:51 +02:00
parent b8d4a2ed33
commit e91083afa8
18 changed files with 124 additions and 69 deletions

View File

@ -140,6 +140,23 @@ namespace Kyoo
.Property(t => t.IsForced) .Property(t => t.IsForced)
.ValueGeneratedNever(); .ValueGeneratedNever();
modelBuilder.Entity<Show>()
.HasMany(x => x.Seasons)
.WithOne(x => x.Show)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Show>()
.HasMany(x => x.Episodes)
.WithOne(x => x.Show)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Season>()
.HasMany(x => x.Episodes)
.WithOne(x => x.Season)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Episode>()
.HasMany(x => x.Tracks)
.WithOne(x => x.Episode)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Provider>() modelBuilder.Entity<Provider>()
.HasMany(x => x.Libraries) .HasMany(x => x.Libraries)
.WithMany(x => x.Providers) .WithMany(x => x.Providers)

View File

@ -27,14 +27,14 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="../Kyoo.CommonAPI/Kyoo.CommonAPI.csproj"> <ProjectReference Include="../Kyoo.CommonAPI/Kyoo.CommonAPI.csproj">
<PrivateAssets>all</PrivateAssets> <!-- <PrivateAssets>all</PrivateAssets>-->
<Private>false</Private> <!-- <Private>false</Private>-->
<ExcludeAssets>runtime</ExcludeAssets> <!-- <ExcludeAssets>runtime</ExcludeAssets>-->
</ProjectReference> </ProjectReference>
<ProjectReference Include="../Kyoo.Common/Kyoo.Common.csproj"> <ProjectReference Include="../Kyoo.Common/Kyoo.Common.csproj">
<PrivateAssets>all</PrivateAssets> <!-- <PrivateAssets>all</PrivateAssets>-->
<Private>false</Private> <!-- <Private>false</Private>-->
<ExcludeAssets>runtime</ExcludeAssets> <!-- <ExcludeAssets>runtime</ExcludeAssets>-->
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Kyoo.Postgresql.Migrations namespace Kyoo.Postgresql.Migrations
{ {
[DbContext(typeof(PostgresContext))] [DbContext(typeof(PostgresContext))]
[Migration("20210616203804_Initial")] [Migration("20210619153358_Initial")]
partial class Initial partial class Initial
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -634,7 +634,8 @@ namespace Kyoo.Postgresql.Migrations
{ {
b.HasOne("Kyoo.Models.Season", "Season") b.HasOne("Kyoo.Models.Season", "Season")
.WithMany("Episodes") .WithMany("Episodes")
.HasForeignKey("SeasonID"); .HasForeignKey("SeasonID")
.OnDelete(DeleteBehavior.Cascade);
b.HasOne("Kyoo.Models.Show", "Show") b.HasOne("Kyoo.Models.Show", "Show")
.WithMany("Episodes") .WithMany("Episodes")

View File

@ -432,7 +432,7 @@ namespace Kyoo.Postgresql.Migrations
column: x => x.SeasonID, column: x => x.SeasonID,
principalTable: "Seasons", principalTable: "Seasons",
principalColumn: "ID", principalColumn: "ID",
onDelete: ReferentialAction.Restrict); onDelete: ReferentialAction.Cascade);
table.ForeignKey( table.ForeignKey(
name: "FK_Episodes_Shows_ShowID", name: "FK_Episodes_Shows_ShowID",
column: x => x.ShowID, column: x => x.ShowID,

View File

@ -632,7 +632,8 @@ namespace Kyoo.Postgresql.Migrations
{ {
b.HasOne("Kyoo.Models.Season", "Season") b.HasOne("Kyoo.Models.Season", "Season")
.WithMany("Episodes") .WithMany("Episodes")
.HasForeignKey("SeasonID"); .HasForeignKey("SeasonID")
.OnDelete(DeleteBehavior.Cascade);
b.HasOne("Kyoo.Models.Show", "Show") b.HasOne("Kyoo.Models.Show", "Show")
.WithMany("Episodes") .WithMany("Episodes")

View File

@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Kyoo.SqLite.Migrations namespace Kyoo.SqLite.Migrations
{ {
[DbContext(typeof(SqLiteContext))] [DbContext(typeof(SqLiteContext))]
[Migration("20210613135157_Initial")] [Migration("20210619154617_Initial")]
partial class Initial partial class Initial
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -614,7 +614,8 @@ namespace Kyoo.SqLite.Migrations
{ {
b.HasOne("Kyoo.Models.Season", "Season") b.HasOne("Kyoo.Models.Season", "Season")
.WithMany("Episodes") .WithMany("Episodes")
.HasForeignKey("SeasonID"); .HasForeignKey("SeasonID")
.OnDelete(DeleteBehavior.Cascade);
b.HasOne("Kyoo.Models.Show", "Show") b.HasOne("Kyoo.Models.Show", "Show")
.WithMany("Episodes") .WithMany("Episodes")

View File

@ -424,7 +424,7 @@ namespace Kyoo.SqLite.Migrations
column: x => x.SeasonID, column: x => x.SeasonID,
principalTable: "Seasons", principalTable: "Seasons",
principalColumn: "ID", principalColumn: "ID",
onDelete: ReferentialAction.Restrict); onDelete: ReferentialAction.Cascade);
table.ForeignKey( table.ForeignKey(
name: "FK_Episodes_Shows_ShowID", name: "FK_Episodes_Shows_ShowID",
column: x => x.ShowID, column: x => x.ShowID,

View File

@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Kyoo.SqLite.Migrations namespace Kyoo.SqLite.Migrations
{ {
[DbContext(typeof(SqLiteContext))] [DbContext(typeof(SqLiteContext))]
[Migration("20210613135215_Triggers")] [Migration("20210619154654_Triggers")]
partial class Triggers partial class Triggers
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -614,7 +614,8 @@ namespace Kyoo.SqLite.Migrations
{ {
b.HasOne("Kyoo.Models.Season", "Season") b.HasOne("Kyoo.Models.Season", "Season")
.WithMany("Episodes") .WithMany("Episodes")
.HasForeignKey("SeasonID"); .HasForeignKey("SeasonID")
.OnDelete(DeleteBehavior.Cascade);
b.HasOne("Kyoo.Models.Show", "Show") b.HasOne("Kyoo.Models.Show", "Show")
.WithMany("Episodes") .WithMany("Episodes")

View File

@ -2,34 +2,34 @@
namespace Kyoo.SqLite.Migrations namespace Kyoo.SqLite.Migrations
{ {
public partial class Triggers : Migration public partial class Triggers : Migration
{ {
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.Sql(@" migrationBuilder.Sql(@"
CREATE TRIGGER SeasonSlugInsert AFTER INSERT ON Seasons FOR EACH ROW CREATE TRIGGER SeasonSlugInsert AFTER INSERT ON Seasons FOR EACH ROW
BEGIN BEGIN
UPDATE Seasons SET Slug = (SELECT Slug from Shows WHERE ID = ShowID) || '-s' || SeasonNumber UPDATE Seasons SET Slug = (SELECT Slug from Shows WHERE ID = ShowID) || '-s' || SeasonNumber
WHERE ID == new.ID; WHERE ID == new.ID;
END"); END");
migrationBuilder.Sql(@" migrationBuilder.Sql(@"
CREATE TRIGGER SeasonSlugUpdate AFTER UPDATE OF SeasonNumber, ShowID ON Seasons FOR EACH ROW CREATE TRIGGER SeasonSlugUpdate AFTER UPDATE OF SeasonNumber, ShowID ON Seasons FOR EACH ROW
BEGIN BEGIN
UPDATE Seasons SET Slug = (SELECT Slug from Shows WHERE ID = ShowID) || '-s' || SeasonNumber UPDATE Seasons SET Slug = (SELECT Slug from Shows WHERE ID = ShowID) || '-s' || SeasonNumber
WHERE ID == new.ID; WHERE ID == new.ID;
END"); END");
migrationBuilder.Sql(@" migrationBuilder.Sql(@"
CREATE TRIGGER ShowSlugUpdate AFTER UPDATE OF Slug ON Shows FOR EACH ROW CREATE TRIGGER ShowSlugUpdate AFTER UPDATE OF Slug ON Shows FOR EACH ROW
BEGIN BEGIN
UPDATE Seasons SET Slug = new.Slug || '-s' || SeasonNumber WHERE ShowID = new.ID; UPDATE Seasons SET Slug = new.Slug || '-s' || SeasonNumber WHERE ShowID = new.ID;
END;"); END;");
} }
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.Sql("DROP TRIGGER SeasonSlugInsert;"); migrationBuilder.Sql("DROP TRIGGER SeasonSlugInsert;");
migrationBuilder.Sql("DROP TRIGGER SeasonSlugUpdate;"); migrationBuilder.Sql("DROP TRIGGER SeasonSlugUpdate;");
migrationBuilder.Sql("DROP TRIGGER ShowSlugUpdate;"); migrationBuilder.Sql("DROP TRIGGER ShowSlugUpdate;");
} }
} }
} }

View File

@ -612,7 +612,8 @@ namespace Kyoo.SqLite.Migrations
{ {
b.HasOne("Kyoo.Models.Season", "Season") b.HasOne("Kyoo.Models.Season", "Season")
.WithMany("Episodes") .WithMany("Episodes")
.HasForeignKey("SeasonID"); .HasForeignKey("SeasonID")
.OnDelete(DeleteBehavior.Cascade);
b.HasOne("Kyoo.Models.Show", "Show") b.HasOne("Kyoo.Models.Show", "Show")
.WithMany("Episodes") .WithMany("Episodes")

View File

@ -23,27 +23,6 @@ namespace Kyoo.Tests.SpecificTests
Assert.False(ReferenceEquals(TestSample.Get<Show>(), TestSample.Get<Show>())); Assert.False(ReferenceEquals(TestSample.Get<Show>(), TestSample.Get<Show>()));
} }
[Fact]
public async Task DeleteShowWithEpisodeAndSeason()
{
Show show = TestSample.Get<Show>();
show.Seasons = new[]
{
TestSample.Get<Season>()
};
show.Seasons.First().Episodes = new[]
{
TestSample.Get<Episode>()
};
await _repositories.Context.AddAsync(show);
Assert.Equal(1, await _repositories.LibraryManager.ShowRepository.GetCount());
await _repositories.LibraryManager.ShowRepository.Delete(show);
Assert.Equal(0, await _repositories.LibraryManager.ShowRepository.GetCount());
Assert.Equal(0, await _repositories.LibraryManager.SeasonRepository.GetCount());
Assert.Equal(0, await _repositories.LibraryManager.EpisodeRepository.GetCount());
}
public void Dispose() public void Dispose()
{ {
_repositories.Dispose(); _repositories.Dispose();

View File

@ -12,6 +12,14 @@ namespace Kyoo.Tests.SpecificTests
{ } { }
} }
[Collection(nameof(Postgresql))]
public class PostgresSeasonTests : SeasonTests
{
public PostgresSeasonTests(PostgresFixture postgres)
: base(new RepositoryActivator(postgres))
{ }
}
public abstract class SeasonTests : RepositoryTests<Season> public abstract class SeasonTests : RepositoryTests<Season>
{ {
private readonly ISeasonRepository _repository; private readonly ISeasonRepository _repository;

View File

@ -268,5 +268,21 @@ namespace Kyoo.Tests.SpecificTests
ICollection<Show> ret = await _repository.Search(query); ICollection<Show> ret = await _repository.Search(query);
KAssert.DeepEqual(value, ret.First()); KAssert.DeepEqual(value, ret.First());
} }
[Fact]
public async Task DeleteShowWithEpisodeAndSeason()
{
Show show = TestSample.Get<Show>();
await Repositories.LibraryManager.Load(show, x => x.Seasons);
await Repositories.LibraryManager.Load(show, x => x.Episodes);
Assert.Equal(1, await _repository.GetCount());
Assert.Equal(1, show.Seasons.Count);
Assert.Equal(1, show.Episodes.Count);
await _repository.Delete(show);
Assert.Equal(0, await Repositories.LibraryManager.ShowRepository.GetCount());
Assert.Equal(0, await Repositories.LibraryManager.SeasonRepository.GetCount());
Assert.Equal(0, await Repositories.LibraryManager.EpisodeRepository.GetCount());
}
} }
} }

View File

@ -4,6 +4,7 @@ using Kyoo.Postgresql;
using Kyoo.SqLite; using Kyoo.SqLite;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Npgsql; using Npgsql;
using Xunit; using Xunit;
@ -80,6 +81,7 @@ namespace Kyoo.Tests
conn.ReloadTypes(); conn.ReloadTypes();
TestSample.FillDatabase(context); TestSample.FillDatabase(context);
conn.Close();
} }
public void Dispose() public void Dispose()
@ -111,12 +113,15 @@ namespace Kyoo.Tests
_context = new DbContextOptionsBuilder<DatabaseContext>() _context = new DbContextOptionsBuilder<DatabaseContext>()
.UseNpgsql(_connection) .UseNpgsql(_connection)
.UseLoggerFactory(LoggerFactory.Create(x => x.AddConsole()))
.EnableSensitiveDataLogging()
.EnableDetailedErrors()
.Options; .Options;
} }
public static string GetConnectionString(string database) public static string GetConnectionString(string database)
{ {
return $"Server=127.0.0.1;Port=5432;Database={database};User ID=kyoo;Password=kyooPassword"; return $"Server=127.0.0.1;Port=5432;Database={database};User ID=kyoo;Password=kyooPassword;Include Error Detail=true";
} }
public override void Dispose() public override void Dispose()

View File

@ -6,6 +6,15 @@ namespace Kyoo.Tests
{ {
public static class TestSample public static class TestSample
{ {
private static readonly Dictionary<Type, Func<object>> NewSamples = new()
{
{
typeof(Show),
() => new Show()
}
};
private static readonly Dictionary<Type, Func<object>> Samples = new() private static readonly Dictionary<Type, Func<object>> Samples = new()
{ {
{ {
@ -26,8 +35,8 @@ namespace Kyoo.Tests
"school students, they had long ceased to think of each other as friends.", "school students, they had long ceased to think of each other as friends.",
Status = Status.Finished, Status = Status.Finished,
TrailerUrl = null, TrailerUrl = null,
StartAir = new DateTime(2011), StartAir = new DateTime(2011, 1, 1),
EndAir = new DateTime(2011), EndAir = new DateTime(2011, 1, 1),
Poster = "poster", Poster = "poster",
Logo = "logo", Logo = "logo",
Backdrop = "backdrop", Backdrop = "backdrop",
@ -84,13 +93,32 @@ namespace Kyoo.Tests
{ {
return (T)Samples[typeof(T)](); return (T)Samples[typeof(T)]();
} }
public static T GetNew<T>()
{
return (T)NewSamples[typeof(T)]();
}
public static void FillDatabase(DatabaseContext context) public static void FillDatabase(DatabaseContext context)
{ {
context.Shows.Add(Get<Show>()); Show show = Get<Show>();
context.Seasons.Add(Get<Season>()); show.ID = 0;
// context.Episodes.Add(Get<Episode>()); context.Shows.Add(show);
// context.People.Add(Get<People>());
Season season = Get<Season>();
season.ID = 0;
season.ShowID = 0;
season.Show = show;
context.Seasons.Add(season);
Episode episode = Get<Episode>();
episode.ID = 0;
episode.ShowID = 0;
episode.Show = show;
episode.SeasonID = 0;
episode.Season = season;
context.Episodes.Add(episode);
context.SaveChanges(); context.SaveChanges();
} }
} }

View File

@ -123,13 +123,9 @@ namespace Kyoo.Controllers
{ {
if (obj == null) if (obj == null)
throw new ArgumentNullException(nameof(obj)); throw new ArgumentNullException(nameof(obj));
_database.Entry(obj).State = EntityState.Deleted; _database.Remove(obj);
obj.ExternalIDs.ForEach(x => _database.Entry(x).State = EntityState.Deleted);
await _database.SaveChangesAsync(); await _database.SaveChangesAsync();
//
// if (obj.Episodes != null)
// await _episodes.Value.DeleteRange(obj.Episodes);
} }
} }
} }

View File

@ -181,7 +181,7 @@ namespace Kyoo.Controllers
/// <inheritdoc /> /// <inheritdoc />
public override async Task Delete(Show obj) public override async Task Delete(Show obj)
{ {
_database.Entry(obj).State = EntityState.Deleted; _database.Remove(obj);
await _database.SaveChangesAsync(); await _database.SaveChangesAsync();
} }
} }

View File

@ -4,6 +4,7 @@ using Kyoo.Authentication;
using Kyoo.Controllers; using Kyoo.Controllers;
using Kyoo.Models; using Kyoo.Models;
using Kyoo.Models.Options; using Kyoo.Models.Options;
using Kyoo.Postgresql;
using Kyoo.SqLite; using Kyoo.SqLite;
using Kyoo.Tasks; using Kyoo.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
@ -46,8 +47,8 @@ namespace Kyoo
// TODO remove postgres from here and load it like a normal plugin. // TODO remove postgres from here and load it like a normal plugin.
_plugins.LoadPlugins(new IPlugin[] { _plugins.LoadPlugins(new IPlugin[] {
new CoreModule(configuration), new CoreModule(configuration),
// new PostgresModule(configuration, host), new PostgresModule(configuration, host),
new SqLiteModule(configuration, host), // new SqLiteModule(configuration, host),
new AuthenticationModule(configuration, loggerFactory, host) new AuthenticationModule(configuration, loggerFactory, host)
}); });
} }