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

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

@ -11,7 +11,7 @@ using StubbedContextLib;
namespace StubbedContextLib.Migrations
{
[DbContext(typeof(StubbedContext))]
[Migration("20240209155948_Mg")]
[Migration("20240215122943_Mg")]
partial class Mg
{
/// <inheritdoc />
@ -22,7 +22,7 @@ namespace StubbedContextLib.Migrations
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.Property<long>("ID")
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
@ -44,11 +44,29 @@ namespace StubbedContextLib.Migrations
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("ID");
b.HasKey("Id");
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 =>
@ -67,7 +85,15 @@ namespace StubbedContextLib.Migrations
b.HasKey("Id");
b.ToTable("PersonSet");
b.ToTable("PersonEntity");
b.HasData(
new
{
Id = 1,
FirstName = "coco",
LastName = "test"
});
});
modelBuilder.Entity("Entities.BookEntity", b =>

@ -2,6 +2,8 @@
#nullable disable
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
namespace StubbedContextLib.Migrations
{
/// <inheritdoc />
@ -11,7 +13,7 @@ namespace StubbedContextLib.Migrations
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "PersonSet",
name: "PersonEntity",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
@ -21,14 +23,14 @@ namespace StubbedContextLib.Migrations
},
constraints: table =>
{
table.PrimaryKey("PK_PersonSet", x => x.Id);
table.PrimaryKey("PK_PersonEntity", x => x.Id);
});
migrationBuilder.CreateTable(
name: "BooksSet",
name: "BookEntity",
columns: table => new
{
ID = table.Column<long>(type: "INTEGER", nullable: false)
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),
@ -38,17 +40,31 @@ namespace StubbedContextLib.Migrations
},
constraints: table =>
{
table.PrimaryKey("PK_BooksSet", x => x.ID);
table.PrimaryKey("PK_BookEntity", x => x.Id);
table.ForeignKey(
name: "FK_BooksSet_PersonSet_OwnerId",
name: "FK_BookEntity_PersonEntity_OwnerId",
column: x => x.OwnerId,
principalTable: "PersonSet",
principalTable: "PersonEntity",
principalColumn: "Id");
});
migrationBuilder.InsertData(
table: "BookEntity",
columns: new[] { "Id", "Author", "Isbn", "OwnerId", "PersonId", "Title" },
values: new object[,]
{
{ 1L, "test", "test", null, 1, "test" },
{ 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_BooksSet_OwnerId",
table: "BooksSet",
name: "IX_BookEntity_OwnerId",
table: "BookEntity",
column: "OwnerId");
}
@ -56,10 +72,10 @@ namespace StubbedContextLib.Migrations
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "BooksSet");
name: "BookEntity");
migrationBuilder.DropTable(
name: "PersonSet");
name: "PersonEntity");
}
}
}

@ -19,7 +19,7 @@ namespace StubbedContextLib.Migrations
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.Property<long>("ID")
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
@ -41,11 +41,29 @@ namespace StubbedContextLib.Migrations
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("ID");
b.HasKey("Id");
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 =>
@ -64,7 +82,15 @@ namespace StubbedContextLib.Migrations
b.HasKey("Id");
b.ToTable("PersonSet");
b.ToTable("PersonEntity");
b.HasData(
new
{
Id = 1,
FirstName = "coco",
LastName = "test"
});
});
modelBuilder.Entity("Entities.BookEntity", b =>

@ -12,19 +12,34 @@ namespace StubbedContextLib
{
public class StubbedContext : LibraryContext
{
public List<PersonEntity> Persons { get; set; } = new List<PersonEntity>();
public List<BookEntity> Books { get; set; } = new List<BookEntity>();
public DbSet<PersonEntity> Persons { get; set; }
public DbSet<BookEntity> Books { get; set; }
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);
Collection<BookEntity> CB = new Collection<BookEntity>();
CB.Add(b0);
CB.Add(b1);
Persons.Add(new PersonEntity() { FirstName = "coco", LastName = "test", Books = CB });
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
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
Console.WriteLine("Hello, World!");
using (var context = new LibraryContext())
using (var context = new StubbedContext())
{
context.Database.EnsureCreated();
PersonEntity p1 = new PersonEntity() { FirstName = "Tom", LastName = "Rambeau" };
PersonEntity p2 = new PersonEntity() { FirstName = "Erwan", LastName = "Manager" };
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"};
context.Add(p1);
@ -29,17 +30,17 @@ using (var context = new LibraryContext())
context.SaveChanges();
}
using (var context = new LibraryContext())
using (var context = new StubbedContext())
{
foreach (var n in context.BooksSet)
{
Console.WriteLine($"Books: {n.ID} - {n.Title}");
Console.WriteLine($"Books: {n.Id} - {n.Title}");
}
context.SaveChanges();
}
using (var context = new LibraryContext())
using (var context = new StubbedContext())
{
var eBooks = context.BooksSet.Where(b => b.Title.StartsWith("t")).First();
Console.WriteLine($"{eBooks.Title} (made by {eBooks.Author})");
@ -47,17 +48,18 @@ using (var context = new LibraryContext())
context.SaveChanges();
}
using (var context = new LibraryContext())
using (var context = new StubbedContext())
{
Console.WriteLine("Deletes one item from de database");
var impostor = context.BooksSet
.SingleOrDefault(n => n.Title.Equals("mistake"));
context.Remove(impostor);
context.PersonSet.Include(pers => pers.Books).ToList();
context.SaveChanges();
}
using (var context = new LibraryContext())
using (var context = new StubbedContext())
{
Console.WriteLine("All people");
var people = context.PersonSet;
@ -68,27 +70,28 @@ using (var context = new LibraryContext())
}
//emprunt
using (var context = new LibraryContext())
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("E")).First();
foreach (var book in books)
{
if (book.Owner == null)
{
book.Owner = context.PersonSet.Where(b => b.FirstName.StartsWith("E")).Include(b => b.Books).First();
Console.WriteLine($" nouveau propriétaire du livre: {book.Owner.FirstName}");
person.Books.Add(book);
break;
}
}
context.SaveChanges();
}
//Rendu
using (var context = new LibraryContext())
using (var context = new StubbedContext())
{
context.PersonSet.Include(pers => pers.Books).ToList();
var books = context.BooksSet;
if (books != null)
{
@ -97,14 +100,13 @@ using (var context = new LibraryContext())
//Console.WriteLine(book.Owner.FirstName);
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;
Console.WriteLine("propriétaire a rendu le libre");
break;
Console.WriteLine($" \n propriétaire a rendu le libre {book.Title} ");
}
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
using (var context = new LibraryContext())
using (var context = new StubbedContext())
{
var allBooks = context.BooksSet;
if ( allBooks != null)

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -1,4 +1,5 @@
using DbContextLib;
using StubbedContextLib;
using Entities;
using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore;
@ -11,38 +12,184 @@ namespace UnitTests
public class UnitTest1
{
[Fact]
public void Add_Test()
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>.UseSqlLite().options;
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//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.EnsureCreated();
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "" };
BookEntity princeOfPersia = new BookEntity ( "princeOfPersia", "", "" );
BookEntity PercyJackson = new BookEntity ("PercyJackson", "", "" );
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();
}
//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
using (var context = new LibraryContext(options))
using (var context = new StubbedContext(options))
{
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();
}
Assert.Equal(3, context.BooksSet.Count());
Assert.Equal("the100", context.BooksSet.First().Title);
//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();
}
}
public void Delete_TestBook()
{
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