public void FindBy_Should_Return_The_Dummy_Data_And_All_Count_As_One(Mock <ISessionManager> mockSessionManager, DefaultSession session, Dummy data)
        {
            //Arrange
            mockSessionManager.Setup(m => m.Current)
            .Returns(session);

            var options = new DbContextOptionsBuilder <DummyContext>()
                          .UseInMemoryDatabase("DummyDb")
                          .Options;

            using (var context = new DummyContext(options, mockSessionManager.Object))
            {
                var repository = new WriteRepository <DummyContext, Dummy>(context);

                repository.Add(data);
            }

            //Act
            List <Dummy> list;

            using (var context = new DummyContext(options, mockSessionManager.Object))
            {
                var repository = new ReadRepository <DummyContext, Dummy>(context);
                list = repository.FindBy(e => e.Id == data.Id).ToList();
            }

            //Assert
            list.Count.Should().Be(1);
            list.First().Should().BeEquivalentTo(data);
        }
        public void FindByAsync_Should_Should_Return_The_Dummy_Data_And_All_Count_As_More_Than_One(Mock <ISessionManager> mockSessionManager, DefaultSession session, Dummy data1, Dummy data2)
        {
            //Arrange
            mockSessionManager.Setup(m => m.Current)
            .Returns(session);

            var options = new DbContextOptionsBuilder <DummyContext>()
                          .UseInMemoryDatabase("DummyDb")
                          .Options;
            string name = "berdan";

            data1.Name = name;
            data2.Name = name;
            using (var context = new DummyContext(options, mockSessionManager.Object))
            {
                var repository = new WriteRepository <DummyContext, Dummy>(context);

                repository.Add(data1);
                repository.Add(data2);
            }

            //Act
            List <Dummy> list;

            using (var context = new DummyContext(options, mockSessionManager.Object))
            {
                var repository = new ReadRepository <DummyContext, Dummy>(context);
                list = repository.FindByAsync(e => e.Name == name).Result;
            }

            //Assert
            list.Count.Should().BeGreaterThan(1);
        }
        public void GetByIdAsync_Should_Return_The_Expected_Data(Mock <ISessionManager> mockSessionManager, DefaultSession session, Dummy data)
        {
            //Arrange
            mockSessionManager.Setup(m => m.Current)
            .Returns(session);

            var options = new DbContextOptionsBuilder <DummyContext>()
                          .UseInMemoryDatabase("DummyDb")
                          .Options;

            using (var context = new DummyContext(options, mockSessionManager.Object))
            {
                var repository = new WriteRepository <DummyContext, Dummy>(context);

                repository.Add(data);
            }

            //Act
            Task <Dummy> dummyData;

            using (var context = new DummyContext(options, mockSessionManager.Object))
            {
                var repository = new ReadRepository <DummyContext, Dummy>(context);
                dummyData = repository.GetByIdAsync(data.Id);
            }

            //Assert
            dummyData.Result.Should().BeEquivalentTo(data);
        }
        public void SaveAsync_Should_Save_Context_And_Should_Return_Expected(Mock <ISessionManager> mockSessionManager, DefaultSession session, Dummy data)
        {
            //Arrange
            mockSessionManager.Setup(m => m.Current)
            .Returns(session);

            var options = new DbContextOptionsBuilder <DummyContext>()
                          .UseInMemoryDatabase("DummyDb")
                          .Options;

            //Act
            var context    = new DummyContext(options, mockSessionManager.Object);
            var repository = new WriteRepository <DummyContext, Dummy>(context);

            repository.AutoSave = false;
            repository.Add(data);
            var result = repository.SaveAsync().Result;

            //Assert
            result.Should().BeGreaterThan(0);

            Dummy dummyData = repository.GetById(data.Id);

            dummyData.Should().BeEquivalentTo(data);

            context.Dispose();
        }
        public void DeleteAsync_Should_Delete_Data_And_Should_Return_Null(Mock <ISessionManager> mockSessionManager, DefaultSession session, Dummy data)
        {
            //Arrange
            mockSessionManager.Setup(m => m.Current)
            .Returns(session);

            var options = new DbContextOptionsBuilder <DummyContext>()
                          .UseInMemoryDatabase("DummyDb")
                          .Options;

            var context    = new DummyContext(options, mockSessionManager.Object);
            var repository = new WriteRepository <DummyContext, Dummy>(context);

            repository.Add(data);

            //Act
            repository.DeleteAsync(data.Id);

            //Assert
            Dummy dummyData = repository.GetById(data.Id);

            dummyData.Should().BeNull();

            context.Dispose();
        }
        public void UpdateAsync_Should_Update_Data(Mock <ISessionManager> mockSessionManager, DefaultSession session, Dummy data, string newName)
        {
            //Arrange
            mockSessionManager.Setup(m => m.Current)
            .Returns(session);

            var options = new DbContextOptionsBuilder <DummyContext>()
                          .UseInMemoryDatabase("DummyDb")
                          .Options;

            var context    = new DummyContext(options, mockSessionManager.Object);
            var repository = new WriteRepository <DummyContext, Dummy>(context);

            repository.Add(data);
            Dummy dummyData = repository.GetById(data.Id);

            //Act
            dummyData.Name = newName;
            repository.UpdateAsync(dummyData);

            //Assert
            Dummy newDummyData = repository.GetById(data.Id);

            dummyData.Name.Should().Be(newName);

            context.Dispose();
        }
        public void Add_Data_Without_Name_Should_Throw_CustomValidationException(Mock <ISessionManager> mockSessionManager, DefaultSession session, Dummy data)
        {
            //Arrange
            mockSessionManager.Setup(m => m.Current)
            .Returns(session);

            var options = new DbContextOptionsBuilder <DummyContext>()
                          .UseInMemoryDatabase("DummyDb")
                          .Options;

            data.Name = null;

            //Act
            var    context    = new DummyContext(options, mockSessionManager.Object);
            var    repository = new WriteRepository <DummyContext, Dummy>(context);
            Action action     = () => repository.Add(data);

            //Assert
            action.Should().Throw <CustomValidationException>();

            context.Dispose();
        }
        public void Add_Should_Dont_Save_Context_When_AutoSave_Is_False(Mock <ISessionManager> mockSessionManager, DefaultSession session, Dummy data)
        {
            //Arrange
            mockSessionManager.Setup(m => m.Current)
            .Returns(session);

            var options = new DbContextOptionsBuilder <DummyContext>()
                          .UseInMemoryDatabase("DummyDb")
                          .Options;

            //Act
            var context    = new DummyContext(options, mockSessionManager.Object);
            var repository = new WriteRepository <DummyContext, Dummy>(context);

            repository.AutoSave = false;
            repository.Add(data);

            //Assert
            Dummy dummyData = repository.GetById(data.Id);

            dummyData.Should().BeNull();

            context.Dispose();
        }