<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Xunit;

namespace LiteDB.Tests.Database
{
    public class IndexSortAndFilterTest : IDisposable
    {
        #region Model

        public class Item
        {
            public string Id { get; set; }
            public string Value { get; set; }
        }

        #endregion

        private LiteCollection<Item> _collection;
        private TempFile _tempFile;
        private LiteDatabase _database;

        public IndexSortAndFilterTest()
        {
            _tempFile = new TempFile();
            _database = new LiteDatabase(_tempFile.Filename);
            _collection = _database.GetCollection<Item>("items");
        }

        public void Dispose()
        {
            _database.Dispose();
            _tempFile.Dispose();
        }

        [Fact(Skip = "Need Review")]
        public void FilterAndSortAscending()
        {
            _collection.EnsureIndex(nameof(Item.Value));

            PrepareData(_collection);
            var result = FilterAndSortById(_collection, Query.Ascending);

            result[0].Id.Should().Be("B");
            result[1].Id.Should().Be("C");
        }

        [Fact]
        public void FilterAndSortAscendingWithoutIndex()
        {
            PrepareData(_collection);
            var result = FilterAndSortById(_collection, Query.Ascending);

            result[0].Id.Should().Be("B");
            result[1].Id.Should().Be("C");
        }

        [Fact(Skip = "Need review")]
        public void FilterAndSortDescending()
        {
            _collection.EnsureIndex(nameof(Item.Value));

            PrepareData(_collection);
            var result = FilterAndSortById(_collection, Query.Descending);

            result[0].Id.Should().Be("C");
            result[1].Id.Should().Be("B");
        }

        private void PrepareData(LiteCollection<Item> collection)
        {
            collection.Upsert(new Item() { Id = "C", Value = "Value 1" });
            collection.Upsert(new Item() { Id = "A", Value = "Value 2" });
            collection.Upsert(new Item() { Id = "B", Value = "Value 1" });
        }

        private List<Item> FilterAndSortById(LiteCollection<Item> collection, int order)
        {
            var filterQuery = Query.EQ(nameof(Item.Value), "Value 1");

            var result = collection.Find(filterQuery).ToList();
            return result;
        }
    }
}<commit_msg>Fix FilterAndSort unit tests<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Xunit;

namespace LiteDB.Tests.Database
{
    public class IndexSortAndFilterTest : IDisposable
    {
        #region Model

        public class Item
        {
            public string Id { get; set; }
            public string Value { get; set; }
        }

        #endregion

        private LiteCollection<Item> _collection;
        private TempFile _tempFile;
        private LiteDatabase _database;

        public IndexSortAndFilterTest()
        {
            _tempFile = new TempFile();
            _database = new LiteDatabase(_tempFile.Filename);
            _collection = _database.GetCollection<Item>("items");

            _collection.Upsert(new Item() { Id = "C", Value = "Value 1" });
            _collection.Upsert(new Item() { Id = "A", Value = "Value 2" });
            _collection.Upsert(new Item() { Id = "B", Value = "Value 1" });

            _collection.EnsureIndex("idx_value", x => x.Value);
        }

        public void Dispose()
        {
            _database.Dispose();
            _tempFile.Dispose();
        }

        [Fact]
        public void FilterAndSortAscending()
        {
            var result = _collection.Query()
                .Where(x => x.Value == "Value 1")
                .OrderBy(x => x.Value, Query.Ascending)
                .ToList();

            result[0].Id.Should().Be("B");
            result[1].Id.Should().Be("C");
        }

        [Fact]
        public void FilterAndSortDescending()
        {
            var result = _collection.Query()
                .Where(x => x.Value == "Value 1")
                .OrderBy(x => x.Value, Query.Descending)
                .ToList();

            result[0].Id.Should().Be("C");
            result[1].Id.Should().Be("B");
        }

        [Fact]
        public void FilterAndSortAscendingWithoutIndex()
        {
            _collection.DropIndex("idx_value");

            var result = _collection.Query()
                .Where(x => x.Value == "Value 1")
                .OrderBy(x => x.Value, Query.Ascending)
                .ToList();

            result[0].Id.Should().Be("B");
            result[1].Id.Should().Be("C");
        }

    }
}
{'fraction_non_alphanumeric': 0.12343096234309624, 'fraction_numerical': 0.004602510460251046, 'mean_word_length': 1.809048178613396, 'pattern_counts': {'":': 0, '<': 10, '<?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': '3394300', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1300, 'n_words': 271}