You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.6 KiB
48 lines
1.6 KiB
using DbContextLib;
|
|
using Entities;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Linq;
|
|
using Xunit;
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
namespace UnitTests
|
|
{
|
|
public class UnitTest1
|
|
{
|
|
[Fact]
|
|
public void Add_Test()
|
|
{
|
|
//connection must be opened to use In-memory database
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
connection.Open();
|
|
|
|
var options = new DbContextOptionsBuilder<LibraryContext>.UseSqlLite().options;
|
|
|
|
//prepares the database with one instance of the context
|
|
using (var context = new LibraryContext(options))
|
|
{
|
|
//context.Database.OpenConnection();
|
|
context.Database.EnsureCreated();
|
|
|
|
BookEntity the100 = new BookEntity( "the100", "", "" );
|
|
BookEntity princeOfPersia = new BookEntity ( "princeOfPersia", "", "" );
|
|
BookEntity PercyJackson = new BookEntity ("PercyJackson", "", "" );
|
|
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 LibraryContext(options))
|
|
{
|
|
context.Database.EnsureCreated();
|
|
|
|
Assert.Equal(3, context.BooksSet.Count());
|
|
Assert.Equal("the100", context.BooksSet.First().Title);
|
|
}
|
|
}
|
|
|
|
}
|
|
} |