MAJ des derniers tests

master
Tom RAMBEAU 1 year ago
parent f040e94c6a
commit e3e8566103

@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Entities
{
[Table("TableBook")]
public class BookEntity
{
public long Id { get; set; }

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

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

@ -45,7 +45,7 @@ namespace StubbedContextLib.Migrations
b.HasIndex("OwnerId");
b.ToTable("BookEntity");
b.ToTable("TableBook");
b.HasData(
new
@ -82,7 +82,7 @@ namespace StubbedContextLib.Migrations
b.HasKey("Id");
b.ToTable("PersonEntity");
b.ToTable("PersonSet");
b.HasData(
new

@ -12,8 +12,6 @@ namespace StubbedContextLib
{
public class StubbedContext : LibraryContext
{
public DbSet<PersonEntity> Persons { get; set; }
public DbSet<BookEntity> Books { get; set; }
public StubbedContext() {

Binary file not shown.

@ -104,8 +104,8 @@ namespace UnitTests
{
context.PersonSet.Include(pers => pers.Books).ToList();
string nameToFind = "princeOfPersia";
Assert.Equal(2, context.Books.Where(n => n.Title.ToLower().Contains(nameToFind)).Count());
var ewok = context.Books.Where(n => n.Title.ToLower().Contains(nameToFind)).First();
Assert.Equal(2, context.BooksSet.Where(n => n.Title.ToLower().Contains(nameToFind)).Count());
var ewok = context.BooksSet.Where(n => n.Title.ToLower().Contains(nameToFind)).First();
ewok.Title = "l'Odyssée";
context.SaveChanges();
}
@ -190,6 +190,78 @@ namespace UnitTests
}
[Fact]
public void Delete_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
//prepares the database with one instance of the context
using (var context = new StubbedContext(options))
{
var books = context.BooksSet;
context.PersonSet.Include(pers => pers.Books).ToList();
var person = context.PersonSet.Where(b => b.FirstName.StartsWith("coco")).First();
var testbook = new BookEntity();
foreach (var book in books)
{
if (book.Owner == null)
{
person.Books.Add(book);
testbook = book;
break;
}
}
Assert.Equal(testbook.Owner, person);
context.SaveChanges();
}
}
[Fact]
public void Delete_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
//prepares the database with one instance of the context
using (var context = new StubbedContext(options))
{
var books = context.BooksSet;
context.PersonSet.Include(pers => pers.Books).ToList();
var person = context.PersonSet.Where(b => b.FirstName.StartsWith("coco")).First();
var testbook = new BookEntity();
foreach (var book in books)
{
if (book.Owner == null)
{
person.Books.Add(book);
testbook = book;
break;
}
}
foreach (var book in books)
{
//Console.WriteLine(book.Owner.FirstName);
if (book.Owner != null)
{
book.Owner = null;
}
}
Assert.Null(testbook.Owner);
context.SaveChanges();
}
}
}

Loading…
Cancel
Save