Best NBi code snippet using NBi.Core.Transformation.TransformationProvider.Transform
TransformationProviderTest.cs
Source:TransformationProviderTest.cs
1using Moq;2using NBi.Core.Injection;3using NBi.Core.ResultSet;4using NBi.Core.Transformation;5using NBi.Core.Transformation.Transformer.Native;6using NBi.Core.Variable;7using NUnit.Framework;8using System;9using System.Collections.Generic;10using System.Linq;11using System.Text;12using System.Threading.Tasks;13namespace NBi.Testing.Core.Transformation14{15 [TestFixture]16 public class TransformationProviderTest17 {18 [Test]19 public void Transform_SimpleTranformation_CorrectHandlingOfColumnNames()20 {21 var resultSet = new NBi.Core.ResultSet.ResultSet();22 resultSet.Load("aaaa;10");23 resultSet.Columns[0].ColumnName = "MyCol0";24 resultSet.Columns[1].ColumnName = "MyCol1";25 var transformation = Mock.Of<ITransformationInfo>26 (27 t => t.Language == LanguageType.CSharp28 && t.OriginalType == ColumnType.Text29 && t.Code == "value.Substring(0,1)"30 );31 var provider = new TransformationProvider(new ServiceLocator(), Context.None);32 provider.Add(new ColumnOrdinalIdentifier(0), transformation);33 provider.Transform(resultSet);34 Assert.That(resultSet.Columns[0].ColumnName, Is.EqualTo("MyCol0"));35 Assert.That(resultSet.Columns[1].ColumnName, Is.EqualTo("MyCol1"));36 Assert.That(resultSet.Columns.Count, Is.EqualTo(2));37 }38 [Test]39 public void Transform_SimpleTranformation_Correct()40 {41 var resultSet = new NBi.Core.ResultSet.ResultSet();42 resultSet.Load("aaaa;10");43 var transformation = Mock.Of<ITransformationInfo>44 (45 t => t.Language == LanguageType.CSharp46 && t.OriginalType == ColumnType.Text47 && t.Code == "value.Substring(0,1)"48 );49 var provider = new TransformationProvider(new ServiceLocator(), Context.None);50 provider.Add(new ColumnOrdinalIdentifier(0), transformation);51 provider.Transform(resultSet);52 Assert.That(resultSet.Rows[0][0], Is.EqualTo("a"));53 }54 [Test]55 public void Transform_NativeTranformationTrim_Correct()56 {57 var resultSet = new NBi.Core.ResultSet.ResultSet();58 resultSet.Load(" aaaa ;10");59 var transformation = Mock.Of<ITransformationInfo>60 (61 t => t.Language == LanguageType.Native62 && t.OriginalType == ColumnType.Text63 && t.Code == "text-to-trim"64 );65 var provider = new TransformationProvider(new ServiceLocator(), Context.None);66 provider.Add(new ColumnOrdinalIdentifier(0), transformation);67 provider.Transform(resultSet);68 Assert.That(resultSet.Rows[0][0], Is.EqualTo("aaaa"));69 }70 [Test]71 public void Transform_NativeTranformationFirstCharWithContext_Correct()72 {73 var resultSet = new NBi.Core.ResultSet.ResultSet();74 resultSet.Load(new[] { new object[] { "123456789", 6 }, new object[] { "abcdefgh", 2 } });75 var transformation = Mock.Of<ITransformationInfo>76 (77 t => t.Language == LanguageType.Native78 && t.OriginalType == ColumnType.Text79 && t.Code == "text-to-first-chars(#1)"80 );81 var provider = new TransformationProvider(new ServiceLocator(), Context.None);82 provider.Add(new ColumnOrdinalIdentifier(0), transformation);83 provider.Transform(resultSet);84 Assert.That(resultSet.Rows[0][0], Is.EqualTo("123456"));85 Assert.That(resultSet.Rows[1][0], Is.EqualTo("ab"));86 }87 [Test]88 public void Transform_NativeTranformationBlankToNull_Correct()89 {90 var resultSet = new NBi.Core.ResultSet.ResultSet();91 resultSet.Load("\t;10");92 var transformation = Mock.Of<ITransformationInfo>93 (94 t => t.Language == LanguageType.Native95 && t.OriginalType == ColumnType.Text96 && t.Code == "blank-to-null"97 );98 var provider = new TransformationProvider(new ServiceLocator(), Context.None);99 provider.Add(new ColumnOrdinalIdentifier(0), transformation);100 provider.Transform(resultSet);101 Assert.That(resultSet.Rows[0][0], Is.EqualTo("(null)"));102 }103 [Test]104 public void Transform_NativeTranformationUnknown_Exception()105 {106 var resultSet = new NBi.Core.ResultSet.ResultSet();107 resultSet.Load("\t;10");108 var transformation = Mock.Of<ITransformationInfo>109 (110 t => t.Language == LanguageType.Native111 && t.OriginalType == ColumnType.Text112 && t.Code == "unknown"113 );114 var provider = new TransformationProvider(new ServiceLocator(), null);115 Assert.Throws<NotImplementedTransformationException>(() => provider.Add(new ColumnOrdinalIdentifier(0), transformation));116 }117 [Test]118 public void Transform_TypeSwitch_Correct()119 {120 var resultSet = new NBi.Core.ResultSet.ResultSet();121 var obj = new object[] { new DateTime(2016,10,1) };122 resultSet.Load(Enumerable.Repeat(obj,1));123 var transformation = Mock.Of<ITransformationInfo>124 (125 t => t.Language == LanguageType.CSharp126 && t.OriginalType == ColumnType.DateTime127 && t.Code == "value.Month + (value.Year-2000)*12"128 );129 var provider = new TransformationProvider(new ServiceLocator(), Context.None);130 provider.Add(new ColumnOrdinalIdentifier(0), transformation);131 provider.Transform(resultSet);132 Assert.That(resultSet.Rows[0][0], Is.EqualTo(202));133 }134 }135}...
ResultSetEqualToBuilder.cs
Source:ResultSetEqualToBuilder.cs
...5using NBi.NUnit.ResultSetComparison;6using NBi.Xml.Constraints;7using NBi.Xml.Items;8using NBi.Xml.Systems;9using NBi.Core.Transformation;10using NBi.Core.ResultSet.Equivalence;11using NBi.NUnit.Builder.Helper;12using NBi.Xml.Settings;13using NBi.Core.Variable;14namespace NBi.NUnit.Builder15{16 class ResultSetEqualToBuilder : AbstractResultSetBuilder17 {18 protected EqualToXml ConstraintXml { get; set; }19 protected virtual EquivalenceKind EquivalenceKind20 { get => EquivalenceKind.EqualTo; }21 public ResultSetEqualToBuilder()22 { }23 protected override void SpecificSetup(AbstractSystemUnderTestXml sutXml, AbstractConstraintXml ctrXml)24 {25 if (!(ctrXml is EqualToXml))26 throw new ArgumentException("Constraint must be a 'EqualToXml'");27 ConstraintXml = (EqualToXml)ctrXml;28 }29 protected override void SpecificBuild()30 {31 Constraint = InstantiateConstraint();32 }33 protected NBiConstraint InstantiateConstraint()34 {35 BaseResultSetComparisonConstraint ctr = null;36 //Manage transformations37 var transformationProvider = new TransformationProvider(ServiceLocator, new Context(Variables));38 foreach (var columnDef in ConstraintXml.ColumnsDef)39 {40 if (columnDef.Transformation != null)41 transformationProvider.Add(columnDef.Identifier, columnDef.Transformation);42 }43 if (ConstraintXml.BaseItem is QueryXml)44 ctr = InstantiateConstraint(((QueryXml)(ConstraintXml.BaseItem)), ConstraintXml.Settings, transformationProvider);45 else if (ConstraintXml.ResultSetOld != null)46 ctr = InstantiateConstraint(ConstraintXml.ResultSetOld, ConstraintXml.Settings, transformationProvider);47 else if (ConstraintXml.ResultSet != null)48 ctr = InstantiateConstraint(ConstraintXml.ResultSet, ConstraintXml.Settings);49 else if (ConstraintXml.XmlSource != null)50 ctr = InstantiateConstraint(ConstraintXml.XmlSource, ConstraintXml.Settings, transformationProvider);51 if (ctr == null)52 throw new ArgumentException();53 //Manage settings for comparaison54 var builder = new SettingsEquivalerBuilder();55 if (ConstraintXml.Behavior == EqualToXml.ComparisonBehavior.SingleRow)56 {57 builder.Setup(false);58 builder.Setup(ConstraintXml.ValuesDefaultType, ConstraintXml.Tolerance);59 builder.Setup(ConstraintXml.ColumnsDef);60 }61 else62 {63 builder.Setup(ConstraintXml.KeysDef, ConstraintXml.ValuesDef);64 builder.Setup(65 ConstraintXml.KeyName?.Replace(" ", "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Distinct(),66 ConstraintXml.ValueName?.Replace(" ", "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Distinct());67 builder.Setup(ConstraintXml.ValuesDefaultType, ConstraintXml.Tolerance);68 builder.Setup(ConstraintXml.ColumnsDef);69 }70 builder.Build();71 var settings = builder.GetSettings();72 var factory = new EquivalerFactory();73 var comparer = factory.Instantiate(settings, EquivalenceKind);74 ctr = ctr.Using(comparer);75 ctr = ctr.Using(settings);76 //Manage parallelism77 if (ConstraintXml.ParallelizeQueries)78 ctr = ctr.Parallel();79 else80 ctr = ctr.Sequential();81 return ctr;82 }83 protected virtual BaseResultSetComparisonConstraint InstantiateConstraint(ResultSetSystemXml xml, SettingsXml settings)84 {85 xml.Settings = settings;86 var builder = new ResultSetServiceBuilder();87 var helper = new ResultSetSystemHelper(ServiceLocator, SettingsXml.DefaultScope.Assert, Variables);88 builder.Setup(helper.InstantiateResolver(xml));89 builder.Setup(helper.InstantiateAlterations(xml));90 var service = builder.GetService();91 return InstantiateConstraint(service);92 }93 protected virtual BaseResultSetComparisonConstraint InstantiateConstraint(object obj, SettingsXml settings, TransformationProvider transformation)94 {95 var argsBuilder = new ResultSetResolverArgsBuilder(ServiceLocator);96 argsBuilder.Setup(obj, settings, SettingsXml.DefaultScope.Assert, Variables);97 argsBuilder.Build();98 var factory = ServiceLocator.GetResultSetResolverFactory();99 var resolver = factory.Instantiate(argsBuilder.GetArgs());100 var serviceBuilder = new ResultSetServiceBuilder();101 serviceBuilder.Setup(resolver);102 if (transformation != null)103 serviceBuilder.Setup(transformation.Transform);104 var service = serviceBuilder.GetService();105 return InstantiateConstraint(service);106 }107 protected virtual BaseResultSetComparisonConstraint InstantiateConstraint(IResultSetService service)108 => new EqualToConstraint(service);109 }110}...
TransformationProvider.cs
Source:TransformationProvider.cs
...8using System.Diagnostics;9using System.Linq;10using System.Text;11using System.Threading.Tasks;12namespace NBi.Core.Transformation13{14 public class TransformationProvider15 {16 private IDictionary<IColumnIdentifier, ITransformer> cacheTransformers;17 private readonly TransformerFactory factory;18 private Context Context { get; }19 public TransformationProvider(ServiceLocator serviceLocator, Context context)20 {21 cacheTransformers = new Dictionary<IColumnIdentifier, ITransformer>();22 factory = new TransformerFactory(serviceLocator, context);23 Context = context;24 }25 public void Add(IColumnIdentifier indentifier, ITransformationInfo transfo)26 {27 var transformer = factory.Instantiate(transfo);28 transformer.Initialize(transfo.Code);29 if (cacheTransformers.ContainsKey(indentifier))30 throw new NBiException($"You can't define two transformers for the same column. The column {indentifier.Label} has already another transformer specified.");31 cacheTransformers.Add(indentifier, transformer);32 }33 public virtual IResultSet Transform(IResultSet resultSet)34 {35 foreach (var identifier in cacheTransformers.Keys)36 {37 var tsStart = DateTime.Now;38 var transformer = cacheTransformers[identifier];39 var newColumn = new DataColumn() { DataType = typeof(object) };40 resultSet.Table.Columns.Add(newColumn);41 var ordinal = (identifier as ColumnOrdinalIdentifier)?.Ordinal ?? resultSet.Table.Columns[(identifier as ColumnNameIdentifier).Name].Ordinal;42 var originalName = resultSet.Table.Columns[ordinal].ColumnName;43 foreach (DataRow row in resultSet.Table.Rows)44 {45 Context.Switch(row);46 row[newColumn.Ordinal] = transformer.Execute(row[ordinal]);47 }48 resultSet.Table.Columns.RemoveAt(ordinal);49 newColumn.SetOrdinal(ordinal);50 newColumn.ColumnName = originalName;51 Trace.WriteLineIf(NBiTraceSwitch.TraceInfo, string.Format("Time needed to transform column {0}: {1}", identifier.Label, DateTime.Now.Subtract(tsStart).ToString(@"d\d\.hh\h\:mm\m\:ss\s\ \+fff\m\s")));52 }...
Transform
Using AI Code Generation
1var provider = new NBi.Core.Transformation.TransformationProvider();2var result = provider.Transform("1", "1", "1", "1", "1", "1", "1", "1", "1");3Console.WriteLine(result);4var provider = new NBi.Core.Transformation.TransformationProvider();5var result = provider.Transform("1", "1", "1", "1", "1", "1", "1", "1", "1");6Console.WriteLine(result);7var provider = new NBi.Core.Transformation.TransformationProvider();8var result = provider.Transform("1", "1", "1", "1", "1", "1", "1", "1", "1");9Console.WriteLine(result);10var provider = new NBi.Core.Transformation.TransformationProvider();11var result = provider.Transform("1", "1", "1", "1", "1", "1", "1", "1", "1");12Console.WriteLine(result);13var provider = new NBi.Core.Transformation.TransformationProvider();14var result = provider.Transform("1", "1", "1", "1", "1", "1", "1", "1", "1");15Console.WriteLine(result);16var provider = new NBi.Core.Transformation.TransformationProvider();17var result = provider.Transform("1", "1", "1", "1", "1", "1", "1", "1", "1");18Console.WriteLine(result);19var provider = new NBi.Core.Transformation.TransformationProvider();20var result = provider.Transform("1", "1", "1", "1", "1", "1", "1", "1", "1");21Console.WriteLine(result);
Transform
Using AI Code Generation
1var transformationProvider = new TransformationProvider();2var result = transformationProvider.Transform("1", new TransformationInfo("Trim"));3Console.WriteLine(result);4var transformationProvider = new TransformationProvider();5var result = transformationProvider.Transform("1", new TransformationInfo("Trim"));6Console.WriteLine(result);7var transformationProvider = new TransformationProvider();8var result = transformationProvider.Transform("1", new TransformationInfo("Trim"));9Console.WriteLine(result);10var transformationProvider = new TransformationProvider();11var result = transformationProvider.Transform("1", new TransformationInfo("Trim"));12Console.WriteLine(result);13var transformationProvider = new TransformationProvider();14var result = transformationProvider.Transform("1", new TransformationInfo("Trim"));15Console.WriteLine(result);16var transformationProvider = new TransformationProvider();17var result = transformationProvider.Transform("1", new TransformationInfo("Trim"));18Console.WriteLine(result);19var transformationProvider = new TransformationProvider();20var result = transformationProvider.Transform("1", new TransformationInfo("Trim"));21Console.WriteLine(result);22var transformationProvider = new TransformationProvider();23var result = transformationProvider.Transform("1", new TransformationInfo("Trim"));24Console.WriteLine(result);25var transformationProvider = new TransformationProvider();26var result = transformationProvider.Transform("1", new TransformationInfo("Trim"));27Console.WriteLine(result);28var transformationProvider = new TransformationProvider();29var result = transformationProvider.Transform("1", new TransformationInfo("Trim"));30Console.WriteLine(result);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!