using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestingDemo.Model;

namespace TestingDemo.Tests
{
    [TestClass]
    public class DatabaseTests
    {
        private AppDbContext testDc;

        [TestInitialize]
        public void Init()
        {
            var builder = new DbContextOptionsBuilder<AppDbContext>()
                .UseInMemoryDatabase("MyDb");

            using (var seedDc = new AppDbContext(builder.Options))
            {
                Seed(seedDc);
            }

            testDc = new AppDbContext(builder.Options);
        }

        private void Seed(AppDbContext seedDc)
        {
            seedDc.Roles.Add(new Role() { Name = "admin" });
            seedDc.Roles.Add(new Role() { Name = "user" });
            seedDc.SaveChanges();
        }

        [TestCleanup]
        public void Cleanup()
        {
            testDc?.Dispose();
        }


        [TestMethod]
        public void RoleCountTest()
        {
            Assert.AreEqual(2, testDc.Roles.Count());
        }
    }
}
