Add UT for delete champion and add seedData

ET_Relation_Entity
Emre KARTAL 2 years ago
parent f220a8f678
commit 1c0bcaa52f

Binary file not shown.

@ -80,7 +80,8 @@ Et enfin cliquer sur l'icône ci-dessous pour faire la connexion à la solution
</div>
Vous pouvez dorénavant voir toutes les tables qui y sont enregistrées !
Vous pouvez dorénavant voir toutes les tables qui y sont enregistrées ! Si vous souhaitez ajouter des modifications à la base de données et les visualiser,
réaliser à nouveau la migration (ou *updater* celui actuel), puis supprimer toutes les tables et lancer la commande : **dotnet ef database update** et enfin rafraichiser la BD !
:information_source: *Notez qu'il est également possible grâce à SQLLite d'ajouter, modifier ou supprimer des données dans les tables.*

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyFlib
{
public static class DataSeeder
{
public static void SeedData(LolDbContext context)
{
ChampionEntity hecarim = new ChampionEntity { Name = "Hecarim", Class = ChampionClassEntity.Assassin, Bio = "", Icon = "", Image = "" };
ChampionEntity nasus = new ChampionEntity { Name = "Nasus", Class = ChampionClassEntity.Tank, Bio = "", Icon = "", Image = "" };
ChampionEntity ashe = new ChampionEntity { Name = "Ashe", Class = ChampionClassEntity.Marksman, Bio = "", Icon = "", Image = "" };
context.AddRange(hecarim, nasus, ashe);
context.SaveChanges();
}
}
}

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -10,14 +11,13 @@ namespace MyFlib
public class ChampionEntity
{
[Key]
public int Id { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[MaxLength(255)]
public string Name { get; set; }
[MaxLength(255)]
public string Bio { get; set; }
[MaxLength(255)]
public string Icon { get; set; }
[MaxLength(255)]
public string Image { get; set; }
public ChampionClassEntity Class { get; set; }

@ -28,12 +28,12 @@ namespace MyFlib
modelBuilder.Entity<ChampionEntity>().HasKey(e => e.Id);
modelBuilder.Entity<ChampionEntity>().Property(e => e.Id).ValueGeneratedOnAdd();
modelBuilder.Entity<ChampionEntity>().HasData(
new ChampionEntity { Id = 1, Name = "Akali", Class = ChampionClassEntity.Assassin, Bio = "", Icon = "", Image = "" },
new ChampionEntity { Id = 2, Name = "Aatrox", Class = ChampionClassEntity.Fighter, Bio = "", Icon = "", Image = "" },
new ChampionEntity { Id = 3, Name = "Ahri", Class = ChampionClassEntity.Mage, Bio = "", Icon = "", Image = "" },
new ChampionEntity { Id = 4, Name = "Akshan", Class = ChampionClassEntity.Marksman, Bio = "", Icon = "", Image = "" },
new ChampionEntity { Id = 5, Name = "Bard", Class = ChampionClassEntity.Support, Bio = "", Icon = "", Image = "" },
new ChampionEntity { Id = 6, Name = "Alistar", Class = ChampionClassEntity.Tank, Bio = "", Icon = "", Image = "" }
new ChampionEntity { Id = Guid.Parse("{4422C524-B2CB-43EF-8263-990C3CEA7CAE}"), Name = "Akali", Class = ChampionClassEntity.Assassin, Bio = "", Icon = "", Image = "" },
new ChampionEntity { Id = Guid.Parse("{A4F84D92-C20F-4F2D-B3F9-CA00EF556E72}"), Name = "Aatrox", Class = ChampionClassEntity.Fighter, Bio = "", Icon = "", Image = "" },
new ChampionEntity { Id = Guid.Parse("{AE5FE535-F041-445E-B570-28B75BC78CB9}"), Name = "Ahri", Class = ChampionClassEntity.Mage, Bio = "", Icon = "", Image = "" },
new ChampionEntity { Id = Guid.Parse("{3708dcfd-02a1-491e-b4f7-e75bf274cf23}"), Name = "Akshan", Class = ChampionClassEntity.Marksman, Bio = "", Icon = "", Image = "" },
new ChampionEntity { Id = Guid.Parse("{7f7746fa-b1cb-49da-9409-4b3e6910500e}"), Name = "Bard", Class = ChampionClassEntity.Support, Bio = "", Icon = "", Image = "" },
new ChampionEntity { Id = Guid.Parse("{36ad2a82-d17b-47de-8a95-6e154a7df557}"), Name = "Alistar", Class = ChampionClassEntity.Tank, Bio = "", Icon = "", Image = "" }
);
}

@ -1,114 +0,0 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MyFlib;
#nullable disable
namespace MyFlib.Migrations
{
[DbContext(typeof(LolDbContext))]
[Migration("20230204131532_MyMigration")]
partial class MyMigration
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.2");
modelBuilder.Entity("MyFlib.ChampionEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Bio")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.Property<int>("Class")
.HasColumnType("INTEGER");
b.Property<string>("Icon")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.Property<string>("Image")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Champions");
b.HasData(
new
{
Id = 1,
Bio = "",
Class = 1,
Icon = "",
Image = "",
Name = "Akali"
},
new
{
Id = 2,
Bio = "",
Class = 2,
Icon = "",
Image = "",
Name = "Aatrox"
},
new
{
Id = 3,
Bio = "",
Class = 3,
Icon = "",
Image = "",
Name = "Ahri"
},
new
{
Id = 4,
Bio = "",
Class = 4,
Icon = "",
Image = "",
Name = "Akshan"
},
new
{
Id = 5,
Bio = "",
Class = 5,
Icon = "",
Image = "",
Name = "Bard"
},
new
{
Id = 6,
Bio = "",
Class = 6,
Icon = "",
Image = "",
Name = "Alistar"
});
});
#pragma warning restore 612, 618
}
}
}

@ -1,53 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
namespace MyFlib.Migrations
{
/// <inheritdoc />
public partial class MyMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Champions",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
Bio = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
Icon = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
Image = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
Class = table.Column<int>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Champions", x => x.Id);
});
migrationBuilder.InsertData(
table: "Champions",
columns: new[] { "Id", "Bio", "Class", "Icon", "Image", "Name" },
values: new object[,]
{
{ 1, "", 1, "", "", "Akali" },
{ 2, "", 2, "", "", "Aatrox" },
{ 3, "", 3, "", "", "Ahri" },
{ 4, "", 4, "", "", "Akshan" },
{ 5, "", 5, "", "", "Bard" },
{ 6, "", 6, "", "", "Alistar" }
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Champions");
}
}
}

@ -1,111 +0,0 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MyFlib;
#nullable disable
namespace MyFlib.Migrations
{
[DbContext(typeof(LolDbContext))]
partial class LolDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.2");
modelBuilder.Entity("MyFlib.ChampionEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Bio")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.Property<int>("Class")
.HasColumnType("INTEGER");
b.Property<string>("Icon")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.Property<string>("Image")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Champions");
b.HasData(
new
{
Id = 1,
Bio = "",
Class = 1,
Icon = "",
Image = "",
Name = "Akali"
},
new
{
Id = 2,
Bio = "",
Class = 2,
Icon = "",
Image = "",
Name = "Aatrox"
},
new
{
Id = 3,
Bio = "",
Class = 3,
Icon = "",
Image = "",
Name = "Ahri"
},
new
{
Id = 4,
Bio = "",
Class = 4,
Icon = "",
Image = "",
Name = "Akshan"
},
new
{
Id = 5,
Bio = "",
Class = 5,
Icon = "",
Image = "",
Name = "Bard"
},
new
{
Id = 6,
Bio = "",
Class = 6,
Icon = "",
Image = "",
Name = "Alistar"
});
});
#pragma warning restore 612, 618
}
}
}

@ -6,7 +6,16 @@ using (var context = new LolDbContext())
foreach (var c in context.Champions)
{
Console.WriteLine($"{c.Name} - {c.Bio}");
Console.WriteLine($"{c.Name} - {c.Class}");
}
Console.WriteLine("\nWith new Champions :\n");
DataSeeder.SeedData(context);
foreach (var c in context.Champions)
{
Console.WriteLine($"{c.Name} - {c.Class}");
}
context.SaveChangesAsync(); // or context.SaveChangesAsync
}

@ -1,4 +1,3 @@
using Microsoft.EntityFrameworkCore;
using MyFlib;
using Xunit;
@ -20,9 +19,7 @@ namespace UT_EF
ChampionEntity hecarim = new ChampionEntity { Name = "Hecarim", Class = ChampionClassEntity.Fighter, Bio = "", Icon = "", Image = "" };
ChampionEntity yuumi = new ChampionEntity { Name = "yuumi", Class = ChampionClassEntity.Mage, Bio = "", Icon = "", Image = "" };
context.Champions.Add(sylas);
context.Champions.Add(hecarim);
context.Champions.Add(yuumi);
context.Champions.AddRange(sylas, hecarim, yuumi);
context.SaveChanges();
}
@ -46,20 +43,22 @@ namespace UT_EF
ChampionEntity hecarim = new ChampionEntity { Name = "Hecarim", Class = ChampionClassEntity.Fighter, Bio = "", Icon = "", Image = "" };
ChampionEntity yuumi = new ChampionEntity { Name = "yuumi", Class = ChampionClassEntity.Mage, Bio = "", Icon = "", Image = "" };
context.Champions.Add(sylas);
context.Champions.Add(hecarim);
context.Champions.Add(yuumi);
context.Champions.AddRange(sylas, hecarim, yuumi);
context.SaveChanges();
}
using (var context = new LolDbContext(options))
{
string nameToFind = "m";
ChampionClassEntity type = ChampionClassEntity.Fighter;
Assert.Equal(2, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count());
Assert.Equal(1, context.Champions.Where(c => c.Class == type).Count());
nameToFind = "yuu";
Assert.Equal(1, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count());
var ewok = context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).First();
ewok.Name = "Garen";
ewok.Bio = "Magic resist";
ewok.Class = type;
context.SaveChanges();
}
@ -68,8 +67,50 @@ namespace UT_EF
string nameToFind = "m";
Assert.Equal(1, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count());
nameToFind = "garen";
string bioToFind = "magic resist";
ChampionClassEntity type = ChampionClassEntity.Fighter;
Assert.Equal(1, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count());
Assert.Equal(1, context.Champions.Where(c => c.Bio.ToLower().Contains(bioToFind)).Count());
Assert.Equal(2, context.Champions.Where(c => c.Class == type).Count());
}
}
[Fact]
public void Delete_Test()
{
var options = new DbContextOptionsBuilder<LolDbContext>()
.UseInMemoryDatabase(databaseName: "Delete_Test_database")
.Options;
using (var context = new LolDbContext(options))
{
ChampionEntity sylas = new ChampionEntity { Name = "Sylas", Class = ChampionClassEntity.Assassin, Bio = "", Icon = "", Image = "" };
ChampionEntity hecarim = new ChampionEntity { Name = "Hecarim", Class = ChampionClassEntity.Fighter, Bio = "", Icon = "", Image = "" };
ChampionEntity yuumi = new ChampionEntity { Name = "yuumi", Class = ChampionClassEntity.Mage, Bio = "", Icon = "", Image = "" };
context.Champions.AddRange(sylas, hecarim, yuumi);
context.SaveChanges();
}
using (var context = new LolDbContext(options))
{
var ewok = context.Champions.First();
string nameToFind = "sylas";
Assert.Equal(1, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count());
Assert.Equal(3, context.Champions.Count());
context.Champions.Remove(ewok);
context.SaveChanges();
}
using (var context = new LolDbContext(options))
{
string nameToFind = "sylas";
Assert.Equal(0, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count());
Assert.Equal(2, context.Champions.Count());
}
}
}
}
Loading…
Cancel
Save