<commit_before>﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using Microsoft.AnalysisServices.AdomdClient;
using NBi.Core.Query;
using NUnitCtr = NUnit.Framework.Constraints;

namespace NBi.NUnit
{
    public class FasterThanConstraint : NUnitCtr.Constraint
    {
        protected IQueryPerformance _engine;
        /// <summary>
        /// Engine dedicated to ResultSet comparaison
        /// </summary>
        protected internal IQueryPerformance Engine
        {
            set
            {
                if (value == null)
                    throw new ArgumentNullException();
                _engine = value;
            }
        }
        
        /// <summary>
        /// Store for the result of the engine's execution
        /// </summary>
        protected PerformanceResult _res;

        protected int _maxTimeMilliSeconds;
        protected bool _cleanCache;

        public FasterThanConstraint()
        {

        }

        public FasterThanConstraint MaxTimeMilliSeconds(int value)
        {
            this._maxTimeMilliSeconds = value;
            return this;
        }

        public FasterThanConstraint CleanCache()
        {
            this._cleanCache = true;
            return this;
        }

        protected IQueryPerformance GetEngine(IDbCommand actual)
        {
            if (_engine == null)
                _engine = new QueryEngineFactory().GetPerformance(actual);
            return _engine;
        }

        /// <summary>
        /// Handle a sql string or a sqlCommand and check it with the engine
        /// </summary>
        /// <param name="actual">SQL string or SQL Command</param>
        /// <returns>true, if the query defined in parameter is executed in less that expected else false</returns>
        public override bool Matches(object actual)
        {
            if (actual.GetType() is IDbCommand)
                return doMatch((IDbCommand)actual);
            else
                return false;
        }

        /// <summary>
        /// Handle a sql string and check it with the engine
        /// </summary>
        /// <param name="actual">SQL string</param>
        /// <returns>true, if the query defined in parameter is executed in less that expected else false</returns>
        public bool doMatch(IDbCommand actual)
        {
            var engine = GetEngine(actual);
            if (_cleanCache)
                engine.CleanCache();
            _res = engine.CheckPerformance();
            return _res.TimeElapsed.TotalMilliseconds < _maxTimeMilliSeconds;
        }

        /// <summary>
        /// 
        /// </summary><commit_msg>Fixed issue with GetType resulting in 3 Unit tests failing<commit_after>﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using Microsoft.AnalysisServices.AdomdClient;
using NBi.Core.Query;
using NUnitCtr = NUnit.Framework.Constraints;

namespace NBi.NUnit
{
    public class FasterThanConstraint : NUnitCtr.Constraint
    {
        protected IQueryPerformance _engine;
        /// <summary>
        /// Engine dedicated to ResultSet comparaison
        /// </summary>
        protected internal IQueryPerformance Engine
        {
            set
            {
                if (value == null)
                    throw new ArgumentNullException();
                _engine = value;
            }
        }
        
        /// <summary>
        /// Store for the result of the engine's execution
        /// </summary>
        protected PerformanceResult _res;

        protected int _maxTimeMilliSeconds;
        protected bool _cleanCache;

        public FasterThanConstraint()
        {

        }

        public FasterThanConstraint MaxTimeMilliSeconds(int value)
        {
            this._maxTimeMilliSeconds = value;
            return this;
        }

        public FasterThanConstraint CleanCache()
        {
            this._cleanCache = true;
            return this;
        }

        protected IQueryPerformance GetEngine(IDbCommand actual)
        {
            if (_engine == null)
                _engine = new QueryEngineFactory().GetPerformance(actual);
            return _engine;
        }

        /// <summary>
        /// Handle a sql string or a sqlCommand and check it with the engine
        /// </summary>
        /// <param name="actual">SQL string or SQL Command</param>
        /// <returns>true, if the query defined in parameter is executed in less that expected else false</returns>
        public override bool Matches(object actual)
        {
            if (actual is IDbCommand)
                return doMatch((IDbCommand)actual);
            else
                return false;
        }

        /// <summary>
        /// Handle a sql string and check it with the engine
        /// </summary>
        /// <param name="actual">SQL string</param>
        /// <returns>true, if the query defined in parameter is executed in less that expected else false</returns>
        public bool doMatch(IDbCommand actual)
        {
            var engine = GetEngine(actual);
            if (_cleanCache)
                engine.CleanCache();
            _res = engine.CheckPerformance();
            return _res.TimeElapsed.TotalMilliseconds < _maxTimeMilliSeconds;
        }

        /// <summary>
        /// 
        /// </summary>
{'fraction_non_alphanumeric': 0.0850232558139535, 'fraction_numerical': 0.00018604651162790697, 'mean_word_length': 1.6786248131539612, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 39, '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': '7369913', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1279, 'n_words': 386}