Adding simple tests on Champion Entity
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
527c0e71f5
commit
cf107b961b
Binary file not shown.
@ -0,0 +1,162 @@
|
||||
using Business;
|
||||
using Entities;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Model;
|
||||
using Shared;
|
||||
|
||||
namespace TestEF
|
||||
{
|
||||
public class TestChampion
|
||||
{
|
||||
|
||||
[Fact]
|
||||
public async void Test_Add()
|
||||
{
|
||||
//connection must be opened to use In-memory database
|
||||
var connection = new SqliteConnection("DataSource=:memory:");
|
||||
connection.Open();
|
||||
|
||||
var options = new DbContextOptionsBuilder<LolDbContext>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
|
||||
using (var context = new LolDbContext(options))
|
||||
{
|
||||
var manager = new DbData(context).ChampionsMgr;
|
||||
|
||||
//context.Database.OpenConnection();
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
Champion batman = new("Batman", ChampionClass.Assassin);
|
||||
Champion endeavor = new("Endeavor", ChampionClass.Tank);
|
||||
Champion escanor = new("Escanor", ChampionClass.Fighter);
|
||||
|
||||
await manager.AddItem(batman);
|
||||
await manager.AddItem(endeavor);
|
||||
await manager.AddItem(escanor);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
//uses another instance of the context to do the tests
|
||||
using (var context = new LolDbContext(options))
|
||||
{
|
||||
var manager = new DbData(context).ChampionsMgr;
|
||||
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
var nbItems = await manager.GetNbItems();
|
||||
Assert.Equal(3, nbItems);
|
||||
|
||||
var items = await manager.GetItemsByName("Batman", 0, nbItems);
|
||||
Assert.Equal("Batman", items.First().Name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Test_Update()
|
||||
{
|
||||
//connection must be opened to use In-memory database
|
||||
var connection = new SqliteConnection("DataSource=:memory:");
|
||||
connection.Open();
|
||||
|
||||
var options = new DbContextOptionsBuilder<LolDbContext>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
|
||||
//prepares the database with one instance of the context
|
||||
using (var context = new LolDbContext(options))
|
||||
{
|
||||
var manager = new DbData(context).ChampionsMgr;
|
||||
|
||||
//context.Database.OpenConnection();
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
Champion batman = new("Batman", ChampionClass.Assassin);
|
||||
Champion endeavor = new("Endeavor", ChampionClass.Tank);
|
||||
Champion escanor = new("Escanor", ChampionClass.Fighter);
|
||||
|
||||
await manager.AddItem(batman);
|
||||
await manager.AddItem(endeavor);
|
||||
await manager.AddItem(escanor);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
//uses another instance of the context to do the tests
|
||||
using (var context = new LolDbContext(options))
|
||||
{
|
||||
var manager = new DbData(context).ChampionsMgr;
|
||||
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
|
||||
var itemsByName = await manager.GetItemsByName("E", 0, 3);
|
||||
Assert.Equal(2, itemsByName.Count());
|
||||
|
||||
var escanor = context.champions.Where(n => n.Name.Contains("Esc")).First();
|
||||
|
||||
escanor.Class = ChampionClass.Tank;
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
//uses another instance of the context to do the tests
|
||||
using (var context = new LolDbContext(options))
|
||||
{
|
||||
var manager = new DbData(context).ChampionsMgr;
|
||||
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
var itemsByName = await manager.GetItemsByClass(ChampionClass.Tank, 0, 3);
|
||||
|
||||
Assert.Equal(2, itemsByName.Count());
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Test_Delete()
|
||||
{
|
||||
//connection must be opened to use In-memory database
|
||||
var connection = new SqliteConnection("DataSource=:memory:");
|
||||
connection.Open();
|
||||
|
||||
var options = new DbContextOptionsBuilder<LolDbContext>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
|
||||
//prepares the database with one instance of the context
|
||||
using (var context = new LolDbContext(options))
|
||||
{
|
||||
var manager = new DbData(context).ChampionsMgr;
|
||||
|
||||
//context.Database.OpenConnection();
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
Champion batman = new("Batman", ChampionClass.Assassin);
|
||||
Champion endeavor = new("Endeavor", ChampionClass.Tank);
|
||||
Champion escanor = new("Escanor", ChampionClass.Fighter);
|
||||
|
||||
await manager.AddItem(batman);
|
||||
await manager.AddItem(endeavor);
|
||||
await manager.AddItem(escanor);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
//uses another instance of the context to do the tests
|
||||
using (var context = new LolDbContext(options))
|
||||
{
|
||||
var manager = new DbData(context).ChampionsMgr;
|
||||
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
var endeavor = (await manager.GetItemsByName("Endeavor", 0, 3)).First();
|
||||
|
||||
var itemsByName = await manager.DeleteItem(endeavor);
|
||||
Assert.Equal(2, await manager.GetNbItems());
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
<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" Version="7.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.3" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
|
||||
<PackageReference Include="xunit" Version="2.4.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
<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="..\Business\Business.csproj" />
|
||||
<ProjectReference Include="..\Entities\Entities.csproj" />
|
||||
<ProjectReference Include="..\Model\Model.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1 @@
|
||||
global using Xunit;
|
Loading…
Reference in new issue