3
votes

I am learning about Django testing. I wrote a simple App with a simple model and would like to run tests to check the validity of a model method, but I get an error message when I run the test:

here's models.py

from django.db import models


class Trip(models.Model):
    origin = models.CharField(max_length=20)
    destination = models.CharField(max_length=20)

    def __str__(self):
        return self.origin

    def is_valid(self):
        return self.origin != self.destination

Here's test.py

from django.test import TestCase
from .models import Trip


# Create your tests here.
class TripModelTests(TestCase):

    def test_trip(self):
        a = Trip.objects.create(origin='a', destination='a')
        self.assertIs(a.is_valid(), True)

here is settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'd57r9kcrhthdc7',
        'USER': 'sdqxaruartlvrd',
        'PASSWORD': 'e7b8f85611596ed125fe3ed4ea590f821f65e317c17ee7871be75b8130d72378',
        'HOST': 'ec2-3-214-46-194.compute-1.amazonaws.com',
        'PORT': '5432',
        'TEST': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': BASE_DIR / 'db.sqlite3',
        }
    }
}

and here is the error message i get when I run python manage.py test transport

Creating test database for alias 'default'...
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\test.py", line 23, in run_from_argv
    super().run_from_argv(argv)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\test.py", line 53, in handle
    failures = test_runner.run_tests(test_labels)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\test\runner.py", line 695, in run_tests
    old_config = self.setup_databases(aliases=databases)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\test\runner.py", line 614, in setup_databases
    return _setup_databases(
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\test\utils.py", line 170, in setup_databases
    connection.creation.create_test_db(
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\creation.py", line 55, in create_test_db
    self._create_test_db(verbosity, autoclobber, keepdb)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\creation.py", line 172, in _create_test_db
    'dbname': self.connection.ops.quote_name(test_database_name),
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\postgresql\operations.py", line 113, in quote_name
    if name.startswith('"') and name.endswith('"'):
AttributeError: 'WindowsPath' object has no attribute 'startswith'

The test works fine if I just use the default django settings and use a sqlite database....

1

1 Answers

2
votes

The error could be due to your BASE_DIR path, in your settings.py, you need to remove the slash / and switch it over to the following

'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

You may need to verify depending on your BASE_DIR contents that it points to the right place. One way to debugging this is to set ipdb() just after your database dictonary, so once you use python manage.py runserver, can can easily inspect the DATABASES structure.

import ipdb; ipdb.set_trace()

Source: https://pypi.org/project/ipdb/