False or True check for yourself. On Wed, 10 May, 2023, 17:29 Vishesh Mangla, <[email protected]> wrote:
> It looks like you are passing a null but null=False is not set > > On Wed, 10 May, 2023, 17:27 'Bob Aalsma' via Django users, < > [email protected]> wrote: > >> I'm trying to test a django-admin command and don't understand why the >> tests give errors on the code while the code works as expected. >> >> I'm under the impression that the *raise CommandError* is ignored by the >> test, but don't understand why this would be. >> >> Please help. >> >> Running the code shows: >> >> (.venv) werker@Werkers-Mac-mini domain_reader % python3 manage.py >> populate_source 0 >> >> *CommandError: Alleen hele getallen > 0 toegestaan* >> >> (.venv) werker@Werkers-Mac-mini domain_reader % python3 manage.py >> populate_source 1 >> >> *Teksten zijn gegenereerd voor tekstlengte 1* >> >> (.venv) werker@Werkers-Mac-mini domain_reader % python3 manage.py >> populate_source 1 >> >> *CommandError: Teksten voor nummer 1 zijn al eerder gegenereerd* >> >> (.venv) werker@Werkers-Mac-mini domain_reader % python3 manage.py >> populate_source 7 >> >> *CommandError: Opgegeven sleutel 7 niet gevonden* >> >> Running the tests shows >> >> (.venv) werker@Werkers-Mac-mini domain_reader % python3 manage.py test >> tests.test_sources_app_command_populate >> >> Found 3 test(s). >> >> Creating test database for alias 'default'... >> >> System check identified no issues (0 silenced). >> >> EEE >> >> ====================================================================== >> >> ERROR: test_command_output_been_there >> (tests.test_sources_app_command_populate.PopulateSourceTests.test_command_output_been_there) >> >> Test for failure: in_use = True. >> >> ---------------------------------------------------------------------- >> >> Traceback (most recent call last): >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", >> line 89, in _execute >> >> return self.cursor.execute(sql, params) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/backends/sqlite3/base.py", >> line 328, in execute >> >> return super().execute(query, params) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> sqlite3.IntegrityError: NOT NULL constraint failed: >> domain_sources_app_characternumber.number_of_combinations >> >> >> The above exception was the direct cause of the following exception: >> >> >> Traceback (most recent call last): >> >> File >> "/Users/werker/Projects/domain_reader/tests/test_sources_app_command_populate.py", >> line 30, in test_command_output_been_there >> >> thingy.save() >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/models/base.py", >> line 814, in save >> >> self.save_base( >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/models/base.py", >> line 877, in save_base >> >> updated = self._save_table( >> >> ^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/models/base.py", >> line 1020, in _save_table >> >> results = self._do_insert( >> >> ^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/models/base.py", >> line 1061, in _do_insert >> >> return manager._insert( >> >> ^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/models/manager.py", >> line 87, in manager_method >> >> return getattr(self.get_queryset(), name)(*args, **kwargs) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/models/query.py", >> line 1805, in _insert >> >> return query.get_compiler(using=using).execute_sql(returning_fields) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/models/sql/compiler.py", >> line 1820, in execute_sql >> >> cursor.execute(sql, params) >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", >> line 67, in execute >> >> return self._execute_with_wrappers( >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", >> line 80, in _execute_with_wrappers >> >> return executor(sql, params, many, context) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", >> line 84, in _execute >> >> with self.db.wrap_database_errors: >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/utils.py", >> line 91, in __exit__ >> >> raise dj_exc_value.with_traceback(traceback) from exc_value >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", >> line 89, in _execute >> >> return self.cursor.execute(sql, params) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/backends/sqlite3/base.py", >> line 328, in execute >> >> return super().execute(query, params) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> django.db.utils.IntegrityError: NOT NULL constraint failed: >> domain_sources_app_characternumber.number_of_combinations >> >> >> ====================================================================== >> >> ERROR: test_command_output_small >> (tests.test_sources_app_command_populate.PopulateSourceTests.test_command_output_small) >> >> Test for failure with value '0'. >> >> ---------------------------------------------------------------------- >> >> Traceback (most recent call last): >> >> File >> "/Users/werker/Projects/domain_reader/tests/test_sources_app_command_populate.py", >> line 20, in test_command_output_small >> >> call_command("populate_source", 0, stdout=out) >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/core/management/__init__.py", >> line 194, in call_command >> >> return command.execute(*args, **defaults) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/core/management/base.py", >> line 458, in execute >> >> output = self.handle(*args, **options) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/domain_sources_app/management/commands/populate_source.py", >> line 36, in handle >> >> raise CommandError('Alleen hele getallen > 0 toegestaan') >> >> django.core.management.base.CommandError: Alleen hele getallen > 0 >> toegestaan >> >> >> ====================================================================== >> >> ERROR: test_command_output_success >> (tests.test_sources_app_command_populate.PopulateSourceTests.test_command_output_success) >> >> Test for success: in_use = False. >> >> ---------------------------------------------------------------------- >> >> Traceback (most recent call last): >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", >> line 89, in _execute >> >> return self.cursor.execute(sql, params) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/backends/sqlite3/base.py", >> line 328, in execute >> >> return super().execute(query, params) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> sqlite3.IntegrityError: NOT NULL constraint failed: >> domain_sources_app_characternumber.number_of_combinations >> >> >> The above exception was the direct cause of the following exception: >> >> >> Traceback (most recent call last): >> >> File >> "/Users/werker/Projects/domain_reader/tests/test_sources_app_command_populate.py", >> line 48, in test_command_output_success >> >> thingy.save() >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/models/base.py", >> line 814, in save >> >> self.save_base( >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/models/base.py", >> line 877, in save_base >> >> updated = self._save_table( >> >> ^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/models/base.py", >> line 1020, in _save_table >> >> results = self._do_insert( >> >> ^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/models/base.py", >> line 1061, in _do_insert >> >> return manager._insert( >> >> ^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/models/manager.py", >> line 87, in manager_method >> >> return getattr(self.get_queryset(), name)(*args, **kwargs) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/models/query.py", >> line 1805, in _insert >> >> return query.get_compiler(using=using).execute_sql(returning_fields) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/models/sql/compiler.py", >> line 1820, in execute_sql >> >> cursor.execute(sql, params) >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", >> line 67, in execute >> >> return self._execute_with_wrappers( >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", >> line 80, in _execute_with_wrappers >> >> return executor(sql, params, many, context) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", >> line 84, in _execute >> >> with self.db.wrap_database_errors: >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/utils.py", >> line 91, in __exit__ >> >> raise dj_exc_value.with_traceback(traceback) from exc_value >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", >> line 89, in _execute >> >> return self.cursor.execute(sql, params) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> File >> "/Users/werker/Projects/domain_reader/.venv/lib/python3.11/site-packages/django/db/backends/sqlite3/base.py", >> line 328, in execute >> >> return super().execute(query, params) >> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> django.db.utils.IntegrityError: NOT NULL constraint failed: >> domain_sources_app_characternumber.number_of_combinations >> >> >> ---------------------------------------------------------------------- >> >> Ran 3 tests in 0.008s >> >> >> FAILED (errors=3) >> >> Destroying test database for alias 'default'... >> >> Code for test: >> from io import StringIO >> >> from django.core.management import call_command >> from django.test import TestCase >> >> from domain_sources_app.models import CharacterNumber >> >> >> class PopulateSourceTests(TestCase): >> """Testcases for Django-admin command populate_source.""" >> def test_command_output_small(self): >> """Test for failure with value '0'.""" >> out = StringIO() >> call_command("populate_source", 0, stdout=out) >> self.assertIn("Alleen hele getallen > 0 toegestaan", out.getvalue()) >> >> def test_command_output_been_there(self): >> """Test for failure: in_use = True.""" >> >> thingy = CharacterNumber( >> number_of_characters=1, >> in_use=True, >> ) >> thingy.save() >> >> out = StringIO() >> call_command( >> "populate_source", >> 1, >> stdout=out, >> ) >> self.assertIn("Teksten voor nummer 1 zijn al \ >> eerder gegenereerd", out.getvalue()) >> >> def test_command_output_success(self): >> """Test for success: in_use = False.""" >> >> thingy = CharacterNumber( >> number_of_characters=1, >> in_use=False, >> ) >> thingy.save() >> >> out = StringIO() >> call_command( >> "populate_source", >> 1, >> stdout=out, >> ) >> self.assertIn("Teksten gegenereerd voor nummer 1", out.getvalue()) >> >> >> >> Code for the command: >> from django.core.management.base import BaseCommand >> from django.core.management.base import CommandError >> >> from domain_sources_app.models import CharacterNumber >> from domain_sources_app.generate_domain_strings import >> generate_domain_strings >> >> >> class Command(BaseCommand): >> """Populate Result with inital strings of >> a certain length. >> >> Args: >> BaseCommand (_type_): _description_ >> >> Raises: >> CommandError: _description_ >> """ >> help = "Genereert de te onderzoeken combinaties bij de opgegeven lengte" >> >> def add_arguments(self, parser): >> parser.add_argument("character_number_ids", nargs="+", type=int) >> >> def handle(self, *args, **options): >> for character_number_id in options["character_number_ids"]: >> if character_number_id < 1: >> raise CommandError('Alleen hele getallen > 0 toegestaan') >> else: >> try: >> thingy = CharacterNumber.objects.get( >> pk=character_number_id >> ) >> except CharacterNumber.DoesNotExist: >> raise CommandError( >> f'Opgegeven sleutel {character_number_id} \ >> niet gevonden') >> >> if not thingy.in_use: >> # generate for this number: >> generate_domain_strings(character_number_id) >> # register use of CharacterNumber >> thingy.in_use = True >> thingy.save() >> >> self.stdout.write( >> self.style.SUCCESS( >> f'Teksten zijn gegenereerd voor \ >> tekstlengte {character_number_id}' >> ) >> ) >> else: >> raise CommandError( >> f'Teksten voor nummer {character_number_id} zijn al \ >> eerder gegenereerd' >> ) >> >> >> >> >> Thanks, >> Bob >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/1d0ce155-35e7-4049-88d4-80124c83dedcn%40googlegroups.com >> <https://groups.google.com/d/msgid/django-users/1d0ce155-35e7-4049-88d4-80124c83dedcn%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACaE8x5xpV8yoGR%2BszkpGtzLzgiaKk%2Bxq9owHA3mQ7PU-Ls5Bg%40mail.gmail.com.

