Update Entity Champion and create UT for Add and Update Champion

EF_Entity_DB
Emre KARTAL 3 years ago
parent 6c2cd1d339
commit c0c61816ff

@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
</ItemGroup>
</Project>

@ -19,7 +19,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiLol", "ApiLol\ApiLol.csp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DTO", "DTO\DTO.csproj", "{3919E408-EB12-4422-989B-C6ED4816D465}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyFlib", "MyFlib\MyFlib.csproj", "{2142AB69-B483-4B0A-96DC-CFA87DEB11A5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyFlib", "MyFlib\MyFlib.csproj", "{2142AB69-B483-4B0A-96DC-CFA87DEB11A5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UT_EF", "Tests\UT_EF\UT_EF.csproj", "{F896C30B-A6FE-42B2-BAC8-08360BC48DC6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -55,6 +57,10 @@ Global
{2142AB69-B483-4B0A-96DC-CFA87DEB11A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2142AB69-B483-4B0A-96DC-CFA87DEB11A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2142AB69-B483-4B0A-96DC-CFA87DEB11A5}.Release|Any CPU.Build.0 = Release|Any CPU
{F896C30B-A6FE-42B2-BAC8-08360BC48DC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F896C30B-A6FE-42B2-BAC8-08360BC48DC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F896C30B-A6FE-42B2-BAC8-08360BC48DC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F896C30B-A6FE-42B2-BAC8-08360BC48DC6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -62,6 +68,7 @@ Global
GlobalSection(NestedProjects) = preSolution
{1889FA6E-B7C6-416E-8628-9449FB9070B9} = {C76D0C23-1FFA-4963-93CD-E12BD643F030}
{B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB} = {2C607793-B163-4731-A4D1-AFE8A7C4C170}
{F896C30B-A6FE-42B2-BAC8-08360BC48DC6} = {C76D0C23-1FFA-4963-93CD-E12BD643F030}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {92F3083D-793F-4552-8A9A-0AD6534159C9}

@ -12,6 +12,9 @@
<ItemGroup>
<Folder Include="enums\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

@ -1,19 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyFlib
{
public class ChampionEntity
{
[Key]
public string Name { get; set; }
public ChampionEntity(string name)
{
Name = name;
}
}
}

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyFlib
{
public class ChampionEntity
{
[Key]
public int 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; }
}
}

@ -10,16 +10,32 @@ namespace MyFlib
public class LolDbContext : DbContext
{
public DbSet<ChampionEntity> Champions { get; set; }
public LolDbContext()
{ }
public LolDbContext(DbContextOptions<LolDbContext> options) : base(options)
{ }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite(@"Data Source=DataBase.db");
optionsBuilder.UseSqlite("Data Source=DataBase.db");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ChampionEntity>().HasData(new ChampionEntity("Akali"));
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 = "" }
);
}
}

@ -1,40 +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("20230201163620_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<string>("Name")
.HasColumnType("TEXT");
b.HasKey("Name");
b.ToTable("Champions");
b.HasData(
new
{
Name = "Akali"
});
});
#pragma warning restore 612, 618
}
}
}

@ -1,37 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace MyFlib.Migrations
{
/// <inheritdoc />
public partial class MyMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Champions",
columns: table => new
{
Name = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Champions", x => x.Name);
});
migrationBuilder.InsertData(
table: "Champions",
column: "Name",
value: "Akali");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Champions");
}
}
}

@ -0,0 +1,114 @@
// <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("20230204102522_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
}
}
}

@ -0,0 +1,53 @@
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");
}
}
}

@ -18,17 +18,91 @@ namespace MyFlib.Migrations
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("Name");
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

@ -16,6 +16,7 @@
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

@ -1,10 +1,12 @@
// See https://aka.ms/new-console-template for more information
using MyFlib;
Console.WriteLine("Hello, World!");
using (var context = new LolDbContext())
{
//var champions = context.Champions.where
foreach (var c in context.Champions)
{
Console.WriteLine($"{c.Name} - {c.Bio}");
}
context.SaveChangesAsync(); // or context.SaveChangesAsync
}

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyFlib
{
public enum ChampionClassEntity
{
Unknown,
Assassin,
Fighter,
Mage,
Marksman,
Support,
Tank,
}
}

@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
</ItemGroup>
</Project>

@ -6,6 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>

@ -15,6 +15,7 @@
<None Remove="Microsoft.Extensions.DependencyInjection" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
</ItemGroup>
</Project>

@ -0,0 +1,75 @@
using Microsoft.EntityFrameworkCore;
using MyFlib;
using Xunit;
namespace UT_EF
{
public class UnitTest1
{
[Fact]
public void TestAdd()
{
var options = new DbContextOptionsBuilder<LolDbContext>()
.UseInMemoryDatabase(databaseName: "Add_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.Add(sylas);
context.Champions.Add(hecarim);
context.Champions.Add(yuumi);
context.SaveChanges();
}
using (var context = new LolDbContext(options))
{
Assert.Equal(3, context.Champions.Count());
Assert.Equal("Sylas", context.Champions.First().Name);
}
}
[Fact]
public void Modify_Test()
{
var options = new DbContextOptionsBuilder<LolDbContext>()
.UseInMemoryDatabase(databaseName: "Modify_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.Add(sylas);
context.Champions.Add(hecarim);
context.Champions.Add(yuumi);
context.SaveChanges();
}
using (var context = new LolDbContext(options))
{
string nameToFind = "m";
Assert.Equal(2, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).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";
context.SaveChanges();
}
using (var context = new LolDbContext(options))
{
string nameToFind = "m";
Assert.Equal(1, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count());
nameToFind = "garen";
Assert.Equal(1, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count());
}
}
}
}

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\MyFlib\MyFlib.csproj" />
</ItemGroup>
</Project>
Loading…
Cancel
Save