Skip to content

Commit

Permalink
Merge branch 'feature/Support_renaming_a_column_of_a_result-set_(#493)…
Browse files Browse the repository at this point in the history
…' into develop
  • Loading branch information
Cédric L. Charlier committed Jul 2, 2019
2 parents 48c66f5 + be5deb9 commit bd05c48
Show file tree
Hide file tree
Showing 17 changed files with 302 additions and 12 deletions.
5 changes: 5 additions & 0 deletions NBi.Core/NBi.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@
<Compile Include="Report\ReportDataSetRequest.cs" />
<Compile Include="Report\ReportingCommand.cs" />
<Compile Include="ResultSet\Alteration\Alter.cs" />
<Compile Include="ResultSet\Alteration\Renaming\IRenamingArgs.cs" />
<Compile Include="ResultSet\Alteration\Renaming\IRenamingEngine.cs" />
<Compile Include="ResultSet\Alteration\Renaming\NewNameRenamingArgs.cs" />
<Compile Include="ResultSet\Alteration\Renaming\NewNameRenamingEngine.cs" />
<Compile Include="ResultSet\Alteration\Renaming\RenamingFactory.cs" />
<Compile Include="ResultSet\Analyzer\AnalyzersFactory.cs" />
<Compile Include="ResultSet\Analyzer\BaseRowsAnalyzer.cs" />
<Compile Include="ResultSet\Analyzer\IRowsAnalyzer.cs" />
Expand Down
11 changes: 11 additions & 0 deletions NBi.Core/ResultSet/Alteration/Renaming/IRenamingArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NBi.Core.ResultSet.Alteration.Renaming
{
public interface IRenamingArgs
{ }
}
13 changes: 13 additions & 0 deletions NBi.Core/ResultSet/Alteration/Renaming/IRenamingEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NBi.Core.ResultSet.Alteration.Renaming
{
public interface IRenamingEngine
{
ResultSet Execute(ResultSet rs);
}
}
18 changes: 18 additions & 0 deletions NBi.Core/ResultSet/Alteration/Renaming/NewNameRenamingArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using NBi.Core.Scalar.Resolver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NBi.Core.ResultSet.Alteration.Renaming
{
public class NewNameRenamingArgs : IRenamingArgs
{
public IColumnIdentifier OriginalIdentification { get; set; }
public IScalarResolver<string> NewIdentification { get; set; }

public NewNameRenamingArgs(IColumnIdentifier originalIdentification, IScalarResolver<string> newIdentification)
=> (OriginalIdentification, NewIdentification) = (originalIdentification, newIdentification);
}
}
24 changes: 24 additions & 0 deletions NBi.Core/ResultSet/Alteration/Renaming/NewNameRenamingEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using NBi.Core.Scalar.Resolver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NBi.Core.ResultSet.Alteration.Renaming
{
class NewNameRenamingEngine : IRenamingEngine
{
private IColumnIdentifier OriginalIdentification { get; }
private IScalarResolver<string> NewIdentification { get; }

public NewNameRenamingEngine(IColumnIdentifier originalIdentification, IScalarResolver<string> newIdentification)
=> (OriginalIdentification, NewIdentification) = (originalIdentification, newIdentification);

public ResultSet Execute(ResultSet rs)
{
OriginalIdentification.GetColumn(rs.Table).ColumnName = NewIdentification.Execute();
return rs;
}
}
}
20 changes: 20 additions & 0 deletions NBi.Core/ResultSet/Alteration/Renaming/RenamingFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NBi.Core.ResultSet.Alteration.Renaming
{
public class RenamingFactory
{
public IRenamingEngine Instantiate(IRenamingArgs args)
{
switch(args)
{
case NewNameRenamingArgs x: return new NewNameRenamingEngine(x.OriginalIdentification, x.NewIdentification);
default: throw new ArgumentException();
}
}
}
}
14 changes: 14 additions & 0 deletions NBi.NUnit/Builder/Helper/ResultSetSystemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NBi.Core.Injection;
using NBi.Core.ResultSet;
using NBi.Core.ResultSet.Alteration;
using NBi.Core.ResultSet.Alteration.Renaming;
using NBi.Core.ResultSet.Conversion;
using NBi.Core.ResultSet.Resolver;
using NBi.Core.Scalar.Conversion;
Expand Down Expand Up @@ -123,6 +124,19 @@ public IEnumerable<Alter> InstantiateAlterations(ResultSetSystemXml resultSetXml
provider.Add(transformationXml.Identifier, transformationXml);
yield return provider.Transform;
}

if (resultSetXml.Alteration.Renamings != null)
{
foreach (var renameXml in resultSetXml.Alteration.Renamings)
{
var helper = new ScalarHelper(serviceLocator, variables);
var newName = helper.InstantiateResolver<string>(renameXml.NewName);

var factory = new RenamingFactory();
var renamer = factory.Instantiate(new NewNameRenamingArgs(renameXml.Identifier, newName));
yield return renamer.Execute;
}
}
}
}
}
48 changes: 38 additions & 10 deletions NBi.Testing/Acceptance/Resources/Positive/ResultSetConstraint.nbits
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,16 @@
<system-under-test>
<result-set>
<query>
select
NationalIdNumber,
h.[DepartmentID]as DepartmentID,
d.Name as DepartmentName
from
[HumanResources].[Employee] e
inner join [HumanResources].[EmployeeDepartmentHistory]
h on e.BusinessEntityId = h.BusinessEntityId
inner join [HumanResources].[Department] d on
h.DepartmentId = d.DepartmentId
select
NationalIdNumber,
h.[DepartmentID]as DepartmentID,
d.Name as DepartmentName
from
[HumanResources].[Employee] e
inner join [HumanResources].[EmployeeDepartmentHistory]
h on e.BusinessEntityId = h.BusinessEntityId
inner join [HumanResources].[Department] d on
h.DepartmentId = d.DepartmentId
</query>
</result-set>
</system-under-test>
Expand Down Expand Up @@ -847,5 +847,33 @@
</equalTo>
</assert>
</test>
<test name="Renamings" uid="0241">
<system-under-test>
<result-set>
<query>
select 'a' as f0, null as f1, 'FOO' as f2 union all select 'B', 'qwarks', 'bar'
</query>
<alteration>
<rename identifier="[f0]" new-name="keyField"/>
<rename identifier="[f2]" new-name="@CY2007"/>
</alteration>
</result-set>
</system-under-test>
<assert>
<equal-to>
<column name="keyField" role="key" type="text"/>
<column name="CY 2007" role="value" type="text"/>
<result-set>
<query connection-string="@ref1">
select 'a' as f0, 'FOO' as f1, null as f2 union all select 'B', 'bar', null
</query>
<alteration>
<rename identifier="[f0]" new-name="keyField"/>
<rename identifier="[f1]" new-name="@CY2007"/>
</alteration>
</result-set>
</equal-to>
</assert>
</test>
</group>
</testSuite>
2 changes: 2 additions & 0 deletions NBi.Testing/NBi.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@
<Compile Include="Unit\Core\Query\Execution\ExecutionEngineFactoryTest.cs" />
<Compile Include="Unit\Core\Query\QueryParameterTest.cs" />
<Compile Include="Unit\Core\Report\FileReportingParserTest.cs" />
<Compile Include="Unit\Core\ResultSet\Alteration\Renaming\NewNameRenamingEngineTest.cs" />
<Compile Include="Unit\Core\ResultSet\Alteration\Renaming\RenamingFactoryTest.cs" />
<Compile Include="Unit\Core\ResultSet\ColumnIdentifierFactory.cs" />
<Compile Include="Unit\Core\ResultSet\Combination\CartesianProductSequenceCombinationTest.cs" />
<Compile Include="Unit\Core\ResultSet\Lookup\ColumnMappingCollectionTest.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using NBi.Core.ResultSet;
using NBi.Core.ResultSet.Alteration.Renaming;
using NBi.Core.ResultSet.Resolver;
using NBi.Core.Scalar.Resolver;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NBi.Testing.Unit.Core.ResultSet.Alteration.Renaming
{
public class RenamingEngineFactoryTest
{
[Test]
public void Execute_FirstColumnIsText_FirstColumnIsNumeric()
{
var args = new ObjectsResultSetResolverArgs(new[] { new[] { "100,12", "Alpha" }, new[] { "100", "Beta" }, new[] { "0,1", "Gamma" } });
var resolver = new ObjectsResultSetResolver(args);
var rs = resolver.Execute();

var renamer = new NewNameRenamingEngine(
new ColumnOrdinalIdentifier(1),
new LiteralScalarResolver<string>("myNewName")
);
var newRs = renamer.Execute(rs);

Assert.That(newRs.Columns[1].ColumnName, Is.EqualTo("myNewName"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using NBi.Core.ResultSet;
using NBi.Core.ResultSet.Alteration.Renaming;
using NBi.Core.ResultSet.Resolver;
using NBi.Core.Scalar.Resolver;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NBi.Testing.Unit.Core.ResultSet.Alteration.Renaming
{
public class RenamingFactoryTest
{
[Test]
public void Instantiate_NewNameRenamingEngineArgs_NewNameRenamingEngineArgs()
{
var factory = new RenamingFactory();
var renamer = factory.Instantiate(new NewNameRenamingArgs(
new ColumnOrdinalIdentifier(1),
new LiteralScalarResolver<string>("myNewName")
));
Assert.That(renamer, Is.Not.Null);
Assert.That(renamer, Is.TypeOf<NewNameRenamingEngine>());
}
}
}
17 changes: 17 additions & 0 deletions NBi.Testing/Unit/Xml/Resources/ResultSetSystemXmlTestSuite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,23 @@
</equalTo>
</assert>
</test>
<test name="Alteration has a renaming" uid="0009">
<system-under-test>
<resultSet>
<query>
<report path="/path/" name="MyReport" dataset="MyDataSet"/>
</query>
<alteration>
<rename identifier="#3" new-name="myNewName"/>
</alteration>
</resultSet>
</system-under-test>
<assert>
<equalTo>
<resultSet file="..\Csv\ResellerOrderCountByYearBefore2006.csv"/>
</equalTo>
</assert>
</test>
<test name="Result-set has csv file" uid="0009">
<system-under-test>
<resultSet>
Expand Down
48 changes: 46 additions & 2 deletions NBi.Testing/Unit/Xml/Systems/ResultSetSystemXmlTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using NBi.Core.ResultSet;
using NBi.Core.Scalar.Resolver;
using NBi.Core.Transformation;
using NBi.Xml;
using NBi.Xml.Items;
using NBi.Xml.Items.Alteration;
using NBi.Xml.Items.Alteration.Conversion;
using NBi.Xml.Items.Alteration.Renaming;
using NBi.Xml.Items.Alteration.Transform;
using NBi.Xml.Items.ResultSet;
using NBi.Xml.SerializationOption;
Expand Down Expand Up @@ -54,7 +57,7 @@ public void Deserialize_SampleFile_CsvFile()
[Test]
public void Deserialize_SampleFileWithParser_CsvFile()
{
int testNr = 9;
int testNr = 10;

// Create an instance of the XmlSerializer specifying type and namespace.
TestSuiteXml ts = DeserializeSample();
Expand All @@ -70,7 +73,7 @@ public void Deserialize_SampleFileWithParser_CsvFile()
[Test]
public void Deserialize_SampleFileWithParserInline_CsvFile()
{
int testNr = 10;
int testNr = 11;

// Create an instance of the XmlSerializer specifying type and namespace.
TestSuiteXml ts = DeserializeSample();
Expand Down Expand Up @@ -240,6 +243,26 @@ public void Deserialize_SampleFile_AlterationTransformation()
Assert.That(rs.Alteration.Transformations[0].Code.Trim(), Is.EqualTo("value.EndsWith(\".\") ? value : value + \".\""));
}

[Test]
public void Deserialize_SampleFile_AlterationRename()
{
int testNr = 9;

// Create an instance of the XmlSerializer specifying type and namespace.
TestSuiteXml ts = DeserializeSample();

// Check the properties of the object.
Assert.That(ts.Tests[testNr].Systems[0], Is.AssignableTo<ResultSetSystemXml>());
var rs = ts.Tests[testNr].Systems[0] as ResultSetSystemXml;

Assert.That(rs.Alteration, Is.Not.Null);
Assert.That(rs.Alteration.Renamings, Is.Not.Null);
Assert.That(rs.Alteration.Renamings, Has.Count.EqualTo(1));

Assert.That(rs.Alteration.Renamings[0].Identifier.Label, Is.EqualTo("#3"));
Assert.That(rs.Alteration.Renamings[0].NewName, Is.EqualTo("myNewName"));
}

[Test]
public void Serialize_FileAndParser_Correct()
{
Expand Down Expand Up @@ -302,5 +325,26 @@ public void Serialize_InlineFileWithoutParser_Correct()
Assert.That(xml, Is.StringContaining("</file>"));
}

[Test]
public void Serialize_Renaming_Correct()
{
var root = new ResultSetSystemXml()
{
File = new FileXml() { Path=@"C:\Temp\foo.txt" },
Alteration = new AlterationXml()
{
Renamings = new List<RenamingXml>() { new RenamingXml()
{ Identifier= new ColumnOrdinalIdentifier(5), NewName = "myNewName" } }
}
};

var manager = new XmlManager();
var xml = manager.XmlSerializeFrom(root);
Console.WriteLine(xml);
Assert.That(xml, Is.StringContaining("<rename"));
Assert.That(xml, Is.StringContaining("#5"));
Assert.That(xml, Is.StringContaining("myNewName"));
}

}
}
3 changes: 3 additions & 0 deletions NBi.Xml/Items/Alteration/AlterationXml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NBi.Xml.Items.Alteration.Conversion;
using NBi.Xml.Items.Alteration.Renaming;
using NBi.Xml.Items.Alteration.Transform;
using NBi.Xml.Items.Calculation;
using NBi.Xml.Items.ResultSet;
Expand All @@ -13,6 +14,8 @@ namespace NBi.Xml.Items.Alteration
{
public class AlterationXml
{
[XmlElement("rename")]
public List<RenamingXml> Renamings { get; set; }
[XmlElement("filter")]
public List<FilterXml> Filters { get; set; }
[XmlElement("convert")]
Expand Down
Loading

0 comments on commit bd05c48

Please sign in to comment.