part2
Tom RAMBEAU 1 year ago
parent 368f7d1442
commit 0c51fa72bd

@ -6,10 +6,9 @@ using System.Threading.Tasks;
namespace Entities namespace Entities
{ {
public class BookEntity public class BookEntity
{ {
public long ID { get; set; } public long Id { get; set; }
public string Title { get; set; } public string Title { get; set; }
public string Author { get; set; } public string Author { get; set; }
public string Isbn { get; set; } public string Isbn { get; set; }

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

@ -1,65 +1,81 @@
using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable #nullable disable
namespace StubbedContextLib.Migrations #pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
{
/// <inheritdoc /> namespace StubbedContextLib.Migrations
public partial class Mg : Migration {
{ /// <inheritdoc />
/// <inheritdoc /> public partial class Mg : Migration
protected override void Up(MigrationBuilder migrationBuilder) {
{ /// <inheritdoc />
migrationBuilder.CreateTable( protected override void Up(MigrationBuilder migrationBuilder)
name: "PersonSet", {
columns: table => new migrationBuilder.CreateTable(
{ name: "PersonEntity",
Id = table.Column<int>(type: "INTEGER", nullable: false) columns: table => new
.Annotation("Sqlite:Autoincrement", true), {
FirstName = table.Column<string>(type: "TEXT", nullable: false), Id = table.Column<int>(type: "INTEGER", nullable: false)
LastName = table.Column<string>(type: "TEXT", nullable: false) .Annotation("Sqlite:Autoincrement", true),
}, FirstName = table.Column<string>(type: "TEXT", nullable: false),
constraints: table => LastName = table.Column<string>(type: "TEXT", nullable: false)
{ },
table.PrimaryKey("PK_PersonSet", x => x.Id); constraints: table =>
}); {
table.PrimaryKey("PK_PersonEntity", x => x.Id);
migrationBuilder.CreateTable( });
name: "BooksSet",
columns: table => new migrationBuilder.CreateTable(
{ name: "BookEntity",
ID = table.Column<long>(type: "INTEGER", nullable: false) columns: table => new
.Annotation("Sqlite:Autoincrement", true), {
Title = table.Column<string>(type: "TEXT", nullable: false), Id = table.Column<long>(type: "INTEGER", nullable: false)
Author = table.Column<string>(type: "TEXT", nullable: false), .Annotation("Sqlite:Autoincrement", true),
Isbn = table.Column<string>(type: "TEXT", nullable: false), Title = table.Column<string>(type: "TEXT", nullable: false),
PersonId = table.Column<int>(type: "INTEGER", nullable: false), Author = table.Column<string>(type: "TEXT", nullable: false),
OwnerId = table.Column<int>(type: "INTEGER", nullable: true) Isbn = table.Column<string>(type: "TEXT", nullable: false),
}, PersonId = table.Column<int>(type: "INTEGER", nullable: false),
constraints: table => OwnerId = table.Column<int>(type: "INTEGER", nullable: true)
{ },
table.PrimaryKey("PK_BooksSet", x => x.ID); constraints: table =>
table.ForeignKey( {
name: "FK_BooksSet_PersonSet_OwnerId", table.PrimaryKey("PK_BookEntity", x => x.Id);
column: x => x.OwnerId, table.ForeignKey(
principalTable: "PersonSet", name: "FK_BookEntity_PersonEntity_OwnerId",
principalColumn: "Id"); column: x => x.OwnerId,
}); principalTable: "PersonEntity",
principalColumn: "Id");
migrationBuilder.CreateIndex( });
name: "IX_BooksSet_OwnerId",
table: "BooksSet", migrationBuilder.InsertData(
column: "OwnerId"); table: "BookEntity",
} columns: new[] { "Id", "Author", "Isbn", "OwnerId", "PersonId", "Title" },
values: new object[,]
/// <inheritdoc /> {
protected override void Down(MigrationBuilder migrationBuilder) { 1L, "test", "test", null, 1, "test" },
{ { 2L, "test2", "test2", null, 1, "test2" }
migrationBuilder.DropTable( });
name: "BooksSet");
migrationBuilder.InsertData(
migrationBuilder.DropTable( table: "PersonEntity",
name: "PersonSet"); columns: new[] { "Id", "FirstName", "LastName" },
} values: new object[] { 1, "coco", "test" });
}
} migrationBuilder.CreateIndex(
name: "IX_BookEntity_OwnerId",
table: "BookEntity",
column: "OwnerId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "BookEntity");
migrationBuilder.DropTable(
name: "PersonEntity");
}
}
}

@ -19,7 +19,7 @@ namespace StubbedContextLib.Migrations
modelBuilder.Entity("Entities.BookEntity", b => modelBuilder.Entity("Entities.BookEntity", b =>
{ {
b.Property<long>("ID") b.Property<long>("Id")
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
@ -41,11 +41,29 @@ namespace StubbedContextLib.Migrations
.IsRequired() .IsRequired()
.HasColumnType("TEXT"); .HasColumnType("TEXT");
b.HasKey("ID"); b.HasKey("Id");
b.HasIndex("OwnerId"); b.HasIndex("OwnerId");
b.ToTable("BooksSet"); b.ToTable("BookEntity");
b.HasData(
new
{
Id = 1L,
Author = "test",
Isbn = "test",
PersonId = 1,
Title = "test"
},
new
{
Id = 2L,
Author = "test2",
Isbn = "test2",
PersonId = 1,
Title = "test2"
});
}); });
modelBuilder.Entity("Entities.PersonEntity", b => modelBuilder.Entity("Entities.PersonEntity", b =>
@ -64,7 +82,15 @@ namespace StubbedContextLib.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("PersonSet"); b.ToTable("PersonEntity");
b.HasData(
new
{
Id = 1,
FirstName = "coco",
LastName = "test"
});
}); });
modelBuilder.Entity("Entities.BookEntity", b => modelBuilder.Entity("Entities.BookEntity", b =>

@ -12,19 +12,34 @@ namespace StubbedContextLib
{ {
public class StubbedContext : LibraryContext public class StubbedContext : LibraryContext
{ {
public List<PersonEntity> Persons { get; set; } = new List<PersonEntity>(); public DbSet<PersonEntity> Persons { get; set; }
public List<BookEntity> Books { get; set; } = new List<BookEntity>(); public DbSet<BookEntity> Books { get; set; }
public StubbedContext() { public StubbedContext() {
BookEntity b0 = new BookEntity() { Title = "test", Author = "test", Isbn = "test" };
BookEntity b1 = new BookEntity() { Title = "test2", Author = "test2", Isbn = "test2" }; }
Books.Add(b0);
Books.Add(b1); protected override void OnModelCreating(ModelBuilder modelBuilder)
Collection<BookEntity> CB = new Collection<BookEntity>(); {
CB.Add(b0); base.OnModelCreating(modelBuilder);
CB.Add(b1);
Persons.Add(new PersonEntity() { FirstName = "coco", LastName = "test", Books = CB }); modelBuilder.Entity<PersonEntity>().HasData(
new PersonEntity() { FirstName = "coco", LastName = "test", Id = 1 }
);
modelBuilder.Entity<BookEntity>().HasData(
new BookEntity() { Title = "test", Author = "test", Isbn = "test" , Id = 1, PersonId = 1},
new BookEntity() { Title = "test2", Author = "test2", Isbn = "test2",Id = 2, PersonId = 1 }
);
} }
public StubbedContext(DbContextOptions<LibraryContext> options)
: base(options)
{ }
} }
} }

@ -9,15 +9,16 @@ using Microsoft.EntityFrameworkCore;
// See https://aka.ms/new-console-template for more information // See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!"); Console.WriteLine("Hello, World!");
using (var context = new LibraryContext()) using (var context = new StubbedContext())
{ {
context.Database.EnsureCreated(); context.Database.EnsureCreated();
PersonEntity p1 = new PersonEntity() { FirstName = "Tom", LastName = "Rambeau" }; PersonEntity p1 = new PersonEntity() { FirstName = "Tom", LastName = "Rambeau" };
PersonEntity p2 = new PersonEntity() { FirstName = "Erwan", LastName = "Manager" }; PersonEntity p2 = new PersonEntity() { FirstName = "Erwan", LastName = "Manager" };
BookEntity chewie = new BookEntity() { Title = "mistake", Author = "test1", Isbn ="test1"}; BookEntity chewie = new BookEntity() { Title = "mistake", Author = "test1", Isbn ="test1"};
BookEntity the100 = new BookEntity() { Title = "the100", Author = "test4", Isbn = "test4", Owner = p1 }; BookEntity the100 = new BookEntity() { Title = "the100", Author = "test4", Isbn = "test4"};
BookEntity GOT = new BookEntity() { Title = "GOT", Author = "lastTest", Isbn = "lastTest"}; BookEntity GOT = new BookEntity() { Title = "GOT", Author = "lastTest", Isbn = "lastTest"};
context.Add(p1); context.Add(p1);
@ -29,17 +30,17 @@ using (var context = new LibraryContext())
context.SaveChanges(); context.SaveChanges();
} }
using (var context = new LibraryContext()) using (var context = new StubbedContext())
{ {
foreach (var n in context.BooksSet) foreach (var n in context.BooksSet)
{ {
Console.WriteLine($"Books: {n.ID} - {n.Title}"); Console.WriteLine($"Books: {n.Id} - {n.Title}");
} }
context.SaveChanges(); context.SaveChanges();
} }
using (var context = new LibraryContext()) using (var context = new StubbedContext())
{ {
var eBooks = context.BooksSet.Where(b => b.Title.StartsWith("t")).First(); var eBooks = context.BooksSet.Where(b => b.Title.StartsWith("t")).First();
Console.WriteLine($"{eBooks.Title} (made by {eBooks.Author})"); Console.WriteLine($"{eBooks.Title} (made by {eBooks.Author})");
@ -47,48 +48,50 @@ using (var context = new LibraryContext())
context.SaveChanges(); context.SaveChanges();
} }
using (var context = new LibraryContext()) using (var context = new StubbedContext())
{ {
Console.WriteLine("Deletes one item from de database"); Console.WriteLine("Deletes one item from de database");
var impostor = context.BooksSet var impostor = context.BooksSet
.SingleOrDefault(n => n.Title.Equals("mistake")); .SingleOrDefault(n => n.Title.Equals("mistake"));
context.Remove(impostor); context.Remove(impostor);
context.PersonSet.Include(pers => pers.Books).ToList();
context.SaveChanges(); context.SaveChanges();
} }
using (var context = new LibraryContext()) using (var context = new StubbedContext())
{ {
Console.WriteLine("All people"); Console.WriteLine("All people");
var people = context.PersonSet; var people = context.PersonSet;
foreach(var p in people) foreach(var p in people)
{ {
Console.WriteLine($"firstname: {p.FirstName}, lastname: {p.LastName}"); Console.WriteLine($"firstname: {p.FirstName}, lastname: {p.LastName}");
} }
} }
//emprunt //emprunt
using (var context = new LibraryContext()) using (var context = new StubbedContext())
{ {
var books = context.BooksSet; var books = context.BooksSet;
foreach(var book in books)
context.PersonSet.Include(pers => pers.Books).ToList();
var person = context.PersonSet.Where(b => b.FirstName.StartsWith("E")).First();
foreach (var book in books)
{ {
if (book.Owner == null) if (book.Owner == null)
{ {
book.Owner = context.PersonSet.Where(b => b.FirstName.StartsWith("E")).Include(b => b.Books).First(); person.Books.Add(book);
Console.WriteLine($" nouveau propriétaire du livre: {book.Owner.FirstName}");
break; break;
} }
} }
context.SaveChanges(); context.SaveChanges();
} }
//Rendu //Rendu
using (var context = new LibraryContext()) using (var context = new StubbedContext())
{ {
context.PersonSet.Include(pers => pers.Books).ToList();
var books = context.BooksSet; var books = context.BooksSet;
if (books != null) if (books != null)
{ {
@ -97,14 +100,13 @@ using (var context = new LibraryContext())
//Console.WriteLine(book.Owner.FirstName); //Console.WriteLine(book.Owner.FirstName);
if (book.Owner != null) if (book.Owner != null)
{ {
Console.WriteLine($" propriétaire du livre avant rendu : {book.Owner.FirstName}"); Console.WriteLine($" \n propriétaire du livre {book.Title} avant rendu : {book.Owner.FirstName}");
book.Owner = null; book.Owner = null;
Console.WriteLine("propriétaire a rendu le libre"); Console.WriteLine($" \n propriétaire a rendu le libre {book.Title} ");
break;
} }
else else
{ {
Console.WriteLine("livre sans propriétaire" + book.Title); Console.WriteLine("\n livre sans propriétaire : " + book.Title);
} }
} }
} }
@ -114,7 +116,7 @@ using (var context = new LibraryContext())
//DeleteAllItem //DeleteAllItem
using (var context = new LibraryContext()) using (var context = new StubbedContext())
{ {
var allBooks = context.BooksSet; var allBooks = context.BooksSet;
if ( allBooks != null) if ( allBooks != null)

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -1,4 +1,5 @@
using DbContextLib; using DbContextLib;
using StubbedContextLib;
using Entities; using Entities;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -11,38 +12,184 @@ namespace UnitTests
public class UnitTest1 public class UnitTest1
{ {
[Fact] [Fact]
public void Add_Test() public void Add_TestBooks()
{ {
//connection must be opened to use In-memory database //connection must be opened to use In-memory database
var connection = new SqliteConnection("DataSource=:memory:"); var connection = new SqliteConnection("DataSource=:memory:");
connection.Open(); connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
var options = new DbContextOptionsBuilder<LibraryContext>.UseSqlLite().options;
//prepares the database with one instance of the context //prepares the database with one instance of the context
using (var context = new LibraryContext(options)) using (var context = new StubbedContext(options))
{ {
//context.Database.OpenConnection(); //context.Database.OpenConnection();
context.Database.EnsureCreated(); context.Database.EnsureCreated();
context.PersonSet.Include(pers => pers.Books).ToList();
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "" }; BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id =3 };
BookEntity princeOfPersia = new BookEntity ( "princeOfPersia", "", "" ); BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 };
BookEntity PercyJackson = new BookEntity ("PercyJackson", "", "" ); BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 };
context.BooksSet.Add(the100); context.BooksSet.Add(the100);
context.BooksSet.Add(princeOfPersia); context.BooksSet.Add(princeOfPersia);
context.BooksSet.Add(PercyJackson); context.BooksSet.Add(PercyJackson);
context.SaveChanges(); context.SaveChanges();
} }
//uses another instance of the context to do the tests
using (var context = new StubbedContext(options))
{
context.Database.EnsureCreated();
Assert.Equal(5, context.BooksSet.Count());
Assert.Equal(1, context.BooksSet.Where(b => b.Title.Contains("the100")).Count());
}
}
[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))
{
//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.PersonSet.Add(p1);
context.PersonSet.Add(p2);
context.SaveChanges();
}
//uses another instance of the context to do the tests //uses another instance of the context to do the tests
using (var context = new LibraryContext(options)) using (var context = new StubbedContext(options))
{ {
context.Database.EnsureCreated(); context.Database.EnsureCreated();
Assert.Equal(3, context.PersonSet.Count());
Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.Contains("Granc")).Count());
}
}
[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
//prepares the database with one instance of the context
using (var context = new StubbedContext(options))
{
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.BooksSet.Add(the100);
context.BooksSet.Add(princeOfPersia);
context.BooksSet.Add(PercyJackson);
context.SaveChanges();
}
//prepares the database with one instance of the context
using (var context = new StubbedContext(options))
{
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();
ewok.Title = "l'Odyssée";
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))
{
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.PersonSet.Add(p1);
context.PersonSet.Add(p2);
context.SaveChanges();
}
//prepares the database with one instance of the context
using (var context = new StubbedContext(options))
{
context.PersonSet.Include(pers => pers.Books).ToList();
string nameToFind = "Jean";
Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.ToLower().Contains(nameToFind)).Count());
var person = context.PersonSet.Where(p => p.FirstName.ToLower().Contains(nameToFind)).First();
person.FirstName = "Jacques";
context.SaveChanges();
}
}
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();
}
}
Assert.Equal(3, context.BooksSet.Count()); public void Delete_TestBook()
Assert.Equal("the100", context.BooksSet.First().Title); {
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
var nb;
//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();
nb = context.BooksSet.Count();
if (nb > 0)
{
context.BooksSet.Remove(context.BooksSet.FirstOrDefault());
}
Assert.Equal(context.BooksSet.Count, nb - 1);
context.SaveChanges();
} }
} }
} }
} }
Loading…
Cancel
Save