<commit_before>﻿namespace Cedar.EventStore
{
    using System.Threading.Tasks;
    using Cedar.EventStore.Exceptions;
    using Xunit;

    public partial class EventStoreAcceptanceTests
    {
        [Fact]
        public async Task When_append_stream_second_time_with_no_stream_expected_and_different_event_then_should_throw()
        {
            using(var fixture = GetFixture())
            {
                using(var eventStore = await fixture.GetEventStore())
                {
                    const string streamId = "stream-1";
                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(1, 2, 3));

                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(2, 3, 4))
                        .ShouldThrow<WrongExpectedVersionException>(
                            Messages.AppendFailedWrongExpectedVersion.FormatWith(streamId, ExpectedVersion.NoStream));
                }
            }
        }

        [Fact]
        public async Task When_append_stream_second_time_with_no_stream_expected_and_same_events_then_should_not_throw()
        {
            // Idempotency
            using (var fixture = GetFixture())
            {
                using (var eventStore = await fixture.GetEventStore())
                {
                    const string streamId = "stream-1";
                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(1, 2, 3));

                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(1, 2))
                        .ShouldNotThrow();
                }
            }
        }

        [Fact]
        public async Task When_append_stream_second_time_with_no_stream_expected_and_same_inital_event_then_should_not_throw()
        {
            // Idempotency
            using (var fixture = GetFixture())
            {
                using (var eventStore = await fixture.GetEventStore())
                {
                    const string streamId = "stream-1";
                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(1, 2, 3));

                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(1, 2))
                        .ShouldNotThrow();
                }
            }
        }

        [Fact]
        public async Task When_append_with_wrong_expected_version_then_should_throw()
        {
            using (var fixture = GetFixture())
            {
                using (var eventStore = await fixture.GetEventStore())
                {
                    const string streamId = "stream-1";
                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(1, 2, 3));

                    await eventStore
                        .AppendToStream(streamId, 1, CreateNewStreamEvents(4, 5, 6))
                        .ShouldThrow<WrongExpectedVersionException>(
                            Messages.AppendFailedWrongExpectedVersion.FormatWith(streamId, 1));
                }
            }
        }

        [Fact]
        public async Task Can_append_stream_with_correct_expected_version()
        {
            using (var fixture = GetFixture())<commit_msg>Add idempotency test for Append NoStream for existing stream but different event Id<commit_after>﻿namespace Cedar.EventStore
{
    using System.Threading.Tasks;
    using Cedar.EventStore.Exceptions;
    using Xunit;

    public partial class EventStoreAcceptanceTests
    {
        [Fact]
        public async Task When_append_stream_second_time_with_no_stream_expected_and_different_event_then_should_throw()
        {
            using(var fixture = GetFixture())
            {
                using(var eventStore = await fixture.GetEventStore())
                {
                    const string streamId = "stream-1";
                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(1, 2, 3));

                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(2, 3, 4))
                        .ShouldThrow<WrongExpectedVersionException>(
                            Messages.AppendFailedWrongExpectedVersion.FormatWith(streamId, ExpectedVersion.NoStream));
                }
            }
        }

        [Fact]
        public async Task When_append_stream_second_time_with_no_stream_expected_and_same_events_then_should_not_throw()
        {
            // Idempotency
            using (var fixture = GetFixture())
            {
                using (var eventStore = await fixture.GetEventStore())
                {
                    const string streamId = "stream-1";
                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(1, 2));

                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(1, 2))
                        .ShouldNotThrow();
                }
            }
        }

        [Fact]
        public async Task When_append_stream_second_time_with_no_stream_expected_and_same_inital_events_then_should_not_throw()
        {
            // Idempotency
            using (var fixture = GetFixture())
            {
                using (var eventStore = await fixture.GetEventStore())
                {
                    const string streamId = "stream-1";
                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(1, 2));

                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(1))
                        .ShouldNotThrow();
                }
            }
        }

        [Fact]
        public async Task When_append_stream_second_time_with_no_stream_expected_and_different_inital_events_then_should_throw()
        {
            using (var fixture = GetFixture())
            {
                using (var eventStore = await fixture.GetEventStore())
                {
                    const string streamId = "stream-1";
                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(1, 2));

                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(2))
                        .ShouldThrow<WrongExpectedVersionException>(
                            Messages.AppendFailedWrongExpectedVersion.FormatWith(streamId, ExpectedVersion.NoStream));
                }
            }
        }

        [Fact]
        public async Task When_append_with_wrong_expected_version_then_should_throw()
        {
            using (var fixture = GetFixture())
            {
                using (var eventStore = await fixture.GetEventStore())
                {
                    const string streamId = "stream-1";
                    await eventStore
                        .AppendToStream(streamId, ExpectedVersion.NoStream, CreateNewStreamEvents(1, 2, 3));

                    await eventStore
                        .AppendToStream(streamId, 1, CreateNewStreamEvents(4, 5, 6))
                        .ShouldThrow<WrongExpectedVersionException>(
                            Messages.AppendFailedWrongExpectedVersion.FormatWith(streamId, 1));
                }
            }
        }

        [Fact]
        public async Task Can_append_stream_with_correct_expected_version()
        {
            using (var fixture = GetFixture())
{'fraction_non_alphanumeric': 0.06760809797550613, 'fraction_numerical': 0.0071232191952012, 'mean_word_length': 1.592484612892776, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, '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': '5220276', 'n_tokens_mistral': 2004, 'n_tokens_neox': 1879, 'n_words': 350}