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

Conversion of LINQtoCSV to a universal portable class library, and some ... #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
156 changes: 92 additions & 64 deletions LINQtoCSV.Tests/CsvContextReadTests.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using LINQtoCSV;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Collections.Generic;

namespace LINQtoCSV.Tests
{
[TestClass()]
[TestClass]
public class CsvContextReadTests : Test
{
[TestMethod()]
[TestMethod]
public void GoodFileUsingOutputFormatForParsingDatesCharUSEnglish()
{
// Arrange
Expand All @@ -18,24 +15,28 @@ public void GoodFileUsingOutputFormatForParsingDatesCharUSEnglish()
{
SeparatorChar = ';',
FirstLineHasColumnNames = false,
UseOutputFormatForParsingCsvValue = true,
UseOutputFormatForParsingCsvValue = true,
EnforceCsvColumnAttribute = true, // default is false
FileCultureName = "en-US" // default is the current culture
FileCultureName = "en-US", // default is the current culture
};

string testInput =
"AAAAAAAA;052308" + Environment.NewLine +
"BBBBBBBB;051212" + Environment.NewLine +
"CCCCCCCC;122308";

var expected = new[] {
new ProductDataParsingOutputFormat() {
var expected = new[]
{
new ProductDataParsingOutputFormat
{
name = "AAAAAAAA", startDate = new DateTime(2008, 5, 23),
},
new ProductDataParsingOutputFormat {
name = "BBBBBBBB", startDate = new DateTime(2012, 5, 12),
new ProductDataParsingOutputFormat
{
name = "BBBBBBBB", startDate = new DateTime(2012, 5, 12),
},
new ProductDataParsingOutputFormat {
new ProductDataParsingOutputFormat
{
name = "CCCCCCCC", startDate = new DateTime(2008, 12, 23),
}
};
Expand All @@ -45,7 +46,7 @@ public void GoodFileUsingOutputFormatForParsingDatesCharUSEnglish()
AssertRead(testInput, fileDescription_namesUs, expected);
}

[TestMethod()]
[TestMethod]
public void GoodFileNoSeparatorCharUseOutputFormatForParsingUSEnglish()
{
// Arrange
Expand All @@ -64,14 +65,18 @@ public void GoodFileNoSeparatorCharUseOutputFormatForParsingUSEnglish()
BBBBBBBB10.31105/12/12\n
CCCCCCCC12.00012/23/08";

var expected = new[] {
new ProductDataCharLength() {
var expected = new[]
{
new ProductDataCharLength
{
name = "AAAAAAAA", weight = 34.184, startDate = new DateTime(2008, 5, 23),
},
new ProductDataCharLength {
name = "BBBBBBBB", weight = 10.311, startDate = new DateTime(2012, 5, 12),
new ProductDataCharLength
{
name = "BBBBBBBB", weight = 10.311, startDate = new DateTime(2012, 5, 12),
},
new ProductDataCharLength {
new ProductDataCharLength
{
name = "CCCCCCCC", weight = 12.000, startDate = new DateTime(2008, 12, 23),
}
};
Expand All @@ -81,7 +86,7 @@ public void GoodFileNoSeparatorCharUseOutputFormatForParsingUSEnglish()
AssertRead(testInput, fileDescription_namesUs, expected);
}

[TestMethod()]
[TestMethod]
public void GoodFileNoSeparatorCharUSEnglish()
{
// Arrange
Expand All @@ -100,14 +105,18 @@ public void GoodFileNoSeparatorCharUSEnglish()
BBBBBBBB10.31105/12/12\n
CCCCCCCC12.00012/23/08";

var expected = new[] {
new ProductDataCharLength() {
var expected = new[]
{
new ProductDataCharLength
{
name = "AAAAAAAA", weight = 34.184, startDate = new DateTime(2008, 5, 23),
},
new ProductDataCharLength {
name = "BBBBBBBB", weight = 10.311, startDate = new DateTime(2012, 5, 12),
new ProductDataCharLength
{
name = "BBBBBBBB", weight = 10.311, startDate = new DateTime(2012, 5, 12),
},
new ProductDataCharLength {
new ProductDataCharLength
{
name = "CCCCCCCC", weight = 12.000, startDate = new DateTime(2008, 12, 23),
}
};
Expand All @@ -117,7 +126,7 @@ public void GoodFileNoSeparatorCharUSEnglish()
AssertRead(testInput, fileDescription_namesUs, expected);
}

[TestMethod()]
[TestMethod]
public void GoodFileCommaDelimitedUseFieldIndexForReadingDataCharUSEnglish()
{
// Arrange
Expand All @@ -137,14 +146,18 @@ public void GoodFileCommaDelimitedUseFieldIndexForReadingDataCharUSEnglish()
"BBBBBBBB,__,10.311,05/12/12" + Environment.NewLine +
"CCCCCCCC,__,12.000,12/23/08";

var expected = new[] {
new ProductDataSpecificFieldIndex() {
var expected = new[]
{
new ProductDataSpecificFieldIndex
{
name = "AAAAAAAA", weight = 34.184, startDate = new DateTime(2008, 5, 23),
},
new ProductDataSpecificFieldIndex {
name = "BBBBBBBB", weight = 10.311, startDate = new DateTime(2012, 5, 12),
new ProductDataSpecificFieldIndex
{
name = "BBBBBBBB", weight = 10.311, startDate = new DateTime(2012, 5, 12),
},
new ProductDataSpecificFieldIndex {
new ProductDataSpecificFieldIndex
{
name = "CCCCCCCC", weight = 12.000, startDate = new DateTime(2008, 12, 23),
}
};
Expand All @@ -154,7 +167,7 @@ public void GoodFileCommaDelimitedUseFieldIndexForReadingDataCharUSEnglish()
AssertRead(testInput, fileDescription_namesUs, expected);
}

[TestMethod()]
[TestMethod]
public void GoodFileCommaDelimitedUseFieldIndexForReadingDataCharUseOutputFormatForParsingUSEnglish()
{
// Arrange
Expand All @@ -176,14 +189,18 @@ public void GoodFileCommaDelimitedUseFieldIndexForReadingDataCharUseOutputFormat
"BBBBBBBB,__,10.311,05/12/12" + Environment.NewLine +
"CCCCCCCC,__,12.000,12/23/08";

var expected = new[] {
new ProductDataSpecificFieldIndex() {
var expected = new[]
{
new ProductDataSpecificFieldIndex
{
name = "AAAAAAAA", weight = 34.184, startDate = new DateTime(2008, 5, 23),
},
new ProductDataSpecificFieldIndex {
name = "BBBBBBBB", weight = 10.311, startDate = new DateTime(2012, 5, 12),
new ProductDataSpecificFieldIndex
{
name = "BBBBBBBB", weight = 10.311, startDate = new DateTime(2012, 5, 12),
},
new ProductDataSpecificFieldIndex {
new ProductDataSpecificFieldIndex
{
name = "CCCCCCCC", weight = 12.000, startDate = new DateTime(2008, 12, 23),
}
};
Expand All @@ -193,8 +210,7 @@ public void GoodFileCommaDelimitedUseFieldIndexForReadingDataCharUseOutputFormat
AssertRead(testInput, fileDescription_namesUs, expected);
}


[TestMethod()]
[TestMethod]
public void GoodFileCommaDelimitedNamesInFirstLineUSEnglish()
{
// Arrange
Expand All @@ -216,21 +232,25 @@ two newlines
and a quoted """"string""""""
dog house, ""45,230,990"",29 Feb 2004, , -56, True,"""", FF10, ""12,008""";

var expected = new [] {
new ProductData {
var expected = new[]
{
new ProductData
{
name = "moonbuggy", weight = 34.184, startDate = new DateTime(2008, 5, 23), launchTime = new DateTime(2009, 5, 5, 16, 11, 0),
nbrAvailable = 1205, onsale = true, shopsAvailable = "Paris, New York", hexProductCode = 31, retailPrice = 540.12M,
description = "newly launched product"
},
new ProductData {
new ProductData
{
name = "mouse trap", weight = 45E-5, startDate = new DateTime(1985, 1, 2), launchTime = new DateTime(1988, 8, 7, 0, 0, 0),
nbrAvailable = 4030, onsale = false, shopsAvailable = @"This field has
a newline", hexProductCode = 256, retailPrice = 78300M,
description = @"This field has quotes(""), and
two newlines
and a quoted ""string"""
},
new ProductData {
new ProductData
{
name = "dog house", weight = 45230990, startDate = new DateTime(2004, 2, 29), launchTime = default(DateTime),
nbrAvailable = -56, onsale = true, shopsAvailable = "", hexProductCode = 65296, retailPrice = 12008M,
description = null
Expand All @@ -242,7 +262,7 @@ and a quoted ""string"""
AssertRead(testInput, fileDescription_namesUs, expected);
}

[TestMethod()]
[TestMethod]
public void GoodFileTabDelimitedNoNamesInFirstLineNLnl()
{
// Arrange
Expand All @@ -257,21 +277,25 @@ public void GoodFileTabDelimitedNoNamesInFirstLineNLnl()

string testInput =
"moonbuggy\t 23/5/08\t 5-Mei-2009 16:11 pm\t 34.184\t \"Paris, New York\"\t 1F\t €540,12\t true\t newly launched product\r\n\"mouse trap\"\t 2/1/1985\t \"7 Augustus 1988\t 0:00\"\t45E-5\t \"This field has\r\na newline\"\t 100\t \"€78.300\"\t FALSE\t \"This field has quotes(\"\"), and\r\ntwo newlines\r\nand a quoted \"\"string\"\"\"\r\ndog house\t29 Feb 2004\t \t \"45.230.990\"\t\"\"\t FF10\t \"12.008\"\t True";
var expected = new[] {
new ProductData {
var expected = new[]
{
new ProductData
{
name = "moonbuggy", weight = 34184, startDate = new DateTime(2008, 5, 23), launchTime = new DateTime(2009, 5, 5, 16, 11, 0),
nbrAvailable = 0, onsale = true, shopsAvailable = "Paris, New York", hexProductCode = 31, retailPrice = 540.12M,
description = "newly launched product"
},
new ProductData {
new ProductData
{
name = "mouse trap", weight = 45E-5, startDate = new DateTime(1985, 1, 2), launchTime = new DateTime(1988, 8, 7, 0, 0, 0),
nbrAvailable = 0, onsale = false, shopsAvailable = @"This field has
a newline", hexProductCode = 256, retailPrice = 78300M,
description = @"This field has quotes(""), and
two newlines
and a quoted ""string"""
},
new ProductData {
new ProductData
{
name = "dog house", weight = 45230990, startDate = new DateTime(2004, 2, 29), launchTime = default(DateTime),
nbrAvailable = 0, onsale = true, shopsAvailable = "", hexProductCode = 65296, retailPrice = 12008M,
description = null
Expand All @@ -283,7 +307,7 @@ and a quoted ""string"""
AssertRead(testInput, fileDescription_nonamesNl, expected);
}

[TestMethod()]
[TestMethod]
public void GoodFileCommaDelimitedWithTrailingSeparatorChars()
{
// Arrange
Expand All @@ -306,21 +330,25 @@ two newlines
and a quoted """"string""""""
dog house, ""45,230,990"",29 Feb 2004, , -56, True,"""", FF10, ""12,008"",";

var expected = new[] {
new ProductData {
var expected = new[]
{
new ProductData
{
name = "moonbuggy", weight = 34.184, startDate = new DateTime(2008, 5, 23), launchTime = new DateTime(2009, 5, 5, 16, 11, 0),
nbrAvailable = 1205, onsale = true, shopsAvailable = "Paris, New York", hexProductCode = 31, retailPrice = 540.12M,
description = "newly launched product"
},
new ProductData {
new ProductData
{
name = "mouse trap", weight = 45E-5, startDate = new DateTime(1985, 1, 2), launchTime = new DateTime(1988, 8, 7, 0, 0, 0),
nbrAvailable = 4030, onsale = false, shopsAvailable = @"This field has
a newline", hexProductCode = 256, retailPrice = 78300M,
description = @"This field has quotes(""), and
two newlines
and a quoted ""string"""
},
new ProductData {
new ProductData
{
name = "dog house", weight = 45230990, startDate = new DateTime(2004, 2, 29), launchTime = default(DateTime),
nbrAvailable = -56, onsale = true, shopsAvailable = "", hexProductCode = 65296, retailPrice = 12008M,
description = null
Expand All @@ -332,17 +360,18 @@ and a quoted ""string"""
AssertRead(testInput, fileDescription_namesUs, expected);
}

[TestMethod()]
public void FileWithUnknownColumns_ShouldDiscardColumns() {
[TestMethod]
public void FileWithUnknownColumns_ShouldDiscardColumns()
{
var description = new CsvFileDescription
{
SeparatorChar = ',',
FirstLineHasColumnNames = true,
IgnoreUnknownColumns = true,
};
{
SeparatorChar = ',',
FirstLineHasColumnNames = true,
IgnoreUnknownColumns = true,
};

//The following input has 5 columns: Id | Name | Last Name | Age | City. Only the Name, Last Name and Age will be read.

string input =
@"Id,Name,Last Name,Age,City
1,John,Doe,15,Washington
Expand All @@ -365,7 +394,6 @@ public void FileWithUnknownColumns_ShouldDiscardColumns() {
};

AssertRead(input, description, expected);

}
}
}
}
8 changes: 3 additions & 5 deletions LINQtoCSV.Tests/CsvContextWriteTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using LINQtoCSV;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace LINQtoCSV.Tests
{
[TestClass()]
[TestClass]
public class CsvContextWriteTests : Test
{
[TestMethod()]
[TestMethod]
public void GoodFileCommaDelimitedNamesInFirstLineNLnl()
{
// Arrange
Expand Down
Loading