using ex_041_004_InMemory;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using Xunit;

namespace ex_041_004_UnitTests
{
    public class NounoursDB_Tests
    {
        [Fact]
        public void Add_Test()
        {
            var options = new DbContextOptionsBuilder<NounoursContext>()
                .UseInMemoryDatabase(databaseName: "Add_writes_to_database")
                .Options;

            // Run the test against one instance of the context
            using (var context = new NounoursContext(options))
            {
                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();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new NounoursContext(options))
            {
                Assert.Equal(3, context.Nounours.Count());
                Assert.Equal("Chewbacca", context.Nounours.First().Nom);
            }
        }
    }
}
