<commit_before>﻿using System;
using System.Threading.Tasks;
using Exceptionless.Api.Tests.Utility;
using Exceptionless.Core.Component;
using Exceptionless.Core.Extensions;
using Exceptionless.Core.Repositories;
using Exceptionless.DateTimeExtensions;
using Exceptionless.Tests.Utility;
using Nest;
using Xunit;

namespace Exceptionless.Api.Tests.Repositories {
    public class StackRepositoryTests {
        private const int NUMBER_OF_STACKS_TO_CREATE = 50;
        private readonly IElasticClient _client = IoC.GetInstance<IElasticClient>();
        private readonly IStackRepository _repository = IoC.GetInstance<IStackRepository>();
        
        [Fact]
        public async Task MarkAsRegressedTest() {
            await RemoveDataAsync().AnyContext();

            await _repository.AddAsync(StackData.GenerateStack(id: TestConstants.StackId, projectId: TestConstants.ProjectId, organizationId: TestConstants.OrganizationId, dateFixed: DateTime.Now.SubtractMonths(1))).AnyContext();
            _client.Refresh();

            var stack = await _repository.GetByIdAsync(TestConstants.StackId).AnyContext();
            Assert.NotNull(stack);
            Assert.False(stack.IsRegressed);
            Assert.NotNull(stack.DateFixed);

            await _repository.MarkAsRegressedAsync(TestConstants.StackId).AnyContext();
            
            _client.Refresh();
            stack = await _repository.GetByIdAsync(TestConstants.StackId).AnyContext();
            Assert.NotNull(stack);
            Assert.True(stack.IsRegressed);
            Assert.Null(stack.DateFixed);
        }

        [Fact]
        public async Task IncrementEventCounterTest() {
            await RemoveDataAsync().AnyContext();

            await _repository.AddAsync(StackData.GenerateStack(id: TestConstants.StackId, projectId: TestConstants.ProjectId, organizationId: TestConstants.OrganizationId)).AnyContext();
            _client.Refresh();

            var stack = await _repository.GetByIdAsync(TestConstants.StackId).AnyContext();
            Assert.NotNull(stack);
            Assert.Equal(0, stack.TotalOccurrences);
            Assert.Equal(DateTime.MinValue, stack.FirstOccurrence);
            Assert.Equal(DateTime.MinValue, stack.LastOccurrence);

            var utcNow = DateTime.UtcNow;
            await _repository.IncrementEventCounterAsync(TestConstants.OrganizationId, TestConstants.ProjectId, TestConstants.StackId, utcNow, utcNow, 1).AnyContext();
            _client.Refresh();

            stack = await _repository.GetByIdAsync(TestConstants.StackId).AnyContext();
            Assert.Equal(1, stack.TotalOccurrences);
            Assert.Equal(utcNow, stack.FirstOccurrence);
            Assert.Equal(utcNow, stack.LastOccurrence);

            await _repository.IncrementEventCounterAsync(TestConstants.OrganizationId, TestConstants.ProjectId, TestConstants.StackId, utcNow.SubtractDays(1), utcNow.SubtractDays(1), 1).AnyContext();
            _client.Refresh();

            stack = await _repository.GetByIdAsync(TestConstants.StackId).AnyContext();
            Assert.Equal(2, stack.TotalOccurrences);
            Assert.Equal(utcNow.SubtractDays(1), stack.FirstOccurrence);
            Assert.Equal(utcNow, stack.LastOccurrence);

            await _repository.IncrementEventCounterAsync(TestConstants.OrganizationId, TestConstants.ProjectId, TestConstants.StackId, utcNow.AddDays(1), utcNow.AddDays(1), 1).AnyContext();
            _client.Refresh();

            stack = await _repository.GetByIdAsync(TestConstants.StackId).AnyContext();
            Assert.Equal(3, stack.TotalOccurrences);
            Assert.Equal(utcNow.SubtractDays(1), stack.FirstOccurrence);
            Assert.Equal(utcNow.AddDays(1), stack.LastOccurrence);
        }

        [Fact]
        public Task GetStackInfoBySignatureHashTest() {
            return TaskHelper.Completed();
        }
        
        [Fact]
        public Task GetMostRecentTest() {
            return TaskHelper.Completed();
        }
        
        [Fact]
        public Task GetNewTest() {
            return TaskHelper.Completed();
        }
        
        [Fact]
        public Task InvalidateCacheTest() {
            return TaskHelper.Completed();
        }

        protected async Task RemoveDataAsync() {
            await _repository.RemoveAllAsync().AnyContext();
        }
    }
}<commit_msg>Fixed a bug where the stack tests were not removing any previous events.<commit_after>﻿using System;
using System.Threading.Tasks;
using Exceptionless.Api.Tests.Utility;
using Exceptionless.Core.Component;
using Exceptionless.Core.Extensions;
using Exceptionless.Core.Repositories;
using Exceptionless.DateTimeExtensions;
using Exceptionless.Tests.Utility;
using Nest;
using Xunit;

namespace Exceptionless.Api.Tests.Repositories {
    public class StackRepositoryTests {
        private const int NUMBER_OF_STACKS_TO_CREATE = 50;
        private readonly IElasticClient _client = IoC.GetInstance<IElasticClient>();
        private readonly IEventRepository _eventRepository = IoC.GetInstance<IEventRepository>();
        private readonly IStackRepository _repository = IoC.GetInstance<IStackRepository>();
        
        [Fact]
        public async Task MarkAsRegressedTest() {
            await RemoveDataAsync().AnyContext();

            await _repository.AddAsync(StackData.GenerateStack(id: TestConstants.StackId, projectId: TestConstants.ProjectId, organizationId: TestConstants.OrganizationId, dateFixed: DateTime.Now.SubtractMonths(1))).AnyContext();
            _client.Refresh();

            var stack = await _repository.GetByIdAsync(TestConstants.StackId).AnyContext();
            Assert.NotNull(stack);
            Assert.False(stack.IsRegressed);
            Assert.NotNull(stack.DateFixed);

            await _repository.MarkAsRegressedAsync(TestConstants.StackId).AnyContext();
            
            _client.Refresh();
            stack = await _repository.GetByIdAsync(TestConstants.StackId).AnyContext();
            Assert.NotNull(stack);
            Assert.True(stack.IsRegressed);
            Assert.Null(stack.DateFixed);
        }

        [Fact]
        public async Task IncrementEventCounterTest() {
            await RemoveDataAsync().AnyContext();

            await _repository.AddAsync(StackData.GenerateStack(id: TestConstants.StackId, projectId: TestConstants.ProjectId, organizationId: TestConstants.OrganizationId)).AnyContext();
            _client.Refresh();

            var stack = await _repository.GetByIdAsync(TestConstants.StackId).AnyContext();
            Assert.NotNull(stack);
            Assert.Equal(0, stack.TotalOccurrences);
            Assert.Equal(DateTime.MinValue, stack.FirstOccurrence);
            Assert.Equal(DateTime.MinValue, stack.LastOccurrence);

            var utcNow = DateTime.UtcNow;
            await _repository.IncrementEventCounterAsync(TestConstants.OrganizationId, TestConstants.ProjectId, TestConstants.StackId, utcNow, utcNow, 1).AnyContext();
            _client.Refresh();

            stack = await _repository.GetByIdAsync(TestConstants.StackId).AnyContext();
            Assert.Equal(1, stack.TotalOccurrences);
            Assert.Equal(utcNow, stack.FirstOccurrence);
            Assert.Equal(utcNow, stack.LastOccurrence);

            await _repository.IncrementEventCounterAsync(TestConstants.OrganizationId, TestConstants.ProjectId, TestConstants.StackId, utcNow.SubtractDays(1), utcNow.SubtractDays(1), 1).AnyContext();
            _client.Refresh();

            stack = await _repository.GetByIdAsync(TestConstants.StackId).AnyContext();
            Assert.Equal(2, stack.TotalOccurrences);
            Assert.Equal(utcNow.SubtractDays(1), stack.FirstOccurrence);
            Assert.Equal(utcNow, stack.LastOccurrence);

            await _repository.IncrementEventCounterAsync(TestConstants.OrganizationId, TestConstants.ProjectId, TestConstants.StackId, utcNow.AddDays(1), utcNow.AddDays(1), 1).AnyContext();
            _client.Refresh();

            stack = await _repository.GetByIdAsync(TestConstants.StackId).AnyContext();
            Assert.Equal(3, stack.TotalOccurrences);
            Assert.Equal(utcNow.SubtractDays(1), stack.FirstOccurrence);
            Assert.Equal(utcNow.AddDays(1), stack.LastOccurrence);
        }

        [Fact]
        public Task GetStackInfoBySignatureHashTest() {
            return TaskHelper.Completed();
        }
        
        [Fact]
        public Task GetMostRecentTest() {
            return TaskHelper.Completed();
        }
        
        [Fact]
        public Task GetNewTest() {
            return TaskHelper.Completed();
        }
        
        [Fact]
        public Task InvalidateCacheTest() {
            return TaskHelper.Completed();
        }

        protected async Task RemoveDataAsync() {
            await _eventRepository.RemoveAllAsync().AnyContext();
            await _repository.RemoveAllAsync().AnyContext();
        }
    }
}
{'fraction_non_alphanumeric': 0.09714031378658061, 'fraction_numerical': 0.0037832424613330365, 'mean_word_length': 2.8858625162127107, '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': '7313831', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2319, 'n_words': 385}