Пример #1
0
        public CachingProviderTests()
        {
            _fakeFrameworkContext = new FakeFrameworkContext();

            var inMemory = new NHibernateInMemoryRepository(_fakeFrameworkContext);

            var dataContextFactory = new DataContextFactory();
            var readWriteUnitOfWorkFactory = new ReadWriteUnitOfWorkFactory();
            var directReaderProvider = new HiveReadWriteProvider(new HiveProviderSetup(_fakeFrameworkContext, "r-unit-tester",
                                                                  new FakeHiveProviderBootstrapper(),
                                                                  readWriteUnitOfWorkFactory, readWriteUnitOfWorkFactory,
                                                                  dataContextFactory));
            var directReadWriteProvider = directReaderProvider;

            // Create hive wrappers for the readers and writers
            var governorRUowFactory = new ReadOnlyUnitOfWorkFactoryWrapper(new[] { directReaderProvider, inMemory.HiveReadProvider });
            var governorRWUowFactory = new ReadWriteUnitOfWorkFactoryWrapper(new[] { directReadWriteProvider, inMemory.ReadWriteProvider });

            _readerProviderViaHiveGovernor = _directReaderProvider =
                new Framework.Hive.PersistenceGovernor.HiveReadProvider(new HiveProviderSetup(_fakeFrameworkContext, "r-unit-wrapper", new FakeHiveProviderBootstrapper(), governorRUowFactory, null, null), new[] { _directReaderProvider });
            _readWriteProviderViaHiveGovernor = _directReadWriteProvider =
                new Framework.Hive.PersistenceGovernor.HiveReadWriteProvider(new HiveProviderSetup(_fakeFrameworkContext, "rw-unit-wrapper", new FakeHiveProviderBootstrapper(), governorRUowFactory, governorRWUowFactory, null), new[] { _directReadWriteProvider });
        }
        public CachingProviderTests()
        {
            _fakeFrameworkContext = new FakeFrameworkContext();

            var inMemory = new NHibernateInMemoryRepository(_fakeFrameworkContext);

            var dataContextFactory         = new DataContextFactory();
            var readWriteUnitOfWorkFactory = new ReadWriteUnitOfWorkFactory();
            var directReaderProvider       = new HiveReadWriteProvider(new HiveProviderSetup(_fakeFrameworkContext, "r-unit-tester",
                                                                                             new FakeHiveProviderBootstrapper(),
                                                                                             readWriteUnitOfWorkFactory, readWriteUnitOfWorkFactory,
                                                                                             dataContextFactory));
            var directReadWriteProvider = directReaderProvider;

            // Create hive wrappers for the readers and writers
            var governorRUowFactory  = new ReadOnlyUnitOfWorkFactoryWrapper(new[] { directReaderProvider, inMemory.HiveReadProvider });
            var governorRWUowFactory = new ReadWriteUnitOfWorkFactoryWrapper(new[] { directReadWriteProvider, inMemory.ReadWriteProvider });

            _readerProviderViaHiveGovernor = _directReaderProvider =
                new Framework.Hive.PersistenceGovernor.HiveReadProvider(new HiveProviderSetup(_fakeFrameworkContext, "r-unit-wrapper", new FakeHiveProviderBootstrapper(), governorRUowFactory, null, null), new[] { _directReaderProvider });
            _readWriteProviderViaHiveGovernor = _directReadWriteProvider =
                new Framework.Hive.PersistenceGovernor.HiveReadWriteProvider(new HiveProviderSetup(_fakeFrameworkContext, "rw-unit-wrapper", new FakeHiveProviderBootstrapper(), governorRUowFactory, governorRWUowFactory, null), new[] { _directReadWriteProvider });
        }
        private object[] GetCheckValuesAfterCommitting(IHiveReadWriteProvider writeProvider, out HiveId generatedId, T mockedEntity)
        {
            var checkValuesAfterCommitting = new object[_valuesAndAssertions.Count];

            using (DisposableTimer.TraceDuration <NHibernateHiveTests>("Start write", "End write"))
            {
                using (var unit = writeProvider.CreateReadWriteUnitOfWork())
                {
                    unit.ReadWriteRepository.AddOrUpdate(mockedEntity);
                    unit.Commit();

                    generatedId = mockedEntity.Id;

                    for (int i = 0; i < _valuesAndAssertions.Count; i++)
                    {
                        checkValuesAfterCommitting[i] = _valuesAndAssertions[i].Item1.Invoke(mockedEntity);
                    }
                }
                _postWriteCallback.Invoke();
            }

            Assert.IsNotNull(generatedId, "Id was not obtained from adding the entity to the db");
            return(checkValuesAfterCommitting);
        }
        private void DoSameResultsCheck(IHiveReadWriteProvider hiveReadWriteProvider, IHiveReadProvider hiveReadProvider)
        {
            TypedEntity entity1 = HiveModelCreationHelper.MockTypedEntity();
            TypedEntity entity2 = HiveModelCreationHelper.MockTypedEntity();

            using (IReadWriteUnitOfWork writer = hiveReadWriteProvider.CreateReadWriteUnitOfWork())
            {
                writer.ReadWriteRepository.AddOrUpdate(entity1);
                writer.ReadWriteRepository.AddOrUpdate(entity2);
                writer.Commit();
            }

            PostWriteCallback.Invoke();

            using (IReadWriteUnitOfWork writer = hiveReadWriteProvider.CreateReadWriteUnitOfWork())
            {
                var check1 = writer.ReadWriteRepository.GetEntity<TypedEntity>(entity1.Id);
                Assert.IsNotNull(check1, "Same writer could not retrieve item 1");
                var check2 = writer.ReadWriteRepository.GetEntity<TypedEntity>(entity2.Id);
                Assert.IsNotNull(check2, "Same writer could not retrieve item 2");
            }

            PostWriteCallback.Invoke();

            using (IReadOnlyUnitOfWork writer = hiveReadProvider.CreateReadOnlyUnitOfWork())
            {
                var check1 = writer.ReadRepository.GetEntity<TypedEntity>(entity1.Id);
                Assert.IsNotNull(check1, "Direct reader could not retrieve item 1 when writer could");
                var check2 = writer.ReadRepository.GetEntity<TypedEntity>(entity2.Id);
                Assert.IsNotNull(check2, "Direct reader could not retrieve item 2 when writer could");
            }
        }
        private static void DoNestedTest(IHiveReadWriteProvider hiveReadWriteProvider, IHiveReadProvider hiveReadProvider, Action postWriteCallback)
        {
            TypedEntity entity = HiveModelCreationHelper.MockTypedEntity();
            TypedEntity entity2 = HiveModelCreationHelper.MockTypedEntity();

            using (IReadWriteUnitOfWork outerUnit = hiveReadWriteProvider.CreateReadWriteUnitOfWork())
            {
                outerUnit.ReadWriteRepository.AddOrUpdate(entity);

                // Do some nested writing
                using (IReadWriteUnitOfWork innerUnit = hiveReadWriteProvider.CreateReadWriteUnitOfWork())
                {
                    innerUnit.ReadWriteRepository.AddOrUpdate(entity2);
                    innerUnit.Commit();
                }

                // Do some nested reading
                using (IReadOnlyUnitOfWork innerUnit = hiveReadProvider.CreateReadOnlyUnitOfWork())
                {
                    innerUnit.ReadRepository.Exists<TypedEntity>(new HiveId(Guid.NewGuid()));
                }

                // Do some nested reading with the writer
                using (IReadWriteUnitOfWork innerUnit = hiveReadWriteProvider.CreateReadWriteUnitOfWork())
                {
                    innerUnit.ReadWriteRepository.Exists<TypedEntity>(new HiveId(Guid.NewGuid()));
                }

                // Do some nested writing
                using (IReadWriteUnitOfWork innerUnit = hiveReadWriteProvider.CreateReadWriteUnitOfWork())
                {
                    innerUnit.ReadWriteRepository.AddOrUpdate(entity2);
                    innerUnit.Commit();
                }

                outerUnit.Commit();
            }

            HiveId id1 = entity.Id;
            HiveId id2 = entity2.Id;

            using (IReadWriteUnitOfWork outerUnit = hiveReadWriteProvider.CreateReadWriteUnitOfWork())
            {
                // Do some nested reading
                using (IReadOnlyUnitOfWork innerUnit = hiveReadProvider.CreateReadOnlyUnitOfWork())
                {
                    innerUnit.ReadRepository.Exists<TypedEntity>(new HiveId(Guid.NewGuid()));
                    //innerUnit.Commit();
                }

                outerUnit.ReadWriteRepository.AddOrUpdate(entity);

                outerUnit.Commit();
            }

            postWriteCallback.Invoke();

            using (IReadOnlyUnitOfWork unit = hiveReadProvider.CreateReadOnlyUnitOfWork())
            {
                var compare = unit.ReadRepository.GetEntity<TypedEntity>(id1);
                var compare2 = unit.ReadRepository.GetEntity<TypedEntity>(id2);

                Assert.IsNotNull(compare);
                Assert.IsNotNull(compare2);
            }
        }
        public PersistenceAssertionBuilder <T> RunAssertionsWithQuery(IHiveReadProvider readerProvider, IHiveReadWriteProvider writeProvider, T mockedEntity, Func <HiveId, Expression <Func <T, bool> > > expression)
        {
            HiveId generatedId;

            object[] checkValuesAfterCommitting = GetCheckValuesAfterCommitting(writeProvider, out generatedId, mockedEntity);

            using (DisposableTimer.TraceDuration <NHibernateHiveTests>("Start read", "End read"))
            {
                using (var unit = readerProvider.CreateReadOnlyUnitOfWork())
                {
                    var result = unit.ReadRepository.QueryContext.Query <T>().Where(expression(generatedId)).FirstOrDefault();

                    Assert.IsNotNull(result,
                                     "No entity was retrieved back from the datastore for query {0} (id was {1})",
                                     expression,
                                     generatedId);

                    Assert.IsTrue(generatedId == result.Id);

                    LogHelper.TraceIfEnabled <PersistenceAssertionBuilder <T> >("Item owned by {0}, retrieved from {1}", () => result.PersistenceMetadata.OwnerProviderAlias, () => result.PersistenceMetadata.ReturnedByProviderAlias);

                    for (int i = 0; i < _valuesAndAssertions.Count; i++)
                    {
                        _valuesAndAssertions[i].Item2.Invoke(result, checkValuesAfterCommitting[i]);
                    }
                }
            }

            return(this);
        }
 public PersistenceAssertionBuilder <T> RunAssertionsWithQuery(IHiveReadProvider readerProvider, IHiveReadWriteProvider writeProvider, Func <T> mockedEntityGenerator, Func <HiveId, Expression <Func <T, bool> > > expression)
 {
     return(RunAssertionsWithQuery(readerProvider, writeProvider, mockedEntityGenerator.Invoke(), expression));
 }
        public PersistenceAssertionBuilder <T> RunAssertions(IHiveReadProvider readerProvider, IHiveReadWriteProvider writeProvider, T mockedEntity)
        {
            HiveId generatedId;

            object[] checkValuesAfterCommitting = GetCheckValuesAfterCommitting(writeProvider, out generatedId, mockedEntity);

            using (DisposableTimer.TraceDuration <NHibernateHiveTests>("Start read", "End read"))
            {
                using (var unit = readerProvider.CreateReadOnlyUnitOfWork())
                {
                    var result = unit.ReadRepository.GetEntity <T>(generatedId);

                    Assert.IsNotNull(result, "No entity was retrieved back from the datastore with id {0}", generatedId);

                    Assert.AreEqual(generatedId, result.Id, "Got a different value back from the database for the Id");

                    LogHelper.TraceIfEnabled <PersistenceAssertionBuilder <T> >("Item owned by {0}, retrieved from {1}", () => result.PersistenceMetadata.OwnerProviderAlias, () => result.PersistenceMetadata.ReturnedByProviderAlias);

                    for (int i = 0; i < _valuesAndAssertions.Count; i++)
                    {
                        _valuesAndAssertions[i].Item2.Invoke(result, checkValuesAfterCommitting[i]);
                    }
                }
                _postWriteCallback.Invoke();
            }

            return(this);
        }
 public PersistenceAssertionBuilder <T> RunAssertions(IHiveReadProvider readerProvider, IHiveReadWriteProvider writeProvider, Func <T> mockedEntityGenerator)
 {
     return(RunAssertions(readerProvider, writeProvider, mockedEntityGenerator.Invoke()));
 }