def handle(self, *args, **options): if len(args) < 1: args = ["8001"] if not options.get('settings'): os.environ[ 'DJANGO_SETTINGS_MODULE'] = 'mainsite.settings_testserver' setup_databases(verbosity=1, interactive=False) call_command("loaddata", os.path.join(TOP_DIR, "fixtures", "testserver.json")) return super(Command, self).handle(*args, **options)
def setup_databases(self, **kwargs): db_name = 'newsblur_test' disconnect() connect(db_name) print('Creating test-database: ' + db_name) return setup_databases(self.verbosity, self.interactive, **kwargs)
def django_db_setup(request, django_db_blocker, postgresql_proc): from django.conf import settings settings.DATABASES["default"].update( { ("ENGINE", "django.db.backends.postgresql"), ("NAME", "tests"), ("USER", postgresql_proc.user), # noqa ("HOST", postgresql_proc.host), # noqa ("PORT", postgresql_proc.port), # noqa } ) with django_db_blocker.unblock(): setup_databases( verbosity=request.config.option.verbose, interactive=False, keepdb=False )
def _setup_environment(self): setup_test_environment() old_config = setup_databases(verbosity=1, interactive=True, keepdb=self.keepdb, debug_sql=False, parallel=0, aliases=None) return old_config
def AssembleApp(self, data, basedir, options): # Fixup Django settings. Turn DEBUG off. This might speed up # program execution if e.g. database queries are not logged. settings.DEBUG = False # Switch to the throw-away test database so no database records # we create in this command are persistent. from django.test.utils import setup_databases, teardown_databases dbinfo = setup_databases(True, False) try: # Read the customized organization name, which substitutes in for # {{organization}} in templates.. organization_name = "<Organization Name>" if isinstance(data.get("organization"), dict) \ and isinstance(data["organization"].get("name"), str): organization_name = data["organization"]["name"] # Create stub data structures that are required to do module logic # but that have mostly no end-user-visible presence. The only thing # visible here is the organization's name, which gets substituted # in {{organization}} variables in document templates. self.dummy_org = Organization.objects.create( name=organization_name, subdomain=get_random_string(12)) self.dummy_user = User.objects.create( username=get_random_string(12)) # Cache app sources and app instances as we load app data into the # database so that when sources and apps occur multiple times we # reuse the existing instances in the database. self.app_sources = {} self.app_instances = {} # Start the app. project = self.start_app(data.get("app"), basedir) if project: # no error # Fill in the answers. self.set_answers(project.root_task, data.setdefault("questions", []), basedir, options) # Generate outputs if outdir was given on the command line. if options["outdir"]: self.save_outputs(project, options["outdir"]) finally: # Clean up the throw-away test database. teardown_databases(dbinfo, 1) for level in ("WARN", "ERROR"): print( termcolor.colored( "{} {}(s)".format( self.logcounts.get(level, 0), LOG_NAMES[level], ), LOG_COLORS[level]))
def handle(self, *accessors, **options): old_config = setup_databases(0, False) try: for fixture in get_fixtures(*accessors): fixture.create(), call_command('flush', verbosity=0, interactive=False) self.stdout.write(f'Fixture {fixture.name} has been created.') except Exception as exception: self.stderr.write(str(exception)) finally: teardown_databases(old_config, 0)
def django_db_setup(request, django_test_environment, django_db_blocker): """ This works with pytest-django plugin. The default django_db_setup fixture creates empty in-memory database and apply migrations at start of test session This fixture overrides the default django_db_setup fixture for the functions under current directory. It tells all functions marked with pytest.mark.django_db in this file to not automatically migrate database while setting up, create an empty database instead. So that we can test xml-importing related functions which apply migration and create database manually """ # this fixture is adapted from that in fixtures.py in pytest-django. # It's the recommended practice from pytest-django. # See https://pytest-django.readthedocs.io/en/latest/database.html#fixtures with django_db_blocker.unblock(): with no_migration(): db_cfg = setup_databases( verbosity=request.config.option.verbose, interactive=False ) # disable foreign_keys constraint for the database # # pytest_django uses transaction to restore database after every test function # As sqlite disallows schema modification in transaction when foreign keys constraints are enabled, # and our migrations have schema modification in them (e.g. creation of indexes in 0006) # the migration would error if foreign_keys constraint were set to ON. cursor = connection.cursor() cursor.execute("PRAGMA foreign_keys = OFF") # drop tables so that we can test creating them from start for delete_from in call_command("sqlflush", verbosity=0).splitlines()[1:-1]: if "API_" in delete_from: cursor.execute( delete_from.replace("DELETE FROM", "DROP TABLE IF EXISTS") ) call_command("migrate", "--fake", "API", "zero") def teardown_database(): with django_db_blocker.unblock(): try: teardown_databases(db_cfg, verbosity=request.config.option.verbose) except Exception as exc: request.node.warn( pytest.PytestWarning( "Error when trying to teardown test databases: %r" % exc ) ) request.addfinalizer(teardown_database)
def main(): # type: (...) -> None """ Wraps :func:`unittest.main` with the necessary Django setup. If your test code also requires importing Django models, call :func:setup before making those imports. """ import django.test.utils as d_t_utils try: d_t_utils.setup_test_environment() old_config = d_t_utils.setup_databases(verbosity=1, interactive=False) unittest.main() finally: d_t_utils.teardown_databases(old_config, verbosity=1) d_t_utils.teardown_test_environment()
def handle(self, *, verbosity, session_config_name, num_participants, export_path, **options): self.prepare_global_state() setup_test_environment() old_config = setup_databases(interactive=False, verbosity=verbosity, aliases={'default'}) try: run_all_bots_for_session_config( session_config_name=session_config_name, num_participants=num_participants, export_path=export_path, ) finally: teardown_databases(old_config, verbosity=verbosity) teardown_test_environment()
def setUpClass(cls): """ Sets up a temporary test database for the whole test case. For regular Django tests, this is usually done by Django's test runner. """ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ctf_gameserver.web.dev_settings') django.setup() # `interactive=False` causes the test database to be destroyed without asking if it already exists cls._old_db_conf = setup_databases(verbosity=1, interactive=False) super().setUpClass() # Get a fresh raw DB connection with as little of Django's pre-configuration as possible cls.connection = connection.get_new_connection(connection.get_connection_params()) # Ensure SQLite's default isolaton level (without autocommit) is being used cls.connection.isolation_level = ''
def handle(self, *args, **options): # Fix up some settings. self.fixup_django_settings() # Start the headless browser. self.start_headless_browser(options['size']) self.mouse_speed = options["mouse_speed"] # Prepare for taking screenshots. if options['path']: self.init_screenshots(options) # Switch to the throw-away test database so no database records # we create in this command are persistent. from django.test.utils import setup_databases, teardown_databases dbinfo = setup_databases(True, False) # Initialize the database. from guidedmodules.management.commands.load_modules import Command as load_modules load_modules().handle() try: # Create a user and organization. self.init_user_organization(options) # Run a script on the headless browser, generating # a bunch of screenshots. if options['app']: self.screenshot_app(options) if options['author_new_app']: self.screenshot_author_new_app(options) # Combine images into a PDF. if getattr(self, 'write_pdf_filename', None): self.write_pdf() finally: # Clean up the throw-away test database. teardown_databases(dbinfo, 1) # Close selenium. self.stop_headless_browser()
def django_db_setup( request, django_test_environment, django_db_blocker, django_db_use_migrations, django_db_keepdb, django_db_createdb, django_db_modify_db_settings, ): """Top level fixture to ensure test databases are available""" from django.test.utils import setup_databases, teardown_databases setup_databases_args = {} if not django_db_use_migrations: _disable_native_migrations() if django_db_keepdb and not django_db_createdb: setup_databases_args["keepdb"] = True with django_db_blocker.unblock(): db_cfg = setup_databases( verbosity=request.config.option.verbose, interactive=False, **setup_databases_args ) def teardown_database(): with django_db_blocker.unblock(): try: teardown_databases(db_cfg, verbosity=request.config.option.verbose) except Exception as exc: request.node.warn( pytest.PytestWarning( "Error when trying to teardown test databases: %r" % exc ) ) if not django_db_keepdb: request.addfinalizer(teardown_database)
def handle(self, *args, **options): # Fix up some settings. self.fixup_django_settings() # Start the headless browser. self.start_headless_browser(options['size']) # Prepare for taking screenshots. self.init_screenshots(options) # Switch to the throw-away database. from django.test.utils import setup_databases, teardown_databases dbinfo = setup_databases(True, False) # Initialize the database. from guidedmodules.management.commands.load_modules import Command as load_modules load_modules().handle() try: # Create a user and organization. self.init_user_organization(options) # Run a script on the headless browser, generating # a bunch of screenshots. if options['app']: self.screenshot_app(options) if options['author_new_app']: self.screenshot_author_new_app(options) # Combine images into a PDF. if self.write_pdf_filename: self.write_pdf() finally: teardown_databases(dbinfo, 1) self.stop_headless_browser()
def pytest_sessionstart(session): from django.test import utils session.django_db_cfg = utils.setup_databases( verbosity=session.config.option.verbose, interactive=False, keepdb=False)
def django_db_setup(request, django_test_environment, django_db_blocker, django_db_use_migrations, django_db_keepdb, django_db_createdb, django_db_modify_db_settings, enable_migration_signals, pytestconfig): if django_db_createdb or enable_migration_signals: warnings.warn("Warning: pre/post migrate signals are enabled \n") else: warnings.warn("Warning: pre/post migrate signals have been disabled\n") import django.core.management.commands.migrate django.core.management.commands.migrate.emit_pre_migrate_signal = MagicMock( ) django.core.management.commands.migrate.emit_post_migrate_signal = MagicMock( ) # """Top level fixture to ensure test databases are available""" from django.test.utils import setup_databases, teardown_databases from pytest_django.fixtures import _disable_native_migrations setup_databases_args = {} if not django_db_use_migrations: _disable_native_migrations() if django_db_keepdb and not django_db_createdb: setup_databases_args["keepdb"] = True # this patch is logically wrong, but we do not use constance permissions # otherwise test fails with # # .venv/lib/python3.9/site-packages/django/db/backends/utils.py:84: in _execute # return self.cursor.execute(sql, params) # E django.db.utils.ProgrammingError: relation "django_content_type" does not exist # E LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co... # E ^ # signals.post_migrate.disconnect(dispatch_uid='constance.create_perm') with django_db_blocker.unblock(): db_cfg = setup_databases(verbosity=request.config.option.verbose, interactive=False, **setup_databases_args) def _teardown_database(): with django_db_blocker.unblock(): teardown_databases(db_cfg, verbosity=request.config.option.verbose) if not django_db_keepdb: request.addfinalizer(_teardown_database) from test_utilities.factories import UserFactory from unicef_rest_framework.models import Service, UserAccessControl from etools_datamart.apps.etl.models import EtlTask from etools_datamart.apps.tracking.models import APIRequestLog with django_db_blocker.unblock(): EtlTask.objects.inspect() Service.objects.load_services() UserAccessControl.objects.all().delete() APIRequestLog.objects.truncate() UserFactory(username='******', is_superuser=True) from django.contrib.sites.models import Site Site.objects.get_or_create(domain='example.com', name='example.com') assert Service.objects.exists() assert not APIRequestLog.objects.exists()
def setUp(self): self.old_config = setup_databases(False, True) self.create_data() SOIL.reset()
def pytest_configure(config): os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_settings' django.setup() setup_databases(verbosity=1, interactive=False)
def wrapped_setup_databases(*args, **kwargs): return setup_databases(*args, time_keeper=NullTimeKeeper(), **kwargs)
args=[data.project.id]), HTTP_HOST=host) write_test_html("apps", "refund_editor", refund_create.content) sticky_header = client.get( reverse("job.editor", args=[data.big_project.id, data.big_job.id]), HTTP_HOST=host, ) write_test_html("editor", "sticky_header", sticky_header.content) activate("de") write_test_html("inputs", "amount", generate_amount_test_html(data)) if __name__ == "__main__": class DisableMigrations: def __contains__(self, item): return True def __getitem__(self, item): return None settings.MIGRATION_MODULES = DisableMigrations() print("Creating test database.") config = setup_databases(verbosity=0, interactive=False) print("Generating scaffolds.") generate_pages() print("Done.") teardown_databases(config, verbosity=0)
def setup_databases(self, **kwargs): db_name = 'newsblur_test' connect(db_name) print 'Creating test-database: ' + db_name return setup_databases(**kwargs)
def ready(self): setup_databases(verbosity=3, interactive=False)
def ready(self): setup_databases(verbosity=3, interactive=False) # add notification objects call_command('loaddata', 'demo')
import unittest from django.test.utils import setup_test_environment, setup_databases from django.test.runner import DiscoverRunner setup_test_environment() names = setup_databases(verbosity=1, interactive=True) runner = DiscoverRunner() suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase) result = runner.run_suite(suite) runner.suite_result(suite, result)
def setup(self, keepdb=settings.KEEP_DATABASE): self.old_config = setup_databases(1, False, keepdb=keepdb)
def ready(self): from . import signals signals.setup() setup_databases(verbosity=3, interactive=False) from django.contrib.auth.models import User User.objects.create_superuser('admin', '*****@*****.**', 'admin')
def pre_test(self): dutils.setup_test_environment() old_conf = dutils.setup_databases(1, False, aliases='default') self.start_app_threat() self._wait_for_live_app() return old_conf
def setUpModule(): django_test_utils.setup_test_environment() runner_state = django_test_utils.setup_databases(verbosity=0, interactive=False) test_state['runner_state'] = runner_state
def ready(self): setup_databases(verbosity=3, interactive=False) # add notification objects call_command("make_notifications") call_command("loaddata", "demo")
jb.AutoNumber.objects.create(type='Job', system_generated=True, last_nbr=1) from job import process_order with open('core-python/tests/unit/mock_data/order.json') as data_file: mock_order_json = json.load(data_file) client = PaperlessClient() client.get_resource = MagicMock(return_value=mock_order_json) order = Order.get(1) process_order(order) self.assertEqual(len(order.order_items), jb.Job.objects.filter(job=F('top_lvl_job')).count()) self.assertEqual( sum([ len([comp for comp in oi.components if not comp.is_hardware]) for oi in order.order_items ]), jb.Job.objects.count()) op_count = 0 for oi in order.order_items: for comp in oi.components: op_count += len(comp.shop_operations) addon_count = sum(len(oi.ordered_add_ons) for oi in order.order_items) self.assertEqual(op_count + addon_count, jb.JobOperation.objects.count()) if __name__ == '__main__': from django.test.utils import setup_databases setup_databases(1, False) unittest.main()