This is the nineteenth part of the SQLxD series. For your convenience you can find other parts in the table of contents in Part 1 – XML Transformation
Last time we implemented expressions, it is high time to test them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
using Model; using NUnit.Framework; using QueryLogic.Expressions.CellExpressions; namespace QueryLogic.Test.Expressions.CellExpressions { internal class GetOriginalCellExpressionTests { [Test] public void Calculate_ShouldReturnOriginalCell() { //Arrange var columnHeader = new ColumnHeader("schema", "Column1"); const string cellValue = "value1"; var cell = new Cell(columnHeader, cellValue); ICellExpression cellExpression = new GetOriginalCellCellExpression(columnHeader); //Act Cell result = cellExpression.Calculate(new Row(new []{cell})); // Assert Assert.That(result, Is.EqualTo(cell)); } [Test] public void Calculate_NullCellPassed_ShouldReturnNull() { //Arrange var columnHeader = new ColumnHeader("schema", "Column1"); var cell = new Cell(columnHeader, null); ICellExpression cellExpression = new GetOriginalCellCellExpression(columnHeader); //Act Cell result = cellExpression.Calculate(new Row(new[] { cell })); // Assert Assert.That(result.Value, Is.Null); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System; using System.Globalization; using Model; using NUnit.Framework; using QueryLogic.Expressions.CellExpressions; namespace QueryLogic.Test.Expressions.CellExpressions { [TestFixture] public class GetDatePartCellExpressionTests { [Test] [TestCase(DatePart.Day, "1")] [TestCase(DatePart.Month, "2")] [TestCase(DatePart.Year, "2013")] [TestCase(DatePart.Hour, "4")] [TestCase(DatePart.Minute, "5")] [TestCase(DatePart.Second, "6")] public void Calculate_DatePassed_ShouldReturnCorrectPart(DatePart part, string expectedValue) { //Arrange var columnHeader = new ColumnHeader("schema", "Column1"); string cellValue = new DateTime(2013, 2, 1, 4, 5, 6).ToString(CultureInfo.InvariantCulture); var cell = new Cell(columnHeader, cellValue); ICellExpression cellExpression = new GetDatePartCellExpression(columnHeader, part); var expectedCell = new Cell(columnHeader, expectedValue); //Act Cell result = cellExpression.Calculate(new Row(new[] { cell })); // Assert Assert.That(result, Is.EqualTo(expectedCell)); } [Test] [ExpectedException(typeof(InvalidOperationException))] public void Calculate_NotDatePassed_ShouldThrowException() { //Arrange var columnHeader = new ColumnHeader("schema", "Column1"); const string cellValue = "abcd"; var cell = new Cell(columnHeader, cellValue); ICellExpression cellExpression = new GetDatePartCellExpression(columnHeader, DatePart.Year); //Act Cell result = cellExpression.Calculate(new Row(new[] { cell })); // Assert } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
using Model; using NUnit.Framework; using QueryLogic.Expressions.CellExpressions; namespace QueryLogic.Test.Expressions.CellExpressions { [TestFixture] public class CatenateStringCellExpressionTests { [Test] public void Calculate_EmptyColumnHeaderPassed_ShouldReturnCatenationWithEmptyColumnHeader() { //Arrange var columnHeader = new ColumnHeader("schema", "Column1"); const string cellValue = "value1"; var cell = new Cell(columnHeader, cellValue); ICellExpression cellExpression = new CatenateStringCellExpression(columnHeader, "aaa"); var expectedCell = new Cell(columnHeader, "value1aaa"); //Act Cell result = cellExpression.Calculate(new Row(new[] { cell })); // Assert Assert.That(result, Is.EqualTo(expectedCell)); } [Test] public void Calculate_NullValuePassed_ShouldTreatAsEmptyString() { //Arrange var columnHeader = new ColumnHeader("schema", "Column1"); var cell = new Cell(columnHeader, null); ICellExpression cellExpression = new CatenateStringCellExpression(columnHeader, "AAA"); var expectedCell = new Cell(columnHeader, "AAA"); //Act Cell result = cellExpression.Calculate(new Row(new[] { cell })); // Assert Assert.That(result, Is.EqualTo(expectedCell)); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using Model; using NUnit.Framework; using QueryLogic.Expressions.CellExpressions; namespace QueryLogic.Test.Expressions.CellExpressions { internal class GetLengthCellExpressionTests { [Test] public void Calculate_EmptyColumnHeaderPassed_ShouldReturnCellLengthWithEmptyColumnHeader() { //Arrange var columnHeader = new ColumnHeader("schema", "Column1"); const string cellValue = "value1"; var cell = new Cell(columnHeader, cellValue); ICellExpression cellExpression = new GetLengthCellExpression(columnHeader); var expectedCell = new Cell(columnHeader, "6"); //Act Cell result = cellExpression.Calculate(new Row(new []{cell})); // Assert Assert.That(result, Is.EqualTo(expectedCell)); } [Test] public void Calculate_NullCellPassed_ShouldReturnNull() { //Arrange var columnHeader = new ColumnHeader("schema", "Column1"); var cell = new Cell(columnHeader, null); ICellExpression cellExpression = new GetLengthCellExpression(columnHeader); //Act Cell result = cellExpression.Calculate(new Row(new[] { cell })); // Assert Assert.That(result.Value, Is.Null); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using Model; using NUnit.Framework; using QueryLogic.Expressions.CellExpressions; namespace QueryLogic.Test.Expressions.CellExpressions { internal class GetSubstringCellExpressionTests { [Test] public void Calculate_ShouldReturnCellWithSubstringValueAndEmptyColumnHeader() { //Arrange var columnHeader = new ColumnHeader("schema", "Column1"); const string cellValue = "value1"; var cell = new Cell(columnHeader, cellValue); ICellExpression cellExpression = new GetSubstringCellExpression(columnHeader, 0, 3); var expectedCell = new Cell(columnHeader, "val"); //Act Cell result = cellExpression.Calculate(new Row(new []{cell})); // Assert Assert.That(result, Is.EqualTo(expectedCell)); } [Test] public void Calculate_NullCellPassed_ShouldReturnNull() { //Arrange var columnHeader = new ColumnHeader("schema", "Column1"); var cell = new Cell(columnHeader, null); ICellExpression cellExpression = new GetSubstringCellExpression(columnHeader, 0, 3); //Act Cell result = cellExpression.Calculate(new Row(new[] { cell })); // Assert Assert.That(result.Value, Is.Null); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using Model; using NUnit.Framework; using QueryLogic.Expressions.CellExpressions; namespace QueryLogic.Test.Expressions.CellExpressions { internal class GetTrimmedCellExpressionTests { [Test] public void Calculate_ShouldReturnCellWithTrimmedValueAndEmptyColumnHeader() { //Arrange var columnHeader = new ColumnHeader("schema", "Column1"); const string cellValue = " value1 "; var cell = new Cell(columnHeader, cellValue); ICellExpression cellExpression = new GetTrimmedCellExpression(columnHeader); var expectedCell = new Cell(columnHeader, "value1"); //Act Cell result = cellExpression.Calculate(new Row(new []{cell})); // Assert Assert.That(result, Is.EqualTo(expectedCell)); } [Test] public void Calculate_NullCellPassed_ShouldReturnNull() { //Arrange var columnHeader = new ColumnHeader("schema", "Column1"); var cell = new Cell(columnHeader, null); ICellExpression cellExpression = new GetTrimmedCellExpression(columnHeader); //Act Cell result = cellExpression.Calculate(new Row(new[] { cell })); // Assert Assert.That(result.Value, Is.Null); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
using Model; using NUnit.Framework; using QueryLogic.Expressions.CellExpressions; using Rhino.Mocks; namespace QueryLogic.Test.Expressions.CellExpressions { internal class ChainedCellExpressionTests { [Test] public void Calucate_EmptyColumnHeaderPassed_ShouldReturnExpressionWithEmptyColumnHeader() { //Arrange var columnHeader = new ColumnHeader("schema", "Column1"); const string cellValue = "value1"; var cell = new Cell(columnHeader, cellValue); var mocks = new MockRepository(); var leftCellExpression = mocks.Stub<ICellExpression>(); var rightCellExpression = mocks.Stub<ICellExpression>(); var cellFromRightExpression = new Cell(new ColumnHeader("", ""), "right" + cell.Value); var cellFromLeftExpression = new Cell(new ColumnHeader("", ""), "left" + cellFromRightExpression.Value); using (mocks.Record()) { rightCellExpression.Calculate(new Row(new []{cell})); LastCall.Return(cellFromRightExpression); leftCellExpression.Calculate(new Row(new[] { cellFromRightExpression })); LastCall.Return(cellFromLeftExpression); } var cellExpression = new ChainedCellExpression(leftCellExpression, rightCellExpression); var expectedCell = new Cell(new ColumnHeader("", ""), "leftrightvalue1"); //Act Cell result = cellExpression.Calculate(new Row(new[] { cell })); // Assert Assert.That(result, Is.EqualTo(expectedCell)); } [Test] public void Calculate_CellIsNull_ShouldReturnNull() { //Arrange var mocks = new MockRepository(); var leftCellExpression = mocks.Stub<ICellExpression>(); var rightCellExpression = mocks.Stub<ICellExpression>(); var columnHeader = new ColumnHeader("schema", "Column1"); var cell = new Cell(columnHeader, null); var cellFromRightExpression = new Cell(columnHeader, null); var cellFromLeftExpression = new Cell(columnHeader, null); using (mocks.Record()) { rightCellExpression.Calculate(new Row(new[] { cell })); LastCall.Return(cellFromRightExpression); leftCellExpression.Calculate(new Row(new[] { cellFromRightExpression })); LastCall.Return(cellFromLeftExpression); } var cellExpression = new ChainedCellExpression(leftCellExpression, rightCellExpression); //Act Cell result = cellExpression.Calculate(new Row(new[] { cell })); // Assert Assert.That(result.Value, Is.Null); } } } |
Having expressions working we can use them in transformers. This is the topic of the next part of this series.