ajouts des tests

part2
Tom RAMBEAU 1 year ago
parent 589a4bd413
commit a014509135

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Entities;
using Microsoft.Extensions.Options;
namespace DbContextLib
@ -16,8 +17,18 @@ namespace DbContextLib
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=tp.Books.db");
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite("Data Source=tp.Books.db");
}
}
public LibraryContext()
{ }
public LibraryContext(DbContextOptions <LibraryContext> options )
: base(options)
{ }
}
}

@ -9,13 +9,13 @@ Console.WriteLine("Hello, World!");
using (var context = new LibraryContext())
{
BookEntity chewie = new BookEntity("B3", "test1", "test1");
BookEntity yoda = new BookEntity ("B4", "test2", "test2");
BookEntity ewok = new BookEntity ("mistake", "test3", "test3");
BookEntity princeOfPersia = new BookEntity ("B4", "test2", "test2");
BookEntity PercyJackson = new BookEntity ("mistake", "test3", "test3");
BookEntity the100 = new BookEntity("the100", "test4", "test4");
context.BooksSet.Add(chewie);
context.BooksSet.Add(yoda);
context.BooksSet.Add(ewok);
context.BooksSet.Add(princeOfPersia);
context.BooksSet.Add(PercyJackson);
context.BooksSet.Add(the100);
context.SaveChanges();

@ -0,0 +1 @@
global using Xunit;

@ -0,0 +1,48 @@
using DbContextLib;
using Entities;
using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using Xunit;
using Microsoft.Data.Sqlite;
namespace UnitTests
{
public class UnitTest1
{
[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<LibraryContext>.UseSqlLite().options;
//prepares the database with one instance of the context
using (var context = new LibraryContext(options))
{
//context.Database.OpenConnection();
context.Database.EnsureCreated();
BookEntity the100 = new BookEntity( "the100", "", "" );
BookEntity princeOfPersia = new BookEntity ( "princeOfPersia", "", "" );
BookEntity PercyJackson = new BookEntity ("PercyJackson", "", "" );
context.BooksSet.Add(the100);
context.BooksSet.Add(princeOfPersia);
context.BooksSet.Add(PercyJackson);
context.SaveChanges();
}
//uses another instance of the context to do the tests
using (var context = new LibraryContext(options))
{
context.Database.EnsureCreated();
Assert.Equal(3, context.BooksSet.Count());
Assert.Equal("the100", context.BooksSet.First().Title);
}
}
}
}

@ -0,0 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<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="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\StubbedContextLib\StubbedContextLib.csproj" />
</ItemGroup>
</Project>

@ -5,11 +5,13 @@ VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestStub", "TestStub\TestStub.csproj", "{8960D74C-259D-4779-80B6-789BA257D2B2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbContextLib", "DbContextLib\DbContextLib.csproj", "{A4130190-8883-42DE-89CA-3DF205DE710A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbContextLib", "DbContextLib\DbContextLib.csproj", "{A4130190-8883-42DE-89CA-3DF205DE710A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubbedContextLib", "StubbedContextLib\StubbedContextLib.csproj", "{F793C071-E178-48D3-BBE6-22F99D102C2B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubbedContextLib", "StubbedContextLib\StubbedContextLib.csproj", "{F793C071-E178-48D3-BBE6-22F99D102C2B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "Entities\Entities.csproj", "{5C673F26-2E43-4AA8-944E-1A3B309927CF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Entities", "Entities\Entities.csproj", "{5C673F26-2E43-4AA8-944E-1A3B309927CF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -33,6 +35,10 @@ Global
{5C673F26-2E43-4AA8-944E-1A3B309927CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C673F26-2E43-4AA8-944E-1A3B309927CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C673F26-2E43-4AA8-944E-1A3B309927CF}.Release|Any CPU.Build.0 = Release|Any CPU
{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Loading…
Cancel
Save