title: How to test Models in Django with Foreign Keys
body: <p>I want to make sure I am testing Models/Objects in isolation and not as one huge system.</p>
  
  <p>If I have an Order object and it has Foreign Keys to Customers, Payments, OrderItems, etc. and I want to test Order functionality, I need to create fixtures for all of that related data, or create it in code. I think what I really need to be doing is mocking out the other items, but I don't see an easy (or possible) solution for that if I am doing queries on these Foreign Keys.</p>
  
  <p>The common solutions (fixtures) don't really let me test one Object at a time. I am sure this is partly caused by my app being <em>way</em> over coupled.</p>
  
  <p>I am trying my darndest to adopt TDD as my main method of working, but the way things work with Django, it seems you can either run very trivial unit tests, or these massive integration tests.</p>
  
  <p><strong>[Edit]</strong> Better explicit example and a some more humility</p>
  
  <p>What I mean is that <strong>I seem</strong> to be able to only run trivial unit tests. I have seen people with very well tested and granular modules. I am certain some of this can be followed back to poor design.</p>
  
  <p>Example:</p>
  
  <p>I have a model call Upsell which is linked to a Product model. Then I have a Choices model which are children of Upsell (do you want what's behind door #1, #2, #3).</p>
  
  <p>The Upsell model has several methods on it that derive items necessary to render the template from their choices. The most important one is that it creates a URL for each choice. It does this through some string mangling etc. If I wanted to test the Upsell.get_urls() method, I want to have it not depend on the values of choices in the fixtures, and I want to not have it depend on the value of Product in the fixtures.</p>
  
  <p>Right now I populate the db in the setUp method for the tests, and that works well with the way Django backs out the transaction every time, but only outside of setUp and tearDown. This works fairly well except some of the Models are fairly complex to set up, while I actually only need to get one attribute for it.</p>
  
  <p>I can't give you an example of that, since I can't accomplish it, but here is the type of thing I am doing now. Basically I input an entire order, create the A/B experiment it was attached to, etc. And that's not counting Product, Categories, etc. all set up by fixtures. It's not the extra work I am concerned as I can't even test one database-based object at a time. The Tests below are important, but they are integration tests. I would like to be building up to something like this by testing each item separately. As you pointed out, maybe I shouldn't have chosen a framework so closely tied to the db. Does any sort of dependency injection exist with something like this? (beyond my testing, but the code itself as well)</p>
  
  <pre><code>class TestMultiSinglePaySwap(TestCase):
      fixtures = ['/srv/asm/fixtures/alchemysites.json','/srv/asm/fixtures/catalog.json','/srv/asm/fixtures/checkout_smallset.json','/srv/asm/fixtures/order-test-fixture.json','/srv/asm/fixtures/offers.json']
  
  def setUp(self):
      self.o = Order()
      self.sp = SiteProfile.objects.get(pk=1)
      self.c = Customer.objects.get(pk=1)
      signals.post_save.disconnect(order_email_first, sender=Order)
      self.o.customer = self.c
      p = Payment()
      p.cc_number = '4444000011110000'
      p.cc_exp_month = '12'
      p.cc_type = 'V'
      p.cc_exp_year = '2020'
      p.cvv2 = '123'
      p.save()
      self.o.payment = p
      self.o.site_profile = self.sp
      self.o.save()
      self.initial_items = []
      self.main_kit = Product.objects.get(pk='MOA1000D6')
      self.initial_items.append(self.main_kit)
      self.o.add_item('MOA1000D6', 1, False)
      self.item1 = Product.objects.get(pk='MOA1041A-6')
      self.initial_items.append(self.item1)
      self.o.add_item('MOA1041A-6', 1, False)
      self.item2 = Product.objects.get(pk='MOA1015-6B')
      self.initial_items.append(self.item2)
      self.o.add_item('MOA1015-6B', 1, False)
      self.item3 = Product.objects.get(pk='STP1001-6E')
      self.initial_items.append(self.item3)
      self.o.add_item('STP1001-6E', 1, False)
      self.swap_item1 = Product.objects.get(pk='MOA1041A-1')
  
  def test_single_pay_swap_wholeorder(self):
      o = self.o
      swap_all_skus(o)
      post_swap_order = Order.objects.get(pk = o.id)
      swapped_skus = ['MOA1000D','MOA1041A-1','MOA1015-1B','STP1001-1E']
      order_items = post_swap_order.get_all_line_items()
      self.assertEqual(order_items.count(), 4)
      pr1 = Product()
      pr1.sku = 'MOA1000D'
      item = OrderItem.objects.get(order = o, sku = 'MOA1000D') 
      self.assertTrue(item.sku.sku == 'MOA1000D')
      pr2 = Product()
      pr2.sku = 'MOA1015-1B'
      item = OrderItem.objects.get(order = o, sku = 'MOA1015-1B') 
      self.assertTrue(item.sku.sku == 'MOA1015-1B')
      pr1 = Product()
      pr1.sku = 'MOA1041A-1'
      item = OrderItem.objects.get(order = o, sku = 'MOA1041A-1') 
      self.assertTrue(item.sku.sku == 'MOA1041A-1')
      pr1 = Product()
      pr1.sku = 'STP1001-1E'
      item = OrderItem.objects.get(order = o, sku = 'STP1001-1E') 
      self.assertTrue(item.sku.sku == 'STP1001-1E')
  </code></pre>
  
  <p>Note that I have never actually used a Mock framework though I have tried. So I may also just be fundamentally missing something here.</p>