From a9cbdf049ddd8daa93af0e1e0f8371f8d151eb9d Mon Sep 17 00:00:00 2001 From: TingLiu6 Date: Sun, 22 May 2016 23:34:14 -0700 Subject: [PATCH 1/2] Add Unit Test for Import-Csv --- test/powershell/Import-Csv.Tests.ps1 | 77 ++++++++++++++++++---------- 1 file changed, 49 insertions(+), 28 deletions(-) diff --git a/test/powershell/Import-Csv.Tests.ps1 b/test/powershell/Import-Csv.Tests.ps1 index bef2865b3..e9422ad66 100644 --- a/test/powershell/Import-Csv.Tests.ps1 +++ b/test/powershell/Import-Csv.Tests.ps1 @@ -2,50 +2,71 @@ Describe "Import-Csv" { $testCsv = Join-Path -Path (Join-Path $PSScriptRoot -ChildPath assets) -ChildPath TestCsv.csv It "Should be able to call without error" { - { Import-Csv $testCsv } | Should Not Throw - } + { Import-Csv $testCsv } | Should Not Throw + } It "Should be able to assign to a variable" { - $actual = Import-Csv $testCsv + $actual = Import-Csv $testCsv - $actual | Should Not BeNullOrEmpty - $actual.GetType().BaseType | Should Be array + $actual | Should Not BeNullOrEmpty + $actual.GetType().BaseType | Should Be array } It "Should have the data from the csv file" { - $actualContent = $(Get-Content $testCsv)[0] - $testContent = $($(Import-Csv $testCsv) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 1 + $actualContent = $(Get-Content $testCsv)[0] + $testContent = $($(Import-Csv $testCsv) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 1 - $actualContent.IndexOf($testContent) | Should BeGreaterThan -1 + $actualContent.IndexOf($testContent) | Should BeGreaterThan -1 } It "Should be able to prepend a custom header" { - $header = "test1","test2","test3" + $header = "test1","test2","test3" - $originalContent = $($(Import-Csv $testCsv) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 1 + $originalContent = $($(Import-Csv $testCsv) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 1 - $testContent = $($(Import-Csv $testCsv -Header $header) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 3 + $testContent = $($(Import-Csv $testCsv -Header $header) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 3 - # the original csv file doesn't contain the headers -$originalContent.IndexOf($header[0]) | Should Be -1 + # the original csv file doesn't contain the headers + $originalContent.IndexOf($header[0]) | Should Be -1 -# but it does with the -Header switch! -$testContent[0] | Should Be $header[0] -$testContent[1] | Should Be $header[1] -$testContent[2] | Should Be $header[2] + # but it does with the -Header switch! + $testContent[0] | Should Be $header[0] + $testContent[1] | Should Be $header[1] + $testContent[2] | Should Be $header[2] + } + + It "Should be able to use the alias without error" { + { Import-Csv $testCsv } | Should Not Throw + } + + It "Should have the same output between the alias and the full cmdlet name" { + $alias = $($(ipcsv $testCsv) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 1 + $cmdlet = $($(Import-Csv $testCsv) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 1 + + $alias[0] | Should Be $cmdlet[0] + $alias[1] | Should Be $cmdlet[1] + $alias[2] | Should Be $cmdlet[2] + + } } -It "Should be able to use the alias without error" { - { Import-Csv $testCsv } | Should Not Throw -} +Describe "Import-Csv DRT Unit Tests" -Tags DRT { -It "Should have the same output between the alias and the full cmdlet name" { - $alias = $($(ipcsv $testCsv) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 1 - $cmdlet = $($(Import-Csv $testCsv) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 1 - - $alias[0] | Should Be $cmdlet[0] - $alias[1] | Should Be $cmdlet[1] - $alias[2] | Should Be $cmdlet[2] + $fileToGenerate = Join-Path $TestDrive -ChildPath "importCSVTest.csv" + $psObject = [pscustomobject]@{ "First" = "1"; "Second" = "2" } + + It "Test import-csv with a delimiter parameter" { + $delimiter = ';' + $psObject | Export-Csv -Path $fileToGenerate -Delimiter $delimiter + $returnObject = Import-Csv -Path $fileToGenerate -Delimiter $delimiter + $returnObject.First | Should Be 1 + $returnObject.Second | Should Be 2 + } -} + It "Test import-csv with UseCulture parameter" { + $psObject | Export-Csv -Path $fileToGenerate -UseCulture + $returnObject = Import-Csv -Path $fileToGenerate -UseCulture + $returnObject.First | Should Be 1 + $returnObject.Second | Should Be 2 + } } From a570b908788b541b09c2ad0a2a910f35fa4cc771 Mon Sep 17 00:00:00 2001 From: TingLiu6 Date: Wed, 1 Jun 2016 19:55:50 -0700 Subject: [PATCH 2/2] Fixed CR issues --- test/powershell/Import-Csv.Tests.ps1 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/powershell/Import-Csv.Tests.ps1 b/test/powershell/Import-Csv.Tests.ps1 index e9422ad66..b7821eaef 100644 --- a/test/powershell/Import-Csv.Tests.ps1 +++ b/test/powershell/Import-Csv.Tests.ps1 @@ -51,9 +51,10 @@ Describe "Import-Csv" { } Describe "Import-Csv DRT Unit Tests" -Tags DRT { - - $fileToGenerate = Join-Path $TestDrive -ChildPath "importCSVTest.csv" - $psObject = [pscustomobject]@{ "First" = "1"; "Second" = "2" } + BeforeAll { + $fileToGenerate = Join-Path $TestDrive -ChildPath "importCSVTest.csv" + $psObject = [pscustomobject]@{ "First" = "1"; "Second" = "2" } + } It "Test import-csv with a delimiter parameter" { $delimiter = ';'