<commit_before>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SubstituteTypeExpressionVisitorTest.cs" company="Quartz Software SRL">
//   Copyright (c) Quartz Software SRL. All rights reserved.
// </copyright>
// <summary>
//   Implements the substitute type expression visitor test class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Kephas.Data.Tests.Linq.Expressions
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;

    using Kephas.Activation;
    using Kephas.Data.Linq.Expressions;
    using Kephas.Reflection;
    using Kephas.Services;

    using NSubstitute;

    using NUnit.Framework;

    [TestFixture]
    public class SubstituteTypeExpressionVisitorTest
    {
        [Test]
        public void Visit_Where()
        {
            var activator = Substitute.For<IActivator>();
            activator.GetImplementationType(typeof(ITest).AsRuntimeTypeInfo(), Arg.Any<IContext>(), Arg.Any<bool>())
                .Returns(typeof(Test).AsRuntimeTypeInfo());

            var baseQuery = (IQueryable<ITest>)new List<Test>(new[]
                                                                  {
                                                                      new Test { Name = "gigi" },
                                                                      new Test { Name = "belogea" }
                                                                  }).AsQueryable();
            var query = baseQuery.Where(t => t.Name == "gigi");
            var visitor = new SubstituteTypeExpressionVisitor(activator: activator);
            var newExpression = visitor.Visit(query.Expression);

            var methodCallExpression = (MethodCallExpression)newExpression;
            var genericArg = methodCallExpression.Method.GetGenericArguments()[0];
            Assert.AreEqual(typeof(Test), genericArg);

            var result = baseQuery.Provider.Execute<IEnumerable<Test>>(newExpression);
            Assert.AreEqual(1, result.Count());
        }

        [Test]
        public void Visit_Where_captured_local_variable()
        {
            var activator = Substitute.For<IActivator>();
            activator.GetImplementationType(typeof(ITest).AsRuntimeTypeInfo(), Arg.Any<IContext>(), Arg.Any<bool>())
                .Returns(typeof(Test).AsRuntimeTypeInfo());
            activator.GetImplementationType(typeof(string).AsRuntimeTypeInfo(), Arg.Any<IContext>(), Arg.Any<bool>())
                .Returns(typeof(string).AsRuntimeTypeInfo());

            var baseQuery = (IQueryable<ITest>)new List<Test>(new[]
                                                                  {
                                                                      new Test { Name = "gigi" },
                                                                      new Test { Name = "belogea" }
                                                                  }).AsQueryable();
            var gigi = "gigi";
            var query = baseQuery.Where(t => t.Name == gigi);
            var visitor = new SubstituteTypeExpressionVisitor(activator: activator);
            var newExpression = visitor.Visit(query.Expression);

            var methodCallExpression = (MethodCallExpression)newExpression;
            var genericArg = methodCallExpression.Method.GetGenericArguments()[0];
            Assert.AreEqual(typeof(Test), genericArg);

            var result = baseQuery.Provider.Execute<IEnumerable<Test>>(newExpression);
            Assert.AreEqual(1, result.Count());
        }

        [Test]
        public void Visit_join()
        {
            var activator = Substitute.For<IActivator>();
            activator.GetImplementationType(typeof(IBetterTest).AsRuntimeTypeInfo(), Arg.Any<IContext>(), Arg.Any<bool>())
                .Returns(typeof(BetterTest).AsRuntimeTypeInfo());

            var baseQuery = (IQueryable<IBetterTest>)new List<BetterTest>(new[]
                                                                  {
                                                                      new BetterTest { Name = "gigi", BetterName = "belogea" },
                                                                      new BetterTest { Name = "sisi", BetterName = "<NAME>" }
                                                                  }).AsQueryable();

            var joinQuery = (IQueryable<IBetterTest>)new List<BetterTest>(new[]
                                                                  {
                                                                      new BetterTest { Name = "gigi", BetterName = "Belogea" },
                                                                      new BetterTest { Name = "belogea", BetterName = "Gigi" }
                                                                  }).AsQueryable();

            var query = from t in baseQuery 
                        join p in joinQuery 
                            on t.Name equals p.Name 
                        select new { t.Name, FirstName = p.BetterName, SecondName = p.BetterName };
            var visitor = new SubstituteTypeExpressionVisitor(activator: activator);
            var newExpression = visitor.Visit(query.Expression);

            var methodCallExpression = (MethodCallExpression)newExpression;
            var genericArg = methodCallExpression.Method.GetGenericArguments()[0];
            Assert.AreEqual(typeof(BetterTest), genericArg);

            var stringExpression = newExpression.ToString();

            Assert.IsFalse(stringExpression.Contains("IBetterTest"));

            //var typedResult = query.ToList();
            //var untypedResult = query.Provider.Execute(newExpression);
            //Assert.IsAssignableFrom<IEnumerable>(untypedResult);
        }

        [Test]
        public void Visit_select()
        {
            var activator = Substitute.For<IActivator>();
            activator.GetImplementationType(typeof(IBetterTest).AsRuntimeTypeInfo(), Arg.Any<IContext>(), Arg.Any<bool>())
                .Returns(typeof(BetterTest).AsRuntimeTypeInfo());

            var baseQuery = (IQueryable<IBetterTest>)new List<BetterTest>(new[]
                                                                  {
                                                                      new BetterTest { Name = "gigi", BetterName = "belogea" },
                                                                      new BetterTest { Name = "sisi", BetterName = "<NAME>" }
                                                                  }).AsQueryable();

            var query = from t in baseQuery 
                        select new { FirstName = t.Name, FamilyName = t.BetterName };
            var visitor = new SubstituteTypeExpressionVisitor(activator: activator);
            var newExpression = visitor.Visit(query.Expression);

            var methodCallExpression = (MethodCallExpression)newExpression;
            var genericArg = methodCallExpression.Method.GetGenericArguments()[0];
            Assert.AreEqual(typeof(BetterTest), genericArg);

            var stringExpression = newExpression.ToString();

            Assert.IsFalse(stringExpression.Contains("IBetterTest"));

            //var typedResult = query.ToList();<commit_msg>Cosmetic changes in unit test<commit_after>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SubstituteTypeExpressionVisitorTest.cs" company="Quartz Software SRL">
//   Copyright (c) Quartz Software SRL. All rights reserved.
// </copyright>
// <summary>
//   Implements the substitute type expression visitor test class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Kephas.Data.Tests.Linq.Expressions
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;

    using Kephas.Activation;
    using Kephas.Data.Linq.Expressions;
    using Kephas.Reflection;
    using Kephas.Services;

    using NSubstitute;

    using NUnit.Framework;

    [TestFixture]
    public class SubstituteTypeExpressionVisitorTest
    {
        [Test]
        public void Visit_Where()
        {
            var activator = Substitute.For<IActivator>();
            activator.GetImplementationType(typeof(ITest).AsRuntimeTypeInfo(), Arg.Any<IContext>(), Arg.Any<bool>())
                .Returns(typeof(Test).AsRuntimeTypeInfo());

            var baseQuery = (IQueryable<ITest>)new List<Test>(new[]
                                                                  {
                                                                      new Test { Name = "gigi" },
                                                                      new Test { Name = "belogea" }
                                                                  }).AsQueryable();
            var query = baseQuery.Where(t => t.Name == "gigi");
            var visitor = new SubstituteTypeExpressionVisitor(activator: activator);
            var newExpression = visitor.Visit(query.Expression);

            var methodCallExpression = (MethodCallExpression)newExpression;
            var genericArg = methodCallExpression.Method.GetGenericArguments()[0];
            Assert.AreEqual(typeof(Test), genericArg);

            var result = baseQuery.Provider.Execute<IEnumerable<Test>>(newExpression);
            Assert.AreEqual(1, result.Count());
        }

        [Test]
        public void Visit_Where_captured_local_variable()
        {
            var activator = Substitute.For<IActivator>();
            activator.GetImplementationType(typeof(ITest).AsRuntimeTypeInfo(), Arg.Any<IContext>(), Arg.Any<bool>())
                .Returns(typeof(Test).AsRuntimeTypeInfo());
            activator.GetImplementationType(typeof(string).AsRuntimeTypeInfo(), Arg.Any<IContext>(), Arg.Any<bool>())
                .Returns(typeof(string).AsRuntimeTypeInfo());

            var baseQuery = (IQueryable<ITest>)new List<Test>(new[]
                                                                  {
                                                                      new Test { Name = "gigi" },
                                                                      new Test { Name = "belogea" }
                                                                  }).AsQueryable();
            var gigi = "gigi";
            var query = baseQuery.Where(t => t.Name == gigi);
            var visitor = new SubstituteTypeExpressionVisitor(activator: activator);
            var newExpression = visitor.Visit(query.Expression);

            var methodCallExpression = (MethodCallExpression)newExpression;
            var genericArg = methodCallExpression.Method.GetGenericArguments()[0];
            Assert.AreEqual(typeof(Test), genericArg);

            var result = baseQuery.Provider.Execute<IEnumerable<Test>>(newExpression);
            Assert.AreEqual(1, result.Count());
        }

        [Test]
        public void Visit_Join()
        {
            var activator = Substitute.For<IActivator>();
            activator.GetImplementationType(typeof(IBetterTest).AsRuntimeTypeInfo(), Arg.Any<IContext>(), Arg.Any<bool>())
                .Returns(typeof(BetterTest).AsRuntimeTypeInfo());

            var baseQuery = (IQueryable<IBetterTest>)new List<BetterTest>(new[]
                                                                  {
                                                                      new BetterTest { Name = "gigi", BetterName = "belogea" },
                                                                      new BetterTest { Name = "sisi", BetterName = "von habsburg" }
                                                                  }).AsQueryable();

            var joinQuery = (IQueryable<IBetterTest>)new List<BetterTest>(new[]
                                                                  {
                                                                      new BetterTest { Name = "gigi", BetterName = "Belogea" },
                                                                      new BetterTest { Name = "belogea", BetterName = "Gigi" }
                                                                  }).AsQueryable();

            var query = from t in baseQuery 
                        join p in joinQuery 
                            on t.Name equals p.Name 
                        select new { t.Name, FirstName = p.BetterName, SecondName = p.BetterName };
            var visitor = new SubstituteTypeExpressionVisitor(activator: activator);
            var newExpression = visitor.Visit(query.Expression);

            var methodCallExpression = (MethodCallExpression)newExpression;
            var genericArg = methodCallExpression.Method.GetGenericArguments()[0];
            Assert.AreEqual(typeof(BetterTest), genericArg);

            var stringExpression = newExpression.ToString();

            Assert.IsFalse(stringExpression.Contains("IBetterTest"));

            //var typedResult = query.ToList();
            //var untypedResult = query.Provider.Execute(newExpression);
            //Assert.IsAssignableFrom<IEnumerable>(untypedResult);
        }

        [Test]
        public void Visit_Select()
        {
            var activator = Substitute.For<IActivator>();
            activator.GetImplementationType(typeof(IBetterTest).AsRuntimeTypeInfo(), Arg.Any<IContext>(), Arg.Any<bool>())
                .Returns(typeof(BetterTest).AsRuntimeTypeInfo());

            var baseQuery = (IQueryable<IBetterTest>)new List<BetterTest>(new[]
                                                                  {
                                                                      new BetterTest { Name = "gigi", BetterName = "belogea" },
                                                                      new BetterTest { Name = "sisi", BetterName = "<NAME>" }
                                                                  }).AsQueryable();

            var query = from t in baseQuery 
                        select new { FirstName = t.Name, FamilyName = t.BetterName };
            var visitor = new SubstituteTypeExpressionVisitor(activator: activator);
            var newExpression = visitor.Visit(query.Expression);

            var methodCallExpression = (MethodCallExpression)newExpression;
            var genericArg = methodCallExpression.Method.GetGenericArguments()[0];
            Assert.AreEqual(typeof(BetterTest), genericArg);

            var stringExpression = newExpression.ToString();

            Assert.IsFalse(stringExpression.Contains("IBetterTest"));

            //var typedResult = query.ToList();
{'fraction_non_alphanumeric': 0.12403410604849453, 'fraction_numerical': 0.0007993605115907274, 'mean_word_length': 1.3777320240734874, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 100, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6206457', 'n_tokens_mistral': 3230, 'n_tokens_neox': 3104, 'n_words': 636}