Code Monkey home page Code Monkey logo

Comments (4)

sallyruthstruik avatar sallyruthstruik commented on July 18, 2024

What version of Django, postgresql do you use?
Can you provide full traceback and detail explonation how to reproduce the bug?
Why did you run apscheduler tests with postgresql connection? Or it affects your project tests? If it is, I will be glad if you provide example of failing test.

from django-apscheduler.

 avatar commented on July 18, 2024

Versions

Django==2.0.4
PostgreSQL 10.1, compiled by Visual C++ build 1800, 64-bit

Traceback

Testing started at 5:18 PM ...
...\Scripts\python.exe "...\pycharm\django_test_manage.py" test accounts.tests ...\Django\project
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
Destroying test database for alias 'default'...
Traceback (most recent call last):
  File "...\project\Lib\site-packages\django\db\backends\utils.py", line 83, in _execute
    return self.cursor.execute(sql)
psycopg2.OperationalError: database "test_projectdb" is being accessed by other users
DETAIL:  There is 1 other session using the database.


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "...\helpers\pycharm\django_test_manage.py", line 168, in <module>
    utility.execute()
  File "...\helpers\pycharm\django_test_manage.py", line 142, in execute
    _create_command().run_from_argv(self.argv)
  File "...\project\Lib\site-packages\django\core\management\commands\test.py", line 26, in run_from_argv
    super().run_from_argv(argv)
  File "...\project\Lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "...\project\Lib\site-packages\django\core\management\base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "...\pycharm\django_test_manage.py", line 104, in handle
    failures = TestRunner(test_labels, **options)
  File "...\pycharm\django_test_runner.py", line 255, in run_tests
    extra_tests=extra_tests, **options)
  File "...\pycharm\django_test_runner.py", line 156, in run_tests
    return super(DjangoTeamcityTestRunner, self).run_tests(test_labels, extra_tests, **kwargs)
  File "...\project\Lib\site-packages\django\test\runner.py", line 604, in run_tests
    self.teardown_databases(old_config)
  File "...\project\Lib\site-packages\django\test\runner.py", line 577, in teardown_databases
    keepdb=self.keepdb,
  File "...\project\Lib\site-packages\django\test\utils.py", line 299, in teardown_databases
    connection.creation.destroy_test_db(old_name, verbosity, keepdb)
  File "...\project\Lib\site-packages\django\db\backends\base\creation.py", line 259, in destroy_test_db
    self._destroy_test_db(test_database_name, verbosity)
  File "...\project\Lib\site-packages\django\db\backends\base\creation.py", line 276, in _destroy_test_db
    % self.connection.ops.quote_name(test_database_name))
  File "...\project\Lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "...\project\Lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "...\project\Lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "...\project\Lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "...\project\Lib\site-packages\django\db\backends\utils.py", line 83, in _execute
    return self.cursor.execute(sql)
django.db.utils.OperationalError: database "test_projectdb" is being accessed by other users
DETAIL:  There is 1 other session using the database.


Process finished with exit code 1

On additional test runs, the test database has to be destroyed manually.

Testing started at 5:33 PM ...
...\Scripts\python.exe "...\pycharm\django_test_manage.py" test accounts.tests ...
Creating test database for alias 'default'...
Got an error creating the test database: database "test_projectdb" already exists

Type 'yes' if you would like to try deleting the test database 'test_projectdb', or 'no' to cancel: yes
Destroying old test database for alias 'default'...
System check identified no issues (0 silenced).
Destroying test database for alias 'default'...

I wanted to include some integration tests for views that call scheduler.add_job(). Postgresql is used for both testing and live. In hindsight, it's probably overkill to test for task persistence when it's all going to be destroyed along with the database.

I'm currently using the MemoryJobStore when running tests, which is probably a better idea (see below).

Created a schedules.py file in an app module:

import sys

from apscheduler.jobstores.memory import MemoryJobStore
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events


scheduler = BackgroundScheduler()
if 'test' in sys.argv:
    scheduler.add_jobstore(MemoryJobStore(), 'default')
else:  # pragma: no cover
    scheduler.add_jobstore(DjangoJobStore(), 'default')
register_events(scheduler)
scheduler.start()

from django-apscheduler.

sallyruthstruik avatar sallyruthstruik commented on July 18, 2024

Awesome, thank you. I'll take a look in the week

from django-apscheduler.

 avatar commented on July 18, 2024

Sorted out how to do integration tests with tasks. Same settings to use MemoryJobStore in testing. In your StaticLiveServerTestCase subclass (or LiveServerTestCase), add to your tearDown method:

def tearDown(self):
    ...
    scheduler.shutdown(wait=10)

After tinkering with different settings, I found it a requirement to set the wait kwarg high enough for all connections to be closed, else the test running returns the error.

from django-apscheduler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.