0

I have the following in my 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)

which should, like in other areas of my code, call the sugarcrm database and access the relevant table. However, for some reason, in the tests.py file, test_ gets inserted before the name:

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'

If I put this code outside of the def, it works fine,

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)
Jon
  • 3,174
  • 11
  • 39
  • 57

1 Answers1

0

The testing suite does several things to keep you from making changes to your production data... and that's a very good thing.

If you want to write tests that use data, you'll want to create a fixture. Among other things, having fixed test data will allow you to know what the correct result is for each test you run.

More info here: http://django-testing-docs.readthedocs.org/en/latest/fixtures.html

dylrei
  • 1,728
  • 10
  • 13