one this is joseph from stardataengineering.com in this video i want to talk about how you can run unit tests on your sql transforms with dbt as always if you'd like to follow this along in the blog format i'll leave the link in the description below so let's get started first of all if you're not familiar with what dbt is i encourage you to check out the video i'll leave the link in the description below as well so if you're wondering how you could implement unit tests on a sql transformation too like dvd or how you
could create your test data and test them only locally in your development machine and not in production environments then follow along so if you'd like to code along i encourage you to install docker get dpt and psql command line tools you could also follow along without typing down the code so what we are going to do is we are going to clone this github repository its open so you can just go ahead and clone it and we step into the github repository now you have these helper scripts you make them executable change modifier grants read
write execute access to the user which is you just for ease we can set our password as an environment variable so the following scripts don't keep asking for password or not again then let's run setup script the setup script basically sets up a production database and a development database and creates a base grading events table in both production and development databases and it also fills up some fake data for the production database to um reflect what you might have in a real life scenario so let's look at the pipeline the pipeline is very simple you
have a base greeting events table you transform it into fat grading events table in dvt the concept of testing happens after the transformation is done this is very different from tdd-based approaches in software programming where you write the test cases and you can test the execution of your function against them but when you think about sql and databases you can check the output only after the transformation has been done it's it's a permanent transformation so in in this video you will see how you could use a mock input data in place of base evergreen events
and you will compare the results of fact greeting events table to an expected output just to make sure that your transformation is successful so let's see how that can be done so in the github repository you already have all the code set up the first thing you want to look at is this sql script let's take a look at that so as you can see you have this complex template here so dbt allows you to use something called a ginger template which allows you to do conditional loops control statements within your sql script what we
are doing here is if your target target is development environment so if you're running it on development environment it chooses data from input based greeting events model but if it's not development environment it just uses brace greeting events model so what is input based greeting events so in dbd there's the concept of c data where you could have raw data and load it in as a table and the structure will be informed by dvt so if we take a look at the data table you will see what this what this input data is is basically
just a marker input data and we also have the mock output data in there which we will use later in our code and the second part is creating a custom macro so a macro is a sql script that you can run in different places by calling it using ginger templates there are two types of tests in dvd one is schema test the other one is data test schema tests are basically queries that return zero if it's true or any number greater than zero if it's false and data tests are similar but instead of number it
returns zero records if it returns more than one record it's it's a false so let's take a look at the macro we have the macro we have is available in the macros folder and we call it test equality sql so let's take a look at that so let's do cad all right so it's pretty straightforward script you can assume you can think of macro as a function so we are defining a function called test equality and then we are getting input parameters which are available through these keyword arguments and then we are doing an if
statement and if we are we are checking that the environment that it's currently running on matches the environment that has given us an input to this macro and then we run this sql query if it's not the environment that is provided through the input parameter we just return zero and remember select zero is a true for schema tests which are test equality macro is so let's take a look at the parsed sql it's a little bit easier to read so what does this do it selects star from one of the input tables except and doesn't
accept with the other input table and vice versa what this script does is basically checks if both the tables have equal num have the same rows it doesn't the sorting order doesn't matter because we are doing an accept and a union and again this is available in the blog feel free to go there and look at the code so now that we have seen how to create the macro we need to use this macro to test the output of our transformation so let's see how to do this in dbt you define your test within a
schema the yaml file which is near your model file our model file is basically a select statement so let's take a look at the schema file so we are testing fact reading events model which is also in the models mods folder so if you see that you can see you have five grading events which is a select statement so you can see we are testing that model we have some uniqueness and not null constraints on the id and then we use the equality test so no see here how we use unit test dvd.equality the unit
sdbd is the name of our project which is available in dvd project.aml you see here and then the equality is the name of the sql script we have here it and then what we say is we give it the input parameters which are compare model and environment so that's how we specify in dev environment get the output from this table and ensure that that is same as the expected transform grading image and again expected transform grading event is just a mock output data we have in the data folder location so that's basically it so let's
go ahead and run this using the run dbt command so what this run dbt command does is it debugs to check if connections are working it loads in the seed data um which is which is the data available in the data folder into your data warehouse and we only do this for oh sorry about that we'll see that in a minute and we only do the seed loading for our development database because we don't need to load these um mock input and output data in our production database and then we run the transformations using run
and then we test it which basically runs these fighters you can see on your left side after tests are done we generate documents and we serve them basically starts a web server where you can see the documentation so let's take a look at that so here you can see your macros your models and now fact events grading events is the model we want to test so let's look at the lineage you can see how we use the input base greeting events transform to staging and finally fact you can also take a look at the sql
query thereby you that was used to test for equality so basically it takes the source and passes it out into the corresponding sql so this worked as expected so that's how you run dvd locally and you could easily let me cancel this you could easily run dbt for the production environment using run dbt prod when we run run dbt prod it basically runs the same debug run test generate serve commands but against the production database note that it does not do the seeding because in production we don't need to test our mock input versus mock
output so again similarly you could go to models and this is in production you could go to models factor greens if you check the tes you will see that it just does a select zero because we have the conditional statement in our macro which just says select zero if no environment is uh given you could also look at the lineage graph you will see that it originates from base greeting events whereas in our production or sorry development database it originates from input-based grading events so you can see the difference there so instead of using a
mock input we're using the actual input available in our production database let's go ahead and stop this web server you can tear down your docker instances using the start down script and that is pretty much it i hope this article gives you a good idea of how you could start doing unit testing based with the sql transform based tools like dbt what this enables is it helps you to keep your development cycles much shorter you don't have to open up a pr and test in a ci environment you could do this locally which ensures that
the code here which make sure that the code you're pushing is of high quality and does not have any logic issues in it so considered writing a unit test the next time you're using doing an elt pipeline and dbt there are other approaches for unit destination sql such as setting up a local environment using python and writing a script to automate all of this but this would be a good place to start and you want to keep evolving and changing as your testing patterns and data testing needs grow so that's it for this video if
you have any questions or comments leave them in the comment section below i'll get to them as soon as i can and thank you for listening if you learned something new or found this useful please feel free to like share subscribe all the good stuff it really helps out with the youtube ranking algorithm thank you