title: Should database connections be mocked?
body: <p>I'm trying to apply unit testing to a project I'm working on. I work with Python an MySQLdb but I think this discussion is language-agnostic.
  I want to test a function, let's call it foobar(). It executes SQL queries that look similar to this:</p>
  
  <pre><code>cursor.execute("SELECT * FROM my_table WHERE a == " +varA+ " AND  b &lt; " +varB)
  </code></pre>
  
  <p>There is a little bit of complexity going on in the function. 
  How and how often <code>cursor.execute(query)</code> gets called depends on foobar's arguments.</p>
  
  <p>Now I'm unsure if I should mock the database connection <code>cursor</code>. 
  If I don't, it's bad because the test depends on the availability of a DB server.
  If I do, it's bad because the mock has to know which queries will be called. But the exact queries are an implementation detail. E.g. whether the queries are written in lower- or uppercase does not affect the correctness of foobar. But changing that will break the test. The test will also be hard to read because of the somewhat complex mock object.</p>
  
  <p>Is this a case of choosing the lesser evil? Is there a third way?</p>