Python Transactional Tests Using SQL Alchemy, Pytest, and Factory Boy

Knowing how to integrate SQLAlchemy for your automated tests can be a bit tricky. Even after reading several blog posts on how to implement transactional tests with SQLAlchemy and Pytest, it took me a bit of time to make it all play nicely with Factory Boy.

Another key concept that several blog posts out there fail to cover is how SQLAlchemy sessions are handled both in the application and in the test suite.

In this blog post I will teach you how you that is achieved.

python sqlalchemy pytest factoryboy

Django: Adding Context Data to Class Based Views Using Properties

When using Class-Based Views in Django, we normally add additional context data to the view by implementing the get_context_data() method:

class MyDetailView(DetailView):

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['foo'] = 'bar'

        return context

This works well. But I am very fond of using another approach, so as to not pollute the get_context_data method. We can achieve this using properties:

django

Django: Custom Middleware for Alerting High Database Querying

In this post I will show you how you can implement a custom middleware that you can use in your development environment to monitor queries to the database in your views when developing Django applications.

Let's begin by creating a middleware.py file (a nice convention) where we will place our new middleware. The middleware is relatively simple and small, this is how it looks like:

django

Custom Admin Action Buttons in Django

In this post I will explain how you can add custom action buttons in the admin detail view of objects in the Django admin.

This is the final result we want to achieve:

Custom Admin Buttons

These buttons can trigger custom actions that we want to perform on these objects.

django
Older Posts →