Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrect way to unit test Django models without transaction errors
    primarykey
    data
    text
    <p>I'm writing some models with constraints like <code>unique=True</code> and <code>blank=False</code> and <code>null=False</code>. I'm trying to write tests for the models with nose. However, if I write a test like this:</p> <pre><code>from job_sites.models import Site, SiteType @raises(IntegrityError) def test_empty_site(): s = Site() s.save() @raises(IntegrityError) def test_empty_site_type(): st = SiteType() st.save() </code></pre> <p>I get a DatabaseError like this: <code>DatabaseError: current transaction is aborted, commands ignored until end of transaction block</code> after it runs the first test.</p> <p>What is the correct way to run DJango model tests when I'm expecting errors?</p> <p>For reference, the models look like this:</p> <pre><code>class SiteType(models.Model): site_type_id = models.AutoField(primary_key=True) site_type = models.CharField(max_length=32, unique=True, blank=False, null=False, default=None) site_type_abbrev = models.CharField(max_length=32, blank=True) class Meta: db_table = u'site_types' class Site(models.Model): site_id = models.AutoField(primary_key=True, blank=False, null=False, db_index=True) site_name = models.CharField(max_length=128, blank=False, null=False, db_index=True) site_type = models.ForeignKey(SiteType, blank=True, null=True) date_entered = models.DateTimeField(auto_now_add=True) class Meta: db_table = u'sites' </code></pre> <p>My constraints and defaults look like this:</p> <pre><code>ALTER TABLE site_types ADD CONSTRAINT site_types_site_type_name_minlen CHECK (char_length(site_type) &gt; 0); ALTER TABLE sites ALTER COLUMN date_entered SET DEFAULT now(); ALTER TABLE sites ADD CONSTRAINT sites_site_name_minlen CHECK (char_length(site_name) &gt; 0); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload