Reputation: 193
I have 2 Django tests that are working perfectly fine locally but it fails on GitHub actions. Python version 3.9
Django==4.1.7
django-crum==0.7.9
django-widget-tweaks==1.4.12
et-xmlfile==1.0.1
flake8==5.0.4
openpyxl==3.0.10
pandas==1.5.1
Pillow==9.3.0
python-dotenv==0.19.0
XlsxWriter==1.1.5
xmltodict==0.13.0
This is the code:
class Empresa(models.Model):
name = models.CharField(max_length=120, verbose_name='Razon Social')
cuit = models.CharField(max_length=11, validators=[MinLengthValidator(11)])
user = models.ForeignKey(User, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self) -> str:
return self.name
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
if not self.user:
user = get_current_user()
if user and not user.pk:
user = None
if not self.pk:
self.user = user
self.user = user
return super().save(force_insert, force_update, using, update_fields)
def toJSON(self):
item = model_to_dict(self)
return item
And I try to test:
This is the test:
class EmpresaTesting(TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects.create(username='testuser', password='12345')
self.user.save()
def test_create_empresa_ok(self):
this_empresa = Empresa.objects.create(name='Empresa 1', cuit='30999999991', user=self.user)
self.assertTrue(isinstance(this_empresa, Empresa))
self.assertEqual(str(this_empresa), 'Empresa 1')
def test_create_empresa_long_name(self):
long_name = "a" * 121
with self.assertRaisesMessage(DataError, "Data too long for column 'name' at row 1"):
Empresa.objects.create(name=long_name, cuit='30999999991', user=self.user)
def test_create_empresa_long_cuit(self):
with self.assertRaisesMessage(DataError, "Data too long for column 'cuit' at row 1"):
Empresa.objects.create(name="Empresa 1", cuit='123456789012', user=self.user)
And this is the error I receive in GitHub actions
FAIL: test_create_empresa_long_cuit (export_lsd.tests.EmpresaTesting)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/work/el_conta/el_conta/export_lsd/tests.py", line 28, in test_create_empresa_long_cuit
Empresa.objects.create(name="Empresa 1", cuit='123456789012', user=self.user)
File "/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/contextlib.py", line 126, in __exit__
next(self.gen)
File "/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/django/test/testcases.py", line 876, in _assert_raises_or_warns_cm
yield cm
AssertionError: DataError not raised
======================================================================
FAIL: test_create_empresa_long_name (export_lsd.tests.EmpresaTesting)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/work/el_conta/el_conta/export_lsd/tests.py", line 24, in test_create_empresa_long_name
Empresa.objects.create(name=long_name, cuit='30999999991', user=self.user)
File "/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/contextlib.py", line 126, in __exit__
next(self.gen)
File "/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/django/test/testcases.py", line 876, in _assert_raises_or_warns_cm
yield cm
AssertionError: DataError not raised
name: Django CI
on:
push:
branches: [ "develop" ]
pull_request:
branches: [ "develop" ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.8, 3.9]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Check pending migrations
run: |
python manage.py makemigrations --check --dry-run
python manage.py migrate
- name: Lint with Flake8
run: |
flake8 --count --show-source --max-complexity=10 --max-line-length=130 --statistics --ignore=C901,F811
- name: Run Tests
run: |
python manage.py test
Upvotes: 0
Views: 531
Reputation: 193
It was solved by adding custom validators in the save() method in models, it looks like SQLite doesn't catch DataError. I actually proved that in the shell and I was able to add a 121 characters name.
Upvotes: 0