public void EventsAreDeletedWhenUnitOfWorkIsCommitted()
        {
            var bus = new MockServiceBus();
            var store = new MockEventStore();

            using (var session = new EventStoreSession(bus, store, new SingleThreadUseGuard()))
            {
                var uow = new UnitOfWork(new SingleThreadUseGuard());
                uow.AddParticipant(session);

                var aggregate1 = new Guid("92EC4FE2-26A8-4274-8674-DC5D95513C83");
                var aggregate2 = new Guid("F08200E4-8790-4ECC-9F06-A3D3BAC9E21C");

                session.Delete(aggregate1);
                session.Delete(aggregate2);

                session.SaveChanges();

                Assert.That(store.DeletedAggregates, Is.Empty);

                uow.Commit();

                Assert.That(store.DeletedAggregates, Is.EquivalentTo(new[] { aggregate1, aggregate2 }));
            }
        }
        public void LoadingRemovingAndAddingObjectInUnitOfWorkResultsInObjectBeingSaved()
        {
            var store = CreateStore();

            var user = new User { Id = Guid.NewGuid() };

            using (var session = OpenSession(store))
            {
                session.Save(user.Id, user);
                session.SaveChanges();
            }

            using (var session = OpenSession(store))
            {
                var uow = new UnitOfWork(new SingleThreadUseGuard());
                uow.AddParticipant((IUnitOfWorkParticipant)session);

                user = session.Get<User>(user.Id);
                session.Delete(user);

                User tmpUser;
                session.TryGet(user.Id, out tmpUser).Should().Be(false);
                session.Save(user);
                session.TryGet(user.Id, out tmpUser).Should().Be(true);
                session.Delete(user);
                session.TryGet(user.Id, out tmpUser).Should().Be(false);
                session.Save(user);
                session.TryGet(user.Id, out tmpUser).Should().Be(true);

                uow.Commit();
            }

            using (var session = OpenSession(store))
            {
                session.TryGet(user.Id, out user).Should().Be(true);
            }
        }
        public void EventsArePublishedOnSaveChangesAndThisInteractsWithUnitOfWorkParticipations()
        {
            var bus = new MockServiceBus();
            var store = new MockEventStore();

            var users = 1.Through(9).Select(i => { var u = new User(); u.Register(i + "@test.com", "abcd", Guid.NewGuid()); u.ChangeEmail("new" + i + "@test.com"); return u; }).ToList();

            using (var session = new EventStoreSession(bus, store, new SingleThreadUseGuard()))
            {
                var uow = new UnitOfWork(new SingleThreadUseGuard());
                uow.AddParticipant(session);

                users.Take(3).ForEach(u => session.Save(u));
                Assert.That(bus.Published.Count, Is.EqualTo(0));
                session.SaveChanges();
                Assert.That(bus.Published.Count, Is.EqualTo(6));

                users.Skip(3).Take(3).ForEach(u => session.Save(u));
                Assert.That(bus.Published.Count, Is.EqualTo(6));
                session.SaveChanges();
                Assert.That(bus.Published.Count, Is.EqualTo(12));

                users.Skip(6).Take(3).ForEach(u => session.Save(u));

                Assert.That(bus.Published.Count, Is.EqualTo(12));
                Assert.That(store.SavedEvents.Count, Is.EqualTo(0));
                uow.Commit();
                Assert.That(bus.Published.Count, Is.EqualTo(18));

                Assert.That(bus.Published.Select(e => e.EventId).Distinct().Count(), Is.EqualTo(18));
                Assert.That(bus.Published, Is.EquivalentTo(store.SavedEvents));
            }
        }
        public void ObjectsWhoseKeysDifferOnlyByTrailingSpacesTrailingWhiteSpaceCaseAreConsideredTheSameObjectForCompatabilityWithSqlServer()
        {
            var store = CreateStore();

            var noWhitespace = new Email("theemail");
            var withWhitespace = new Email(noWhitespace.TheEmail + "  ");

            using (var session = OpenSession(store))
            {
                var uow = new UnitOfWork(new SingleThreadUseGuard());
                uow.AddParticipant((IUnitOfWorkParticipant)session);

                session.Save(noWhitespace.TheEmail, noWhitespace);
                Assert.Throws<AttemptToSaveAlreadyPersistedValueException>(() => session.Save(withWhitespace.TheEmail, withWhitespace));

                session.Get<Email>(noWhitespace.TheEmail).Should().Be(session.Get<Email>(withWhitespace.TheEmail));

                uow.Commit();
            }

            using (var session = OpenSession(store))
            {
                var uow = new UnitOfWork(new SingleThreadUseGuard());
                uow.AddParticipant((IUnitOfWorkParticipant)session);

                Assert.Throws<AttemptToSaveAlreadyPersistedValueException>(() => session.Save(withWhitespace.TheEmail, withWhitespace));
                session.Get<Email>(withWhitespace.TheEmail).TheEmail.Should().Be(noWhitespace.TheEmail);
                session.Get<Email>(noWhitespace.TheEmail).Should().Be(session.Get<Email>(withWhitespace.TheEmail));

                session.Delete<Email>(withWhitespace.TheEmail);
                Assert.Throws<NoSuchDocumentException>(() => session.Delete<Email>(withWhitespace.TheEmail));
                Assert.Throws<NoSuchDocumentException>(() => session.Delete<Email>(noWhitespace.TheEmail));


                uow.Commit();
            }
        }