I recently modified my Django project’s settings to that it uses SQLite3 when I run tests instead of PostgreSQL so that my tests will run faster. However, once I did that, one of my negative unit tests that checks to verify that Django will throw an error if I try to create a user whose username exceeds 30 characters started failing. Up until this time, the test had always passed.
If I run the following test using PostgreSQL, an exception (specifically a DatabaseError) is raised and the test passes. However, if I use SQLite3 instead, no exception is raised and the test fails. I double-checked the SQLite3 auth_user schema and confirmed username is a varchar(30) field. Has anyone else experienced this? Thanks.
from django.test import TestCase
from django.contrib.auth.models import User
class SimpleTest(TestCase):
def test_simple(self):
exception_raised = True # Exception should be raised since username > 30 chars
try:
user = User.objects.create_user(username='testusertestusertestusertestuser')
except Exception as e:
exception_raised = False
assert not exception_raised, "Exception wasn't raised"
I believe that the reason for this is that sqlite3 converts all varchar data types to the text data type under the hood. This is why the DBMS won’t catch any values that exceed your limit. The documentation is a little bit unclear in this regard, but I ran into this problem before and from the documentation, that was the best answer I could come up with. So basically,
Will end up being identical to:
Check out the documentation here (section 2.2): http://www.sqlite.org/datatype3.html
A test you can do to see that this is just the functionality of the DBMS:
As you can see, there are no errors, and no truncation.
Hope that helps,