Ejemplo n.º 1
0
    def test_get_server_indices(self):
        """Assert indices are found for a given server."""
        servers = {}
        indices = {
            'used': {},
            'not_used': {}
        }
        handler = ConnectionHandler(servers, indices)

        test_server = {
            'INDICES': ['used'],
        }

        indices = handler.get_server_indices(test_server)
        expected_indices = {
            'used': {
                'NAME': 'used',
                'ALIASES': [],
                'SETTINGS': None,
                'TEST': {
                    'NAME': 'used_test',
                    'ALIASES': [],
                    'SETTINGS': None,
                }
            }
        }

        assert indices == expected_indices
Ejemplo n.º 2
0
    def test_prepare_index_test_settings_aliases_improperly_configured(self):
        """Assert raise when name and test name are the same."""
        servers = {}
        indices = {
            'index': {
                'NAME': 'index',
                'ALIASES': ['alias_prod', 'alias_prod_2'],
                'TEST': {
                    'NAME': 'index_valid_test_name',
                    'ALIASES': ['alias_prod', 'alias_test']
                }
            }
        }

        handler = ConnectionHandler(servers, indices)
        handler.ensure_index_defaults('index')

        with self.assertRaises(ImproperlyConfigured) as raised:
            # A simple call to servers must raise.
            handler.prepare_index_test_settings('index')

        assert str(raised.exception) == (
            'Index \'index\' uses improperly the same index alias in ALIASES '
            'and in TEST\'s ALIASES settings: \'alias_prod\'.'
        )
Ejemplo n.º 3
0
    def test_items(self):
        """Assertions about key:value behavior of ConnectionHandler."""
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['index_1'],
            },
        }
        indices = {'index_1': {}, 'index_2': {}}

        handler = ConnectionHandler(servers, indices)

        # Get the connection wrapper
        wrapper = handler['default']
        assert wrapper.indices == ['index_1']

        # Change handler settings
        handler.servers['default']['INDICES'] = ['index_2']

        # The wrapper is not updated
        wrapper = handler['default']
        assert wrapper.indices == ['index_1']

        # Delete the `default` connection
        del handler['default']

        # The new wrapper now use the new index
        wrapper = handler['default']
        assert wrapper.indices == ['index_2']

        # Also, set item works without control
        handler['something'] = 'else'
        assert handler['something'] == 'else'
Ejemplo n.º 4
0
    def test_get_server_indices(self):
        """Assert indices are found for a given server."""
        servers = {}
        indices = {'used': {}, 'not_used': {}}
        handler = ConnectionHandler(servers, indices)

        test_server = {
            'INDICES': ['used'],
        }

        indices = handler.get_server_indices(test_server)
        expected_indices = {
            'used': {
                'NAME': 'used',
                'ALIASES': [],
                'SETTINGS': None,
                'TEST': {
                    'NAME': 'used_test',
                    'ALIASES': [],
                    'SETTINGS': None,
                }
            }
        }

        assert indices == expected_indices
Ejemplo n.º 5
0
    def test_check_for_multiprocess(self):
        """Assert method will reset connections with a different PID.

        .. note::

            We don't really test "multi-processing" behavior. We are only
            messing with a flag here to test connections reset.

        """
        servers = {'default': {'HOSTS': ['localhost']}}

        handler = ConnectionHandler(servers, {})

        conn = handler['default']
        conn_again = handler['default']

        assert conn is conn_again
        assert id(conn) == id(conn_again)

        # Changing the PID to "reset" connections.
        handler._pid = 1
        conn_again = handler['default']

        assert conn is not conn_again
        assert id(conn) != id(conn_again)
Ejemplo n.º 6
0
    def test_check_for_multiprocess(self):
        """Assert method will reset connections with a different PID.

        .. note::

            We don't really test "multi-processing" behavior. We are only
            messing with a flag here to test connections reset.

        """
        servers = {
            'default': {
                'HOSTS': ['localhost']
            }
        }

        handler = ConnectionHandler(servers, {})

        conn = handler['default']
        conn_again = handler['default']

        assert conn is conn_again
        assert id(conn) == id(conn_again)

        # Changing the PID to "reset" connections.
        handler._pid = 1
        conn_again = handler['default']

        assert conn is not conn_again
        assert id(conn) != id(conn_again)
Ejemplo n.º 7
0
    def test_ensure_server_defaults_not_exists(self):
        """Assert raise when the argument given is not a configured server."""
        servers = {}
        indices = {}

        handler = ConnectionHandler(servers, indices)

        with self.assertRaises(ConnectionDoesNotExist) as raised:
            handler.ensure_server_defaults('index')

        assert str(raised.exception) == '%r' % 'index'
Ejemplo n.º 8
0
    def test_ensure_server_defaults_not_exists(self):
        """Assert raise when the argument given is not a configured server."""
        servers = {}
        indices = {}

        handler = ConnectionHandler(servers, indices)

        with self.assertRaises(ConnectionDoesNotExist) as raised:
            handler.ensure_server_defaults('index')

        assert str(raised.exception) == '%r' % 'index'
Ejemplo n.º 9
0
    def test_prepare_index_test_settings_not_exists(self):
        """Assert raise when the argument given is not a configured index."""
        servers = {}
        indices = {}

        handler = ConnectionHandler(servers, indices)

        with self.assertRaises(IndexDoesNotExist) as raised:
            handler.prepare_index_test_settings('index')

        assert str(raised.exception) == '%r' % 'index'
Ejemplo n.º 10
0
    def test_prepare_index_test_settings_not_exists(self):
        """Assert raise when the argument given is not a configured index."""
        servers = {}
        indices = {}

        handler = ConnectionHandler(servers, indices)

        with self.assertRaises(IndexDoesNotExist) as raised:
            handler.prepare_index_test_settings('index')

        assert str(raised.exception) == '%r' % 'index'
Ejemplo n.º 11
0
    def test_loading_elasticsearch(self):
        servers = {
            'default': {
                'ENGINE': 'djangoes.backends.elasticsearch.SimpleHttpBackend'
            }
        }
        indices = {}

        handler = ConnectionHandler(servers, indices)

        result = handler.load_backend('default')

        assert isinstance(result, elasticsearch.SimpleHttpBackend)
Ejemplo n.º 12
0
    def test_load_backend(self):
        """Assert load_backend method loads the configured server engine."""
        servers = {'default': {'ENGINE': 'tests.backend.ConnectionWrapper'}}
        indices = {}
        handler = ConnectionHandler(servers, indices)

        result = handler.load_backend('default')

        assert isinstance(result, Base)
        assert result.alias == 'default'
        assert result.indices == []
        assert result.index_names == []
        assert result.alias_names == []
Ejemplo n.º 13
0
    def test_loading_elasticsearch(self):
        servers = {
            'default': {
                'ENGINE': 'djangoes.backends.elasticsearch.SimpleHttpBackend'
            }
        }
        indices = {}

        handler = ConnectionHandler(servers, indices)

        result = handler.load_backend('default')

        assert isinstance(result, elasticsearch.SimpleHttpBackend)
Ejemplo n.º 14
0
    def test_prepare_server_test_settings_not_exists(self):
        """Assert raise when the argument given is not a configured server."""
        servers = {
            'default': {
                'ENGINE': 'djangoes.backends.elasticsearch.SimpleHttpBackend'
            }
        }
        indices = {}

        handler = ConnectionHandler(servers, indices)

        with self.assertRaises(ConnectionDoesNotExist) as raised:
            handler.prepare_server_test_settings('index')

        assert str(raised.exception) == '%r' % 'index'
Ejemplo n.º 15
0
    def test_prepare_server_test_settings_not_exists(self):
        """Assert raise when the argument given is not a configured server."""
        servers = {
            'default': {
                'ENGINE': 'djangoes.backends.elasticsearch.SimpleHttpBackend'
            }
        }
        indices = {}

        handler = ConnectionHandler(servers, indices)

        with self.assertRaises(ConnectionDoesNotExist) as raised:
            handler.prepare_server_test_settings('index')

        assert str(raised.exception) == '%r' % 'index'
Ejemplo n.º 16
0
    def test_empty_ensure_server_defaults(self):
        """Assert default values are set properly on an empty server."""
        handler = ConnectionHandler({}, {})
        handler.ensure_server_defaults('default')

        default_server = handler.servers['default']

        expected_server = {
            'ENGINE': 'djangoes.backends.elasticsearch.SimpleHttpBackend',
            'HOSTS': [],
            'PARAMS': {},
            'INDICES': []
        }

        assert default_server == expected_server
Ejemplo n.º 17
0
    def test_empty_ensure_server_defaults(self):
        """Assert default values are set properly on an empty server."""
        handler = ConnectionHandler({}, {})
        handler.ensure_server_defaults('default')

        default_server = handler.servers['default']

        expected_server = {
            'ENGINE': 'djangoes.backends.elasticsearch.SimpleHttpBackend',
            'HOSTS': [],
            'PARAMS': {},
            'INDICES': []
        }

        assert default_server == expected_server
Ejemplo n.º 18
0
    def test_empty_get_server_indices(self):
        """Assert there is no index by default, ie. `_all` will be used.

        ElasticSearch allows query on all indices. It is not safe for testing
        purposes, but it does not have to be checked in the connection handler.
        """
        handler = ConnectionHandler({}, {})

        # Yes, it is acceptable to get indices from a non-configured servers.
        # The purpose of get_server_indices is not to validate the input.
        test_server = {'INDICES': []}

        indices = handler.get_server_indices(test_server)

        assert indices == {}
Ejemplo n.º 19
0
    def test_empty_ensure_index_defaults(self):
        """Assert default values are set properly on an empty index."""
        indices = {'index': {}}

        handler = ConnectionHandler({}, indices)
        handler.ensure_index_defaults('index')

        index = handler.indices['index']

        expected_index = {
            'NAME': 'index',
            'ALIASES': [],
            'SETTINGS': None,
        }

        assert index == expected_index
Ejemplo n.º 20
0
    def test_empty_get_server_indices(self):
        """Assert there is no index by default, ie. `_all` will be used.

        ElasticSearch allows query on all indices. It is not safe for testing
        purposes, but it does not have to be checked in the connection handler.
        """
        handler = ConnectionHandler({}, {})

        # Yes, it is acceptable to get indices from a non-configured servers.
        # The purpose of get_server_indices is not to validate the input.
        test_server = {
            'INDICES': []
        }

        indices = handler.get_server_indices(test_server)

        assert indices == {}
Ejemplo n.º 21
0
    def test_load_backend(self):
        """Assert load_backend method loads the configured server engine."""
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper'
            }
        }
        indices = {}
        handler = ConnectionHandler(servers, indices)

        result = handler.load_backend('default')

        assert isinstance(result, Base)
        assert result.alias == 'default'
        assert result.indices == []
        assert result.index_names == []
        assert result.alias_names == []
Ejemplo n.º 22
0
    def test_empty_prepare_server_test_settings(self):
        """Assert prepare adds a TEST key in the defaul server's settings."""
        servers = {
            'default': {
                'ENGINE': 'djangoes.backends.elasticsearch.SimpleHttpBackend'
            }
        }

        handler = ConnectionHandler(servers, {})
        handler.prepare_server_test_settings('default')

        default_server = handler.servers['default']

        expected_test_server = {'INDICES': []}

        assert 'TEST' in default_server
        assert default_server['TEST'] == expected_test_server
Ejemplo n.º 23
0
    def test_empty_ensure_index_defaults(self):
        """Assert default values are set properly on an empty index."""
        indices = {
            'index': {}
        }

        handler = ConnectionHandler({}, indices)
        handler.ensure_index_defaults('index')

        index = handler.indices['index']

        expected_index = {
            'NAME': 'index',
            'ALIASES': [],
            'SETTINGS': None,
        }

        assert index == expected_index
Ejemplo n.º 24
0
    def test_empty_with_default_fallback(self):
        """Assert the fallback configuration is acceptable as input."""
        servers = {'default': {}}
        indices = {}

        handler = ConnectionHandler(servers, indices)

        assert handler.servers == {'default': {}}
        assert handler.indices == {}
Ejemplo n.º 25
0
    def test_empty_prepare_index_test_settings(self):
        indices = {
            'index': {}
        }

        handler = ConnectionHandler({}, indices)
        handler.ensure_index_defaults('index')
        handler.prepare_index_test_settings('index')

        index = handler.indices['index']

        expected_test_index = {
            'NAME': 'index_test',
            'ALIASES': [],
            'SETTINGS': None,
        }

        assert 'TEST' in index
        assert index['TEST'] == expected_test_index
Ejemplo n.º 26
0
    def test_empty_prepare_server_test_settings(self):
        """Assert prepare adds a TEST key in the defaul server's settings."""
        servers = {
            'default': {
                'ENGINE': 'djangoes.backends.elasticsearch.SimpleHttpBackend'
            }
        }

        handler = ConnectionHandler(servers, {})
        handler.prepare_server_test_settings('default')

        default_server = handler.servers['default']

        expected_test_server = {
            'INDICES': []
        }

        assert 'TEST' in default_server
        assert default_server['TEST'] == expected_test_server
Ejemplo n.º 27
0
    def test_empty(self):
        """Assert an empty configuration fallback on default values."""
        servers = {}
        indices = {}

        handler = ConnectionHandler(servers, indices)

        # A default alias appear in servers, while nothing changed in indices.
        assert handler.servers == {'default': {}}
        assert handler.indices == indices
Ejemplo n.º 28
0
 def test_project_settings_by_default(self):
     """Assert values come from the django project settings if not given."""
     servers = {'default': {}, 'by_settings': {}}
     indices = {'index_by_settings': {}}
     with override_settings(ES_SERVERS=servers, ES_INDICES=indices):
         # No argument
         handler = ConnectionHandler()
         # Servers and indices are the one set in django settings.
         assert handler.servers == servers
         assert handler.indices == indices
Ejemplo n.º 29
0
    def test_prepare_index_test_settings_use_alias_not_index_name(self):
        """Assert raise even if the index NAME is given as argument.

        The prepare_index_test_settings method expects an index alias as used
        in the indices dict, not its NAME (nor any of its ALIASES).
        """
        servers = {}
        indices = {
            'index': {
                'NAME': 'not_this_index',
                'ALIASES': ['not_this_index']
            }
        }

        handler = ConnectionHandler(servers, indices)

        with self.assertRaises(IndexDoesNotExist) as raised:
            handler.prepare_index_test_settings('not_this_index')

        assert str(raised.exception) == '%r' % 'not_this_index'
Ejemplo n.º 30
0
    def test_prepare_index_test_settings_use_alias_not_index_name(self):
        """Assert raise even if the index NAME is given as argument.

        The prepare_index_test_settings method expects an index alias as used
        in the indices dict, not its NAME (nor any of its ALIASES).
        """
        servers = {}
        indices = {
            'index': {
                'NAME': 'not_this_index',
                'ALIASES': ['not_this_index']
            }
        }

        handler = ConnectionHandler(servers, indices)

        with self.assertRaises(IndexDoesNotExist) as raised:
            handler.prepare_index_test_settings('not_this_index')

        assert str(raised.exception) == '%r' % 'not_this_index'
Ejemplo n.º 31
0
    def test_load_backend_with_index(self):
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['index_1'],
            }
        }
        indices = {
            'index_1': {
                'NAME': 'index_1',
                'ALIASES': ['alias_1', 'alias_2'],
            }
        }
        handler = ConnectionHandler(servers, indices)

        result = handler.load_backend('default')

        assert sorted(result.indices) == ['alias_1', 'alias_2']
        assert result.index_names == ['index_1']
        assert sorted(result.alias_names) == ['alias_1', 'alias_2']
Ejemplo n.º 32
0
    def test_load_backend_with_index(self):
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['index_1'],
            }
        }
        indices = {
            'index_1': {
                'NAME': 'index_1',
                'ALIASES': ['alias_1', 'alias_2'],
            }
        }
        handler = ConnectionHandler(servers, indices)

        result = handler.load_backend('default')

        assert sorted(result.indices) == ['alias_1', 'alias_2']
        assert result.index_names == ['index_1']
        assert sorted(result.alias_names) == ['alias_1', 'alias_2']
Ejemplo n.º 33
0
    def test_all(self):
        """Assert all connection wrappers are returned."""
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
            },
            'task': {
                'ENGINE': 'tests.backend.ConnectionWrapper'
            }
        }
        indices = {}
        handler = ConnectionHandler(servers, indices)

        all_connections = handler.all()

        assert len(all_connections) == 2
        assert isinstance(all_connections[0], Base)
        assert isinstance(all_connections[1], Base)

        assert sorted([c.alias for c in all_connections]) == ['default', 'task']
Ejemplo n.º 34
0
    def test_iterable(self):
        """Assertions about list behavior of ConnectionHandler."""
        servers = {
            'default': {},
            'task': {},
        }
        indices = {}

        handler = ConnectionHandler(servers, indices)

        assert sorted(list(handler)) == ['default', 'task']
Ejemplo n.º 35
0
    def test_improperly_configured_servers(self):
        """Assert raise when settings are not empty but without `default`."""
        servers = {'not_default': {}}

        handler = ConnectionHandler(servers, {})

        with self.assertRaises(ImproperlyConfigured) as raised:
            # A simple call to servers must raise.
            handler.servers

        assert str(raised.exception
                   ) == "You must define a 'default' ElasticSearch server"
Ejemplo n.º 36
0
    def test_all(self):
        """Assert all connection wrappers are returned."""
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
            },
            'task': {
                'ENGINE': 'tests.backend.ConnectionWrapper'
            }
        }
        indices = {}
        handler = ConnectionHandler(servers, indices)

        all_connections = handler.all()

        assert len(all_connections) == 2
        assert isinstance(all_connections[0], Base)
        assert isinstance(all_connections[1], Base)

        assert sorted([c.alias
                       for c in all_connections]) == ['default', 'task']
Ejemplo n.º 37
0
    def test_prepare_index_test_settings_name_improperly_configured(self):
        """Assert raise when name and test name are the same."""
        servers = {}
        indices = {
            'index': {
                'NAME': 'index_production_name',
                'ALIASES': [],
                'TEST': {
                    'NAME': 'index_production_name',
                    'ALIASES': [],
                }
            }
        }

        handler = ConnectionHandler(servers, indices)

        with self.assertRaises(ImproperlyConfigured) as raised:
            # A simple call to servers must raise.
            handler.prepare_index_test_settings('index')

        assert str(raised.exception) == (
            'Index \'index\' uses improperly the same NAME and TEST\'s NAME '
            'settings: \'index_production_name\'.')
Ejemplo n.º 38
0
    def test_prepare_index_test_settings_name_improperly_configured(self):
        """Assert raise when name and test name are the same."""
        servers = {}
        indices = {
            'index': {
                'NAME': 'index_production_name',
                'ALIASES': [],
                'TEST': {
                    'NAME': 'index_production_name',
                    'ALIASES': [],
                }
            }
        }

        handler = ConnectionHandler(servers, indices)

        with self.assertRaises(ImproperlyConfigured) as raised:
            # A simple call to servers must raise.
            handler.prepare_index_test_settings('index')

        assert str(raised.exception) == (
            'Index \'index\' uses improperly the same NAME and TEST\'s NAME '
                'settings: \'index_production_name\'.'
        )
Ejemplo n.º 39
0
    def test_items(self):
        """Assertions about key:value behavior of ConnectionHandler."""
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['index_1'],
            },
        }
        indices = {
            'index_1': {},
            'index_2': {}
        }

        handler = ConnectionHandler(servers, indices)

        # Get the connection wrapper
        wrapper = handler['default']
        assert wrapper.indices == ['index_1']

        # Change handler settings
        handler.servers['default']['INDICES'] = ['index_2']

        # The wrapper is not updated
        wrapper = handler['default']
        assert wrapper.indices == ['index_1']

        # Delete the `default` connection 
        del handler['default']

        # The new wrapper now use the new index
        wrapper = handler['default']
        assert wrapper.indices == ['index_2']

        # Also, set item works without control
        handler['something'] = 'else'
        assert handler['something'] == 'else'
Ejemplo n.º 40
0
    def test_empty_with_default(self):
        """Assert the ensured default configuration is acceptable as input."""
        servers = {
            'default': {
                'ENGINE': 'djangoes.backends.elasticsearch.SimpleHttpBackend',
                'HOSTS': [],
                'PARAMS': {},
                'INDICES': []
            }
        }
        indices = {'index': {'NAME': 'index', 'ALIASES': []}}

        handler = ConnectionHandler(servers, indices)

        # Both must be equal, without changes.
        assert handler.servers == servers
        assert handler.indices == indices
Ejemplo n.º 41
0
    def test_empty_prepare_index_test_settings(self):
        indices = {'index': {}}

        handler = ConnectionHandler({}, indices)
        handler.ensure_index_defaults('index')
        handler.prepare_index_test_settings('index')

        index = handler.indices['index']

        expected_test_index = {
            'NAME': 'index_test',
            'ALIASES': [],
            'SETTINGS': None,
        }

        assert 'TEST' in index
        assert index['TEST'] == expected_test_index
Ejemplo n.º 42
0
    def test_prepare_index_test_settings_aliases_improperly_configured(self):
        """Assert raise when name and test name are the same."""
        servers = {}
        indices = {
            'index': {
                'NAME': 'index',
                'ALIASES': ['alias_prod', 'alias_prod_2'],
                'TEST': {
                    'NAME': 'index_valid_test_name',
                    'ALIASES': ['alias_prod', 'alias_test']
                }
            }
        }

        handler = ConnectionHandler(servers, indices)
        handler.ensure_index_defaults('index')

        with self.assertRaises(ImproperlyConfigured) as raised:
            # A simple call to servers must raise.
            handler.prepare_index_test_settings('index')

        assert str(raised.exception) == (
            'Index \'index\' uses improperly the same index alias in ALIASES '
            'and in TEST\'s ALIASES settings: \'alias_prod\'.')