从tests.py调用数据库表

时间:2015-01-22 14:09:09

标签: django django-models django-testing django-tests

我的tests.py

中有以下内容
class TestSugar(TestCase):
    def test_empty_sugar_applicants(self):
        Sugar_Applicant.objects.using('sugarcrm').all().delete();
        number_of_sugar_applicants = Sugar_Applicant.objects.using('sugarcrm').filter(deleted=0).count()
        self.assertEqual(number_of_sugar_applicants, 0)

应该像我的代码的其他区域一样调用sugarcrm数据库并访问相关表。但是,出于某种原因,在tests.py文件中,test_会在名称之前插入:

Traceback (most recent call last):
  File "C:\Users\jont\Documents\application_trackingwc\atp\jobs\tests.py", line 27, in test_empty_sugar_applicants
    number_of_sugar_applicants = Sugar_Applicant.objects.using('sugarcrm').filter(deleted=0).count()
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 338, in count
    return self.query.get_count(using=self.db)
  File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 424, in get_count
    number = obj.get_aggregation(using=using)[None]
  File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 390, in get_aggregation
    result = query.get_compiler(using).execute_sql(SINGLE)
  File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 128, in execute
    return self.cursor.execute(query, args)
  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
ProgrammingError: (1146, "Table 'test_sugarcrm.a1234_applicants' doesn't exist")

Models.py

class Sugar_Applicant(models.Model):
    id = models.CharField(max_length=36, blank=False, primary_key=True)
    name = models.CharField(max_length=100, blank=True)
    applicant_title = models.CharField(max_length=100, blank=True)
    local_id = models.CharField(max_length=255, blank=True)
    other_title = models.CharField(max_length=255, blank=True)
    dob = models.CharField(max_length=255, blank=True)
    location = models.CharField(max_length=255, blank=True)
    nationality = models.CharField(max_length=255, blank=True)
    other_nationality = models.CharField(max_length=255, blank=True)
    national_insurance_number = models.CharField(max_length=255, blank=True)
    primary_address_street = models.CharField(max_length=150, blank=True)
    primary_address_street_2 = models.CharField(max_length=150, blank=True)
    primary_address_district = models.CharField(max_length=255, blank=True)
    primary_address_city = models.CharField(max_length=100, blank=True)
    primary_address_county = models.CharField(max_length=255, blank=True)
    primary_address_postcode = models.CharField(max_length=20, blank=True)
    telephone = models.CharField(max_length=100, blank=True)
    mobile_phone = models.CharField(max_length=100, blank=True)
    date_modified = models.DateTimeField(default=timezone.now)
    deleted = models.IntegerField(blank=True)
    approved = models.CharField(max_length=255, blank=True)
    job = models.CharField(max_length=50, blank=True)
    location = models.CharField(max_length=50, blank=True)

    class Meta:
        managed = False
        db_table = 'a1234_applicants'

如果我将此代码放在def之外,则可以正常使用,

class TestSugar(TestCase):
    Sugar_Applicant.objects.using('sugarcrm').all().delete();
    number_of_sugar_applicants = Sugar_Applicant.objects.using('sugarcrm').filter(deleted=0).count()
    def test_empty_sugar_applicants(self):
        self.assertEqual(number_of_sugar_applicants, 0)

1 个答案:

答案 0 :(得分:0)

测试套件可以做一些事情来阻止您对生产数据进行更改......这是一件非常好的事情。

如果要编写使用数据的测试,则需要创建一个fixture。除此之外,拥有固定的测试数据可以让您知道运行的每个测试的正确结果。

此处有更多信息:http://django-testing-docs.readthedocs.org/en/latest/fixtures.html