avancement part4

master
Tom RAMBEAU 1 year ago
parent e3e8566103
commit af5b49759c

@ -7,7 +7,6 @@ using System.Threading.Tasks;
namespace Entities namespace Entities
{ {
[Table("TableBook")]
public class BookEntity public class BookEntity
{ {
public long Id { get; set; } public long Id { get; set; }

@ -0,0 +1,47 @@
using System.Reflection;
namespace Model
{
public class Book
{
private long id;
public long Id
{
get { return id; }
set { id = value;}
}
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
private string author;
public string Author
{
get { return author; }
set { author = value; }
}
private string isbn;
public string Isbn {
get { return isbn; }
set { isbn = value; }
}
public Book(long id, string title, string author, string isbn)
{
Id = id;
Title = title;
Author = author;
Isbn = isbn;
}
}
}

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
public enum BookOrderCriteria
{
None,
ByTitle,
ByAuthor,
ByIsbn
}
}

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
public interface IDataManager
{
//public IEnumerable<Book> GetBooks(int index, int count, BookOrderCriteria orderCriterium);
//public IEnumerable<Book> GetBooksByTitle(string title, int index, int count, BookOrderCriteria orderCriterium);
//public IEnumerable<Book> GetBooksByAuthor(string author, int index, int count, BookOrderCriteria orderCriterium);
//public IEnumerable<Book> GetBooksByIsbn(string isbn, int index, int count, BookOrderCriteria orderCriterium);
public Book GetBookById(long id);
//public void CreateBook(string title, string author, string isbn);
//public void UpdateBook(long id, Book book);
//public void DeleteBook(long id);
}
}

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,26 @@
using DbContextLib;
using Entities;
using Model;
namespace Model2Entities
{
public class DbDataManager : IDataManager
{
public Book GetBookById(long id)
{
List<BookEntity> books = new List<BookEntity>();
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Id == id)
{
return i.ConvertToModel();
}
}
return null;
}
}
}
}

@ -0,0 +1,20 @@
using Entities;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Model2Entities
{
public static class Extensions
{
public static Book ConvertToModel(this BookEntity book)
{
return new Book(book.Id, book.Title, book.Author, book.Isbn);
}
}
}

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -11,7 +11,7 @@ using StubbedContextLib;
namespace StubbedContextLib.Migrations namespace StubbedContextLib.Migrations
{ {
[DbContext(typeof(StubbedContext))] [DbContext(typeof(StubbedContext))]
[Migration("20240215144801_Mg")] [Migration("20240215154956_Mg")]
partial class Mg partial class Mg
{ {
/// <inheritdoc /> /// <inheritdoc />
@ -48,7 +48,7 @@ namespace StubbedContextLib.Migrations
b.HasIndex("OwnerId"); b.HasIndex("OwnerId");
b.ToTable("TableBook"); b.ToTable("BooksSet");
b.HasData( b.HasData(
new new

@ -27,7 +27,7 @@ namespace StubbedContextLib.Migrations
}); });
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "TableBook", name: "BooksSet",
columns: table => new columns: table => new
{ {
Id = table.Column<long>(type: "INTEGER", nullable: false) Id = table.Column<long>(type: "INTEGER", nullable: false)
@ -40,21 +40,16 @@ namespace StubbedContextLib.Migrations
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("PK_TableBook", x => x.Id); table.PrimaryKey("PK_BooksSet", x => x.Id);
table.ForeignKey( table.ForeignKey(
name: "FK_TableBook_PersonSet_OwnerId", name: "FK_BooksSet_PersonSet_OwnerId",
column: x => x.OwnerId, column: x => x.OwnerId,
principalTable: "PersonSet", principalTable: "PersonSet",
principalColumn: "Id"); principalColumn: "Id");
}); });
migrationBuilder.InsertData( migrationBuilder.InsertData(
table: "PersonSet", table: "BooksSet",
columns: new[] { "Id", "FirstName", "LastName" },
values: new object[] { 1, "coco", "test" });
migrationBuilder.InsertData(
table: "TableBook",
columns: new[] { "Id", "Author", "Isbn", "OwnerId", "PersonId", "Title" }, columns: new[] { "Id", "Author", "Isbn", "OwnerId", "PersonId", "Title" },
values: new object[,] values: new object[,]
{ {
@ -62,9 +57,14 @@ namespace StubbedContextLib.Migrations
{ 2L, "test2", "test2", null, 1, "test2" } { 2L, "test2", "test2", null, 1, "test2" }
}); });
migrationBuilder.InsertData(
table: "PersonSet",
columns: new[] { "Id", "FirstName", "LastName" },
values: new object[] { 1, "coco", "test" });
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_TableBook_OwnerId", name: "IX_BooksSet_OwnerId",
table: "TableBook", table: "BooksSet",
column: "OwnerId"); column: "OwnerId");
} }
@ -72,7 +72,7 @@ namespace StubbedContextLib.Migrations
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "TableBook"); name: "BooksSet");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "PersonSet"); name: "PersonSet");

@ -45,7 +45,7 @@ namespace StubbedContextLib.Migrations
b.HasIndex("OwnerId"); b.HasIndex("OwnerId");
b.ToTable("TableBook"); b.ToTable("BooksSet");
b.HasData( b.HasData(
new new

@ -12,7 +12,6 @@ namespace StubbedContextLib
{ {
public class StubbedContext : LibraryContext public class StubbedContext : LibraryContext
{ {
public StubbedContext() { public StubbedContext() {
} }
@ -32,8 +31,6 @@ namespace StubbedContextLib
} }
public StubbedContext(DbContextOptions<LibraryContext> options) public StubbedContext(DbContextOptions<LibraryContext> options)
: base(options) : base(options)

@ -0,0 +1,3 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

Binary file not shown.

@ -11,6 +11,55 @@ namespace UnitTests
{ {
public class UnitTest1 public class UnitTest1
{ {
[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))
{
context.PersonSet.Include(pers => pers.Books).ToList();
var allPers = context.PersonSet;
foreach (var person in allPers)
{
context.PersonSet.Remove(person);
}
Assert.Equal(0, context.PersonSet.Count());
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(options))
{
context.PersonSet.Include(pers => pers.Books).ToList();
var allBooks = context.BooksSet;
foreach (var book in allBooks)
{
context.BooksSet.Remove(book);
}
Assert.Equal(0, context.BooksSet.Count());
context.SaveChanges();
}
}
[Fact] [Fact]
public void Add_TestBooks() public void Add_TestBooks()
{ {
@ -25,9 +74,9 @@ namespace UnitTests
//context.Database.OpenConnection(); //context.Database.OpenConnection();
context.Database.EnsureCreated(); context.Database.EnsureCreated();
context.PersonSet.Include(pers => pers.Books).ToList(); context.PersonSet.Include(pers => pers.Books).ToList();
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id =3 }; BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id =8 };
BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 }; BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 10 };
BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 }; BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 9 };
context.BooksSet.Add(the100); context.BooksSet.Add(the100);
context.BooksSet.Add(princeOfPersia); context.BooksSet.Add(princeOfPersia);
context.BooksSet.Add(PercyJackson); context.BooksSet.Add(PercyJackson);
@ -84,7 +133,7 @@ namespace UnitTests
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options; 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
//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(options))
{ {
@ -119,7 +168,6 @@ namespace UnitTests
//prepares the database with one instance of the context //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(options))
{ {
@ -143,55 +191,10 @@ namespace UnitTests
} }
} }
[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))
{
context.PersonSet.Include(pers => pers.Books).ToList();
var nb = context.PersonSet.Count();
if (nb > 0)
{
context.PersonSet.Remove(context.PersonSet.FirstOrDefault());
}
Assert.Equal(context.PersonSet.Count(), nb - 1);
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
//prepares the database with one instance of the context
using (var context = new StubbedContext(options))
{
context.PersonSet.Include(pers => pers.Books).ToList();
var nb = context.BooksSet.Count();
if (nb > 0)
{
context.BooksSet.Remove(context.BooksSet.FirstOrDefault());
}
Assert.Equal(context.BooksSet.Count(), nb - 1);
context.SaveChanges();
}
}
[Fact] [Fact]
public void Delete_TestEmprunt() public void Update_TestEmprunt()
{ {
var connection = new SqliteConnection("DataSource=:memory:"); var connection = new SqliteConnection("DataSource=:memory:");
connection.Open(); connection.Open();
@ -199,7 +202,6 @@ namespace UnitTests
//prepares the database with one instance of the context //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(options))
{ {
var books = context.BooksSet; var books = context.BooksSet;
@ -223,7 +225,7 @@ namespace UnitTests
} }
[Fact] [Fact]
public void Delete_TestRendu() public void Update_TestRendu()
{ {
var connection = new SqliteConnection("DataSource=:memory:"); var connection = new SqliteConnection("DataSource=:memory:");
connection.Open(); connection.Open();
@ -231,8 +233,7 @@ namespace UnitTests
//prepares the database with one instance of the context //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(options))
{ {
var books = context.BooksSet; var books = context.BooksSet;

Loading…
Cancel
Save