|
| 1 | +#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" } |
| 2 | +param( |
| 3 | + $ModuleName = "dbatools", |
| 4 | + $CommandName = "Get-BulkRowsCopiedCount", |
| 5 | + $PSDefaultParameterValues = $TestConfig.Defaults |
| 6 | +) |
| 7 | + |
| 8 | +Describe $CommandName -Tag UnitTests { |
| 9 | + Context "Reading the rows copied counter" { |
| 10 | + It "Returns the counter value below Int32.MaxValue" { |
| 11 | + InModuleScope "dbatools" { |
| 12 | + $bulkCopy = New-Object -TypeName Microsoft.Data.SqlClient.SqlBulkCopy -ArgumentList "Server=dummy" |
| 13 | + $rowsCopiedField = [Microsoft.Data.SqlClient.SqlBulkCopy].GetField("_rowsCopied", [Reflection.BindingFlags]"NonPublic,GetField,Instance") |
| 14 | + $rowsCopiedField.SetValue($bulkCopy, [long]100000) |
| 15 | + |
| 16 | + Get-BulkRowsCopiedCount $bulkCopy | Should -Be 100000 |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + It "Returns the counter value above Int32.MaxValue" { |
| 21 | + # The field is an Int64 in current Microsoft.Data.SqlClient. A cast to [int] throws above |
| 22 | + # [int32]::MaxValue, the catch then returned -1, and the callers took that for a wrapped |
| 23 | + # counter and inflated the reported total by billions of rows (#10675). |
| 24 | + InModuleScope "dbatools" { |
| 25 | + $bulkCopy = New-Object -TypeName Microsoft.Data.SqlClient.SqlBulkCopy -ArgumentList "Server=dummy" |
| 26 | + $rowsCopiedField = [Microsoft.Data.SqlClient.SqlBulkCopy].GetField("_rowsCopied", [Reflection.BindingFlags]"NonPublic,GetField,Instance") |
| 27 | + $rowsCopiedField.SetValue($bulkCopy, [long]3000000000) |
| 28 | + |
| 29 | + Get-BulkRowsCopiedCount $bulkCopy | Should -Be 3000000000 |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + It "Keeps the total of the callers correct above Int32.MaxValue" { |
| 34 | + # The pattern of Copy-DbaDbTableData, Import-DbaCsv and Import-DbaParquet: the running total |
| 35 | + # from the copy notifications, plus the adjustment for the rows after the last notification. |
| 36 | + InModuleScope "dbatools" { |
| 37 | + $bulkCopy = New-Object -TypeName Microsoft.Data.SqlClient.SqlBulkCopy -ArgumentList "Server=dummy" |
| 38 | + $rowsCopiedField = [Microsoft.Data.SqlClient.SqlBulkCopy].GetField("_rowsCopied", [Reflection.BindingFlags]"NonPublic,GetField,Instance") |
| 39 | + $rowsCopiedField.SetValue($bulkCopy, [long]3000000000) |
| 40 | + |
| 41 | + $totalRowsCopied = [long]2999995000 |
| 42 | + $prevRowsCopied = [long]2999995000 |
| 43 | + $finalRowCountReported = Get-BulkRowsCopiedCount $bulkCopy |
| 44 | + if ($finalRowCountReported -ge 0) { |
| 45 | + $totalRowsCopied += (Get-AdjustedTotalRowsCopied -ReportedRowsCopied $finalRowCountReported -PreviousRowsCopied $prevRowsCopied).NewRowCountAdded |
| 46 | + } |
| 47 | + |
| 48 | + $totalRowsCopied | Should -Be 3000000000 |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments