parent
5ed4479c7d
commit
2a1f585932
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,100 @@
|
||||
using DTO;
|
||||
using EntityFramwork;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace TestUnitaireBDD
|
||||
{
|
||||
public class TestChampions
|
||||
{
|
||||
[Fact]
|
||||
public void Add_Test()
|
||||
{
|
||||
//connection must be opened to use In-memory database
|
||||
var connection = new SqliteConnection("DataSource=:memory:");
|
||||
connection.Open();
|
||||
|
||||
var options = new DbContextOptionsBuilder<BDDContext>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
|
||||
//prepares the database with one instance of the context
|
||||
using (var context = new BDDContext(options))
|
||||
{
|
||||
//context.Database.OpenConnection();
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
EntityChampions chewie = new EntityChampions { Name = "Chewbacca",Bio ="Inconnu",Icon="Inconnu" };
|
||||
EntityChampions yoda = new EntityChampions { Name = "Yoda",Bio = "Inconnu", Icon = "Inconnu" };
|
||||
EntityChampions ewok = new EntityChampions { Name = "Ewok",Bio = "Inconnu", Icon = "Inconnu" };
|
||||
|
||||
context.Add(chewie);
|
||||
context.Add(yoda);
|
||||
context.Add(ewok);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
//uses another instance of the context to do the tests
|
||||
using (var context = new BDDContext(options))
|
||||
{
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
Assert.Equal(3, context.Champions.Count());
|
||||
Assert.Equal("Chewbacca", context.Champions.First().Name);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Modify_Test()
|
||||
{
|
||||
//connection must be opened to use In-memory database
|
||||
var connection = new SqliteConnection("DataSource=:memory:");
|
||||
connection.Open();
|
||||
|
||||
var options = new DbContextOptionsBuilder<BDDContext>()
|
||||
.UseSqlite(connection)
|
||||
.Options;
|
||||
|
||||
//prepares the database with one instance of the context
|
||||
using (var context = new BDDContext(options))
|
||||
{
|
||||
//context.Database.OpenConnection();
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
EntityChampions chewie = new EntityChampions { Name = "Chewbacca", Bio = "Inconnu", Icon = "Inconnu" };
|
||||
EntityChampions yoda = new EntityChampions { Name = "Yoda", Bio = "Inconnu", Icon = "Inconnu" };
|
||||
EntityChampions ewok = new EntityChampions { Name = "Ewok", Bio = "Inconnu", Icon = "Inconnu" };
|
||||
|
||||
context.Add(chewie);
|
||||
context.Add(yoda);
|
||||
context.Add(ewok);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
//uses another instance of the context to do the tests
|
||||
using (var context = new BDDContext(options))
|
||||
{
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
string nameToFind = "ew";
|
||||
Assert.Equal(2, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
|
||||
nameToFind = "wo";
|
||||
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
|
||||
var ewok = context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).First();
|
||||
ewok.Name = "Wicket";
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
//uses another instance of the context to do the tests
|
||||
using (var context = new BDDContext(options))
|
||||
{
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
string nameToFind = "ew";
|
||||
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
|
||||
nameToFind = "wick";
|
||||
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<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.EntityFrameworkCore.Sqlite" Version="7.0.2" />
|
||||
<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="..\EntityFramwork\EntityFramwork.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1 @@
|
||||
global using Xunit;
|
Loading…
Reference in new issue