Files
PowerShell/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Csv.Tests.ps1
T

73 lines
2.8 KiB
PowerShell
Raw Normal View History

2016-07-25 14:01:41 -07:00
Describe "Import-Csv" -Tags "CI" {
$testCsv = Join-Path -Path (Join-Path $PSScriptRoot -ChildPath assets) -ChildPath TestCsv.csv
2015-10-20 12:17:42 -07:00
It "Should be able to call without error" {
2016-05-22 23:34:14 -07:00
{ Import-Csv $testCsv } | Should Not Throw
}
2015-10-20 12:17:42 -07:00
It "Should be able to assign to a variable" {
2016-05-22 23:34:14 -07:00
$actual = Import-Csv $testCsv
2015-10-20 12:17:42 -07:00
2016-05-22 23:34:14 -07:00
$actual | Should Not BeNullOrEmpty
$actual.GetType().BaseType | Should Be array
2015-10-20 12:17:42 -07:00
}
It "Should have the data from the csv file" {
2016-05-22 23:34:14 -07:00
$actualContent = $(Get-Content $testCsv)[0]
$testContent = $($(Import-Csv $testCsv) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 1
2015-10-20 12:17:42 -07:00
2016-05-22 23:34:14 -07:00
$actualContent.IndexOf($testContent) | Should BeGreaterThan -1
2015-10-20 12:17:42 -07:00
}
It "Should be able to prepend a custom header" {
2016-05-22 23:34:14 -07:00
$header = "test1","test2","test3"
2015-10-20 12:17:42 -07:00
2016-05-22 23:34:14 -07:00
$originalContent = $($(Import-Csv $testCsv) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 1
2015-10-20 12:17:42 -07:00
2016-05-22 23:34:14 -07:00
$testContent = $($(Import-Csv $testCsv -Header $header) | Get-Member) | ? { $_.MemberType -eq "NoteProperty" } | % { $_.Name } | Select-Object -First 3
2015-10-20 12:17:42 -07:00
2016-05-22 23:34:14 -07:00
# the original csv file doesn't contain the headers
$originalContent.IndexOf($header[0]) | Should Be -1
2015-10-20 12:17:42 -07:00
2016-05-22 23:34:14 -07:00
# 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]
}
}
2015-10-20 12:17:42 -07:00
2016-07-25 14:01:41 -07:00
Describe "Import-Csv DRT Unit Tests" -Tags "CI" {
2016-06-01 19:55:50 -07:00
BeforeAll {
$fileToGenerate = Join-Path $TestDrive -ChildPath "importCSVTest.csv"
$psObject = [pscustomobject]@{ "First" = "1"; "Second" = "2" }
}
2016-05-22 23:34:14 -07:00
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
}
2015-10-20 12:17:42 -07:00
2016-05-22 23:34:14 -07:00
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
}
2015-10-20 12:17:42 -07:00
}