'Tests creating/deleting unique constraints.'

@skipIf((db.backend_name == 'sqlite3'), "SQLite    backend    doesn't    support    this    yet.")
def test_unique(self):

    db.create_table('test_unique2', [('id', models.AutoField(primary_key=True))])
    db.create_table('test_unique', [('spam', models.BooleanField(default=False)), ('eggs', models.IntegerField()), ('ham', models.ForeignKey(db.mock_model('Unique2', 'test_unique2')))])
    db.execute_deferred_sql()
    db.create_unique('test_unique', ['spam'])
    db.execute_deferred_sql()
    db.dry_run = True
    db.delete_unique('test_unique', ['spam'])
    db.dry_run = False
    db.delete_unique('test_unique', ['spam'])
    db.create_unique('test_unique', ['spam'])
    if (db.backend_name == 'pyodbc'):
        db.execute('SET    IDENTITY_INSERT    test_unique2    ON;')
    db.execute('INSERT    INTO    test_unique2    (id)    VALUES    (1)')
    db.execute('INSERT    INTO    test_unique2    (id)    VALUES    (2)')
    db.commit_transaction()
    db.start_transaction()
    TRUE = (True,)
    FALSE = (False,)
    db.execute('INSERT    INTO    test_unique    (spam,    eggs,    ham_id)    VALUES    (%s,    0,    1)', TRUE)
    db.execute('INSERT    INTO    test_unique    (spam,    eggs,    ham_id)    VALUES    (%s,    1,    2)', FALSE)
    try:
        db.execute('INSERT    INTO    test_unique    (spam,    eggs,    ham_id)    VALUES    (%s,    2,    1)', FALSE)
    except:
        db.rollback_transaction()
    else:
        self.fail('Could    insert    non-unique    item.')
    db.delete_unique('test_unique', ['spam'])
    db.execute('DELETE    FROM    test_unique')
    db.create_unique('test_unique', ['eggs'])
    db.start_transaction()
    db.execute('INSERT    INTO    test_unique    (spam,    eggs,    ham_id)    VALUES    (%s,    0,    1)', TRUE)
    db.execute('INSERT    INTO    test_unique    (spam,    eggs,    ham_id)    VALUES    (%s,    1,    2)', FALSE)
    try:
        db.execute('INSERT    INTO    test_unique    (spam,    eggs,    ham_id)    VALUES    (%s,    1,    1)', TRUE)
    except:
        db.rollback_transaction()
    else:
        self.fail('Could    insert    non-unique    item.')
    db.delete_unique('test_unique', ['eggs'])
    db.execute('DELETE    FROM    test_unique')
    db.create_unique('test_unique', ['spam', 'eggs', 'ham_id'])
    db.start_transaction()
    db.execute('INSERT    INTO    test_unique    (spam,    eggs,    ham_id)    VALUES    (%s,    0,    1)', TRUE)
    db.execute('INSERT    INTO    test_unique    (spam,    eggs,    ham_id)    VALUES    (%s,    1,    1)', FALSE)
    try:
        db.execute('INSERT    INTO    test_unique    (spam,    eggs,    ham_id)    VALUES    (%s,    0,    1)', TRUE)
    except:
        db.rollback_transaction()
    else:
        self.fail('Could    insert    non-unique    pair.')
    db.delete_unique('test_unique', ['spam', 'eggs', 'ham_id'])
    db.start_transaction()
