Compare commits
15 Commits
Author | SHA1 | Date |
---|---|---|
|
b356d9665f | 1 year ago |
|
5aacc394b5 | 1 year ago |
|
be6777ab42 | 1 year ago |
|
c0298586d1 | 1 year ago |
|
af5b49759c | 1 year ago |
|
e3e8566103 | 1 year ago |
|
f040e94c6a | 1 year ago |
|
0c51fa72bd | 1 year ago |
|
368f7d1442 | 1 year ago |
|
a014509135 | 1 year ago |
![]() |
3dd8e6dcb7 | 1 year ago |
![]() |
c2dc6889ae | 1 year ago |
|
589a4bd413 | 1 year ago |
![]() |
e1fcd7d1f9 | 1 year ago |
|
bda361a1e3 | 1 year ago |
@ -1,2 +1,10 @@
|
|||||||
# tp1Entity
|
# tp1Entity
|
||||||
|
|
||||||
|
Le répot correspond au projet d'EntityFrameWork.
|
||||||
|
|
||||||
|
Vous y trouverez le tp1 dans la branche part1.
|
||||||
|
|
||||||
|
Concernant l'organisation de dépôt, un choix temporaire a été réalisé
|
||||||
|
de mêttre dans toutes les classes dans le projet TestStub qui
|
||||||
|
correpond à l'application console. Ce choix a été
|
||||||
|
réalisé pour régler un problème de migrations.
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1 @@
|
|||||||
|
global using Xunit;
|
@ -0,0 +1,353 @@
|
|||||||
|
using DbContextLib;
|
||||||
|
using StubbedContextLib;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System.Linq;
|
||||||
|
using Xunit;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using System;
|
||||||
|
using static System.Reflection.Metadata.BlobBuilder;
|
||||||
|
|
||||||
|
namespace UnitTests
|
||||||
|
{
|
||||||
|
public class UnitTest1
|
||||||
|
{
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Add_TestBooks()
|
||||||
|
{
|
||||||
|
//connection must be opened to use In-memory database
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
|
||||||
|
|
||||||
|
//prepares the database with one instance of the context
|
||||||
|
//using (var context = new StubbedContext(options))
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
context.PersonSet.Include(pers => pers.Books).ToList();
|
||||||
|
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id =3 };
|
||||||
|
BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 };
|
||||||
|
BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 };
|
||||||
|
context.AddBook(the100);
|
||||||
|
context.AddBook(princeOfPersia);
|
||||||
|
context.AddBook(PercyJackson);
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//uses another instance of the context to do the tests
|
||||||
|
//using (var context = new StubbedContext(options))
|
||||||
|
using (var context = new StubbedContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
Assert.Equal(3, context.BooksSet.Count());
|
||||||
|
Assert.Equal(1, context.BooksSet.Where(b => b.Title.Contains("the100")).Count());
|
||||||
|
context.RemoveAll();
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Add_TestPersons()
|
||||||
|
{
|
||||||
|
//connection must be opened to use In-memory database
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
|
||||||
|
|
||||||
|
//prepares the database with one instance of the context
|
||||||
|
//using (var context = new StubbedContext(options))
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
//context.Database.OpenConnection();
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
context.PersonSet.Include(pers => pers.Books).ToList();
|
||||||
|
PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 };
|
||||||
|
PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 };
|
||||||
|
context.AddPerson(p1);
|
||||||
|
context.AddPerson(p2);
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//uses another instance of the context to do the tests
|
||||||
|
//using (var context = new StubbedContext(options))
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
Assert.Equal(2, context.PersonSet.Count());
|
||||||
|
Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.Contains("Jean")).Count());
|
||||||
|
context.RemoveAll();
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Modify_TestBook()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
|
||||||
|
|
||||||
|
//prepares the database with one instance of the context
|
||||||
|
|
||||||
|
//using (var context = new StubbedContext(options))
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
context.PersonSet.Include(pers => pers.Books).ToList();
|
||||||
|
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id = 3 };
|
||||||
|
BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 };
|
||||||
|
BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 };
|
||||||
|
context.AddBook(the100);
|
||||||
|
context.AddBook(princeOfPersia);
|
||||||
|
context.AddBook(PercyJackson);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//prepares the database with one instance of the context
|
||||||
|
//using (var context = new StubbedContext(options))
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
context.PersonSet.Include(pers => pers.Books).ToList();
|
||||||
|
var book = context.BooksSet.Where(n => n.Title.Contains("princeOfPersia")).FirstOrDefault();
|
||||||
|
book.Title = "l'Odyssée";
|
||||||
|
Assert.Equal("l'Odyssée", book.Title);
|
||||||
|
context.RemoveAll();
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Modify_TestPerson()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
|
||||||
|
|
||||||
|
//prepares the database with one instance of the context
|
||||||
|
|
||||||
|
//prepares the database with one instance of the context
|
||||||
|
//using (var context = new StubbedContext(options))
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
|
||||||
|
context.PersonSet.Include(pers => pers.Books).ToList();
|
||||||
|
PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 };
|
||||||
|
PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 };
|
||||||
|
context.AddPerson(p1);
|
||||||
|
context.AddPerson(p2);
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
//prepares the database with one instance of the context
|
||||||
|
//using (var context = new StubbedContext(options))
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
context.PersonSet.Include(pers => pers.Books).ToList();
|
||||||
|
string nameToFind = "Jean";
|
||||||
|
Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.Contains(nameToFind)).Count());
|
||||||
|
var person = context.PersonSet.Where(p => p.FirstName.Contains(nameToFind)).First();
|
||||||
|
person.FirstName = "Jacques";
|
||||||
|
|
||||||
|
Assert.Equal("Jacques", person.FirstName);
|
||||||
|
|
||||||
|
context.RemoveAll();
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Delete_TestPerson()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
|
||||||
|
|
||||||
|
//prepares the database with one instance of the context
|
||||||
|
//using (var context = new StubbedContext(options))
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 };
|
||||||
|
PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 };
|
||||||
|
context.AddPerson(p1);
|
||||||
|
context.AddPerson(p2);
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
//using (var context = new StubbedContext(options))
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
|
||||||
|
context.PersonSet.Include(pers => pers.Books).ToList();
|
||||||
|
var Persons = context.PersonSet;
|
||||||
|
if (Persons.Count() != 0)
|
||||||
|
{
|
||||||
|
foreach (var person in Persons)
|
||||||
|
{
|
||||||
|
context.PersonSet.Remove(person);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
context.SaveChanges();
|
||||||
|
Assert.Equal( 0, context.PersonSet.Count());
|
||||||
|
context.RemoveAll();
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Delete_TestBook()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
|
||||||
|
|
||||||
|
//prepares the database with one instance of the context
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id = 3 };
|
||||||
|
BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 };
|
||||||
|
BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 };
|
||||||
|
context.AddBook(the100);
|
||||||
|
context.AddBook(princeOfPersia);
|
||||||
|
context.AddBook(PercyJackson);
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
//using (var context = new StubbedContext(options))
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
context.PersonSet.Include(pers => pers.Books).ToList();
|
||||||
|
var Books = context.BooksSet;
|
||||||
|
if (Books.Count() != 0)
|
||||||
|
{
|
||||||
|
foreach (var book in Books)
|
||||||
|
{
|
||||||
|
context.BooksSet.Remove(book);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
context.SaveChanges();
|
||||||
|
Assert.Equal(0, context.BooksSet.Count());
|
||||||
|
context.RemoveAll();
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Update_TestEmprunt()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
|
||||||
|
|
||||||
|
//prepares the database with one instance of the context
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
|
||||||
|
context.PersonSet.Include(pers => pers.Books).ToList();
|
||||||
|
PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 };
|
||||||
|
PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 };
|
||||||
|
context.AddPerson(p1);
|
||||||
|
context.AddPerson(p2);
|
||||||
|
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id = 3 };
|
||||||
|
BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 };
|
||||||
|
BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 };
|
||||||
|
context.AddBook(the100);
|
||||||
|
context.AddBook(princeOfPersia);
|
||||||
|
context.AddBook(PercyJackson);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//using (var context = new StubbedContext(options))
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
var books = context.BooksSet;
|
||||||
|
|
||||||
|
context.PersonSet.Include(pers => pers.Books).ToList();
|
||||||
|
var person = context.PersonSet.Where(b => b.FirstName.Contains("Jean")).First();
|
||||||
|
foreach (var book in books)
|
||||||
|
{
|
||||||
|
if (book.Owner == null)
|
||||||
|
{
|
||||||
|
book.Owner = person;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
Assert.NotNull(context.BooksSet.Where(b => b.Owner.FirstName.Contains("Jean")).First());
|
||||||
|
context.RemoveAll();
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Update_TestRendu()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
|
||||||
|
|
||||||
|
//prepares the database with one instance of the context
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
|
||||||
|
context.PersonSet.Include(pers => pers.Books).ToList();
|
||||||
|
PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 };
|
||||||
|
PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 };
|
||||||
|
context.AddPerson(p1);
|
||||||
|
context.AddPerson(p2);
|
||||||
|
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id = 3 };
|
||||||
|
BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 };
|
||||||
|
BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 };
|
||||||
|
context.AddBook(the100);
|
||||||
|
context.AddBook(princeOfPersia);
|
||||||
|
context.AddBook(PercyJackson);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//using (var context = new StubbedContext(options))
|
||||||
|
using (var context = new StubbedContext())
|
||||||
|
{
|
||||||
|
var books = context.BooksSet;
|
||||||
|
|
||||||
|
context.PersonSet.Include(pers => pers.Books).ToList();
|
||||||
|
var person = context.PersonSet.Where(b => b.FirstName.StartsWith("Jean")).First();
|
||||||
|
var testbook = new BookEntity();
|
||||||
|
foreach (var book in books)
|
||||||
|
{
|
||||||
|
if (book.Owner == null)
|
||||||
|
{
|
||||||
|
book.Owner = person;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var book in books)
|
||||||
|
{
|
||||||
|
//Console.WriteLine(book.Owner.FirstName);
|
||||||
|
if (book.Owner != null)
|
||||||
|
{
|
||||||
|
book.Owner = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Assert.Equal(context.BooksSet.Count(), context.BooksSet.Where(b => b.Owner == null).Count());
|
||||||
|
context.RemoveAll();
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -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>
|
Loading…
Reference in new issue