Best NBi code snippet using NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine.Execute
LookupReplaceEngineTest.cs
Source:LookupReplaceEngineTest.cs
...16{17 public class LookupReplaceEngineTest18 {19 [Test]20 public void Execute_AllLookupFound_CorrectReplacement()21 {22 var candidate = new ObjectsResultSetResolver(23 new ObjectsResultSetResolverArgs(24 new[] {25 new object[] { 1, "A", 100 },26 new object[] { 2, "B", 101 },27 new object[] { 3, "A", 125 },28 new object[] { 4, "B", 155 }29 }30 )).Execute();31 var reference = new ResultSetService(32 new ObjectsResultSetResolver(33 new ObjectsResultSetResolverArgs(34 new[] {35 new object[] { "A", "alpha" },36 new object[] { "B", "beta" },37 }38 )).Execute, null);39 var engine = new LookupReplaceEngine(40 new LookupReplaceArgs( 41 reference, 42 new ColumnMapping(new ColumnOrdinalIdentifier(1), new ColumnOrdinalIdentifier(0), ColumnType.Text),43 new ColumnOrdinalIdentifier(1)44 ));45 var result = engine.Execute(candidate);46 Assert.That(result.Columns.Count, Is.EqualTo(3));47 Assert.That(result.Rows.Count, Is.EqualTo(4));48 Assert.That(result.Rows.Cast<DataRow>().Select(x => x[1]).Distinct(), Does.Contain("alpha"));49 Assert.That(result.Rows.Cast<DataRow>().Select(x => x[1]).Distinct(), Does.Contain("beta"));50 Assert.That(result.Rows.Cast<DataRow>().Select(x => x[1] as string).Where(x => x != "alpha" && x != "beta"), Is.Empty); 51 }52 [Test]53 public void Execute_AllLookupFoundSwitchingFromTextToNumeric_CorrectReplacement()54 {55 var candidate = new ObjectsResultSetResolver(56 new ObjectsResultSetResolverArgs(57 new[] {58 new object[] { 1, "A", 100 },59 new object[] { 2, "B", 101 },60 new object[] { 3, "A", 125 },61 new object[] { 4, "B", 155 }62 }63 )).Execute();64 var reference = new ResultSetService(65 new ObjectsResultSetResolver(66 new ObjectsResultSetResolverArgs(67 new[] {68 new object[] { "A", 10.2 },69 new object[] { "B", 21.1 },70 }71 )).Execute, null);72 var engine = new LookupReplaceEngine(73 new LookupReplaceArgs(74 reference,75 new ColumnMapping(new ColumnOrdinalIdentifier(1), new ColumnOrdinalIdentifier(0), ColumnType.Text),76 new ColumnOrdinalIdentifier(1)77 ));78 var result = engine.Execute(candidate);79 Assert.That(result.Columns.Count, Is.EqualTo(3));80 Assert.That(result.Rows.Count, Is.EqualTo(4));81 Assert.That(result.Rows.Cast<DataRow>().Select(x => x[1]).Distinct(), Does.Contain(10.2));82 Assert.That(result.Rows.Cast<DataRow>().Select(x => x[1]).Distinct(), Does.Contain(21.1));83 Assert.That(result.Rows.Cast<DataRow>().Select(x => Convert.ToDecimal(x[1])).Where(x => x != 10.2m && x != 21.1m), Is.Empty);84 }85 [Test]86 public void ExecuteWithFailureStretegy_OneLookupMissing_ExceptionThrown()87 {88 var candidate = new ObjectsResultSetResolver(89 new ObjectsResultSetResolverArgs(90 new[] {91 new object[] { 1, "A", 100 },92 new object[] { 2, "B", 101 },93 new object[] { 3, "A", 125 },94 new object[] { 4, "C", 155 }95 }96 )).Execute();97 var reference = new ResultSetService(98 new ObjectsResultSetResolver(99 new ObjectsResultSetResolverArgs(100 new[] {101 new object[] { "A", "alpha" },102 new object[] { "B", "beta" },103 }104 )).Execute, null);105 var engine = new LookupReplaceEngine(106 new LookupReplaceArgs(107 reference,108 new ColumnMapping(new ColumnOrdinalIdentifier(1), new ColumnOrdinalIdentifier(0), ColumnType.Text),109 new ColumnOrdinalIdentifier(1),110 new FailureMissingStrategy()111 ));112 var ex = Assert.Throws<NBiException>(() => engine.Execute(candidate));113 Assert.That(ex.Message, Does.Contain("'C'"));114 }115 [Test]116 public void ExecuteWithDefaultValueStrategy_OneLookupMissing_DefaultValueApplied()117 {118 var candidate = new ObjectsResultSetResolver(119 new ObjectsResultSetResolverArgs(120 new[] {121 new object[] { 1, "A", 100 },122 new object[] { 2, "B", 101 },123 new object[] { 3, "A", 125 },124 new object[] { 4, "C", 155 }125 }126 )).Execute();127 var reference = new ResultSetService(128 new ObjectsResultSetResolver(129 new ObjectsResultSetResolverArgs(130 new[] {131 new object[] { "A", "alpha" },132 new object[] { "B", "beta" },133 }134 )).Execute, null);135 var engine = new LookupReplaceEngine(136 new LookupReplaceArgs(137 reference,138 new ColumnMapping(new ColumnOrdinalIdentifier(1), new ColumnOrdinalIdentifier(0), ColumnType.Text),139 new ColumnOrdinalIdentifier(1),140 new DefaultValueMissingStrategy("omega")141 ));142 var result = engine.Execute(candidate);143 Assert.That(result.Rows.Count, Is.EqualTo(4));144 Assert.That(result.Rows.Cast<DataRow>().Select(x => x[1]).Distinct(), Does.Contain("alpha"));145 Assert.That(result.Rows.Cast<DataRow>().Select(x => x[1]).Distinct(), Does.Contain("beta"));146 var otherValues = result.Rows.Cast<DataRow>().Select(x => x[1] as string).Where(x => x != "alpha" && x != "beta");147 Assert.That(otherValues, Is.Not.Empty);148 Assert.That(otherValues, Does.Contain("omega"));149 }150 [Test]151 public void ExecuteWithOriginalValueStrategy_OneLookupMissing_OriginalValueApplied()152 {153 var candidate = new ObjectsResultSetResolver(154 new ObjectsResultSetResolverArgs(155 new[] {156 new object[] { 1, "A", 100 },157 new object[] { 2, "B", 101 },158 new object[] { 3, "A", 125 },159 new object[] { 4, "C", 155 }160 }161 )).Execute();162 var reference = new ResultSetService(163 new ObjectsResultSetResolver(164 new ObjectsResultSetResolverArgs(165 new[] {166 new object[] { "A", "alpha" },167 new object[] { "B", "beta" },168 }169 )).Execute, null);170 var engine = new LookupReplaceEngine(171 new LookupReplaceArgs(172 reference,173 new ColumnMapping(new ColumnOrdinalIdentifier(1), new ColumnOrdinalIdentifier(0), ColumnType.Text),174 new ColumnOrdinalIdentifier(1),175 new OriginalValueMissingStrategy()176 ));177 var result = engine.Execute(candidate);178 Assert.That(result.Rows.Count, Is.EqualTo(4));179 Assert.That(result.Rows.Cast<DataRow>().Select(x => x[1]).Distinct(), Does.Contain("alpha"));180 Assert.That(result.Rows.Cast<DataRow>().Select(x => x[1]).Distinct(), Does.Contain("beta"));181 var otherValues = result.Rows.Cast<DataRow>().Select(x => x[1] as string).Where(x => x != "alpha" && x != "beta");182 Assert.That(otherValues, Is.Not.Empty);183 Assert.That(otherValues, Does.Contain("C"));184 }185 [Test]186 public void ExecuteWithDiscardRowStrategy_OneLookupMissing_LessRowsReturned()187 {188 var candidate = new ObjectsResultSetResolver(189 new ObjectsResultSetResolverArgs(190 new[] {191 new object[] { 1, "A", 100 },192 new object[] { 2, "B", 101 },193 new object[] { 3, "A", 125 },194 new object[] { 4, "C", 155 }195 }196 )).Execute();197 var reference = new ResultSetService(198 new ObjectsResultSetResolver(199 new ObjectsResultSetResolverArgs(200 new[] {201 new object[] { "A", "alpha" },202 new object[] { "B", "beta" },203 }204 )).Execute, null);205 var engine = new LookupReplaceEngine(206 new LookupReplaceArgs(207 reference,208 new ColumnMapping(new ColumnOrdinalIdentifier(1), new ColumnOrdinalIdentifier(0), ColumnType.Text),209 new ColumnOrdinalIdentifier(1),210 new DiscardRowMissingStrategy()211 ));212 var result = engine.Execute(candidate);213 Assert.That(result.Rows.Count, Is.EqualTo(3));214 Assert.That(result.Rows.Cast<DataRow>().Select(x => x[1]).Distinct(), Does.Contain("alpha"));215 Assert.That(result.Rows.Cast<DataRow>().Select(x => x[1]).Distinct(), Does.Contain("beta"));216 var otherValues = result.Rows.Cast<DataRow>().Select(x => x[1] as string).Where(x => x != "alpha" && x != "beta");217 Assert.That(otherValues, Is.Empty);218 }219 }220}...
LookupReplaceEngine.cs
Source:LookupReplaceEngine.cs
...14 {15 private LookupReplaceArgs Args { get; }16 public LookupReplaceEngine(LookupReplaceArgs args)17 => (Args) = (args);18 public IResultSet Execute(IResultSet candidate)19 {20 var reference = Args.Reference.Execute();21 var stopWatch = new Stopwatch();22 stopWatch.Start();23 var referenceKeyRetriever = BuildColumnsRetriever(Args.Mapping, x => x.ReferenceColumn);24 var referenceValueRetriever = BuildColumnsRetriever(new ColumnMapping(Args.Replacement, ColumnType.Untyped), x => x.ReferenceColumn);25 var index = BuildReferenceIndex(reference.Table, referenceKeyRetriever, referenceValueRetriever);26 Trace.WriteLineIf(Extensibility.NBiTraceSwitch.TraceInfo, $"Built the index for reference table containing {index.Count()} rows [{stopWatch.Elapsed:d'.'hh':'mm':'ss'.'fff'ms'}]");27 stopWatch.Restart();28 var candidateKeyBuilder = BuildColumnsRetriever(Args.Mapping, x => x.CandidateColumn);29 var originalColumn = candidate.Table.GetColumn(Args.Mapping.CandidateColumn);30 var newColumn = candidate.Table.Columns.Add($"tmp_{originalColumn.ColumnName}", typeof(object));31 foreach (DataRow row in candidate.Table.Rows)32 {33 var candidateKeys = candidateKeyBuilder.GetColumns(row);34 if (index.Keys.Contains(candidateKeys))35 row[newColumn.Ordinal] = index[candidateKeys].Single().Members[0];36 else37 Args.MissingStrategy.Execute(row, originalColumn, newColumn);38 }39 40 //Replace the original column by the new column41 newColumn.SetOrdinal(originalColumn.Ordinal);42 var columnName = originalColumn.ColumnName;43 candidate.Table.Columns.Remove(originalColumn);44 newColumn.ColumnName = columnName;45 Trace.WriteLineIf(Extensibility.NBiTraceSwitch.TraceInfo, $"Performed lookup replacement (based on keys) for the {candidate.Rows.Count} rows from candidate table [{stopWatch.Elapsed:d'.'hh':'mm':'ss'.'fff'ms'}]");46 candidate.Table.AcceptChanges();47 return candidate;48 }49 protected CellRetriever BuildColumnsRetriever(ColumnMapping column, Func<ColumnMapping, IColumnIdentifier> target)50 {51 var defColumns = new Collection<IColumnDefinition>();...
Execute
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Data;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7using NBi.Core.ResultSet.Alteration.Lookup;8{9 {10 private readonly LookupReplaceCommand command;11 public LookupReplaceEngine(LookupReplaceCommand command)12 {13 this.command = command;14 }15 public void Execute(DataTable table)16 {17 if (table == null)18 throw new ArgumentNullException();19 if (table.Rows.Count == 0)20 return;21 var lookupTable = command.LookupTable.Build();22 if (lookupTable.Rows.Count == 0)23 return;24 var lookupEngine = new LookupEngine(command);25 var lookupResult = lookupEngine.ExecuteLookup(table, lookupTable);26 var columnOrdinal = table.Columns[command.Column].Ordinal;27 foreach (var row in lookupResult)28 row[columnOrdinal] = row[lookupTable.Columns.Count];29 }30 }31}32using System;33using System.Collections.Generic;34using System.Data;35using System.Linq;36using System.Text;37using System.Threading.Tasks;38using NBi.Core.ResultSet.Alteration.Lookup;39using NBi.Core.ResultSet.Alteration.Lookup.Strategies;40{41 {42 private readonly LookupCommand command;43 public LookupEngine(LookupCommand command)44 {45 this.command = command;46 }47 public void Execute(DataTable table)48 {49 if (table == null)50 throw new ArgumentNullException();51 if (table.Rows.Count == 0)52 return;53 var lookupTable = command.LookupTable.Build();54 if (lookupTable.Rows.Count == 0)55 return;56 ExecuteLookup(table, lookupTable);57 }58 public DataTable ExecuteLookup(DataTable table, DataTable lookupTable)59 {60 var strategy = new LookupStrategyFactory().Instantiate(command);61 return strategy.Execute(table, lookupTable);62 }63 }64}65using System;66using System.Collections.Generic;67using System.Data;68using System.Linq;69using System.Text;70using System.Threading.Tasks;71using NBi.Core.ResultSet.Alteration.Lookup.Strategies;72{
Execute
Using AI Code Generation
1using NBi.Core.ResultSet.Alteration.Lookup;2LookupReplaceEngine engine = new LookupReplaceEngine();3engine.Execute();4using NBi.Core.ResultSet.Alteration.Lookup;5LookupReplaceEngine engine = new LookupReplaceEngine();6engine.Execute();7using NBi.Core.ResultSet.Alteration.Lookup;8LookupReplaceEngine engine = new LookupReplaceEngine();9engine.Execute();10using NBi.Core.ResultSet.Alteration.Lookup;11LookupReplaceEngine engine = new LookupReplaceEngine();12engine.Execute();13using NBi.Core.ResultSet.Alteration.Lookup;14LookupReplaceEngine engine = new LookupReplaceEngine();15engine.Execute();16using NBi.Core.ResultSet.Alteration.Lookup;17LookupReplaceEngine engine = new LookupReplaceEngine();18engine.Execute();19using NBi.Core.ResultSet.Alteration.Lookup;20LookupReplaceEngine engine = new LookupReplaceEngine();21engine.Execute();22using NBi.Core.ResultSet.Alteration.Lookup;23LookupReplaceEngine engine = new LookupReplaceEngine();24engine.Execute();25using NBi.Core.ResultSet.Alteration.Lookup;26LookupReplaceEngine engine = new LookupReplaceEngine();27engine.Execute();28using NBi.Core.ResultSet.Alteration.Lookup;29LookupReplaceEngine engine = new LookupReplaceEngine();30engine.Execute();31using NBi.Core.ResultSet.Alteration.Lookup;
Execute
Using AI Code Generation
1var engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();2var result = engine.Execute(resultSet, lookupTable, "Column1", "Column2");3var engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();4var result = engine.Execute(resultSet, lookupTable, "Column1", "Column2");5var engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();6var result = engine.Execute(resultSet, lookupTable, "Column1", "Column2");7var engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();8var result = engine.Execute(resultSet, lookupTable, "Column1", "Column2");9var engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();10var result = engine.Execute(resultSet, lookupTable, "Column1", "Column2");11var engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();12var result = engine.Execute(resultSet, lookupTable, "Column1", "Column2");13var engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();14var result = engine.Execute(resultSet, lookupTable, "Column1", "
Execute
Using AI Code Generation
1using NBi.Core.ResultSet.Alteration.Lookup;2using NBi.Core.ResultSet;3using System.Collections.Generic;4using System.Data;5using System.Data.SqlClient;6using System.Linq;7using System;8using System.IO;9{10 {11 static void Main(string[] args)12 {13 DataTable dt = new DataTable();14 dt.Columns.Add("ID");15 dt.Columns.Add("Name");16 dt.Rows.Add("1", "Name1");17 dt.Rows.Add("2", "Name2");18 dt.Rows.Add("3", "Name3");19 dt.Rows.Add("4", "Name4");20 dt.Rows.Add("5", "Name5");21 dt.Rows.Add("6", "Name6");22 DataTable lookupTable = new DataTable();23 lookupTable.Columns.Add("ID");24 lookupTable.Columns.Add("Name");25 lookupTable.Rows.Add("1", "Name1");26 lookupTable.Rows.Add("2", "Name2");27 lookupTable.Rows.Add("3", "Name3");28 lookupTable.Rows.Add("4", "Name4");29 lookupTable.Rows.Add("5", "Name5");30 lookupTable.Rows.Add("6", "Name6");31 LookupReplaceEngine lookupReplaceEngine = new LookupReplaceEngine();32 ResultSet rs = new ResultSet(dt);33 ResultSet lookupRs = new ResultSet(lookupTable);34 LookupReplaceArgs lookupReplaceArgs = new LookupReplaceArgs("ID", "ID", "Name");35 lookupReplaceEngine.Execute(rs, lookupRs, lookupReplaceArgs);36 foreach (DataRow row in rs.Table.Rows)37 {38 Console.WriteLine(row["ID"].ToString() + " " + row["Name"].ToString());39 }40 }41 }42}43using NBi.Core.ResultSet.Alteration.Lookup;44using NBi.Core.ResultSet;45using System.Collections.Generic;46using System.Data;47using System.Data.SqlClient;48using System.Linq;49using System;50using System.IO;51{52 {53 static void Main(string[] args)
Execute
Using AI Code Generation
1var engine = new LookupReplaceEngine( new ResultSet( new Object[ 1 , 2 ] { { 1 , 2 }, { 3 , 4 } }), 1 , 2 , 1 , 2 );2engine.Execute();3 var result = engine.Result;4 var engine = new LookupReplaceEngine( new ResultSet( new Object[ 1 , 2 ] { { 1 , 2 }, { 3 , 4 } }), 1 , 2 , 1 , 2 );5engine.Execute();6 var result = engine.Result;7 var engine = new LookupReplaceEngine( new ResultSet( new Object[ 1 , 2 ] { { 1 , 2 }, { 3 , 4 } }), 1 , 2 , 1 , 2 );8engine.Execute();9 var result = engine.Result;10 var engine = new LookupReplaceEngine( new ResultSet( new Object[ 1 , 2 ] { { 1 , 2 }, { 3 , 4 } }), 1 , 2 , 1 , 2 );11engine.Execute();12 var result = engine.Result;13 var engine = new LookupReplaceEngine( new ResultSet( new Object[ 1 , 2 ] { { 1 , 2 }, { 3 , 4 } }), 1 , 2 , 1 , 2 );14engine.Execute();15 var result = engine.Result;
Execute
Using AI Code Generation
1NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();2engine.Execute(resultset, lookupTable, "LookupColumn", "LookupColumn", "ColumnToReplace");3NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();4engine.Execute(resultset, lookupTable, "LookupColumn", "LookupColumn", "ColumnToReplace");5NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();6engine.Execute(resultset, lookupTable, "LookupColumn", "LookupColumn", "ColumnToReplace");7NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();8engine.Execute(resultset, lookupTable, "LookupColumn", "LookupColumn", "ColumnToReplace");9NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();10engine.Execute(resultset, lookupTable, "LookupColumn", "LookupColumn", "ColumnToReplace");11NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();12engine.Execute(resultset, lookupTable, "LookupColumn", "LookupColumn", "ColumnToReplace");13NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();14engine.Execute(resultset, lookupTable, "LookupColumn", "LookupColumn", "ColumnToReplace");15NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();16engine.Execute(resultset, lookupTable, "LookupColumn", "LookupColumn", "ColumnToReplace");
Execute
Using AI Code Generation
1var engine = new LookupReplaceEngine ( ) ; 2 var result = engine . Execute ( dataset , lookupTable , columnMapping , lookupColumn , replacementColumn ) ;3var engine = new LookupReplaceEngine ( ) ; 4 var result = engine . Execute ( dataset , lookupTable , columnMapping , lookupColumn , replacementColumn , ignoreCase ) ;5var engine = new LookupReplaceEngine ( ) ; 6 var result = engine . Execute ( dataset , lookupTable , columnMapping , lookupColumn , replacementColumn , ignoreCase , missingValue ) ;7var engine = new LookupReplaceEngine ( ) ; 8 var result = engine . Execute ( dataset , lookupTable , columnMapping , lookupColumn , replacementColumn , ignoreCase , missingValue , missingValueReplacement ) ;9var engine = new LookupReplaceEngine ( ) ; 10 var result = engine . Execute ( dataset , lookupTable , columnMapping , lookupColumn , replacementColumn , ignoreCase , missingValue , missingValueReplacement , missingValueTolerance ) ;11var engine = new LookupReplaceEngine ( ) ; 12 var result = engine . Execute ( dataset , lookupTable , columnMapping , lookupColumn , replacementColumn , ignoreCase , missingValue , missingValueReplacement , missingValueTolerance , missingValueToleranceType ) ;
Execute
Using AI Code Generation
1var engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();2engine.Execute(new string[] { "1", "2", "3" }, "1", "4");3var engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();4engine.Execute(new string[] { "1", "2", "3" }, "1", "4");5var engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();6engine.Execute(new string[] { "1", "2", "3" }, "1", "4");7var engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();8engine.Execute(new string[] { "1", "2", "3" }, "1", "4");9var engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();10engine.Execute(new string[] { "1", "2", "3" }, "1", "4");11var engine = new NBi.Core.ResultSet.Alteration.Lookup.LookupReplaceEngine();12engine.Execute(new string[]
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!!