<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using RElmah.Models;
using RElmah.Models.Configuration;
using RElmah.Services;
using Xunit;

namespace RElmah.Tests
{
    public class ConfigurationHolderTester
    {
        const string ClusterName = "c1";

        [Fact]
        public async Task AddCluster()
        {
            //Arrange
            var cs = new Dictionary<string, Cluster>();
            var fc = new Func<string, Cluster>(n =>
            {
                cs.Add(n, Cluster.Create(n));
                return cs[n];
            });
            var sut = new ConfigurationHolder(new Fakes.StubIConfigurationUpdater
            {
                AddClusterString = n => Task.FromResult(new ValueOrError<Cluster>(fc(n))),
                GetClusters = () => Task.FromResult((IEnumerable<Cluster>)cs.Values)
            });

            Delta<Cluster> observed = null;
            sut.ObserveClusters().Subscribe(p =>
            {
                observed = p;
            });

            //Act
            var cluster = await sut.AddCluster(ClusterName);
            var check = (await sut.GetClusters()).Single();

            //Assert
            Assert.NotNull(cluster);
            Assert.True(cluster.HasValue);
            Assert.NotNull(cluster.Value);
            Assert.Equal(ClusterName, cluster.Value.Name);

            Assert.Equal(cluster.Value.Name, check.Name);

            Assert.NotNull(observed);
            Assert.NotNull(observed.Target);
            Assert.Equal(ClusterName, observed.Target.Name);
            Assert.Equal(DeltaType.Added, observed.Type);
        }
    }
}<commit_msg>Testing removal of cluster<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using RElmah.Models;
using RElmah.Models.Configuration;
using RElmah.Services;
using Xunit;

namespace RElmah.Tests
{
    public class ConfigurationHolderTester
    {
        const string ClusterName = "c1";

        [Fact]
        public async Task AddCluster()
        {
            //Arrange
            var cs = new Dictionary<string, Cluster>();
            var fc = new Func<string, Cluster>(n =>
            {
                cs.Add(n, Cluster.Create(n));
                return cs[n];
            });
            var sut = new ConfigurationHolder(new Fakes.StubIConfigurationUpdater
            {
                AddClusterString = n => Task.FromResult(new ValueOrError<Cluster>(fc(n))),
                GetClusters = () => Task.FromResult((IEnumerable<Cluster>)cs.Values)
            });

            Delta<Cluster> observed = null;
            sut.ObserveClusters().Subscribe(p =>
            {
                observed = p;
            });

            //Act
            var cluster = await sut.AddCluster(ClusterName);
            var check = (await sut.GetClusters()).Single();

            //Assert
            Assert.NotNull(cluster);
            Assert.True(cluster.HasValue);
            Assert.NotNull(cluster.Value);
            Assert.Equal(ClusterName, cluster.Value.Name);

            Assert.Equal(cluster.Value.Name, check.Name);

            Assert.NotNull(observed);
            Assert.NotNull(observed.Target);
            Assert.Equal(ClusterName, observed.Target.Name);
            Assert.Equal(DeltaType.Added, observed.Type);
        }

        [Fact]
        public async Task AddClusterThenRemoveCluster()
        {
            //Arrange
            var cs = new Dictionary<string, Cluster>();
            var adder = new Func<string, Cluster>(n =>
            {
                cs.Add(n, Cluster.Create(n));
                return cs[n];
            });
            var remover = new Func<string, bool>(cs.Remove);

            var sut = new ConfigurationHolder(new Fakes.StubIConfigurationUpdater
            {
                AddClusterString = n => Task.FromResult(new ValueOrError<Cluster>(adder(n))),
                GetClusters = () => Task.FromResult((IEnumerable<Cluster>)cs.Values),
                RemoveClusterString = n => Task.FromResult(new ValueOrError<bool>(remover(n)))
            });

            Delta<Cluster> observed = null;
            sut.ObserveClusters().Subscribe(p =>
            {
                observed = p;
            });

            //Act
            var cluster = await sut.AddCluster(ClusterName);
            var check = (await sut.GetClusters()).Single();

            //Assert
            Assert.NotNull(cluster);
            Assert.True(cluster.HasValue);
            Assert.NotNull(cluster.Value);
            Assert.Equal(ClusterName, cluster.Value.Name);

            Assert.Equal(cluster.Value.Name, check.Name);

            Assert.NotNull(observed);
            Assert.NotNull(observed.Target);
            Assert.Equal(ClusterName, observed.Target.Name);
            Assert.Equal(DeltaType.Added, observed.Type);

            //Act
            var r = await sut.RemoveCluster(ClusterName);

            //Assert
            Assert.True(r.HasValue);
            Assert.True(r.Value);

            Assert.NotNull(observed);
            Assert.NotNull(observed.Target);
            Assert.Equal(ClusterName, observed.Target.Name);
            Assert.Equal(DeltaType.Removed, observed.Type);
        }
    }
}
{'fraction_non_alphanumeric': 0.11209162598573037, 'fraction_numerical': 0.0003755163349605708, 'mean_word_length': 1.7672727272727273, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 33, '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': '4622069', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1358, 'n_words': 256}