<commit_before>﻿using System;
using System.Linq;
using NoRM.BSON;
using NoRM.Linq;
using NoRM.Protocol.Messages;
using Xunit;

namespace NoRM.Tests
{
    public class MongoOptimizationTests
    {
        [Fact]
        public void MongoCollectionEnsuresIndicies()
        {
            using (var session = new Session())
            {
                session.Drop<Product>();

                session.Add(new Product
                {
                    Name = "ExplainProduct",
                    Price = 10,
                    Supplier = new Supplier { Name = "Supplier", CreatedOn = DateTime.Now }
                });

                session.Provider.DB.GetCollection<Product>().CreateIndex(p => p.Supplier.Name, "Test", true, IndexOption.Ascending);
            }
        }

        [Fact]
        public void MongoQueryExplainsExecutionPlansForFlyweightQueries()
        {
            using (var session = new Session())
            {
                session.Drop<Product>();

                session.Provider.DB.GetCollection<Product>().CreateIndex(p => p.Supplier.Name, "TestIndex", true, IndexOption.Ascending);

                session.Add(new Product
                                {
                                    Name = "ExplainProduct",
                                    Price = 10,
                                    Supplier = new Supplier { Name = "Supplier", CreatedOn = DateTime.Now }
                                });

                // To see this manually you can run the following command in Mongo.exe against 
                //the Product collection db.Product.ensureIndex({"Supplier.Name":1})

                // Then you can run this command to see a detailed explain plan
                // db.Product.find({"Supplier.Name":"abc"})

                // The following query is the same as runging: db.Product.find({"Supplier.Name":"abc"}).explain()
                var query = new Flyweight();
                query["Supplier.Name"] = Q.Equals("Supplier");

                var result = session.Provider.DB.GetCollection<Product>().Explain(query);

                Assert.Equal("BtreeCursor TestIndex", result.Cursor);
            }
        }

        [Fact]
        public void MongoQueryExplainsExecutionPlans()
        {
            using (var session = new Session())
            {
                session.Drop<Product>();

                session.Provider.DB.GetCollection<Product>().CreateIndex(p => p.Name, "TestIndex", true, IndexOption.Ascending);

                session.Add(new Product
                {
                    Name = "ExplainProduct",
                    Price = 10,
                    Supplier = new Supplier { Name = "Supplier", CreatedOn = DateTime.Now }
                });


                var result = session.Provider.DB.GetCollection<Product>().Explain(new { Name = "ExplainProduct" });

                Assert.Equal("BtreeCursor TestIndex", result.Cursor);
            }
        }

        [Fact]
        public void MongoQueryExplainsLinqExecutionPlans()
        {
            using (var session = new Session())
            {
                session.Drop<Product>();
               
                session.Provider.DB.GetCollection<Product>().CreateIndex(p => p.Supplier.Name, "TestIndex", true, IndexOption.Ascending);

                session.Add(new Product
                {
                    Name = "ExplainProduct",
                    Price = 10,
                    Supplier = new Supplier { Name = "Supplier", CreatedOn = DateTime.Now }
                });

                var result = session.Products
                    .Where(x => x.Supplier.Name == "Supplier")
                    .Explain();

                Assert.Equal("BtreeCursor TestIndex", result.Cursor);
            }
        }

        [Fact]
        public void MongoQuerySupportsHintsForLinqQueries()
        {
            using (var session = new Session())
            {
                session.Drop<Product>();

                session.Add(new Product
                                {
                                    Name = "ExplainProduct",
                                    Price = 10,
                                    Supplier = new Supplier {Name = "Supplier", CreatedOn = DateTime.Now}
                                });

                var query = new Flyweight();
                query["Supplier.Name"] = Q.Equals("Supplier");

                var result = session.Provider.DB.GetCollection<Product>()
                    .Find(query)
                    .Hint(p => p.Name, IndexOption.Ascending);

                Assert.Equal(1, result.Count());
            }
        }

        [Fact]
        public void MongoQuerySupportschainingHintsForLinqQueries()
        {
            using (var session = new Session())
            {
                session.Drop<Product>();

                session.Add(new Product
                {
                    Name = "ExplainProduct",
                    Price = 10,<commit_msg>still attemping a profile name change.<commit_after>﻿using System;
using System.Linq;
using NoRM.BSON;
using NoRM.Linq;
using NoRM.Protocol.Messages;
using Xunit;

namespace NoRM.Tests
{
    public class MongoOptimizationTests
    {
        [Fact]
        public void MongoCollectionEnsuresIndicies()
        {
            using (var session = new Session())
            {
                session.Drop<Product>();

                session.Add(new Product
                {
                    Name = "ExplainProduct",
                    Price = 10,
                    Supplier = new Supplier { Name = "Supplier", CreatedOn = DateTime.Now }
                });

                session.Provider.DB.GetCollection<Product>().CreateIndex(p => p.Supplier.Name, "Test", true, IndexOption.Ascending);
            }
        }

        [Fact]
        public void MongoQueryExplainsExecutionPlansForFlyweightQueries()
        {
            using (var session = new Session())
            {
                session.Drop<Product>();

                session.Provider.DB.GetCollection<Product>().CreateIndex(p => p.Supplier.Name, "TestIndex", true, IndexOption.Ascending);

                session.Add(new Product
                                {
                                    Name = "ExplainProduct",
                                    Price = 10,
                                    Supplier = new Supplier { Name = "Supplier", CreatedOn = DateTime.Now }
                                });

                // To see this manually you can run the following command in Mongo.exe against 
                //the Product collection db.Product.ensureIndex({"Supplier.Name":1})

                // Then you can run this command to see a detailed explain plan
                // db.Product.find({"Supplier.Name":"abc"})

                // The following query is the same as runging: db.Product.find({"Supplier.Name":"abc"}).explain()
                var query = new Flyweight();
                query["Supplier.Name"] = Q.Equals("Supplier");

                var result = session.Provider.DB.GetCollection<Product>().Explain(query);

                Assert.Equal("BtreeCursor TestIndex", result.Cursor);
            }
        }

        [Fact]
        public void MongoQueryExplainsExecutionPlans()
        {
            using (var session = new Session())
            {
                session.Drop<Product>();

                session.Provider.DB.GetCollection<Product>().CreateIndex(p => p.Name, "TestIndex", true, IndexOption.Ascending);

                session.Add(new Product
                {
                    Name = "ExplainProduct",
                    Price = 10,
                    Supplier = new Supplier { Name = "Supplier", CreatedOn = DateTime.Now }
                });


                var result = session.Provider.DB.GetCollection<Product>().Explain(new { Name = "ExplainProduct" });

                Assert.Equal("BtreeCursor TestIndex", result.Cursor);
            }
        }

        [Fact]
        public void MongoQueryExplainsLinqExecutionPlans()
        {
            using (var session = new Session())
            {
                session.Drop<Product>();
               
                session.Provider.DB.GetCollection<Product>().CreateIndex(p => p.Supplier.Name, "TestIndex", true, IndexOption.Ascending);

                session.Add(new Product
                {
                    Name = "ExplainProduct",
                    Price = 10,
                    Supplier = new Supplier { Name = "Supplier", CreatedOn = DateTime.Now }
                });

                var result = session.Products
                    .Where(x => x.Supplier.Name == "Supplier")
                    .Explain();

                Assert.Equal("BtreeCursor TestIndex", result.Cursor);
            }
        }

        [Fact]
        public void MongoQuerySupportsHintsForLinqQueries()
        {
            using (var session = new Session())
            {
                session.Drop<Product>();

                session.Add(new Product
                                {
                                    Name = "ExplainProduct",
                                    Price = 10,
                                    Supplier = new Supplier {Name = "Supplier", CreatedOn = DateTime.Now}
                                });

                var query = new Flyweight();
                query["Supplier.Name"] = Q.Equals("Supplier");

                var result = session.Provider.DB
                    .GetCollection<Product>()
                    .Find(query)
                    .Hint(p => p.Name, IndexOption.Ascending);

                Assert.Equal(1, result.Count());
            }
        }

        [Fact]
        public void MongoQuerySupportschainingHintsForLinqQueries()
        {
            using (var session = new Session())
            {
                session.Drop<Product>();

                session.Add(new Product
                {
                    Name = "ExplainProduct",
                    Price = 10,
{'fraction_non_alphanumeric': 0.10083615369301215, 'fraction_numerical': 0.002787178976707147, 'mean_word_length': 1.2927886809675948, 'pattern_counts': {'":': 6, '<': 29, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3611990', 'n_tokens_mistral': 2538, 'n_tokens_neox': 2291, 'n_words': 509}