Skip to content

Commit

Permalink
Add test for dupes w/2 IDs and >2 objects
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteLoser committed Nov 19, 2017
1 parent 4b3cdee commit ab2436d
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions MergeCsv.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Describe "Merge-Csv" {
}

It "Merges two simple objects with three IDs correctly" {

$EmailObjects = @([PSCustomObject] @{
Username = "John"
Email = "john@example.com"
Expand All @@ -61,9 +62,11 @@ Describe "Merge-Csv" {
ConvertTo-Json -Depth 100 -Compress) 3> $null) -eq `
'[{"Username":"Jane","Email":"jane@example.com","Department":"IT"},{"Username":"Janet","Email":"janet@maintexample.com","Department":"Maintenance"},{"Username":"John","Email":"john@example.com","Department":"HR"}]' |
Should -Be $True

}

It "Merges two simple CSV files with three IDs correctly" {

$FirstPath, $SecondPath = "simplecsv1.csv", "simplecsv2.csv" |
ForEach-Object {
InternalTestPathCSV -FilePath $_
Expand All @@ -74,9 +77,11 @@ Describe "Merge-Csv" {
ConvertTo-Json -Depth 100 -Compress) -eq `
'[{"Username":"Jane","Email":"jane@example.com","Department":"IT"},{"Username":"Janet","Email":"janet@maintexample.com","Department":"Maintenance"},{"Username":"John","Email":"john@example.com","Department":"HR"}]' |
Should -Be $True

}

It "Merges three somewhat complex CSV files with two IDs properly" {

$FirstPath, $SecondPath, $ThirdPath = "csv1.csv", "csv2.csv", "csv3.csv" |
ForEach-Object {
InternalTestPathCSV -FilePath $_
Expand All @@ -88,9 +93,11 @@ Describe "Merge-Csv" {
'[{"ComputerName":"ServerA","Uh":"UhA","OSFamily":"Windows","Env":"Production","PSVer":"5.1","OrderFile3":"1"},{"ComputerName":"ServerB","Uh":"UhB","OSFamily":"Windows","Env":"Test","PSVer":"5.1","OrderFile3":"5"}]' |
Should -Be $True
$Warnings.Count | Should -Be 9

}

It "Merges three somewhat complex CSV files with two IDs properly, with -AllowDuplicates" {

$FirstPath, $SecondPath, $ThirdPath = "csv1.csv", "csv2.csv", "csv3.csv" |
ForEach-Object {
InternalTestPathCSV -FilePath $_
Expand All @@ -101,9 +108,11 @@ Describe "Merge-Csv" {
'[{"ComputerName":"ServerA","Uh":"UhA","OSFamily":"Windows","Env":"Production","PSVer":"5.1","OrderFile3":"1"},{"ComputerName":"ServerA","Uh":"UhA","OSFamily":"Linux","Env":"Test","PSVer":"6.0","OrderFile3":"2"},{"ComputerName":"ServerA","Uh":"UhA","OSFamily":null,"Env":"Production","PSVer":"3.0","OrderFile3":"3"},{"ComputerName":"ServerA","Uh":"UhA","OSFamily":null,"Env":null,"PSVer":null,"OrderFile3":"4"},{"ComputerName":"ServerB","Uh":"UhB","OSFamily":"Windows","Env":"Test","PSVer":"5.1","OrderFile3":"5"},{"ComputerName":"ServerB","Uh":"UhB","OSFamily":"Linux","Env":"Legacy","PSVer":"6.0","OrderFile3":"6"}]' |
Should -Be $True
$Warnings.Count | Should -Be 0

}

It "Correctly warns about duplicates, without -AllowDuplicates" {

$Object1 = @([PSCustomObject] @{
Username = "Repeated"
foo = "bar"
Expand All @@ -125,6 +134,7 @@ Describe "Merge-Csv" {
}

It "Correctly warns about duplicates with two ID fields, without -AllowDuplicates" {

$Object1 = @([PSCustomObject] @{
Username = "Repeated"
ID2 = "a"
Expand All @@ -148,4 +158,35 @@ Describe "Merge-Csv" {

}

It "Correctly warns about duplicates with two ID fields and >2 objects" {

$Object1 = @([PSCustomObject] @{
Username = "Repeated"
ID2 = "a"
foo = "bar"
}, [PSCustomObject] @{
Username = "Repeated"
ID2 = "a"
foo = "barf"
})
$Object2 = [PSCustomObject] @{
Username = "Repeated"
ID2 = "a"
bar = "foo"
}
$Object3 = [PSCustomObject] @{
Username = "Repeated"
ID2 = "a"
baz = "boo"
}

# Check position 3 is reported correctly.
(Merge-Csv -InputObject $Object3, $Object2, $Object1 -Identity Username, ID2 -WarningVariable Warnings) 3> $null | Out-Null
$Warnings | Should -Match "Duplicate identifying \(shared column\(s\) ID\) entry found in CSV data/file 3: Repeated, a"
# Check in other order.
(Merge-Csv -InputObject $Object2, $Object1, $Object3 -Identity Username, ID2 -WarningVariable Warnings) 3> $null | Out-Null
$Warnings | Should -Match "Duplicate identifying \(shared column\(s\) ID\) entry found in CSV data/file 2: Repeated, a"

}

}

0 comments on commit ab2436d

Please sign in to comment.