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.
103 lines
3.7 KiB
103 lines
3.7 KiB
5 years ago
|
using ex_041_004_TestingInMemory;
|
||
5 years ago
|
using Microsoft.EntityFrameworkCore;
|
||
|
using System.Linq;
|
||
|
using Xunit;
|
||
5 years ago
|
using Microsoft.Data.Sqlite;
|
||
5 years ago
|
|
||
5 years ago
|
|
||
|
namespace ex_041_004_UnitTests_w_SQLiteInMemory
|
||
5 years ago
|
{
|
||
|
public class NounoursDB_Tests
|
||
|
{
|
||
|
[Fact]
|
||
|
public void Add_Test()
|
||
|
{
|
||
5 years ago
|
//connection must be opened to use In-memory database
|
||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||
|
connection.Open();
|
||
|
|
||
5 years ago
|
var options = new DbContextOptionsBuilder<NounoursContext>()
|
||
5 years ago
|
.UseSqlite(connection)
|
||
5 years ago
|
.Options;
|
||
|
|
||
5 years ago
|
//prepares the database with one instance of the context
|
||
5 years ago
|
using (var context = new NounoursContext(options))
|
||
5 years ago
|
{
|
||
5 years ago
|
//context.Database.OpenConnection();
|
||
|
context.Database.EnsureCreated();
|
||
|
|
||
5 years ago
|
Nounours chewie = new Nounours { Nom = "Chewbacca" };
|
||
|
Nounours yoda = new Nounours { Nom = "Yoda" };
|
||
|
Nounours ewok = new Nounours { Nom = "Ewok" };
|
||
|
|
||
|
context.Nounours.Add(chewie);
|
||
|
context.Nounours.Add(yoda);
|
||
|
context.Nounours.Add(ewok);
|
||
|
context.SaveChanges();
|
||
|
}
|
||
|
|
||
5 years ago
|
//uses another instance of the context to do the tests
|
||
5 years ago
|
using (var context = new NounoursContext(options))
|
||
5 years ago
|
{
|
||
5 years ago
|
context.Database.EnsureCreated();
|
||
|
|
||
5 years ago
|
Assert.Equal(3, context.Nounours.Count());
|
||
|
Assert.Equal("Chewbacca", context.Nounours.First().Nom);
|
||
|
}
|
||
|
}
|
||
5 years ago
|
|
||
|
[Fact]
|
||
|
public void Modify_Test()
|
||
|
{
|
||
5 years ago
|
//connection must be opened to use In-memory database
|
||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||
|
connection.Open();
|
||
|
|
||
5 years ago
|
var options = new DbContextOptionsBuilder<NounoursContext>()
|
||
5 years ago
|
.UseSqlite(connection)
|
||
5 years ago
|
.Options;
|
||
|
|
||
5 years ago
|
//prepares the database with one instance of the context
|
||
5 years ago
|
using (var context = new NounoursContext(options))
|
||
|
{
|
||
5 years ago
|
//context.Database.OpenConnection();
|
||
|
context.Database.EnsureCreated();
|
||
|
|
||
5 years ago
|
Nounours chewie = new Nounours { Nom = "Chewbacca" };
|
||
|
Nounours yoda = new Nounours { Nom = "Yoda" };
|
||
|
Nounours ewok = new Nounours { Nom = "Ewok" };
|
||
|
|
||
|
context.Nounours.Add(chewie);
|
||
|
context.Nounours.Add(yoda);
|
||
|
context.Nounours.Add(ewok);
|
||
|
context.SaveChanges();
|
||
|
}
|
||
|
|
||
5 years ago
|
//uses another instance of the context to do the tests
|
||
5 years ago
|
using (var context = new NounoursContext(options))
|
||
|
{
|
||
5 years ago
|
context.Database.EnsureCreated();
|
||
|
|
||
5 years ago
|
string nameToFind = "ew";
|
||
5 years ago
|
Assert.Equal(2, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count());
|
||
|
nameToFind = "wo";
|
||
|
Assert.Equal(1, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count());
|
||
|
var ewok = context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).First();
|
||
5 years ago
|
ewok.Nom = "Wicket";
|
||
|
context.SaveChanges();
|
||
|
}
|
||
|
|
||
5 years ago
|
//uses another instance of the context to do the tests
|
||
5 years ago
|
using (var context = new NounoursContext(options))
|
||
|
{
|
||
5 years ago
|
context.Database.EnsureCreated();
|
||
|
|
||
5 years ago
|
string nameToFind = "ew";
|
||
5 years ago
|
Assert.Equal(1, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count());
|
||
5 years ago
|
nameToFind = "wick";
|
||
5 years ago
|
Assert.Equal(1, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count());
|
||
5 years ago
|
}
|
||
|
}
|
||
5 years ago
|
}
|
||
|
}
|