2014-11-15 129 views
0

django測試中有我的第一步。如何測試mptt模型?

我在目前的情況下,測試的內容:

我用MPTT在我的項目和新的實例創建時 - 這是父實例獲取日期字段的新值。

什麼是一個問題:

創造的測試用例,TreeNode.objects.get()安裝方法實例無法找到創建的實例或者Django中沒有看到三,我不知道以後有什麼發生了,但是在底部的TreeNode.save()中描述的創建新的孩子不起作用。

對於exapmple後

TreeNode.objects.create(id=4, parent_id=1) 

TreeNode.objects.get(ID = 4).LEVEL(自動字段中MPTT)等於無,但必須是0,如果例如沒有父或parent.level + 1如果父存在。有時TreeNode.objects.get(id = 4)根本找不到由TreeNode.objects.create(id = 4)創建的對象。

goal:定義創建子節點的OK方法將contract_date,id和parent作爲屬性並在TreeNode.objects.create之後運行save()的節點。

有很多問題,我想爲他們的一部分得到答案。主要問題是:

如何在測試中創建TreeNode對象(或者在模型中,如果我做錯了什麼)? 如何正確定義對象作爲另一個TreeNode的子元素?

我的代碼:

測試:

# method for creating a newbie. 
def instantiate_treenode(uid, contract_date, parent_id=None): 
TreeNode.objects.create(id=uid, 
           contract_date=contract_date, 
           date_of_birth=date.today(), 
           account_id=uid, 
           identity_expire_date=date.today(), 
           parent=TreeNode.objects.get(id=parent_id) if parent_id else None,) 


class BigTestTree(TestCase): 
# 
# 
# 
#  # 
##### # 
def setUp(self): 
    instantiate_treenode(uid=1, contract_date=date(2014, 1, 1)) 

    instantiate_treenode(uid=2, contract_date=date(2014, 1, 2), parent_id=1) 

    instantiate_treenode(uid=3, contract_date=date(2014, 1, 3), parent_id=2) 

    instantiate_treenode(uid=4, contract_date=date(2014, 1, 4), parent_id=3) 
    instantiate_treenode(uid=5, contract_date=date(2014, 1, 5), parent_id=3) 

    instantiate_treenode(uid=6, contract_date=date(2014, 1, 6), parent_id=4) 
    instantiate_treenode(uid=7, contract_date=date(2014, 1, 7), parent_id=4) 
    instantiate_treenode(uid=8, contract_date=date(2014, 1, 8), parent_id=4) 
    instantiate_treenode(uid=9, contract_date=date(2014, 1, 9), parent_id=4) 

    for i in range(1, 10): 
     self.assertEqual(TreeNode.objects.get(id=i).paid_income, 0) 

def test_add_newbies_to_fourth_line(self): 

    instantiate_treenode(uid=11, contract_date=date(2014, 1, 10), parent_id=5) 

    self.assertEqual(TreeNode.objects.get(id=1).expected_income, 5 * 550) 
    self.assertEqual(TreeNode.objects.get(id=2).paid_income, 5 * 1000) 

    for i in range(3, 10): 
     self.assertEqual(TreeNode.objects.get(id=i).paid_income, 0) 

    self.assertEqual(TreeNode.objects.get(id=7).is_parent_rewarded(3), True) 
    self.assertEqual(TreeNode.objects.get(id=7).is_parent_rewarded(1), False) 

    instantiate_treenode(uid=10, contract_date=date(2014, 1, 10), left=13, right=14, lvl=4, 
         parent_id=4) 

    self.assertEqual(TreeNode.objects.get(id=1).paid_income, 5 * 550) 
    self.assertEqual(TreeNode.objects.get(id=2).paid_income, 5 * 1000) 
    self.assertEqual(TreeNode.objects.get(id=3).paid_income, 5 * 3500) 
    self.assertEqual(TreeNode.objects.get(id=4).paid_income, 5 * 5400) 

    for i in range(5, 10): 
     self.assertEqual(TreeNode.objects.get(id=i).paid_income, 0) 

MPTT型號

class TreeNode(MPTTModel): 

    account = models.OneToOneField(User, unique=True, verbose_name='Аккаунт') 

    # contract 
    id = models.CharField(verbose_name='ID', max_length=10, primary_key=True) 
    parent = TreeForeignKey('self', verbose_name='ID пригласившего', null=True, blank=True, related_name='child') 
    contract_date = models.DateField(null=False, blank=False) 

    invite_period_expire_date = models.DateField(null=True, blank=True) 

    def is_parent_rewarded(self, line): 
     return getattr(self, self.rewarded_parents_fields[line - 1]) if line else self 

    def get_parent_of_level(self, level): 
     if self.get_level() - level >= 0: 
      return self.get_ancestors().get(level=self.get_level() - level) 
     return None 

    def get_relative_level(self, level, newbies_only): 
     result = self.get_descendants().filter(level=self.get_level() + level) 
     if newbies_only: 
      result = filter(lambda x: not x.is_parent_rewarded(level), result) 
     return result 

    def pay_to_parent(self, line, five_per_period): 
     parent = self.get_parent_of_level(line) 
     if parent: 
      from payments.models import CompanyToClientPayment 

      if line == 1: 
       payment = CompanyToClientPayment.objects.create(client=parent, use_date=self.contract_date, 
                   total=prices[0][1] 
                   if five_per_period 
                   else prices[0][0]) 
      elif line == 2: 
       payment = CompanyToClientPayment.objects.create(client=parent, use_date=self.contract_date, 
                   total=prices[1][1] 
                   if five_per_period 
                   else prices[1][0]) 
      elif line == 3: 
       payment = CompanyToClientPayment.objects.create(client=parent, 
                   use_date=self.contract_date 
                   if self.contract_date.day <= 5 
                   else next_fifth_day_of_month(self.contract_date), 
                   total=prices[2]) 
      elif line == 4: 
       payment = CompanyToClientPayment.objects.create(client=parent, 
                   use_date=self.contract_date 
                   if self.contract_date.day <= 5 
                   else next_fifth_day_of_month(self.contract_date), 
                   total=prices[3]) 
      else: 

       raise ValueError('Invalid line to pay for invite') 

      #print('paymentID' + str(payment.id)) 
     setattr(self, self.rewarded_parents_fields[line - 1], True) 
     self.save() 
     parent.save() 
     # print getattr(self, self.rewarded_parents_fields[line-1]) 

    def close_invite_period(self, expired): 
     for child in filter(lambda x: x.contract_date <= self.invite_period_expire_date, 
          self.get_relative_level(level=1, newbies_only=True)): 
      child.pay_to_parent(1, five_per_period=not expired) 

      if child.get_parent_of_level(2): 
       child.pay_to_parent(2, five_per_period=not expired) 

     self.invite_period_expire_date = None 
     self.save() 

    def start_invite_period(self, start_date): 
     if self.invite_period_expire_date: 
      raise ReopeningInvitePeriodException 
     self.invite_period_expire_date = start_date + invite_period 
     self.save() 

    def save(self, *args, **kwargs): 

     if self.get_parent_of_level(1) and TreeNode.objects.filter(id=self.id).exists(): 
      if not self.get_parent_of_level(1).invite_period_expire_date: 
       self.get_parent_of_level(1).start_invite_period(self.contract_date) 
      elif self.get_parent_of_level(1).invite_period_expire_date <= self.contract_date: 
       self.get_parent_of_level(1).close_invite_period(expired=True) 
       self.get_parent_of_level(1).start_invite_period(self.contract_date) 

      newbie_brothers = self.get_parent_of_level(1).get_relative_level(level=1, newbies_only=True) 

      if len(newbie_brothers) == 5: 
       self.get_parent_of_level(1).close_invite_period(expired=False) 

      if self.get_parent_of_level(3): 
       newbie_brothers = self.get_parent_of_level(3).get_relative_level(level=3, newbies_only=True) 
       if len(newbie_brothers) == 5: 
        for newbie in newbie_brothers: 
         newbie.pay_to_parent(3, five_per_period=False) 

       if self.get_parent_of_level(4): 
        newbie_brothers = self.get_parent_of_level(4).get_relative_level(level=4, newbies_only=True) 
        if len(newbie_brothers) == 5: 
         for newbie in newbie_brothers: 
          newbie.pay_to_parent(4, five_per_period=False) 
     super(TreeNode, self).save(*args, **kwargs) 
+0

爲什麼你將id定義爲CharField而不是使用默認的autoincremented int? –

+0

每個客戶都有一個由公司生成的ID。它包括國家代碼。正因爲如此,我使用自定義ID字段而不是自動增量ID。 – amazingbasil

回答

0

所以,我的問題是,我認爲,如果對象已存在instance.save()品牌 '更新' 查詢。我錯了。爲調用'更新'查詢instance.update(*kwargs)方法應該被調用。對不起。我是一個茶壺。