<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NSubstitute;

namespace Ledger.Stores.Fs.Tests
{
	public class TestBase
	{
		public List<string> Events { get; private set; }
		public List<string> Snapshots { get; private set; }

		public TestBase()
		{
			Events = new List<string>();
			Snapshots = new List<string>();
		}

		protected void Save(AggregateRoot<Guid> aggregate)
		{
			var events = new MemoryStream();
			var snapshots = new MemoryStream();

			var fileSystem = Substitute.For<IFileSystem>();
			fileSystem.AppendTo("fs\\Guid.events.json").Returns(events);
			fileSystem.AppendTo("fs\\Guid.snapshots.json").Returns(snapshots);

			var fs = new FileEventStore(fileSystem, "fs");
			var store = new AggregateStore<Guid>(fs);

			store.Save(aggregate);

			Events.Clear();
			Events.AddRange(FromStream(events));

			Snapshots.Clear();
			Snapshots.AddRange(FromStream(snapshots));
		}

		private static List<string> FromStream(MemoryStream stream)
		{
			var copy = new MemoryStream(stream.ToArray());

			using (var reader = new StreamReader(copy))
			{
				return reader
					.ReadToEnd()
					.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
					.ToList();
			}
		}

	}
}<commit_msg>tests: add loading baseclass methods<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Ledger.Infrastructure;
using NSubstitute;
using TestsDomain;

namespace Ledger.Stores.Fs.Tests
{
	public class TestBase
	{
		public List<string> Events { get; private set; }
		public List<string> Snapshots { get; private set; }

		public TestBase()
		{
			Events = new List<string>();
			Snapshots = new List<string>();
		}

		protected void Save(AggregateRoot<Guid> aggregate)
		{
			var events = new MemoryStream();
			var snapshots = new MemoryStream();

			var fileSystem = Substitute.For<IFileSystem>();
			fileSystem.AppendTo("fs\\Guid.events.json").Returns(events);
			fileSystem.AppendTo("fs\\Guid.snapshots.json").Returns(snapshots);

			var fs = new FileEventStore(fileSystem, "fs");
			var store = new AggregateStore<Guid>(fs);

			store.Save(aggregate);

			Events.Clear();
			Events.AddRange(FromStream(events));

			Snapshots.Clear();
			Snapshots.AddRange(FromStream(snapshots));
		}

		protected Candidate Load(Guid id)
		{
			var events = ToStream(Events);
			var snapshots = ToStream(Snapshots);

			var fileSystem = Substitute.For<IFileSystem>();
			fileSystem.AppendTo("fs\\Guid.events.json").Returns(events);
			fileSystem.AppendTo("fs\\Guid.snapshots.json").Returns(snapshots);

			var fs = new FileEventStore(fileSystem, "fs");
			var store = new AggregateStore<Guid>(fs);

			return store.Load(id, () => new Candidate());
		}

		private static List<string> FromStream(MemoryStream stream)
		{
			var copy = new MemoryStream(stream.ToArray());

			using (var reader = new StreamReader(copy))
			{
				return reader
					.ReadToEnd()
					.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
					.ToList();
			}
		}

		private static Stream ToStream(IEnumerable<string> items)
		{
			var ms = new MemoryStream();
			var sw = new StreamWriter(ms);

			items.ForEach(x => sw.WriteLine(x));

			ms.Position = 0;
			return ms;
		}

	}
}
{'fraction_non_alphanumeric': 0.1347496206373293, 'fraction_numerical': 0.00030349013657056146, 'mean_word_length': 4.016742770167427, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 24, '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': '5505574', 'n_tokens_mistral': 1226, 'n_tokens_neox': 1033, 'n_words': 231}