title: How to patch a module's internal functions with mock?
body: <p>By "internal function", I mean a function that is called from within the same module it is defined in.</p>
  
  <p>I am using the <a href="http://www.voidspace.org.uk/python/mock/">mock</a> library, specifically the <a href="http://www.voidspace.org.uk/python/mock/patch.html">patch</a> decorators, in my unit tests.  They're Django unit tests, but this should apply to any python tests.</p>
  
  <p>I have one module with several functions, many of which call each other.  For example (fictitious code, ignore the lack of decimal.Decimal):</p>
  
  <pre><code>TAX_LOCATION = 'StateName, United States'
  
  def add_tax(price, user):
      tax = 0
      if TAX_LOCATION == 'StateName, UnitedStates':
          tax = price * .75
      return (tax, price+tax)
  
  def build_cart(...):
      # build a cart object for `user`
      tax, price = add_tax(cart.total, cart.user)
      return cart
  </code></pre>
  
  <p>These are part of a deeper calling chain (func1 -> func2 -> build_cart -> add_tax), all of which are in the same module.</p>
  
  <p>In my unit tests, I'd like to disable taxes to get consistent results.  As I see it, my two options are 1) patch out TAX_LOCATION (with an empty string, say) so that add_tax doesn't actually do anything or 2) patch out add_tax to simply return (0, price).</p>
  
  <p>However, when I try to patch either of these the patch seems to work externally (I can import the patched part inside the test and print it out, getting expected values), but seems to have no effect internally (the results I get from the code behave as if the patch were not applied).</p>
  
  <p>My tests are like this (again, fictitious code):</p>
  
  <pre><code>from mock import patch
  from django.test import TestCase
  
  class MyTests(TestCase):
  
      @patch('mymodule.TAX_LOCATION', '')
      def test_tax_location(self):
          import mymodule
          print mymodule.TAX_LOCATION # ''
          mymodule.func1()
          self.assertEqual(cart.total, original_price) # fails, tax applied
  
      @patch('mymodule.add_tax', lambda p, u: (0, p))
      def test_tax_location(self):
          import mymodule
          print mymodule.add_tax(50, None) # (0, 50)
          mymodule.func1()
          self.assertEqual(cart.total, original_price) # fails, tax applied
  </code></pre>
  
  <p>Does anyone know if it's possible for mock to patch out functions used internally like this, or am I out of luck?</p>