<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using InfinniPlatform.Api.RestApi.Auth;
using InfinniPlatform.Index.ElasticSearch.Factories;
using InfinniPlatform.Index.ElasticSearch.Implementation.ElasticProviders;
using InfinniPlatform.Index.ElasticSearch.Tests.Builders;
using InfinniPlatform.Sdk.Dynamic;
using InfinniPlatform.Sdk.Environment.Index;
using InfinniPlatform.SystemConfig.Multitenancy;
using NUnit.Framework;

namespace InfinniPlatform.Index.ElasticSearch.Tests.ElasticWrappers
{
    [TestFixture]
    [NUnit.Framework.Category(TestCategories.IntegrationTest)]
    public class ElasticSearchAggregationExecutorTests
    {
        private const string IndexName = "aggrunittestindex";
        
        private ICrudOperationProvider _elasticSearchProvider;
        private IIndexStateProvider _indexStateProvider;
        
        [SetUp]
        public void InitializeElasticSearch()
        {
            _indexStateProvider = new ElasticFactory(new MultitenancyProvider()).BuildIndexStateProvider();
            _indexStateProvider.CreateIndexType(IndexName, IndexName);
			_elasticSearchProvider = new ElasticFactory(new MultitenancyProvider()).BuildCrudOperationProvider(IndexName, IndexName, AuthorizationStorageExtensions.AnonimousUser);

            foreach (var school in SchoolsFactory.CreateSchoolsForFacetsTesting())
            {
                // Преобразование школы в объект типа dynamic
                IDictionary<string, object> expando = new ExpandoObject();
                foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(school.GetType()))
                    expando.Add(property.Name, property.GetValue(school));

                dynamic dynSchool = expando as ExpandoObject;
                dynSchool.Id = Guid.NewGuid().ToString().ToLowerInvariant();

                _elasticSearchProvider.Set(dynSchool);
            }

            _elasticSearchProvider.Refresh();
        }

        [Test]
        public void CompleteAggregationBehavior()
        {
            var executor = new ElasticSearchAggregationProvider(IndexName, IndexName, AuthorizationStorageExtensions.AnonimousUser);

            var result = executor.ExecuteTermAggregation(
                new[] { "Name", "Principal.LastName" }, 
                AggregationType.Avg, 
                "Rating");

            // Количество школ с имененм badschool
            var count = result.First(s => s.Name == "badschool").DocCount;

            Assert.IsTrue(count > 0);

            // Получение списка имен школ
            var names = result.Select(school => school.Name).ToList();

            var ratings = new List<string>();

            // Проход по рейтингам всех школ
            foreach (var school in result)
            {
                foreach (var principal in school.Buckets)
                {
                    ratings.Add(
                        string.Format("Школа {0} - Фамилия директора {1} - Рейтинг {2}",
                                       school.Name, principal.Name, principal.Values[0]));
                }
            }

            dynamic points = new DynamicWrapper();
            points.Property = "Rating";
            points.CriteriaType = CriteriaType.IsIn;
	        points.Value = string.Format("{0}\n{1}", 2.5, 4.5);<commit_msg>ElasticWrappers tests fix (second try)<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Dynamic;
using System.Globalization;
using System.Linq;
using InfinniPlatform.Api.RestApi.Auth;
using InfinniPlatform.Index.ElasticSearch.Factories;
using InfinniPlatform.Index.ElasticSearch.Implementation.ElasticProviders;
using InfinniPlatform.Index.ElasticSearch.Tests.Builders;
using InfinniPlatform.Sdk.Dynamic;
using InfinniPlatform.Sdk.Environment.Index;
using InfinniPlatform.SystemConfig.Multitenancy;
using NUnit.Framework;

namespace InfinniPlatform.Index.ElasticSearch.Tests.ElasticWrappers
{
    [TestFixture]
    [NUnit.Framework.Category(TestCategories.IntegrationTest)]
    public class ElasticSearchAggregationExecutorTests
    {
        private const string IndexName = "aggrunittestindex";
        
        private ICrudOperationProvider _elasticSearchProvider;
        private IIndexStateProvider _indexStateProvider;
        
        [SetUp]
        public void InitializeElasticSearch()
        {
            _indexStateProvider = new ElasticFactory(new MultitenancyProvider()).BuildIndexStateProvider();
            _indexStateProvider.CreateIndexType(IndexName, IndexName);
			_elasticSearchProvider = new ElasticFactory(new MultitenancyProvider()).BuildCrudOperationProvider(IndexName, IndexName, AuthorizationStorageExtensions.AnonimousUser);

            foreach (var school in SchoolsFactory.CreateSchoolsForFacetsTesting())
            {
                // Преобразование школы в объект типа dynamic
                IDictionary<string, object> expando = new ExpandoObject();
                foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(school.GetType()))
                    expando.Add(property.Name, property.GetValue(school));

                dynamic dynSchool = expando as ExpandoObject;
                dynSchool.Id = Guid.NewGuid().ToString().ToLowerInvariant();

                _elasticSearchProvider.Set(dynSchool);
            }

            _elasticSearchProvider.Refresh();
        }

        [Test]
        public void CompleteAggregationBehavior()
        {
            var executor = new ElasticSearchAggregationProvider(IndexName, IndexName, AuthorizationStorageExtensions.AnonimousUser);

            var result = executor.ExecuteTermAggregation(
                new[] { "Name", "Principal.LastName" }, 
                AggregationType.Avg, 
                "Rating");

            // Количество школ с имененм badschool
            var count = result.First(s => s.Name == "badschool").DocCount;

            Assert.IsTrue(count > 0);

            // Получение списка имен школ
            var names = result.Select(school => school.Name).ToList();

            var ratings = new List<string>();

            // Проход по рейтингам всех школ
            foreach (var school in result)
            {
                foreach (var principal in school.Buckets)
                {
                    ratings.Add(
                        string.Format("Школа {0} - Фамилия директора {1} - Рейтинг {2}",
                                       school.Name, principal.Name, principal.Values[0]));
                }
            }

            dynamic points = new DynamicWrapper();
            points.Property = "Rating";
            points.CriteriaType = CriteriaType.IsIn;
	        points.Value = string.Format("{0}\n{1}", 2.5.ToString(CultureInfo.CurrentCulture), 4.5.ToString(CultureInfo.CurrentCulture));
{'fraction_non_alphanumeric': 0.0854553301683211, 'fraction_numerical': 0.003165012228456337, 'mean_word_length': 2.5075681130171543, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3233890', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1786, 'n_words': 353}