﻿using System;
using DailyEnterprise.Core.Models.Abstract;
using DailyEnterprise.Repository.RavenDB.Helpers;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace DailyEnterprise.Repository.Tests.Helpers
{
    [TestClass]
    public class InMemoryBaseTests
    {
        private class TestClass : BaseModel
        {
            public string Name { get; }
            public TestClass() {}
            public TestClass(Guid id, string name) : base(id)
            {
                Name = name;
            }
        }

        private class TestRepo : InMemoryRepositoryBase<TestClass>
        {
        }

        // This test is sortof irrelivant because I need to instert a new item into the repo to test the other functions.
        [TestMethod]
        public void CanStoreItem()
        {
            var repo = new TestRepo();
            var toSave = new TestClass();
            repo.Insert(toSave);
            toSave.Id.Should().NotBe(Guid.Empty);
        }

        [TestMethod]
        public void CanLoadItemAfterSavingIt()
        {
            var repo = new TestRepo();
            var toSave = new TestClass();
            repo.Insert(toSave);
            var loaded = repo.Load(toSave.Id);
            loaded.Should().NotBeNull();
            loaded.Id.Should().Be(toSave.Id);
        }

        [TestMethod]
        public void CanUpdate()
        {
            var repo = new TestRepo();
            var toSave = new TestClass();
            repo.Insert(toSave);
            var oldId = toSave.Id;
            var beforeName = toSave.Name;
            const string afterName = "Something Else";
            var toReplace = new TestClass(oldId, afterName);
            repo.Update(toReplace);
            var loaded = repo.Load(toSave.Id);
            loaded.Name.Should().Be(afterName);
            loaded.Name.Should().NotBe(beforeName);
        }

        [TestMethod]
        public void CanDelete()
        {
            var repo = new TestRepo();
            var toSave = new TestClass();
            repo.Insert(toSave);
            var beforeId = toSave.Id;
            repo.Delete(beforeId);
            repo.Load(beforeId).Should().BeNull();
        }

    }
}
