Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add namespaces to csharp scalar resolver (#584) #587

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions NBi.Core/Scalar/Resolver/CSharpScalarResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.XPath;

namespace NBi.Core.Scalar.Resolver
{
Expand Down Expand Up @@ -38,6 +40,10 @@ private MethodInfo CreateFunction(string code)
{
string codeTemplate = @"
using System;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Xml.XPath;

namespace {1}
{{
Expand All @@ -59,6 +65,14 @@ public static object Function()
{
GenerateInMemory = true,
GenerateExecutable = false,
ReferencedAssemblies =
{
"System.Xml.dll",
"System.Xml.Linq.dll",
"System.Linq.dll",
"System.Core.dll",
"System.Xml.XPath.dll"
}
};

var results = provider.CompileAssemblyFromSource(parameters, finalCode);
Expand Down
6 changes: 5 additions & 1 deletion NBi.Testing.Core/NBi.Testing.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,11 @@
<ItemGroup>
<EmbeddedResource Include="DataSerialization\Flattenizer\Resources\PurchaseOrders.json" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<EmbeddedResource Include="Scalar\Resolver\Resources\PurchaseOrders.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
Expand Down
30 changes: 30 additions & 0 deletions NBi.Testing.Core/Scalar/Resolver/CSharpScalarResolverTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.XPath;

namespace NBi.Testing.Core.Scalar.Resolver
{
Expand Down Expand Up @@ -33,5 +35,33 @@ public void Instantiate_GetValueInt_CorrectComputation()
Assert.That(output, Is.EqualTo(DateTime.Now.Year));
}

[Test]
public void Instantiate_GetValueXmlLinq_CorrectComputation()
{
var xmlPath = new Uri(FileOnDisk.CreatePhysicalFile("PurchaseOrders.xml", "NBi.Testing.Core.Scalar.Resolver.Resources.PurchaseOrders.xml")).AbsolutePath;
string xmlDoc = string.Format(@"XDocument.Load(""{0}"").Root.Name.ToString()", xmlPath);

var args = new CSharpScalarResolverArgs(xmlDoc);
var resolver = new CSharpScalarResolver<string>(args);

var output = resolver.Execute();

Assert.That(output, Is.EqualTo(XDocument.Load(xmlPath).Root.Name.ToString()));
}

[Test]
public void Instantiate_GetValueXmlXpath_CorrectComputation()
{
var xPath = "./PurchaseOrders/PurchaseOrder/Address/Name";
var xmlPath = new Uri(FileOnDisk.CreatePhysicalFile("PurchaseOrders.xml", "NBi.Testing.Core.Scalar.Resolver.Resources.PurchaseOrders.xml")).AbsolutePath;
string xmlDoc = string.Format(@"XDocument.Load(""{0}"").XPathSelectElement(""{1}"").Value.ToString()", xmlPath, xPath);

var args = new CSharpScalarResolverArgs(xmlDoc);
var resolver = new CSharpScalarResolver<string>(args);

var output = resolver.Execute();

Assert.That(output, Is.EqualTo(XDocument.Load(xmlPath).XPathSelectElement(xPath).Value.ToString()));
}
}
}
122 changes: 122 additions & 0 deletions NBi.Testing.Core/Scalar/Resolver/Resources/PurchaseOrders.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?xml version="1.0" encoding="utf-8" ?>
<PurchaseOrders>
<PurchaseOrder PurchaseOrderNumber="99503" OrderDate="1999-10-20">
<Address Type="Shipping">
<Name>Ellen Adams</Name>
<Street>123 Maple Street</Street>
<City>Mill Valley</City>
<State>CA</State>
<Zip>10999</Zip>
<Country>USA</Country>
</Address>
<Address Type="Billing">
<Name>Tai Yee</Name>
<Street>8 Oak Avenue</Street>
<City>Old Town</City>
<State>PA</State>
<Zip>95819</Zip>
<Country>USA</Country>
</Address>
<DeliveryNotes>Please leave packages in shed by driveway.</DeliveryNotes>
<Items>
<Item PartNumber="872-AA">
<ProductName>Lawnmower</ProductName>
<Quantity>1</Quantity>
<USPrice>148.95</USPrice>
<Comment>Confirm this is electric</Comment>
</Item>
<Item PartNumber="926-AA">
<ProductName>Baby Monitor</ProductName>
<Quantity>2</Quantity>
<USPrice>39.98</USPrice>
<ShipDate>1999-05-21</ShipDate>
</Item>
</Items>
</PurchaseOrder>
<PurchaseOrder PurchaseOrderNumber="99505" OrderDate="1999-10-22">
<Address Type="Shipping">
<Name>Cristian Osorio</Name>
<Street>456 Main Street</Street>
<City>Buffalo</City>
<State>NY</State>
<Zip>98112</Zip>
<Country>USA</Country>
</Address>
<Address Type="Billing">
<Name>Cristian Osorio</Name>
<Street>456 Main Street</Street>
<City>Buffalo</City>
<State>NY</State>
<Zip>98112</Zip>
<Country>USA</Country>
</Address>
<DeliveryNotes>Please notify by email before shipping.</DeliveryNotes>
<Items>
<Item PartNumber="456-NM">
<ProductName>Power Supply</ProductName>
<Quantity>1</Quantity>
<USPrice>45.99</USPrice>
</Item>
</Items>
</PurchaseOrder>
<PurchaseOrder PurchaseOrderNumber="99504" OrderDate="1999-10-22">
<Address Type="Shipping">
<Name>Jessica Arnold</Name>
<Street>4055 Madison Ave</Street>
<City>Seattle</City>
<State>WA</State>
<Zip>98112</Zip>
<Country>USA</Country>
</Address>
<Address Type="Billing">
<Name>Jessica Arnold</Name>
<Street>4055 Madison Ave</Street>
<City>Buffalo</City>
<State>NY</State>
<Zip>98112</Zip>
<Country>USA</Country>
</Address>
<DeliveryNotes>Please do not deliver on Saturday.</DeliveryNotes>
<Items>
<Item PartNumber="898-AZ">
<ProductName>Computer Keyboard</ProductName>
<Quantity>1</Quantity>
<USPrice>29.99</USPrice>
</Item>
<Item PartNumber="898-AM">
<ProductName>Wireless Mouse</ProductName>
<Quantity>1</Quantity>
<USPrice>14.99</USPrice>
</Item>
</Items>
</PurchaseOrder>
<aw:PurchaseOrder PONumber="11223" Date="2000-01-15" xmlns:aw="http://www.adventure-works.com">
<aw:ShippingAddress>
<aw:Name>Chris Preston</aw:Name>
<aw:Street>123 Main St.</aw:Street>
<aw:City>Seattle</aw:City>
<aw:State>WA</aw:State>
<aw:Zip>98113</aw:Zip>
<aw:Country>USA</aw:Country>
</aw:ShippingAddress>
<aw:BillingAddress>
<aw:Name>Chris Preston</aw:Name>
<aw:Street>123 Main St.</aw:Street>
<aw:City>Seattle</aw:City>
<aw:State>WA</aw:State>
<aw:Zip>98113</aw:Zip>
<aw:Country>USA</aw:Country>
</aw:BillingAddress>
<aw:DeliveryInstructions>Ship only complete order.</aw:DeliveryInstructions>
<aw:Item PartNum="LIT-01">
<aw:ProductID>Litware Networking Card</aw:ProductID>
<aw:Qty>1</aw:Qty>
<aw:Price>20.99</aw:Price>
</aw:Item>
<aw:Item PartNum="LIT-25">
<aw:ProductID>Litware 17in LCD Monitor</aw:ProductID>
<aw:Qty>1</aw:Qty>
<aw:Price>199.99</aw:Price>
</aw:Item>
</aw:PurchaseOrder>
</PurchaseOrders>
20 changes: 20 additions & 0 deletions NBi.Testing/Acceptance/Resources/Positive/Variable.nbits
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<parameter name="to">@prevcommon</parameter>
</custom>
</variable>
<variable name="xmlVar">
<script language="c-sharp">XDocument.Load(@"./Acceptance/Resources/PurchaseOrders.xml").XPathSelectElement("./PurchaseOrders/PurchaseOrder/Address/Zip").Value.ToString()</script>
</variable>
</variables>
<test name="'Reseller Order Count' by year before 2006 (csv)" uid="0001">
<system-under-test>
Expand Down Expand Up @@ -142,4 +145,21 @@
</all-rows>
</assert>
</test>
<test name="Variable retrieved from Xml file" uid="0301">
<system-under-test>
<result-set>
<query>
<parameter name="var">@xmlVar</parameter>
select @var;
</query>
</result-set>
</system-under-test>
<assert>
<single-row>
<predicate operand="#0">
<equal>10999</equal>
</predicate>
</single-row>
</assert>
</test>
</testSuite>