Return home

Test-Driven Development with Django & South

This is going to be a vastly geekier blog post than my last few, so apologies if this isn't on a topic that interests you!

I'd basically given up on attempting test-driven with Django, given the project I'm currently working on uses models with a lot of South migrations. Just building the database and running the migrations could take a minute or so when running manage.py test, and resetting the database to a clean state meant the test suite would take several minutes to run.

I've had an idea in the back of my mind for a while, and today I finally got around to making it work.

When using SQLite, the test runner doesn't bother to actually hit the filesystem, it just does the whole thing in memory, which is a good deal quicker. Previously, I couldn't use SQLite, because South doesn't like it (since SQLite doesn't support ALTER TABLE).

My realisation was that if I turned off South, I could use SQLite, which I did with this rather hackish file called test_settings.py:

from settings import *

INSTALLED_APPS = [app for app in INSTALLED_APPS if app != 'south']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'test_database.db'
    }
}

I can then do a quick run of the test suite using:

manage.py test --settings=test_settings

Obviously, this doesn't run through my South migrations, which I probably should do from time to time (especially when adding new migrations). I can still test those with:

manage.py test

since by default the test runner will hit my normal settings module, which still has South in INSTALLED_APPS, and which hits my MySQL database.

With my test_settings, I get the following output:

Ran 121 tests in 3.218s

With my default settings, I get:

Ran 121 tests in 326.742s

If I run tests for a particular app (which is generally all I need to do), that difference is 0.312s to 31.064s.

That makes my test run approximately 99% quicker, and well within what I consider an acceptable time to run every time I make small changes to my code.

Perspective

I've been very grumpy this week. A few things have gone wrong at home, and they've bugged me. A couple of companies have (arguably) treated me unfairly; our washing machine's broken; our internet has been down for a few days; our house is a bit of a mess ready for some work to be done on it.

All this has left me feeling unsettled, despite Hannah's constant reminders that actually, none of this matters.

I phoned BT yesterday when I got home from work, to find out when our internet might start working. I went through the normal rigmarole associated with getting through the BT call centre obstacle course, pressing '1' repeatedly, entering my home number, and got through to an operator.

She did her best to help, though she no doubt spends most her day talking to people who are as frustrated as I was. I kept saying that I know it's not her fault, that I was just frustrated that we'd not had internet access! For a few days! As if without access to Twitter and Facebook in my non-working hours my world might fall apart.

I doubt those words were much comfort though, from the tone of the rest of my call. "I know it's not your fault BUT WHY ISN'T THIS FIXED, NOW? I MUST UPDATE MY TWITTER STATUS, AND I CAN'T, AND IT'S YOUR FAULT." *

Oh, the wonder of hindsight.

It's easy to be gracious in person, when you are constantly reminded that there is a real human being affected by the things you say. Less so when the other person is the other side of a telephone exchange, or an internet server.

This is character - the part of me that is shown up in the way I treat people who will never meet me. I've got an awfully long way to go.

* That's not quite how the conversation went, but for the life of me I can't figure out now why being able to access the internet last night was important.

Whatever is Not From Faith

I listened a little while ago to a talk from John Piper entitled "Whatever is Not From Faith is Sin".

The title is a reference to Romans 14:23, which reads like this in the ESV:

For whatever does not proceed from faith is sin.

This passage comes out of the context of Paul talking about what is allowable to eat (bearing in mind that the majority of the very early Church to whom Paul was writing was Jewish, and had lived most their lives under strict food laws). Paul declares that no food is unclean in and of itself (Romans 14:14), but then points out that that fact is effectively irrelevant.

The complementary doctrine is that, just as much as no food is unclean in and of itself, everything is sin unless it stems from faith. In other words, it doesn't matter if what you're eating, or what you're doing is not unclean (or sinful) in and of itself, what matters is whether your heart is directed towards God.

Piper says:

The most penetrating and devastating definition of sin that I am aware of in Scripture is the last part of Romans 14:23: "Whatever is not from faith is sin." The reason it is penetrating is that it goes to the root of all sinful actions and attitudes, namely, the failure to trust God. And the reason it is devastating is that it sweeps away all our lists of dos and don'ts and makes anything, from preaching to house-painting, a candidate for sin... Anything, absolutely any act or attitude which is owing to a lack of trust in God is sin, no matter how moral it may appear to men. God looks on the heart.

This was the mistake of the Pharisees (Luke 11:42) - focusing on a list of "good actions" and a list of "bad actions", and utterly failing to love God. Everything they did then, came out of a desire to do everything on the good list, and avoid everything on the bad list; and so somehow make themselves sufficiently pleasing to God so that he might free their nation from Roman rule. The problem? It would never, ever work:

And without faith it is impossible to please God (Hebrews 11:6)

The mistake of the Pharisees is a mistake I own, time and time again. I want to honour God in the small things, and in the attempt, I make honouring God about the small things. Jesus said about this:

"Woe to you, teachers of the law and Pharisees, you hypocrites! You give a tenth of your spices—mint, dill and cumin. But you have neglected the more important matters of the law—justice, mercy and faithfulness. (Matthew 23:23)

This week I want to remember first to love God, and follow God out of a love for Him. Anything less is faithless, and whatever is not from faith is sin.

Breathe Life Into Me

This song by The Glorious Unseen doesn't really express how I'm feeling right now - but is beautiful, and expresses exactly how I've felt on many, many occasions.

Thought I'd post it here in case it fits where you are.

God of the broken, God of the weak
Be a light in my darkness.
Breathe life into me.
Father, come speak your truth.
I offer my life, I give it to you.

Lord, take from me all my unbelief.
Where are you when I feel alone?
When I try to find arms
that are strong enough to hold,
all that I am and ever will be,
I give unto you, my Jesus, my king.

God of all comfort,
I'm wrecked and I'm torn.
Would you use me in weakness?
Would you speak to this storm?

All that you are is all that I need.
Father, release yourself please.
My spirit is at war with a dark enemy.
Oh Lord, will you come breathe life into me?

Struggling for Freedom

By the amazing grace of God, I've been set free, and

... if the Son sets you free, you will be free indeed. (John 8:36)

Therefore, I should:

live as people who are free... (1 Peter 2:16)

The first time I do something that I know pleases God, I feel alive, like I'm doing the very thing I was made to do: when I sit down in the study and open my Bible in the evening, not because I know I should, but because I want to; when I talk to God on my walk to the station in the morning; when I spend time talking to someone about following Jesus.

Then I decide to make a habit of it. The second time I feel alive, but a little less so. Gradually, the habit sucks out the life from the thing I was doing, until it becomes a duty; a habit.

This journal is an obvious example - I started off writing pretty regularly, but gradually I've left it, and it's been unwritten for a month or two. When I first started writing these entries on my train journeys to and from work it was something I really looked forward to. Partly it was that I was spending time with God at the time, and I had things I wanted to write about, to help me process them; but partly it was because it was new and therefore exciting to me.

Bible study has, at various times over the last 5 years gone through a similar cycle. I get back in the habit of reading the Bible, and for a time it is the most exciting thing in the world. I learn from what I'm reading, and I grow in my walk with God. Then comes the second day/week/month, and gradually it gets stale, and my eyes gloss over the passage when I sit down to read. The passage finishes, and I close my Bible and get on with my day. Gradually, the habit dies, and I stop reading my Bible. A little time passes (generally around a month or two), I pick it up again and the cycle restarts.

Right now, I'm about half-way through the cycle - I'm reading my Bible, but it's a real struggle to take things in, and to hear what God has to say to me through it.

So this is my question - how do you remain free? How do you spend time with God each day without it becoming a ritual? How can I be in my Bible each day without getting bored?

I know that God set me free from the law (Romans 6:14; Galatians 5:1; 18), but I still know I should be spending regular time with God. How does that work?

If this is something you find hard, I'd value hearing that too!

N.B. Comments welcome via Facebook, or drop me an e-mail (try putting dominic@ before the domain name you're looking at).

Older entries are available in the archives.