Esempio n. 1
0
        public void Transform_NativeTranformationUnknown_Exception()
        {
            var resultSet = new NBi.Core.ResultSet.ResultSet();

            resultSet.Load("\t;10");

            var transformation = Mock.Of <ITransformationInfo>
                                 (
                t => t.Language == LanguageType.Native &&
                t.OriginalType == NBi.Core.ResultSet.ColumnType.Text &&
                t.Code == "unknown"
                                 );

            var provider = new TransformationProvider();

            Assert.Throws <NotImplementedTransformationException>(() => provider.Add(new ColumnOrdinalIdentifier(0), transformation));
        }
Esempio n. 2
0
        public void Transform_NativeTranformationBlankToNull_Correct()
        {
            var resultSet = new NBi.Core.ResultSet.ResultSet();

            resultSet.Load("\t;10");

            var transformation = Mock.Of <ITransformationInfo>
                                 (
                t => t.Language == LanguageType.Native &&
                t.OriginalType == NBi.Core.ResultSet.ColumnType.Text &&
                t.Code == "blank-to-null"
                                 );

            var provider = new TransformationProvider();

            provider.Add(new ColumnOrdinalIdentifier(0), transformation);
            provider.Transform(resultSet);

            Assert.That(resultSet.Rows[0][0], Is.EqualTo("(null)"));
        }
        public void Transform_NativeTranformationTrim_Correct()
        {
            var resultSet = new NBi.Core.ResultSet.ResultSet();

            resultSet.Load(" aaaa  ;10");

            var transformation = Mock.Of <ITransformationInfo>
                                 (
                t => t.Language == LanguageType.Native &&
                t.OriginalType == NBi.Core.ResultSet.ColumnType.Text &&
                t.Code == "text-to-trim"
                                 );

            var provider = new TransformationProvider();

            provider.Add(0, transformation);
            provider.Transform(resultSet);

            Assert.That(resultSet.Rows[0][0], Is.EqualTo("aaaa"));
        }
Esempio n. 4
0
        public void Transform_SimpleTranformation_Correct()
        {
            var resultSet = new NBi.Core.ResultSet.ResultSet();

            resultSet.Load("aaaa;10");

            var transformation = Mock.Of <ITransformationInfo>
                                 (
                t => t.Language == LanguageType.CSharp &&
                t.OriginalType == NBi.Core.ResultSet.ColumnType.Text &&
                t.Code == "value.Substring(0,1)"
                                 );

            var provider = new TransformationProvider();

            provider.Add(new ColumnOrdinalIdentifier(0), transformation);
            provider.Transform(resultSet);

            Assert.That(resultSet.Rows[0][0], Is.EqualTo("a"));
        }
Esempio n. 5
0
        public void Transform_TypeSwitch_Correct()
        {
            var resultSet = new NBi.Core.ResultSet.ResultSet();
            var obj       = new object[] { new DateTime(2016, 10, 1) };

            resultSet.Load(Enumerable.Repeat(obj, 1));

            var transformation = Mock.Of <ITransformationInfo>
                                 (
                t => t.Language == LanguageType.CSharp &&
                t.OriginalType == NBi.Core.ResultSet.ColumnType.DateTime &&
                t.Code == "value.Month + (value.Year-2000)*12"
                                 );

            var provider = new TransformationProvider();

            provider.Add(new ColumnOrdinalIdentifier(0), transformation);
            provider.Transform(resultSet);

            Assert.That(resultSet.Rows[0][0], Is.EqualTo(202));
        }
Esempio n. 6
0
        public void Transform_NativeTranformationFirstCharWithContext_Correct()
        {
            var resultSet = new NBi.Core.ResultSet.ResultSet();

            resultSet.Load(new[] { new object[] { "123456789", 6 }, new object[] { "abcdefgh", 2 } });

            var transformation = Mock.Of <ITransformationInfo>
                                 (
                t => t.Language == LanguageType.Native &&
                t.OriginalType == ColumnType.Text &&
                t.Code == "text-to-first-chars(#1)"
                                 );

            var provider = new TransformationProvider(new ServiceLocator(), Context.None);

            provider.Add(new ColumnOrdinalIdentifier(0), transformation);
            provider.Transform(resultSet);

            Assert.That(resultSet.Rows[0][0], Is.EqualTo("123456"));
            Assert.That(resultSet.Rows[1][0], Is.EqualTo("ab"));
        }
Esempio n. 7
0
        public void Transform_SimpleTranformation_CorrectHandlingOfColumnNames()
        {
            var resultSet = new NBi.Core.ResultSet.ResultSet();

            resultSet.Load("aaaa;10");
            resultSet.Columns[0].ColumnName = "MyCol0";
            resultSet.Columns[1].ColumnName = "MyCol1";

            var transformation = Mock.Of <ITransformationInfo>
                                 (
                t => t.Language == LanguageType.CSharp &&
                t.OriginalType == ColumnType.Text &&
                t.Code == "value.Substring(0,1)"
                                 );

            var provider = new TransformationProvider(new ServiceLocator(), Context.None);

            provider.Add(new ColumnOrdinalIdentifier(0), transformation);
            provider.Transform(resultSet);

            Assert.That(resultSet.Columns[0].ColumnName, Is.EqualTo("MyCol0"));
            Assert.That(resultSet.Columns[1].ColumnName, Is.EqualTo("MyCol1"));
            Assert.That(resultSet.Columns.Count, Is.EqualTo(2));
        }
Esempio n. 8
0
        protected NBiConstraint InstantiateConstraint()
        {
            EqualToConstraint ctr = null;

            if (ConstraintXml.GetCommand() != null)
            {
                var commandText      = ConstraintXml.GetCommand().CommandText;
                var connectionString = ConstraintXml.GetCommand().Connection.ConnectionString;
                var timeout          = ((QueryXml)(ConstraintXml.BaseItem)).Timeout;
                IEnumerable <IQueryParameter>        parameters = null;
                IEnumerable <IQueryTemplateVariable> variables  = null;
                if (ConstraintXml.Query != null)
                {
                    parameters = ConstraintXml.Query.GetParameters();
                    variables  = ConstraintXml.Query.GetVariables();
                }

                var commandBuilder = new CommandBuilder();
                var cmd            = commandBuilder.Build(connectionString, commandText, parameters, variables, timeout);
                ctr = new EqualToConstraint(cmd);
            }
            else if (ConstraintXml.ResultSet != null)
            {
                if (!string.IsNullOrEmpty(ConstraintXml.ResultSet.File))
                {
                    Trace.WriteLineIf(NBiTraceSwitch.TraceVerbose, "ResultSet defined in external file!");
                    ctr = new EqualToConstraint(ConstraintXml.ResultSet.GetFile());
                    if (ConstraintXml.Settings.CsvProfile != null)
                    {
                        ctr = ctr.CsvProfile(ConstraintXml.Settings.CsvProfile);
                    }
                }
                else if (ConstraintXml.ResultSet.Rows != null)
                {
                    Trace.WriteLineIf(NBiTraceSwitch.TraceVerbose, "ResultSet defined in embedded resultSet!");
                    ctr = new EqualToConstraint(ConstraintXml.ResultSet.Rows);
                }
            }
            else if (ConstraintXml.XmlSource != null)
            {
                var selects = new List <AbstractSelect>();
                var factory = new SelectFactory();
                foreach (var select in ConstraintXml.XmlSource.XPath.Selects)
                {
                    selects.Add(factory.Instantiate(select.Value, select.Attribute, select.Evaluate));
                }

                XPathEngine engine = null;
                if (ConstraintXml.XmlSource.File != null)
                {
                    Trace.WriteLineIf(NBiTraceSwitch.TraceVerbose, string.Format("Xml file at '{0}'", ConstraintXml.XmlSource.GetFile()));
                    engine = new XPathFileEngine(ConstraintXml.XmlSource.GetFile(), ConstraintXml.XmlSource.XPath.From.Value, selects);
                }
                else if (ConstraintXml.XmlSource.Url != null)
                {
                    Trace.WriteLineIf(NBiTraceSwitch.TraceVerbose, string.Format("Xml file at '{0}'", ConstraintXml.XmlSource.Url.Value));
                    engine = new XPathUrlEngine(ConstraintXml.XmlSource.Url.Value, ConstraintXml.XmlSource.XPath.From.Value, selects);
                }
                else
                {
                    throw new ArgumentException("File or Url can't be both empty when declaring an xml-source.");
                }

                ctr = new EqualToConstraint(engine);
            }

            if (ctr == null)
            {
                throw new ArgumentException();
            }

            //Manage settings for comparaison
            ResultSetComparisonSettings settings = null;

            if (ConstraintXml.Behavior == EqualToXml.ComparisonBehavior.SingleRow)
            {
                ctr      = ctr.Using(new SingleRowComparer());
                settings = new SingleRowComparisonSettings(
                    ConstraintXml.ValuesDefaultType,
                    new NumericToleranceFactory().Instantiate(ConstraintXml.Tolerance),
                    ConstraintXml.ColumnsDef
                    );
            }
            else
            {
                settings = new ResultSetComparisonSettings(
                    ConstraintXml.KeysDef,
                    ConstraintXml.ValuesDef,
                    ConstraintXml.ValuesDefaultType,
                    new NumericToleranceFactory().Instantiate(ConstraintXml.Tolerance),
                    ConstraintXml.ColumnsDef
                    );
            }

            ctr.Using(settings);

            //Manage transformations
            var transformationProvider = new TransformationProvider();

            foreach (var columnDef in ConstraintXml.ColumnsDef)
            {
                if (columnDef.Transformation != null)
                {
                    transformationProvider.Add(columnDef.Index, columnDef.Transformation);
                }
            }

            ctr = ctr.Using(transformationProvider);

            //Manage parallelism
            if (ConstraintXml.ParallelizeQueries)
            {
                ctr = ctr.Parallel();
            }
            else
            {
                ctr = ctr.Sequential();
            }

            //Manage persistance
            //EqualToConstraint.PersistanceItems persi = 0;
            //if (ConstraintXml.GetCommand() != null)
            //    persi += (int)EqualToConstraint.PersistanceItems.actual;
            //if (SystemUnderTestXml is QueryXml)
            //    persi += (int)EqualToConstraint.PersistanceItems.expected;
            //if (!(persi==0 || ConstraintXml.Query==null || string.IsNullOrEmpty(ConstraintXml.Test.Name)))
            //    ctr.Persist(ConstraintXml.Persistance, persi, ConstraintXml.Test.Name);

            return(ctr);
        }
Esempio n. 9
0
        protected NBiConstraint InstantiateConstraint()
        {
            BaseResultSetComparisonConstraint ctr = null;

            //Manage transformations
            var transformationProvider = new TransformationProvider();

            foreach (var columnDef in ConstraintXml.ColumnsDef)
            {
                if (columnDef.Transformation != null)
                {
                    transformationProvider.Add(columnDef.Index, columnDef.Transformation);
                }
            }

            if (ConstraintXml.GetCommand() != null)
            {
                ctr = InstantiateConstraint(((QueryXml)(ConstraintXml.BaseItem)), ConstraintXml.Settings, transformationProvider);
            }
            else if (ConstraintXml.ResultSet != null)
            {
                ctr = InstantiateConstraint(ConstraintXml.ResultSet, ConstraintXml.Settings, transformationProvider);
            }
            else if (ConstraintXml.XmlSource != null)
            {
                ctr = InstantiateConstraint(ConstraintXml.XmlSource, ConstraintXml.Settings, transformationProvider);
            }

            if (ctr == null)
            {
                throw new ArgumentException();
            }

            //Manage settings for comparaison
            var builder = new SettingsEquivalerBuilder();

            if (ConstraintXml.Behavior == EqualToXml.ComparisonBehavior.SingleRow)
            {
                builder.Setup(false);
                builder.Setup(ConstraintXml.ValuesDefaultType, ConstraintXml.Tolerance);
                builder.Setup(ConstraintXml.ColumnsDef);
            }
            else
            {
                builder.Setup(ConstraintXml.KeysDef, ConstraintXml.ValuesDef);
                builder.Setup(
                    ConstraintXml.KeyName?.Replace(" ", "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Distinct(),
                    ConstraintXml.ValueName?.Replace(" ", "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Distinct());
                builder.Setup(ConstraintXml.ValuesDefaultType, ConstraintXml.Tolerance);
                builder.Setup(ConstraintXml.ColumnsDef);
            }

            builder.Build();
            var settings = builder.GetSettings();

            var factory  = new EquivalerFactory();
            var comparer = factory.Instantiate(settings, EquivalenceKind);

            ctr = ctr.Using(comparer);
            ctr = ctr.Using(settings);

            //Manage parallelism
            if (ConstraintXml.ParallelizeQueries)
            {
                ctr = ctr.Parallel();
            }
            else
            {
                ctr = ctr.Sequential();
            }

            return(ctr);
        }
Esempio n. 10
0
        public IEnumerable <Alter> InstantiateAlterations(ResultSetSystemXml resultSetXml)
        {
            if (resultSetXml.Alteration == null)
            {
                yield break;
            }

            if (resultSetXml.Alteration.Filters != null)
            {
                foreach (var filterXml in resultSetXml.Alteration.Filters)
                {
                    var expressions = new List <IColumnExpression>();
                    if (filterXml.Expression != null)
                    {
                        expressions.Add(filterXml.Expression);
                    }

                    var factory = new PredicateFilterFactory();
                    if (filterXml.Predication != null)
                    {
                        yield return(factory.Instantiate
                                     (
                                         filterXml.Aliases
                                         , expressions
                                         , filterXml.Predication
                                     ).Apply);
                    }
                    if (filterXml.Combination != null)
                    {
                        yield return(factory.Instantiate
                                     (
                                         filterXml.Aliases
                                         , expressions
                                         , filterXml.Combination.Operator
                                         , filterXml.Combination.Predicates
                                     ).Apply);
                    }
                }
            }

            if (resultSetXml.Alteration.Conversions != null)
            {
                foreach (var conversionXml in resultSetXml.Alteration.Conversions)
                {
                    var factory   = new ConverterFactory();
                    var converter = factory.Instantiate(conversionXml.Converter.From, conversionXml.Converter.To, conversionXml.Converter.DefaultValue, conversionXml.Converter.Culture);
                    var engine    = new ConverterEngine(conversionXml.Column, converter);
                    yield return(engine.Execute);
                }
            }

            if (resultSetXml.Alteration.Transformations != null)
            {
                var provider = new TransformationProvider();
                foreach (var transformationXml in resultSetXml.Alteration.Transformations)
                {
                    provider.Add(transformationXml.ColumnIndex, transformationXml);
                }
                yield return(provider.Transform);
            }
        }