Unit tests are one of the very important tools in our developer arsenal to get peace of mind. But many developers are often neglecting this step, often by lack of time or motivation to have sufficient coverage on their code. This is difficult, because it's one of these features that you miss the most when you really, really need it. For example, unit tests are critically important when a feature of a referenced library changes, because a new version is released. Or, when a portion of code is refactored, to make sure that the rest of the code is not negatively affected, and still works as expected.
In the new video I just posted, my colleague Gwyn "GPS" Peña-Siguenza demonstrates how GitHub Copilot can create a unit test by using the "slash test" command that we already saw in previous posts.
Like in every other profession, there are tasks that we developers love to perform, and others that we don't really feel like doing. Scaffolding a new application is one of these examples, it's a lot of work to start a new project and get it to a point where you can finally be creative. Similarly, writing unit tests can be quite a repetitive task and finding motivation can be difficult. A good unit test should test one specific feature of the code. So, to provide a good coverage, you're going to need a lot of test methods and test classes.
Generative AI is a good tool to perform these repetitive tasks. In the video above, Gwyn shows that being very specific in the prompt will help create very specific test code, which is exactly what we need.
You can learn more about the importance of unit tests, and how to write and run them in Visual Studio in this article.
We published a collection with resources here, and I am updating this with new resources as I discover them. In this collection, you can also explore the previous posts and videos in this series. All the videos referenced are published on the Visual Studio YouTube channel.
As a follow-up on the presentation I did at CloudBrew about Azure Static Web Apps I want to write a series of blog posts.
So far I have shown you 2 different possibilities to integrate an API inside your Azure Static Web App:
If we look at most applications, these API’s are used to read and write data in a database. If this is also the case for your application, Azure Static Web App has a 3th possibility; the database connection feature.
The database connection feature allows you to access a database from your static web app without writing custom server-side code. It is based on the Data API Builder project I talked about in a previous post and allows to create a REST and GraphQL api based on your data source.
Both relational(Azure SQL, MySQL and PostgreSQL) as non-relational (Azure Cosmos DB) databases are supported.
It is the perfect fit if you want an API with simple CRUD operations, built-in authorization, and relationships.
It all starts by creating a swa-db-connections folder at the root of your repository. Inside this folder you should add a staticwebapp.database.config.json
file. This file defines the runtime behavior of the database connection.
Remark: You can use a different folder if you want by changing the data_api_location setting in your build pipeline:
Inside the staticwebapp.database.config.json
file, we can specify if we want to expose both a REST and GraphQL endpoint, which Tables or Entities can be accessed, which operations are supported and so on…
Here is an example where I allow all CRUD actions on a dbo.Product table:
Remark: If you want to learn more on how to configure the Data API Builder, have a look at my previous post and the Data API Builder documentation.
Once the configuration file is in place, we can go to the Azure Portal and link a database to our static web app:
Remark: If your Azure Static Web App isn’t able to connect to your database, check that Allow Azure services and resources to access this server is checked in the Networking –> Exceptions section of your database server.
We can now call the API through the /data-api endpoint. As we have set in our configuration that anonymous access is allowed, we can directly call the endpoint without authenticating first.
Here is the call using the REST endpoint:
And here we are using the GraphQL endpoint(notice that we get introspection for free):
Remark: The first call can take a little bit longer so certainly implement a retry strategy.
Connecting to a database with Azure Static Web Apps | Microsoft Learn