Previously we successfully prepared tests for variables and parameters using
ParameterCommand. Now it’s time to communicate with the database, and for that, I will use connection manager defined on the project level. I know from the ssisUnit tutorials it works perfect with package connection managers, so it’s time to verify it against the projects. I will test the package
10_ProjectCM.dtsx– it is just getting a single value from the table in a database and storing it in a variable. All the packages and unit tests are on my GitHub.
The package contains three SQL Tasks: the first just checks if we can communicate with the database using
SELECT 1statement, the second gets the information from the table, and the third repeats the second on the container level.
Click through for the tests.
Today we updated the HADR tests to add the capability to test multiple availability groups and fix a couple of bugs
Once you have installed dbachecks you will need to set some configuration so that you can perform the tests. You can see all of the configuration items and their values using
1 Get-DbcConfig | Out-GridView
Read on for more about these updates.
The result shows 1 test run, 1 test passed, 2 asserts run, 2 asserts passed.
Wait, what? We have prepared only one assert, why does it show two?
The second assert is: “Task Completed: Actual result (Success) was equal to the expected result (Success).“. Great. Where does it come from? Let’s find out.
This is a nice introduction to the topic; if you fuss about with SSIS packages, you should check this out.
While tweaking my Invoke-DbcCheck the list of -ExcludeCheck checks keeps growing and growing.
1 Invoke-DbcCheck -SqlInstance $Servers -ComputerName $Servers -Check $_ -ExcludeDatabase ReportServer, ReportServerTempDB -ExcludeCheck TestLastBackup, TestLastBackupVerifyOnly, LinkedServerConnection, SPN, MaintenanceSolution, SaRenamed, LastGoodCheckDb, LogShipping, InvalidDatabaseOwner -PassThru | Update-DbcPowerBiDataSource -Environment Production
Sure does make for a long command line to scroll thru.
Click through to see how to save these excluded checks in a configuration file.
The method ‘ testOperation ‘ takes the output of the operation performed on the ‘inputPair’ and check whether it is equal to the ‘outputPair’ and just like this, we can test our business logic.
This short snippet lets you test your business logic without forcing you to create even a Spark session. You can mock the whole streaming environment and test your business logic easily.
This was a simple example of unary operations on DStreams. Similarly, we can test binary operations and window operations on DStreams.
Click through for an example with code.
This release adds the Because parameter to the all assertions. This means that you can add a reason why the test has failed. As JAKUB JAREŠ writes here
Reasons force you think more
Reasons document your intent
Reasons make your TestCases clearer
Click through for examples galore. 4.2.1 should have a -Parent macro which inputs -Because “I said so”.
So what’s the problem? How to execute streaming logic in a test environment.
We can write Integration test cases and provide the actual environment in the integration test. But for unit testing, we need a testing environment which should not depend on any external application.
Click through for the example.
The base return is the TSQLFragment object, which in turn has a Batches object, which in turn holds… well it can hold a lot of different things. When the text is parsed, it will determine what type of object to return based on the statement it determines it is. For example, if it’s an insert statement it will be a certain type of object with a given set of properties and methods, and if it’s, say, a create index statement you’ll get different properties, such as which table or view is getting the index along with the indexed columns and included columns. It really is interesting.
But interesting can a double-edged sword: since the statement object that gets returned can be different for each parsed piece of code, that means to set up any type of intelligence around the stuff we’re dealing with, we need to check for very specific objects.
Unfortunately, I never got past the first animated GIF, whose subtitle was wrong. You, however, should read the whole thing.
The problem with Test Cases is that we can only easily loop through one collection, but as Pester is just PowerShell we can simply use ForEach if we wanted to loop through multiple ones, like instances and then databases.
I like to use the ForEach method as it is slightly quicker than other methods. It will only work with PowerShell version 4 and above. Below that version you need to pipe the collection to For-EachObject.
Check it out for a good explanation of running groups of Powershell tests.
When your tests start to know too much about the internals of the system under test (SUT), that leads to false positives during refactoring. Which means they won’t act as a safety net anymore and instead will impede your refactoring efforts because of the necessity to refactor them along with the implementation details they are bound to. Basically, they will stop fulfilling their main objective: providing you with the confidence in code correctness.
When it comes to unit testing, you need to follow this one rule: test only the public API of the SUT, don’t expose its implementation details in order to enable unit testing. Your tests should use the SUT the same way its regular clients do, don’t give them any special privileges. Here you can read more about what an implementation detail is and how it is different from public API: link.
In the database world, this is one reason why I like using stored procedures: they give the equivalent of a public API for database code, so you can write tests for them.