Пример #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
Пример #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\'.'
        )
Пример #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'
Пример #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
Пример #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)
Пример #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)
Пример #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'
Пример #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'
Пример #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'
Пример #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'
Пример #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)
Пример #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 == []
Пример #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)
Пример #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'
Пример #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'
Пример #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
Пример #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
Пример #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 == {}
Пример #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
Пример #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 == {}
Пример #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 == []
Пример #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
Пример #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
Пример #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 == {}
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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'
Пример #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'
Пример #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']
Пример #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']
Пример #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']
Пример #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']
Пример #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"
Пример #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']
Пример #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\'.')
Пример #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\'.'
        )
Пример #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'
Пример #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
Пример #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
Пример #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\'.')