Testing a reusable app which uses a database

40 views
Skip to first unread message

Victor Porton

unread,
Oct 3, 2016, 3:41:59 PM10/3/16
to Django users
I try to make a reusable app which uses transaction.atomic().

When I run my test.py (below) for my reusable app, I get the below errors.

Is it possible to test this reusable app outside of a Django project?

[[[
import unittest
from transaction_utils.transaction import retry


class TestRetry(unittest.TestCase):
    @retry
    def my_func(self):
        return 123

    def test_passthrough(self):
        self.assertEqual(self.my_func(), 123)

if __name__ == '__main__':
    unittest.main()
]]]

[[[
E
======================================================================
ERROR: test_passthrough (__main__.TestRetry)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 11, in test_passthrough
    self.assertEqual(self.my_func(), 123)
  File "/home/porton/Projects/transaction_utils/transaction_utils/transaction.py", line 24, in func_wrapper
    with transaction.atomic():
  File "/usr/lib/python3/dist-packages/django/db/transaction.py", line 152, in __enter__
    connection = get_connection(self.using)
  File "/usr/lib/python3/dist-packages/django/db/transaction.py", line 21, in get_connection
    return connections[using]
  File "/usr/lib/python3/dist-packages/django/db/utils.py", line 208, in __getitem__
    self.ensure_defaults(alias)
  File "/usr/lib/python3/dist-packages/django/db/utils.py", line 176, in ensure_defaults
    conn = self.databases[alias]
  File "/usr/lib/python3/dist-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/lib/python3/dist-packages/django/db/utils.py", line 156, in databases
    self._databases = settings.DATABASES
  File "/usr/lib/python3/dist-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/usr/lib/python3/dist-packages/django/conf/__init__.py", line 39, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)
]]]

Carl Meyer

unread,
Oct 3, 2016, 3:46:56 PM10/3/16
to django...@googlegroups.com
On 10/03/2016 01:41 PM, Victor Porton wrote:
> I try to make a reusable app which uses transaction.atomic().
>
> When I run my test.py (below) for my reusable app, I get the below errors.
>
> Is it possible to test this reusable app outside of a Django project?

It is not possible to test it without setting up Django, no. But you
don't need a full-fledged "project" for this, the bare minimum you need
to do is

import django
from django.conf import settings

settings.configure() # if you need any settings, pass as a dict
django.setup()

before running your tests.

Carl

signature.asc

Victor Porton

unread,
Oct 3, 2016, 4:16:30 PM10/3/16
to Django users
Well, I did it.
Reply all
Reply to author
Forward
0 new messages