mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Fixing test database fill
This commit is contained in:
parent
b8d4a2ed33
commit
e91083afa8
@ -140,6 +140,23 @@ namespace Kyoo
|
||||
.Property(t => t.IsForced)
|
||||
.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>()
|
||||
.HasMany(x => x.Libraries)
|
||||
.WithMany(x => x.Providers)
|
||||
|
@ -27,14 +27,14 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../Kyoo.CommonAPI/Kyoo.CommonAPI.csproj">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<Private>false</Private>
|
||||
<ExcludeAssets>runtime</ExcludeAssets>
|
||||
<!-- <PrivateAssets>all</PrivateAssets>-->
|
||||
<!-- <Private>false</Private>-->
|
||||
<!-- <ExcludeAssets>runtime</ExcludeAssets>-->
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="../Kyoo.Common/Kyoo.Common.csproj">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<Private>false</Private>
|
||||
<ExcludeAssets>runtime</ExcludeAssets>
|
||||
<!-- <PrivateAssets>all</PrivateAssets>-->
|
||||
<!-- <Private>false</Private>-->
|
||||
<!-- <ExcludeAssets>runtime</ExcludeAssets>-->
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
namespace Kyoo.Postgresql.Migrations
|
||||
{
|
||||
[DbContext(typeof(PostgresContext))]
|
||||
[Migration("20210616203804_Initial")]
|
||||
[Migration("20210619153358_Initial")]
|
||||
partial class Initial
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
@ -634,7 +634,8 @@ namespace Kyoo.Postgresql.Migrations
|
||||
{
|
||||
b.HasOne("Kyoo.Models.Season", "Season")
|
||||
.WithMany("Episodes")
|
||||
.HasForeignKey("SeasonID");
|
||||
.HasForeignKey("SeasonID")
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
b.HasOne("Kyoo.Models.Show", "Show")
|
||||
.WithMany("Episodes")
|
@ -432,7 +432,7 @@ namespace Kyoo.Postgresql.Migrations
|
||||
column: x => x.SeasonID,
|
||||
principalTable: "Seasons",
|
||||
principalColumn: "ID",
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_Episodes_Shows_ShowID",
|
||||
column: x => x.ShowID,
|
@ -632,7 +632,8 @@ namespace Kyoo.Postgresql.Migrations
|
||||
{
|
||||
b.HasOne("Kyoo.Models.Season", "Season")
|
||||
.WithMany("Episodes")
|
||||
.HasForeignKey("SeasonID");
|
||||
.HasForeignKey("SeasonID")
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
b.HasOne("Kyoo.Models.Show", "Show")
|
||||
.WithMany("Episodes")
|
||||
|
@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
namespace Kyoo.SqLite.Migrations
|
||||
{
|
||||
[DbContext(typeof(SqLiteContext))]
|
||||
[Migration("20210613135157_Initial")]
|
||||
[Migration("20210619154617_Initial")]
|
||||
partial class Initial
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
@ -614,7 +614,8 @@ namespace Kyoo.SqLite.Migrations
|
||||
{
|
||||
b.HasOne("Kyoo.Models.Season", "Season")
|
||||
.WithMany("Episodes")
|
||||
.HasForeignKey("SeasonID");
|
||||
.HasForeignKey("SeasonID")
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
b.HasOne("Kyoo.Models.Show", "Show")
|
||||
.WithMany("Episodes")
|
@ -424,7 +424,7 @@ namespace Kyoo.SqLite.Migrations
|
||||
column: x => x.SeasonID,
|
||||
principalTable: "Seasons",
|
||||
principalColumn: "ID",
|
||||
onDelete: ReferentialAction.Restrict);
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_Episodes_Shows_ShowID",
|
||||
column: x => x.ShowID,
|
@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
namespace Kyoo.SqLite.Migrations
|
||||
{
|
||||
[DbContext(typeof(SqLiteContext))]
|
||||
[Migration("20210613135215_Triggers")]
|
||||
[Migration("20210619154654_Triggers")]
|
||||
partial class Triggers
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
@ -614,7 +614,8 @@ namespace Kyoo.SqLite.Migrations
|
||||
{
|
||||
b.HasOne("Kyoo.Models.Season", "Season")
|
||||
.WithMany("Episodes")
|
||||
.HasForeignKey("SeasonID");
|
||||
.HasForeignKey("SeasonID")
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
b.HasOne("Kyoo.Models.Show", "Show")
|
||||
.WithMany("Episodes")
|
@ -2,34 +2,34 @@
|
||||
|
||||
namespace Kyoo.SqLite.Migrations
|
||||
{
|
||||
public partial class Triggers : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.Sql(@"
|
||||
public partial class Triggers : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.Sql(@"
|
||||
CREATE TRIGGER SeasonSlugInsert AFTER INSERT ON Seasons FOR EACH ROW
|
||||
BEGIN
|
||||
UPDATE Seasons SET Slug = (SELECT Slug from Shows WHERE ID = ShowID) || '-s' || SeasonNumber
|
||||
WHERE ID == new.ID;
|
||||
END");
|
||||
migrationBuilder.Sql(@"
|
||||
migrationBuilder.Sql(@"
|
||||
CREATE TRIGGER SeasonSlugUpdate AFTER UPDATE OF SeasonNumber, ShowID ON Seasons FOR EACH ROW
|
||||
BEGIN
|
||||
UPDATE Seasons SET Slug = (SELECT Slug from Shows WHERE ID = ShowID) || '-s' || SeasonNumber
|
||||
WHERE ID == new.ID;
|
||||
END");
|
||||
migrationBuilder.Sql(@"
|
||||
migrationBuilder.Sql(@"
|
||||
CREATE TRIGGER ShowSlugUpdate AFTER UPDATE OF Slug ON Shows FOR EACH ROW
|
||||
BEGIN
|
||||
UPDATE Seasons SET Slug = new.Slug || '-s' || SeasonNumber WHERE ShowID = new.ID;
|
||||
END;");
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.Sql("DROP TRIGGER SeasonSlugInsert;");
|
||||
migrationBuilder.Sql("DROP TRIGGER SeasonSlugUpdate;");
|
||||
migrationBuilder.Sql("DROP TRIGGER ShowSlugUpdate;");
|
||||
}
|
||||
}
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.Sql("DROP TRIGGER SeasonSlugInsert;");
|
||||
migrationBuilder.Sql("DROP TRIGGER SeasonSlugUpdate;");
|
||||
migrationBuilder.Sql("DROP TRIGGER ShowSlugUpdate;");
|
||||
}
|
||||
}
|
||||
}
|
@ -612,7 +612,8 @@ namespace Kyoo.SqLite.Migrations
|
||||
{
|
||||
b.HasOne("Kyoo.Models.Season", "Season")
|
||||
.WithMany("Episodes")
|
||||
.HasForeignKey("SeasonID");
|
||||
.HasForeignKey("SeasonID")
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
b.HasOne("Kyoo.Models.Show", "Show")
|
||||
.WithMany("Episodes")
|
||||
|
@ -23,27 +23,6 @@ namespace Kyoo.Tests.SpecificTests
|
||||
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()
|
||||
{
|
||||
_repositories.Dispose();
|
@ -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>
|
||||
{
|
||||
private readonly ISeasonRepository _repository;
|
||||
|
@ -268,5 +268,21 @@ namespace Kyoo.Tests.SpecificTests
|
||||
ICollection<Show> ret = await _repository.Search(query);
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ using Kyoo.Postgresql;
|
||||
using Kyoo.SqLite;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
using Xunit;
|
||||
|
||||
@ -80,6 +81,7 @@ namespace Kyoo.Tests
|
||||
conn.ReloadTypes();
|
||||
|
||||
TestSample.FillDatabase(context);
|
||||
conn.Close();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
@ -111,12 +113,15 @@ namespace Kyoo.Tests
|
||||
|
||||
_context = new DbContextOptionsBuilder<DatabaseContext>()
|
||||
.UseNpgsql(_connection)
|
||||
.UseLoggerFactory(LoggerFactory.Create(x => x.AddConsole()))
|
||||
.EnableSensitiveDataLogging()
|
||||
.EnableDetailedErrors()
|
||||
.Options;
|
||||
}
|
||||
|
||||
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()
|
||||
|
@ -6,6 +6,15 @@ namespace Kyoo.Tests
|
||||
{
|
||||
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()
|
||||
{
|
||||
{
|
||||
@ -26,8 +35,8 @@ namespace Kyoo.Tests
|
||||
"school students, they had long ceased to think of each other as friends.",
|
||||
Status = Status.Finished,
|
||||
TrailerUrl = null,
|
||||
StartAir = new DateTime(2011),
|
||||
EndAir = new DateTime(2011),
|
||||
StartAir = new DateTime(2011, 1, 1),
|
||||
EndAir = new DateTime(2011, 1, 1),
|
||||
Poster = "poster",
|
||||
Logo = "logo",
|
||||
Backdrop = "backdrop",
|
||||
@ -85,12 +94,31 @@ namespace Kyoo.Tests
|
||||
return (T)Samples[typeof(T)]();
|
||||
}
|
||||
|
||||
public static T GetNew<T>()
|
||||
{
|
||||
return (T)NewSamples[typeof(T)]();
|
||||
}
|
||||
|
||||
public static void FillDatabase(DatabaseContext context)
|
||||
{
|
||||
context.Shows.Add(Get<Show>());
|
||||
context.Seasons.Add(Get<Season>());
|
||||
// context.Episodes.Add(Get<Episode>());
|
||||
// context.People.Add(Get<People>());
|
||||
Show show = Get<Show>();
|
||||
show.ID = 0;
|
||||
context.Shows.Add(show);
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -124,12 +124,8 @@ namespace Kyoo.Controllers
|
||||
if (obj == null)
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
|
||||
_database.Entry(obj).State = EntityState.Deleted;
|
||||
obj.ExternalIDs.ForEach(x => _database.Entry(x).State = EntityState.Deleted);
|
||||
_database.Remove(obj);
|
||||
await _database.SaveChangesAsync();
|
||||
//
|
||||
// if (obj.Episodes != null)
|
||||
// await _episodes.Value.DeleteRange(obj.Episodes);
|
||||
}
|
||||
}
|
||||
}
|
@ -181,7 +181,7 @@ namespace Kyoo.Controllers
|
||||
/// <inheritdoc />
|
||||
public override async Task Delete(Show obj)
|
||||
{
|
||||
_database.Entry(obj).State = EntityState.Deleted;
|
||||
_database.Remove(obj);
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ using Kyoo.Authentication;
|
||||
using Kyoo.Controllers;
|
||||
using Kyoo.Models;
|
||||
using Kyoo.Models.Options;
|
||||
using Kyoo.Postgresql;
|
||||
using Kyoo.SqLite;
|
||||
using Kyoo.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
@ -46,8 +47,8 @@ namespace Kyoo
|
||||
// TODO remove postgres from here and load it like a normal plugin.
|
||||
_plugins.LoadPlugins(new IPlugin[] {
|
||||
new CoreModule(configuration),
|
||||
// new PostgresModule(configuration, host),
|
||||
new SqLiteModule(configuration, host),
|
||||
new PostgresModule(configuration, host),
|
||||
// new SqLiteModule(configuration, host),
|
||||
new AuthenticationModule(configuration, loggerFactory, host)
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user