fin merge et ajouts tests consoles

master
Tom RAMBEAU 1 year ago
commit b356d9665f

@ -29,6 +29,40 @@ namespace DbContextLib
: base(options)
{ }
public void AddBook(BookEntity book)
{
if (!BooksSet.Contains(book))
{
BooksSet.Add(book);
}
}
public void AddPerson(PersonEntity person)
{
if (!PersonSet.Contains(person))
{
PersonSet.Add(person);
}
}
public void RemoveAll()
{
if (BooksSet.Count() != 0)
{
foreach (var book in BooksSet)
{
BooksSet.Remove(book);
}
}
if (PersonSet.Count() != 0)
{
foreach (var person in PersonSet)
{
PersonSet.Remove(person);
}
}
}
}
}

@ -0,0 +1,89 @@
// <auto-generated />
using System;
using DbContextLib;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DbContextLib.Migrations
{
[DbContext(typeof(LibraryContext))]
[Migration("20240216040156_Mg")]
partial class Mg
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.1");
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Author")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Isbn")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int?>("OwnerId")
.HasColumnType("INTEGER");
b.Property<int>("PersonId")
.HasColumnType("INTEGER");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("OwnerId");
b.ToTable("BooksSet");
});
modelBuilder.Entity("Entities.PersonEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("PersonSet");
});
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.HasOne("Entities.PersonEntity", "Owner")
.WithMany("Books")
.HasForeignKey("OwnerId");
b.Navigation("Owner");
});
modelBuilder.Entity("Entities.PersonEntity", b =>
{
b.Navigation("Books");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,65 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace DbContextLib.Migrations
{
/// <inheritdoc />
public partial class Mg : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "PersonSet",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
FirstName = table.Column<string>(type: "TEXT", nullable: false),
LastName = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_PersonSet", x => x.Id);
});
migrationBuilder.CreateTable(
name: "BooksSet",
columns: table => new
{
Id = table.Column<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Title = table.Column<string>(type: "TEXT", nullable: false),
Author = table.Column<string>(type: "TEXT", nullable: false),
Isbn = table.Column<string>(type: "TEXT", nullable: false),
PersonId = table.Column<int>(type: "INTEGER", nullable: false),
OwnerId = table.Column<int>(type: "INTEGER", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_BooksSet", x => x.Id);
table.ForeignKey(
name: "FK_BooksSet_PersonSet_OwnerId",
column: x => x.OwnerId,
principalTable: "PersonSet",
principalColumn: "Id");
});
migrationBuilder.CreateIndex(
name: "IX_BooksSet_OwnerId",
table: "BooksSet",
column: "OwnerId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "BooksSet");
migrationBuilder.DropTable(
name: "PersonSet");
}
}
}

@ -0,0 +1,86 @@
// <auto-generated />
using System;
using DbContextLib;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DbContextLib.Migrations
{
[DbContext(typeof(LibraryContext))]
partial class LibraryContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.1");
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Author")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Isbn")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int?>("OwnerId")
.HasColumnType("INTEGER");
b.Property<int>("PersonId")
.HasColumnType("INTEGER");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("OwnerId");
b.ToTable("BooksSet");
});
modelBuilder.Entity("Entities.PersonEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("PersonSet");
});
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.HasOne("Entities.PersonEntity", "Owner")
.WithMany("Books")
.HasForeignKey("OwnerId");
b.Navigation("Owner");
});
modelBuilder.Entity("Entities.PersonEntity", b =>
{
b.Navigation("Books");
});
#pragma warning restore 612, 618
}
}
}

@ -9,6 +9,7 @@ namespace Entities
{
public class BookEntity
{
public long Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }

@ -1,4 +1,5 @@
using System.Reflection;
using System.Text;
namespace Model
{
@ -31,7 +32,7 @@ namespace Model
get { return isbn; }
set { isbn = value; }
}
public Book(long id, string title, string author, string isbn)
public Book(long id, string title = "", string author = "", string isbn = "")
{
Id = id;
Title = title;
@ -39,6 +40,11 @@ namespace Model
Isbn = isbn;
}
public string Tostring(StringBuilder sb)
{
return Id.ToString() + " " + Title.ToString() + " " + Author.ToString() + " " + Isbn.ToString() ;
}
}

@ -1,13 +1,40 @@
using DbContextLib;
using Entities;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Model;
namespace Model2Entities
{
public class DbDataManager : IDataManager
{
public void CreateBook(Book book) {
using (var context = new LibraryContext())
{
context.Database.EnsureCreated();
if (!context.BooksSet.Contains(book.ConvertToEntity())){
context.BooksSet.Add(book.ConvertToEntity());
}
context.SaveChanges();
}
}
public List<Book> GetAllBooks()
{
List<Book> books = new List<Book>();
using (var context = new LibraryContext())
{
foreach (var bookEntity in context.BooksSet) {
books.Add(bookEntity.ConvertToModel());
}
return books;
}
}
public Book GetBookById(long id)
{
List<BookEntity> books = new List<BookEntity>();
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
@ -21,6 +48,93 @@ namespace Model2Entities
}
}
public Book GetBookByAuthor(string author)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Author == author)
{
return i.ConvertToModel();
}
}
return null;
}
}
public Book GetBookByTitle(string title)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Title == title)
{
return i.ConvertToModel();
}
}
return null;
}
}
public Book GetBookByIsbn(string isbn)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Isbn == isbn)
{
return i.ConvertToModel();
}
}
return null;
}
}
public void UpdateAuthor(long id, string author)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Id == id)
{
i.Author = author;
}
}
context.SaveChanges();
}
}
public void DeleteBook(long id)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Id == id)
{
context.Remove(i);
}
}
context.SaveChanges();
}
}
public void DeleteAll()
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
context.Remove(i);
}
context.SaveChanges();
}
}
}
}

@ -16,5 +16,9 @@ namespace Model2Entities
return new Book(book.Id, book.Title, book.Author, book.Isbn);
}
public static BookEntity ConvertToEntity(this Book book)
{
return new BookEntity() {Id= book.Id, Title = book.Title, Author = book.Author, Isbn = book.Isbn };
}
}
}

@ -11,7 +11,7 @@ using StubbedContextLib;
namespace StubbedContextLib.Migrations
{
[DbContext(typeof(StubbedContext))]
[Migration("20240215154956_Mg")]
[Migration("20240215195621_Mg")]
partial class Mg
{
/// <inheritdoc />

@ -1,3 +1,70 @@
// See https://aka.ms/new-console-template for more information
using Model;
using Entities;
using Model2Entities;
using Microsoft.Extensions.DependencyModel;
using DbContextLib;
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
DbDataManager dbDataManager = new DbDataManager();
dbDataManager.CreateBook(new Book(1, "mistake", "test1", "test1"));
dbDataManager.CreateBook(new Book(2, "the100", "test2", "test2"));
dbDataManager.CreateBook(new Book(3, "GOT", "lastTest", "lastTest"));
Console.WriteLine("test de récupération de tout les livres");
List<Book> books = dbDataManager.GetAllBooks();
foreach (var book in books)
{
Console.WriteLine("Titre du livre : " + book.Title);
}
Console.WriteLine("\ntest de récupération de livre par ID");
Book b1 = dbDataManager.GetBookById(3);
if (b1 != null)
{
Console.WriteLine("Le livre : " + b1.Title);
}
Console.WriteLine("\ntest de récupération de livre par Autheur");
Book b2 = dbDataManager.GetBookByIsbn("test2");
Console.WriteLine("Le livre : " + b2.Title);
Console.WriteLine("\ntest de récupération de livre par Titre");
Book b3 = dbDataManager.GetBookByTitle("the100");
Console.WriteLine("Le livre : " + b3.Title);
Console.WriteLine("\ntest de récupération de livre par ISBn");
Book b4 = dbDataManager.GetBookByAuthor("lastTest");
Console.WriteLine("Le livre : " + b4.Title);
Console.WriteLine("\n MAJ de l'auteur du livre th100");
dbDataManager.UpdateAuthor(2, "Kass Morgan");
Book the100 = dbDataManager.GetBookByAuthor("Kass Morgan");
Console.WriteLine(the100.Title + " " + the100.Author);
Console.WriteLine("\nsupresion du livre ou id = 1");
dbDataManager.DeleteBook(1);
Book test = dbDataManager.GetBookById(1);
if (test == null)
{
Console.WriteLine("livre supprimée");
}
Console.WriteLine("\nsuppression de tous les livres");
dbDataManager.DeleteAll();

@ -7,4 +7,19 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\Model2Entities\Model2Entities.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>

Binary file not shown.

@ -6,13 +6,348 @@ 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();
}
}
}
}
Loading…
Cancel
Save