<commit_before>﻿using System;
using System.Linq;
using Ledger.Acceptance.TestObjects;
using Ledger.Infrastructure;
using Newtonsoft.Json;
using Shouldly;
using Xunit;

namespace Ledger.Acceptance
{
	public abstract class AcceptanceTests
	{
		public readonly EventStoreContext SnapshotStream = new EventStoreContext( "SnapshotAggregateStream", new DefaultTypeResolver());
		public readonly EventStoreContext DefaultStream = new EventStoreContext("TestAggregateStream", new DefaultTypeResolver());

		private readonly IEventStore _eventStore;
		private readonly AggregateStore<Guid> _aggregateStore;

		protected AcceptanceTests(IEventStore eventStore)
		{
			_eventStore = eventStore;
			_aggregateStore = new AggregateStore<Guid>(_eventStore);
		}

		[Fact]
		[Trait("acceptance", "true")]
		public void When_there_are_no_events()
		{
			var aggregate = new SnapshotAggregate(DefaultStamper.Now);
			aggregate.GenerateID();

			_aggregateStore.Save(SnapshotStream.StreamName, aggregate);

			using (var reader = _eventStore.CreateReader<Guid>(SnapshotStream))
			{
				reader.LoadEvents(aggregate.ID).ShouldBeEmpty();
			}

		}

		[Fact]
		[Trait("acceptance", "true")]
		public void When_events_are_saved_globalSequence_is_set()
		{
			var id = Guid.NewGuid();
			var t1 = DateTime.UtcNow;
			var t2 = t1.AddSeconds(5);

			using (var writer = _eventStore.CreateWriter<Guid>(DefaultStream))
			{
				writer.SaveEvents(new[]
				{
					new TestEvent {AggregateID = id, Stamp = t1},
					new TestEvent {AggregateID = id, Stamp = t2},
				});
			}

			using (var reader = _eventStore.CreateReader<Guid>(DefaultStream))
			{
				var events = reader.LoadAllEvents().ToList();

				events.ShouldSatisfyAllConditions(
					() => events[0].StreamSequence.ShouldBe(new StreamSequence(0)),
					() => events[1].StreamSequence.ShouldBe(new StreamSequence(1))
				);
			}

		}

		[Fact]
		[Trait("acceptance", "true")]
		public void When_the_event_store_has_newer_events()
		{
			var aggregate = new SnapshotAggregate(DefaultStamper.Now);
			aggregate.GenerateID();

			using (var writer = _eventStore.CreateWriter<Guid>(SnapshotStream))
			{
				writer.SaveEvents(new[] { new TestEvent { AggregateID = aggregate.ID, Stamp = DateTime.UtcNow } });
			}<commit_msg>acceptanceTests: check the streamsequence is just ordered, rather than specific numbers<commit_after>﻿using System;
using System.Linq;
using Ledger.Acceptance.TestObjects;
using Ledger.Infrastructure;
using Newtonsoft.Json;
using Shouldly;
using Xunit;

namespace Ledger.Acceptance
{
	public abstract class AcceptanceTests
	{
		public readonly EventStoreContext SnapshotStream = new EventStoreContext("SnapshotAggregateStream", new DefaultTypeResolver());
		public readonly EventStoreContext DefaultStream = new EventStoreContext("TestAggregateStream", new DefaultTypeResolver());

		private readonly IEventStore _eventStore;
		private readonly AggregateStore<Guid> _aggregateStore;

		protected AcceptanceTests(IEventStore eventStore)
		{
			_eventStore = eventStore;
			_aggregateStore = new AggregateStore<Guid>(_eventStore);
		}

		[Fact]
		[Trait("acceptance", "true")]
		public void When_there_are_no_events()
		{
			var aggregate = new SnapshotAggregate(DefaultStamper.Now);
			aggregate.GenerateID();

			_aggregateStore.Save(SnapshotStream.StreamName, aggregate);

			using (var reader = _eventStore.CreateReader<Guid>(SnapshotStream))
			{
				reader.LoadEvents(aggregate.ID).ShouldBeEmpty();
			}

		}

		[Fact]
		[Trait("acceptance", "true")]
		public void When_events_are_saved_streamSequence_is_set()
		{
			var id = Guid.NewGuid();
			var t1 = DateTime.UtcNow;
			var t2 = t1.AddSeconds(5);

			using (var writer = _eventStore.CreateWriter<Guid>(DefaultStream))
			{
				writer.SaveEvents(new[]
				{
					new TestEvent {AggregateID = id, Stamp = t1},
					new TestEvent {AggregateID = id, Stamp = t2},
				});
			}

			using (var reader = _eventStore.CreateReader<Guid>(DefaultStream))
			{
				var events = reader.LoadAllEvents().ToList();

				var first = events[0].StreamSequence;
				var second = events[1].StreamSequence;

				second.ShouldBeGreaterThan(first);
			}

		}

		[Fact]
		[Trait("acceptance", "true")]
		public void When_the_event_store_has_newer_events()
		{
			var aggregate = new SnapshotAggregate(DefaultStamper.Now);
			aggregate.GenerateID();

			using (var writer = _eventStore.CreateWriter<Guid>(SnapshotStream))
			{
				writer.SaveEvents(new[] { new TestEvent { AggregateID = aggregate.ID, Stamp = DateTime.UtcNow } });
			}
{'fraction_non_alphanumeric': 0.10953750829829609, 'fraction_numerical': 0.00398318211993804, 'mean_word_length': 4.7360406091370555, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '4608479', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1394, 'n_words': 285}