Ejemplo n.º 1
0
    def test_create_repo_with_recreate_index(self):
        """
        Test that we can create a new repo and that the refresh_index command
        indexes appropriately. Note that this test has custom setUp
        and tearDown code.
        """
        # When user installs LORE they must call refresh_index
        # or recreate_index to create the index.
        call_command("recreate_index")

        repo = create_repo("new repo", "new repo", self.user.id)
        search_url = "{api_base}repositories/{repo_slug}/search/".format(
            api_base=API_BASE, repo_slug=repo.slug)

        resp = self.client.get(search_url)
        self.assertEqual(resp.status_code, HTTP_200_OK)
        result = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(0, result['count'])

        # Import. This should index the resources automatically.
        import_course_from_file(self.get_course_zip(), repo.id, self.user.id)

        resp = self.client.get(search_url)
        self.assertEqual(resp.status_code, HTTP_200_OK)
        result = json.loads(resp.content.decode('utf-8'))
        self.assertTrue(result['count'] > 0)
Ejemplo n.º 2
0
    def _fixture_setup (self):
        print 'IN VIEWS TESTS BEFORE'
        from django.test.testcases import connections_support_transactions, transaction, disable_transaction_methods, call_command
        if not connections_support_transactions():
            return super(TestCase, self)._fixture_setup()

        assert not self.reset_sequences, 'reset_sequences cannot be used on TestCase instances'

        self.atomics = {}
        for db_name in self._databases_names():
            self.atomics[db_name] = transaction.atomic(using=db_name)
            self.atomics[db_name].__enter__()
        # Remove this when the legacy transaction management goes away.
        disable_transaction_methods()

        for db_name in self._databases_names(include_mirrors=False):
            if self.fixtures:
                try:
                    call_command('loaddata', *self.fixtures,
                                 **{
                                     'verbosity': 1,
                                     'commit': False,
                                     'database': db_name,
                                     'skip_checks': False,
                                 })
                except Exception, e:
                    print 'DBNAME', db_name
                    self._fixture_teardown()

                    if breakpoint:
                      from IPython import embed
                      embed()  # breaks into an ipython shell here!

                    raise
Ejemplo n.º 3
0
    def test_create_repo_with_recreate_index(self):
        """
        Test that we can create a new repo and that the refresh_index command
        indexes appropriately. Note that this test has custom setUp
        and tearDown code.
        """
        # When user installs LORE they must call refresh_index
        # or recreate_index to create the index.
        call_command("recreate_index")

        repo = create_repo("new repo", "new repo", self.user.id)
        search_url = "{api_base}repositories/{repo_slug}/search/".format(
            api_base=API_BASE,
            repo_slug=repo.slug
        )

        resp = self.client.get(search_url)
        self.assertEqual(resp.status_code, HTTP_200_OK)
        result = json.loads(resp.content.decode('utf-8'))
        self.assertEqual(0, result['count'])

        # Import. This should index the resources automatically.
        import_course_from_file(self.get_course_zip(), repo.id, self.user.id)

        resp = self.client.get(search_url)
        self.assertEqual(resp.status_code, HTTP_200_OK)
        result = json.loads(resp.content.decode('utf-8'))
        self.assertTrue(result['count'] > 0)
Ejemplo n.º 4
0
    def startTest(self, test):
        """
        When preparing the database, check for the `selenium_fixtures`
        attribute and load those.
        """

        from django.test.testcases import call_command

        test_case = get_test_case_class(test)
        fixtures = getattr(test_case, "selenium_fixtures", [])

        if fixtures:
            call_command('loaddata', *fixtures, **{
                'verbosity': 1,
                # Necessary to let the test server access them.
                'commit': True
            })
Ejemplo n.º 5
0
    def _fixture_setup(self):
        """ Ported from TransactionTestCase without the `flush` command call.
        We will leave the DB clean in tearDown """
        from django.test.testcases import (connections, DEFAULT_DB_ALIAS,
            call_command)

        # If the test case has a multi_db=True flag, flush all databases.
        # Otherwise, just flush default.
        if getattr(self, 'multi_db', False):
            databases = connections
        else:
            databases = [DEFAULT_DB_ALIAS]
        for db in databases:

            if hasattr(self, 'fixtures'):
                # We have to use this slightly awkward syntax due to the fact
                # that we're using *args and **kwargs together.
                call_command('loaddata', *self.fixtures,
                             **{'verbosity': 0, 'database': db})
Ejemplo n.º 6
0
    def startTest(self, test):
        """
        When preparing the database, check for the `selenium_fixtures`
        attribute and load those.
        """

        from django.test.testcases import call_command

        test_case = get_test_case_class(test)
        fixtures = getattr(test_case, "selenium_fixtures", [])

        if fixtures:
            call_command(
                'loaddata',
                *fixtures,
                **{
                    'verbosity': 1,
                    # Necessary to let the test server access them.
                    'commit': True
                })
Ejemplo n.º 7
0
    def _fixture_setup(self):
        for db_name in self._databases_names(include_mirrors=False):
            # Reset sequences
            if self.reset_sequences:
                self._reset_sequences(db_name)

            # If we need to provide replica initial data from migrated apps,
            # then do so.
            if self.serialized_rollback and hasattr(connections[db_name], "_test_serialized_contents"):
                if self.available_apps is not None:
                    apps.unset_available_apps()
                connections[db_name].creation.deserialize_db_from_string(
                    connections[db_name]._test_serialized_contents
                )
                if self.available_apps is not None:
                    apps.set_available_apps(self.available_apps)

            if self.fixtures:
                # We have to use this slightly awkward syntax due to the fact
                # that we're using *args and **kwargs together.
                call_command('loaddatabulk', *self.fixtures,
                             **{'verbosity': 0, 'database': db_name})
Ejemplo n.º 8
0
    def setUpClass(cls):
        super(TestCase, cls).setUpClass()
        if not connections_support_transactions():
            return
        cls.cls_atomics = cls._enter_atomics()

        if cls.fixtures:
            for db_name in cls._databases_names(include_mirrors=False):
                    try:
                        call_command('loaddatabulk', *cls.fixtures, **{
                            'verbosity': 0,
                            'commit': False,
                            'database': db_name,
                        })
                    except Exception:
                        cls._rollback_atomics(cls.cls_atomics)
                        raise
        try:
            cls.setUpTestData()
        except Exception:
            cls._rollback_atomics(cls.cls_atomics)
            raise
Ejemplo n.º 9
0
    def setUpClass(cls):
        super(TestCase, cls).setUpClass()
        if not connections_support_transactions():
            return
        cls.cls_atomics = cls._enter_atomics()

        if cls.fixtures:
            for db_name in cls._databases_names(include_mirrors=False):
                try:
                    call_command(
                        'loaddatabulk', *cls.fixtures, **{
                            'verbosity': 0,
                            'commit': False,
                            'database': db_name,
                        })
                except Exception:
                    cls._rollback_atomics(cls.cls_atomics)
                    raise
        try:
            cls.setUpTestData()
        except Exception:
            cls._rollback_atomics(cls.cls_atomics)
            raise
Ejemplo n.º 10
0
    def run(self):
        """
        Sets up test server and database and loops over handling http requests.
        """
        try:
            handler = basehttp.AdminMediaHandler(WSGIHandler())
            server_address = (self.address, self.port)
            httpd = StoppableWSGIServer(server_address,
                                        WSGIRequestHandler)
            httpd.set_app(handler)
            self.started.set()
        except basehttp.WSGIServerException as e:
            self.error = e
            self.started.set()
            return

        # Must do database stuff in this new thread if database in memory.
        from django.conf import settings

        if hasattr(settings, 'DATABASES'):
            db_engine = settings.DATABASES['default']['ENGINE']
            test_db_name = settings.DATABASES['default']['TEST_NAME']
        else:
            db_engine = settings.DATABASE_ENGINE
            test_db_name = settings.TEST_DATABASE_NAME

        if (db_engine.endswith('sqlite3') and
            (not test_db_name or test_db_name == ':memory:')):
            # Import the fixture data into the test database.
            if hasattr(self, 'fixtures'):
                # We have to use this slightly awkward syntax due to the fact
                # that we're using *args and **kwargs together.
                testcases.call_command('loaddata', verbosity=0, *self.fixtures)

        # Loop until we get a stop event.
        while not self._stopevent.isSet():
            httpd.handle_request()
Ejemplo n.º 11
0
    def run(self):
        """
        Sets up test server and database and loops over handling http requests.
        """
        try:
            handler = basehttp.AdminMediaHandler(WSGIHandler())
            server_address = (self.address, self.port)
            httpd = StoppableWSGIServer(server_address,
                                        WSGIRequestHandler)
            httpd.set_app(handler)
            self.started.set()
        except basehttp.WSGIServerException as e:
            self.error = e
            self.started.set()
            return

        # Must do database stuff in this new thread if database in memory.
        from django.conf import settings

        if hasattr(settings, 'DATABASES'):
            db_engine = settings.DATABASES['default']['ENGINE']
            test_db_name = settings.DATABASES['default']['TEST_NAME']
        else:
            db_engine = settings.DATABASE_ENGINE
            test_db_name = settings.TEST_DATABASE_NAME

        if (db_engine.endswith('sqlite3') and
            (not test_db_name or test_db_name == ':memory:')):
            # Import the fixture data into the test database.
            if hasattr(self, 'fixtures'):
                # We have to use this slightly awkward syntax due to the fact
                # that we're using *args and **kwargs together.
                testcases.call_command('loaddata', verbosity=0, *self.fixtures)

        # Loop until we get a stop event.
        while not self._stopevent.isSet():
            httpd.handle_request()
Ejemplo n.º 12
0
    def _fixture_setup(self):
        for db_name in self._databases_names(include_mirrors=False):
            # Reset sequences
            if self.reset_sequences:
                self._reset_sequences(db_name)

            # If we need to provide replica initial data from migrated apps,
            # then do so.
            if self.serialized_rollback and hasattr(
                    connections[db_name], "_test_serialized_contents"):
                if self.available_apps is not None:
                    apps.unset_available_apps()
                connections[db_name].creation.deserialize_db_from_string(
                    connections[db_name]._test_serialized_contents)
                if self.available_apps is not None:
                    apps.set_available_apps(self.available_apps)

            if self.fixtures:
                # We have to use this slightly awkward syntax due to the fact
                # that we're using *args and **kwargs together.
                call_command('loaddatabulk', *self.fixtures, **{
                    'verbosity': 0,
                    'database': db_name
                })