Skip to content

Commit

Permalink
Merge branch 'feature/load_optional_file_for_genbil_(#582)' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric L. Charlier committed Feb 3, 2020
2 parents 7fc8c07 + 7671306 commit 6baa70a
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 3 deletions.
38 changes: 38 additions & 0 deletions NBi.Testing.GenbiL/Action/Case/LoadFileOptionalCaseActionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using NBi.GenbiL;
using NBi.GenbiL.Action.Case;
using NBi.GenbiL.Stateful;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NBi.Testing.GenbiL.Action.Case
{
public class LoadFileOptionalCaseActionTest
{
public class LoadOptionalCaseFromFileActionTestable : LoadOptionalCaseFromFileAction
{
public LoadOptionalCaseFromFileActionTestable(string filename, IEnumerable<string> columnNames)
: base(filename, columnNames) { }

protected override bool IsExistingFile() => false;
}

[Test]
public void Execute_FileMissing_EmptyDataSetWithExpectedColumns()
{
var state = new GenerationState();
var action = new LoadOptionalCaseFromFileActionTestable("file.csv", new[] { "foo", "bar" });
action.Execute(state);

var caseSet = state.CaseCollection.First().Value;
Assert.That(caseSet.Content.Rows, Has.Count.EqualTo(0));
Assert.That(caseSet.Content.Columns, Has.Count.EqualTo(2));
Assert.That(caseSet.Content.Columns.Cast<DataColumn>().Select(x => x.ColumnName), Does.Contain("foo"));
Assert.That(caseSet.Content.Columns.Cast<DataColumn>().Select(x => x.ColumnName), Does.Contain("bar"));
}
}
}
1 change: 1 addition & 0 deletions NBi.Testing.GenbiL/NBi.Testing.GenbiL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Compile Include="Action\Case\ConcatenateCaseActionTest.cs" />
<Compile Include="Action\Case\CopyCaseActionTest.cs" />
<Compile Include="Action\Case\CrossFullCaseActionTest.cs" />
<Compile Include="Action\Case\LoadFileOptionalCaseActionTest.cs" />
<Compile Include="Action\Case\CrossJoinCaseActionTest.cs" />
<Compile Include="Action\Case\CrossVectorCaseActionTest.cs" />
<Compile Include="Action\Case\DuplicateCaseActionTest.cs" />
Expand Down
13 changes: 13 additions & 0 deletions NBi.Testing.GenbiL/Parser/CaseParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ public void SentenceParser_CaseLoadFileString_ValidCaseLoadSentence()
Assert.That(((LoadCaseFromFileAction)result).Filename, Is.EqualTo("filename.csv"));
}

[Test]
public void SentenceParser_CaseLoadOptionalFileString_ValidCaseLoadOptionalSentence()
{
var input = "case load optional file 'filename.csv' with columns 'foo', 'bar';";
var result = Case.Parser.Parse(input);

Assert.That(result, Is.Not.Null);
Assert.That(result, Is.InstanceOf<LoadOptionalCaseFromFileAction>());
Assert.That(((LoadOptionalCaseFromFileAction)result).Filename, Is.EqualTo("filename.csv"));
Assert.That(((LoadOptionalCaseFromFileAction)result).ColumnNames, Does.Contain("foo"));
Assert.That(((LoadOptionalCaseFromFileAction)result).ColumnNames, Does.Contain("bar"));
}

[Test]
public void SentenceParser_CaseLoadQueryFileString_ValidCaseLoadSentence()
{
Expand Down
4 changes: 2 additions & 2 deletions NBi.genbiL/Action/Case/LoadCaseFromFileAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public LoadCaseFromFileAction(string filename)

public void Execute(GenerationState state) => Execute(state.CaseCollection.CurrentScope);

public void Execute(CaseSet testCases)
public virtual void Execute(CaseSet testCases)
{
var csvReader = new CsvReader();
testCases.Content = csvReader.ToDataTable(Filename, true);
testCases.Content.AcceptChanges();
}

public string Display => $"Loading TestCases from CSV file '{Filename}'";
public virtual string Display => $"Loading TestCases from CSV file '{Filename}'";
}
}
35 changes: 35 additions & 0 deletions NBi.genbiL/Action/Case/LoadOptionalCaseFromFileAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using NBi.Core.FlatFile;
using NBi.GenbiL.Stateful;

namespace NBi.GenbiL.Action.Case
{
public class LoadOptionalCaseFromFileAction : LoadCaseFromFileAction
{
public IEnumerable<string> ColumnNames { get; set; }

public LoadOptionalCaseFromFileAction(string filename, IEnumerable<string> columnNames)
: base(filename) => ColumnNames = columnNames;

public override void Execute(CaseSet testCases)
{
if (IsExistingFile())
base.Execute(testCases);
else
{
testCases.Content = new DataTable();
foreach (var columnName in ColumnNames)
testCases.Content.Columns.Add(new DataColumn(columnName, typeof(object)));
testCases.Content.AcceptChanges();
}
}

protected virtual bool IsExistingFile() => File.Exists(Filename);

public override string Display => $"Loading test-cases from CSV file '{Filename}', if missing create empty test-case with following columns {string.Join(", ", ColumnNames)}.";
}
}
1 change: 1 addition & 0 deletions NBi.genbiL/NBi.genbiL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="Action\Case\DuplicateCaseAction.cs" />
<Compile Include="Action\Case\ICaseAction.cs" />
<Compile Include="Action\Case\LoadCaseFromDatatableAction.cs" />
<Compile Include="Action\Case\LoadOptionalCaseFromFileAction.cs" />
<Compile Include="Action\Case\ReduceCaseAction.cs" />
<Compile Include="Action\Case\GroupCaseAction.cs" />
<Compile Include="Action\Case\SplitCaseAction.cs" />
Expand Down
15 changes: 14 additions & 1 deletion NBi.genbiL/Parser/Case.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ from filename in Grammar.QuotedTextual
select new LoadCaseFromFileAction(filename)
);

readonly static Parser<ICaseAction> caseLoadOptionalFileParser =
(
from load in Keyword.Load
from optional in Keyword.Optional
from loadType in loadTypeFileParser
from filename in Grammar.QuotedTextual
from with in Keyword.With
from columns in Keyword.Columns.Or(Keyword.Column)
from columnNames in Grammar.QuotedRecordSequence
select new LoadOptionalCaseFromFileAction(filename, columnNames)
);

readonly static Parser<ICaseAction> caseLoadQueryFileParser =
(
from load in Keyword.Load
Expand All @@ -50,7 +62,7 @@ from connectionString in Grammar.QuotedTextual
);

readonly static Parser<ICaseAction> caseLoadParser =
caseLoadFileParser.Or(caseLoadQueryFileParser).Or(caseLoadQueryParser);
caseLoadFileParser.Or(caseLoadOptionalFileParser).Or(caseLoadQueryFileParser).Or(caseLoadQueryParser);

readonly static Parser<ICaseAction> caseRemoveParser =
(
Expand Down Expand Up @@ -105,6 +117,7 @@ from text in Grammar.ExtendedQuotedRecordSequence
select new FilterCaseAction(variableName, @operator, text, negation.IsDefined)
);


readonly static Parser<ICaseAction> caseScopeParser =
(
from scope in Keyword.Scope
Expand Down
1 change: 1 addition & 0 deletions NBi.genbiL/Parser/Keyword.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ class Keyword
public static readonly Parser<string> Left = Parse.IgnoreCase("Left").Text().Token();
public static readonly Parser<string> Right = Parse.IgnoreCase("Right").Text().Token();
public static readonly Parser<string> All = Parse.IgnoreCase("All").Text().Token();
public static readonly Parser<string> Optional = Parse.IgnoreCase("Optional").Text().Token();
}
}

0 comments on commit 6baa70a

Please sign in to comment.