public void sequence_1em1_2e1_3e2_4e3_5e4_6e5_1em1_idempotent()
        {
            const string stream = "appending_to_implicitly_created_stream_using_transaction_sequence_1em1_2e1_3e2_4e3_5e4_6e5_1em1_idempotent";
            using (var store = EventStoreConnection.Create())
            {
                store.Connect(_node.TcpEndPoint);

                var events = Enumerable.Range(1, 6).Select(x => TestEvent.NewTestEvent(Guid.NewGuid())).ToArray();
                var writer = new TransactionalWriter(store, stream);

                Assert.DoesNotThrow(() => writer.StartTransaction(-1).Write(events).Commit());
                Assert.DoesNotThrow(() => writer.StartTransaction(-1).Write(events.First()).Commit());

                var total = EventsStream.Count(store, stream);
                Assert.That(total, Is.EqualTo(events.Length + 1));
            }
        }
        public void sequence_0em1_1e0_2e1_3e2_4e3_5e4_0any_idempotent()
        {
            const string stream = "appending_to_implicitly_created_stream_using_transaction_sequence_0em1_1e0_2e1_3e2_4e3_5e4_0any_idempotent";
            using (var store = TestConnection.Create(_node.TcpEndPoint))
            {
                store.ConnectAsync().Wait();

                var events = Enumerable.Range(0, 6).Select(x => TestEvent.NewTestEvent(Guid.NewGuid())).ToArray();
                var writer = new TransactionalWriter(store, stream);

                Assert.AreEqual(5, writer.StartTransaction(-1).Write(events).Commit().NextExpectedVersion);
                Assert.AreEqual(0, writer.StartTransaction(ExpectedVersion.Any).Write(events.First()).Commit().NextExpectedVersion);

                var total = EventsStream.Count(store, stream);
                Assert.That(total, Is.EqualTo(events.Length));
            }
        }
        public void sequence_S_0em1_1em1_E_S_0em1_1em1_2em1_E_idempotancy_fail()
        {
            const string stream = "appending_to_implicitly_created_stream_using_transaction_sequence_S_0em1_1em1_E_S_0em1_1em1_2em1_E_idempotancy_fail";
            using (var store = TestConnection.Create(_node.TcpEndPoint))
            {
                store.ConnectAsync().Wait();

                var events = Enumerable.Range(0, 2).Select(x => TestEvent.NewTestEvent(Guid.NewGuid())).ToArray();
                var writer = new TransactionalWriter(store, stream);

                Assert.AreEqual(1, writer.StartTransaction(-1).Write(events).Commit().NextExpectedVersion);
                Assert.That(() => writer.StartTransaction(-1)
                                        .Write(events.Concat(new[] { TestEvent.NewTestEvent(Guid.NewGuid()) }).ToArray())
                                        .Commit(),
                            Throws.Exception.TypeOf<AggregateException>().With.InnerException.TypeOf<WrongExpectedVersionException>());
            }
        }
        public void sequence_0em1_1e0_2e1_3e2_4e3_5e4_0e4_wev()
        {
            const string stream = "appending_to_implicitly_created_stream_using_transaction_sequence_0em1_1e0_2e1_3e2_4e3_5e4_0e4_wev";
            using (var store = TestConnection.Create(_node.TcpEndPoint))
            {
                store.Connect();

                var events = Enumerable.Range(0, 6).Select(x => TestEvent.NewTestEvent(Guid.NewGuid())).ToArray();
                var writer = new TransactionalWriter(store, stream);

                Assert.AreEqual(5, writer.StartTransaction(-1).Write(events).Commit().NextExpectedVersion);
                Assert.That(() => writer.StartTransaction(4).Write(events.First()).Commit(), 
                            Throws.Exception.TypeOf<AggregateException>().With.InnerException.TypeOf<WrongExpectedVersionException>());
            }
        }
        public void sequence_0_S_1e0_2e0_E_S_1e0_2e0_3e0_E_idempotency_fail()
        {
            const string stream = "appending_to_explicitly_created_stream_using_transaction_sequence_0_S_1e0_2e0_E_S_1e0_2e0_3e0_E_idempotency_fail";
            using (var store = EventStoreConnection.Create())
            {
                store.Connect(_node.TcpEndPoint);
                var create = store.CreateStreamAsync(stream, Guid.NewGuid(), false, new byte[0]);
                Assert.DoesNotThrow(create.Wait);

                var events = Enumerable.Range(1, 2).Select(x => TestEvent.NewTestEvent(Guid.NewGuid())).ToArray();
                var writer = new TransactionalWriter(store, stream);

                Assert.DoesNotThrow(() => writer.StartTransaction(0).Write(events).Commit());
                Assert.That(() => writer.StartTransaction(0).Write(events.Concat(new[] {TestEvent.NewTestEvent(Guid.NewGuid())}).ToArray()).Commit(),
                            Throws.Exception.TypeOf<AggregateException>().With.InnerException.TypeOf<WrongExpectedVersionException>());
            }
        }
        public void sequence_0_1e0_2e1_3e2_2any_2any_idempotent()
        {
            const string stream = "appending_to_explicitly_created_stream_using_transaction_sequence_0_1e0_2e1_3e2_2any_2any_idempotent";
            using (var store = EventStoreConnection.Create())
            {
                store.Connect(_node.TcpEndPoint);
                var create = store.CreateStreamAsync(stream, Guid.NewGuid(), false, new byte[0]);
                Assert.DoesNotThrow(create.Wait);

                var events = Enumerable.Range(1, 3).Select(x => TestEvent.NewTestEvent(Guid.NewGuid())).ToArray();
                var writer = new TransactionalWriter(store, stream);

                Assert.DoesNotThrow(() => writer.StartTransaction(0).Write(events).Commit());
                Assert.DoesNotThrow(() => writer.StartTransaction(ExpectedVersion.Any).Write(events[1]).Write(events[1]).Commit());

                var total = EventsStream.Count(store, stream);
                Assert.That(total, Is.EqualTo(events.Length + 1));
            }
        }
        public void sequence_0em1_0e0_non_idempotent()
        {
            const string stream = "appending_to_implicitly_created_stream_using_transaction_sequence_0em1_0e0_non_idempotent";
            using (var store = BuildConnection(_node))
            {
                store.ConnectAsync().Wait();

                var events = Enumerable.Range(0, 1).Select(x => TestEvent.NewTestEvent(Guid.NewGuid())).ToArray();
                var writer = new TransactionalWriter(store, stream);

                Assert.AreEqual(0, writer.StartTransaction(-1).Write(events).Commit().NextExpectedVersion);
                Assert.AreEqual(1, writer.StartTransaction(0).Write(events.First()).Commit().NextExpectedVersion);

                var total = EventsStream.Count(store, stream);
                Assert.That(total, Is.EqualTo(events.Length + 1));
            }
        }