﻿namespace MappingEf.InheritanceTo.TablePerHierarchy
{
  using Domain.Inheritance;

  using FluentAssertions;

  using MappingEf.InheritanceTo.TablePerType;

  using NUnit.Framework;

  [TestFixture(Category = "Mapping: TPH Inheritance")]
  public class TablePerHierarchyTest
  {
    [OneTimeSetUp]
    public void Setup()
    {
      using (var ctx = new TphContextEf())
      {
        ctx.Database.Delete();
        ctx.Database.CreateIfNotExists();
      }
    }

    [Test]
    public void Insert_EfConventionTablePerHierarchy_ItemInserted()
    {
      var ebook = this.GetEbookTestee();
      using (var testee = new TphContextEf())
      {
        testee.Books.Add(ebook);
        testee.SaveChanges();

        ebook.Id.Should().BeGreaterThan(0);
      }
    }

    [Test]
    public void Update_EfConventionTablePerHierarchy_ItemUpdated()
    {
      var ebook = this.GetEbookTestee();
      using (var arrange = new TphContextEf())
      {
        arrange.Books.Add(ebook);
        arrange.SaveChanges();
      }

      using (var testee = new TphContextEf())
      {
        var book = testee.Books.Find(ebook.Id) as EBook;
        book.Name = "Zurück an die Arbeit";
        testee.SaveChanges();

        ebook.Name.Should().NotBe(book.Name);
      }
    }

    [Test]
    public void Delete_EfConventionTablePerHierarchy_ItemDeleted()
    {
      var ebook = this.GetEbookTestee();
      using (var arrange = new TphContextEf())
      {
        arrange.Books.Add(ebook);
        arrange.SaveChanges();
      }

      using (var testee = new TphContextEf())
      {
        var book = testee.Books.Find(ebook.Id);
        testee.Books.Remove(book);
        testee.SaveChanges();

        testee.Books.Find(ebook.Id).Should().BeNull();
      }
    }

    public EBook GetEbookTestee()
    {
      return new EBook
      {
        Description = "blablabla",
        Name = "The EBook",
        ISBN10 = "10-...",
        ISBN13 = "13-...",
        Pages = 200,
        Filename = "file.pdf"
      };
    }

    public Hardcover GetHardcoverTestee()
    {
      return new Hardcover()
      {
        Description = "blablabla",
        Name = "The Harcover",
        ISBN10 = "10-...",
        ISBN13 = "13-...",
        Pages = 200,
        Weight = 75.0
      };
    }
  }
}