<commit_before>﻿using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Runtime.Serialization;

using Moq;
using NUnit.Framework;

using WhenItsDone.Data.Contracts;
using WhenItsDone.Data.Repositories;
using WhenItsDone.Models.Contracts;

namespace WhenItsDone.Data.Tests.RepositoriesTests.AsyncGenericRepositoryTests
{
    [TestFixture]
    public class Update_Should
    {
        [Test]
        public void ThrowArgumentNullExceptionWithCorrectMessage_WhenEntityParameterIsNull()
        {
            var fakeDbSet = new Mock<DbSet<IDbModel>>();
            var mockDbContext = new Mock<IWhenItsDoneDbContext>();
            mockDbContext.Setup(mock => mock.Set<IDbModel>()).Returns(fakeDbSet.Object);

            var asyncGenericRepositoryInstace = new AsyncGenericRepository<IDbModel>(mockDbContext.Object);

            IDbModel entity = null;
            Assert.That(
                () => asyncGenericRepositoryInstace.Update(entity),
                Throws.InstanceOf<ArgumentNullException>().With.Message.Contains(nameof(entity)));
        }

        [Test]
        public void InvokeDbContextEntryMethodOnce_WhenParametersAreValid()
        {
            var fakeDbSet = new Mock<DbSet<IDbModel>>();
            var entity = (DbEntityEntry<IDbModel>)FormatterServices.GetSafeUninitializedObject(typeof(DbEntityEntry<IDbModel>));
            var mockDbContext = new Mock<IWhenItsDoneDbContext>();
            mockDbContext.Setup(mock => mock.Set<IDbModel>()).Returns(fakeDbSet.Object);
            mockDbContext.Setup(mock => mock.Entry(It.IsAny<IDbModel>())).Returns(entity);

            var asyncGenericRepositoryInstace = new AsyncGenericRepository<IDbModel>(mockDbContext.Object);
            var fakeDbModel = new Mock<IDbModel>();

            try
            {
                asyncGenericRepositoryInstace.Update(fakeDbModel.Object);
            }
            catch (NullReferenceException)
            {
                // This will throw due to not being able to instantiate an InternalEntityEntry object.
                // Because of that all DbEntityEntry<T> public properties throw a NullReferenceException.

                // namespace System.Data.Entity.Internal
                // internal class InternalEntityEntry
                // https://github.com/mono/entityframework/blob/master/src/EntityFramework/Internal/EntityEntries/InternalEntityEntry.cs
            }

            mockDbContext.Verify(mock => mock.Entry(It.IsAny<IDbModel>()), Times.Once());
        }

        [Test]
        public void InvokeDbContextEntryMethodWithCorrectParameter_WhenParametersAreValid()
        {
            var fakeDbSet = new Mock<DbSet<IDbModel>>();
            var entity = (DbEntityEntry<IDbModel>)FormatterServices.GetSafeUninitializedObject(typeof(DbEntityEntry<IDbModel>));
            var mockDbContext = new Mock<IWhenItsDoneDbContext>();
            mockDbContext.Setup(mock => mock.Set<IDbModel>()).Returns(fakeDbSet.Object);
            mockDbContext.Setup(mock => mock.Entry(It.IsAny<IDbModel>())).Returns(entity);

            var asyncGenericRepositoryInstace = new AsyncGenericRepository<IDbModel>(mockDbContext.Object);
            var fakeDbModel = new Mock<IDbModel>();

            try
            {
                asyncGenericRepositoryInstace.Update(fakeDbModel.Object);
            }
            catch (NullReferenceException)
            {
                // This will throw due to not being able to instantiate an InternalEntityEntry object.
                // Because of that all DbEntityEntry<T> public properties throw a NullReferenceException.

                // namespace System.Data.Entity.Internal
                // internal class InternalEntityEntry
                // https://github.com/mono/entityframework/blob/master/src/EntityFramework/Internal/EntityEntries/InternalEntityEntry.cs
            }

            mockDbContext.Verify(mock => mock.Entry(fakeDbModel.Object), Times.Once());
        }
    }<commit_msg>change repo.Update_Should tests<commit_after>﻿using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Runtime.Serialization;

using Moq;
using NUnit.Framework;

using WhenItsDone.Data.Contracts;
using WhenItsDone.Data.Repositories;
using WhenItsDone.Models.Contracts;

namespace WhenItsDone.Data.Tests.RepositoriesTests.AsyncGenericRepositoryTests
{
    [TestFixture]
    public class Update_Should
    {
        [Test]
        public void ThrowArgumentNullExceptionWithCorrectMessage_WhenEntityParameterIsNull()
        {
            // Arange
            var fakeDbSet = new Mock<DbSet<IDbModel>>();

            var mockDbContext = new Mock<IWhenItsDoneDbContext>();
            mockDbContext.Setup(mock => mock.Set<IDbModel>()).Returns(fakeDbSet.Object);

            var asyncGenericRepositoryInstace = new AsyncGenericRepository<IDbModel>(mockDbContext.Object);

            // Act & Assert
            IDbModel entity = null;
            Assert.That(
                () => asyncGenericRepositoryInstace.Update(entity),
                Throws.InstanceOf<ArgumentNullException>().With.Message.Contains(nameof(entity)));
        }

        [Test]
        public void InvokeDbContextEntryMethodOnce_WhenParametersAreValid()
        {
            // Arange
            var mockedStaful = new Mock<IStateful<IDbModel>>();

            var fakeDbSet = new Mock<DbSet<IDbModel>>();

            var fakeDbModel = new Mock<IDbModel>();

            var mockDbContext = new Mock<IWhenItsDoneDbContext>();
            mockDbContext.Setup(mock => mock.Set<IDbModel>()).Returns(fakeDbSet.Object);
            mockDbContext.Setup(mock => mock.GetStateful(It.IsAny<IDbModel>())).Returns(mockedStaful.Object);

            var asyncGenericRepositoryInstace = new AsyncGenericRepository<IDbModel>(mockDbContext.Object);

            // Act
            asyncGenericRepositoryInstace.Update(fakeDbModel.Object);

            // Assert
            mockDbContext.Verify(mock => mock.GetStateful(It.IsAny<IDbModel>()), Times.Once());
        }

        [Test]
        public void InvokeDbContextEntryMethodWithCorrectParameter_WhenParametersAreValid()
        {
            // Arange
            var mockedStaful = new Mock<IStateful<IDbModel>>();

            var fakeDbSet = new Mock<DbSet<IDbModel>>();

            var fakeDbModel = new Mock<IDbModel>();

            var mockDbContext = new Mock<IWhenItsDoneDbContext>();
            mockDbContext.Setup(mock => mock.Set<IDbModel>()).Returns(fakeDbSet.Object);
            mockDbContext.Setup(mock => mock.GetStateful(It.IsAny<IDbModel>())).Returns(mockedStaful.Object);

            var asyncGenericRepositoryInstace = new AsyncGenericRepository<IDbModel>(mockDbContext.Object);

            // Act
            asyncGenericRepositoryInstace.Update(fakeDbModel.Object);

            // Assert
            mockDbContext.Verify(mock => mock.GetStateful(fakeDbModel.Object), Times.Once());
        }

        [Test]
        public void ChangeStateOf_Stateful_WhenCalledWithValidParameters()
        {
            // Arange
            var mockedStaful = new Mock<IStateful<IDbModel>>();
            mockedStaful.SetupSet(x => x.State = EntityState.Modified).Verifiable();

            var fakeDbSet = new Mock<DbSet<IDbModel>>();

            var fakeDbModel = new Mock<IDbModel>();


            var mockDbContext = new Mock<IWhenItsDoneDbContext>();
            mockDbContext.Setup(mock => mock.Set<IDbModel>()).Returns(fakeDbSet.Object);
            mockDbContext.Setup(mock => mock.GetStateful(It.IsAny<IDbModel>())).Returns(mockedStaful.Object);

            var asyncGenericRepositoryInstace = new AsyncGenericRepository<IDbModel>(mockDbContext.Object);

            // Act
            asyncGenericRepositoryInstace.Update(fakeDbModel.Object);

            // Assert
            mockedStaful.Verify();
        }
    }
{'fraction_non_alphanumeric': 0.09973454683352294, 'fraction_numerical': 0.0, 'mean_word_length': 2.6663577386468953, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 83, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '686013', 'n_tokens_mistral': 2126, 'n_tokens_neox': 2047, 'n_words': 333}