Beispiel #1
0
    def test_setup_elasticsearch_index_names(self):
        """Assert index names are replaced by test values."""
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['test_index_1', 'test_index_2']
            }
        }
        indices = {
            'test_index_1': {
                'NAME': 'index_prod',
            },
            'test_index_2': {
                'NAME': 'index_prod_backup',
            }
        }

        with override_settings(ES_SERVERS=servers, ES_INDICES=indices):
            from djangoes import connections

            conn = connections['default']
            expected_names = ['index_prod', 'index_prod_backup']
            assert sorted(conn.index_names) == sorted(expected_names)

            setup_djangoes()

            expected_names = sorted(['index_prod_test',
                                     'index_prod_backup_test'])
            assert sorted(conn.index_names) == expected_names
Beispiel #2
0
    def test_setup_elasticsearch_global_override_aliases(self):
        """Assert server test settings can override globally test aliases."""
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['index'],
                'TEST': {
                    'INDICES': ['override']
                }
            }
        }
        indices = {
            'index': {
                'NAME': 'index_prod',
                'ALIASES': ['alias_prod']
            },
            'override': {
                'NAME': 'overridden',
                'ALIASES': ['alias_overridden']
            }
        }

        with override_settings(ES_SERVERS=servers, ES_INDICES=indices):
            from djangoes import connections

            conn = connections['default']
            assert conn.indices == ['alias_prod']

            setup_djangoes()

            assert conn.indices == ['alias_overridden_test']
Beispiel #3
0
    def test_setup_elasticsearch_no_test_indices(self):
        """Assert tests can not be set up when settings are invalid.

        Each server connection must provide either a list of indices, or a test
        settings with a list of indices to use for tests.
        """
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': [],
                'TEST': {
                    'INDICES': []
                }
            }
        }
        indices = {}

        with override_settings(ES_SERVERS=servers, ES_INDICES=indices):
            from djangoes import connections

            conn = connections['default']
            assert conn.indices == []

            with self.assertRaises(RuntimeError):
                setup_djangoes()
Beispiel #4
0
    def test_setup_elasticsearch_index_names(self):
        """Assert index names are replaced by test values."""
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['test_index_1', 'test_index_2']
            }
        }
        indices = {
            'test_index_1': {
                'NAME': 'index_prod',
            },
            'test_index_2': {
                'NAME': 'index_prod_backup',
            }
        }

        with override_settings(ES_SERVERS=servers, ES_INDICES=indices):
            from djangoes import connections

            conn = connections['default']
            expected_names = ['index_prod', 'index_prod_backup']
            assert sorted(conn.index_names) == sorted(expected_names)

            setup_djangoes()

            expected_names = sorted(
                ['index_prod_test', 'index_prod_backup_test'])
            assert sorted(conn.index_names) == expected_names
Beispiel #5
0
    def test_setup_elasticsearch_global_override_aliases(self):
        """Assert server test settings can override globally test aliases."""
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['index'],
                'TEST': {
                    'INDICES': ['override']
                }
            }
        }
        indices = {
            'index': {
                'NAME': 'index_prod',
                'ALIASES': ['alias_prod']
            },
            'override': {
                'NAME': 'overridden',
                'ALIASES': ['alias_overridden']
            }
        }

        with override_settings(ES_SERVERS=servers, ES_INDICES=indices):
            from djangoes import connections

            conn = connections['default']
            assert conn.indices == ['alias_prod']

            setup_djangoes()

            assert conn.indices == ['alias_overridden_test']
Beispiel #6
0
    def test_setup_elasticsearch_no_test_indices(self):
        """Assert tests can not be set up when settings are invalid.

        Each server connection must provide either a list of indices, or a test
        settings with a list of indices to use for tests.
        """
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': [],
                'TEST': {
                    'INDICES': []
                }
            }
        }
        indices = {}

        with override_settings(ES_SERVERS=servers, ES_INDICES=indices):
            from djangoes import connections

            conn = connections['default']
            assert conn.indices == []

            with self.assertRaises(RuntimeError):
                setup_djangoes()
Beispiel #7
0
    def test_setup_elasticsearch_settings_with_settings(self):
        """Assert index settings are replaced by test values."""
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['test_index']
            }
        }
        indices = {
            'test_index': {
                'NAME': 'index_prod',
                'ALIASES': ['alias_prod'],
                'SETTINGS': {
                    'index': {
                        'number_of_replicas': 1
                    }
                },
                'TEST': {
                    'SETTINGS': {
                        'index': {
                            'number_of_replicas': 0
                        }
                    },
                }
            }
        }

        with override_settings(ES_SERVERS=servers, ES_INDICES=indices):
            from djangoes import connections

            conn = connections['default']
            index_settings = conn.get_indices_with_settings()

            assert 'index_prod' in index_settings
            assert index_settings['index_prod'] == {
                'index': {
                    'number_of_replicas': 1
                }
            }

            setup_djangoes()

            index_settings = conn.get_indices_with_settings()

            assert 'index_prod' not in index_settings
            assert 'index_prod_test' in index_settings
            assert index_settings['index_prod_test'] == {
                'index': {
                    'number_of_replicas': 0
                }
            }
Beispiel #8
0
    def test_setup_elasticsearch_settings_with_settings(self):
        """Assert index settings are replaced by test values."""
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['test_index']
            }
        }
        indices = {
            'test_index': {
                'NAME': 'index_prod',
                'ALIASES': ['alias_prod'],
                'SETTINGS': {
                    'index': {
                        'number_of_replicas': 1
                    }
                },
                'TEST': {
                    'SETTINGS': {
                        'index': {
                            'number_of_replicas': 0
                        }
                    },
                }
            }
        }

        with override_settings(ES_SERVERS=servers, ES_INDICES=indices):
            from djangoes import connections

            conn = connections['default']
            index_settings = conn.get_indices_with_settings()

            assert 'index_prod' in index_settings
            assert index_settings['index_prod'] == {
                'index': {
                    'number_of_replicas': 1
                }
            }

            setup_djangoes()

            index_settings = conn.get_indices_with_settings()

            assert 'index_prod' not in index_settings
            assert 'index_prod_test' in index_settings
            assert index_settings['index_prod_test'] == {
                'index': {
                    'number_of_replicas': 0
                }
            }
Beispiel #9
0
    def test_setup_elasticsearch_global_override_reuse_index(self):
        """Assert server test settings can override globally test indices."""
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['index'],
                'TEST': {
                    'INDICES': ['override']
                }
            },
            'copy': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['index'],
                'TEST': {
                    'INDICES': ['override']
                }
            }
        }
        indices = {
            'index': {
                'NAME': 'index_prod',
            },
            'override': {
                'NAME': 'overridden',
            }
        }

        with override_settings(ES_SERVERS=servers, ES_INDICES=indices):
            from djangoes import connections

            setup_djangoes()
            conn = connections['default']
            conn_copy = connections['copy']

            expected_indices = sorted(['overridden_test'])
            assert conn.indices == expected_indices
            assert conn_copy.indices == expected_indices
Beispiel #10
0
    def test_setup_elasticsearch_global_override_reuse_index(self):
        """Assert server test settings can override globally test indices."""
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['index'],
                'TEST': {
                    'INDICES': ['override']
                }
            },
            'copy': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['index'],
                'TEST': {
                    'INDICES': ['override']
                }
            }
        }
        indices = {
            'index': {
                'NAME': 'index_prod',
            },
            'override': {
                'NAME': 'overridden',
            }
        }

        with override_settings(ES_SERVERS=servers, ES_INDICES=indices):
            from djangoes import connections

            setup_djangoes()
            conn = connections['default']
            conn_copy = connections['copy']

            expected_indices = sorted(['overridden_test'])
            assert conn.indices == expected_indices
            assert conn_copy.indices == expected_indices
Beispiel #11
0
    def test_setup_elasticsearch_reuse_index(self):
        """Assert indices and index names are replaced by test values.

        In this case, we want to be sure that two connections can access the
        same index, and that the configuration of one won't cause any issue.
        """
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['index']
            },
            'copy' : {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['index']
            }
        }
        indices = {
            'index': {
                'NAME': 'index_prod',
                'TEST': {
                    'NAME': 'index_test'
                }
            },
        }

        with override_settings(ES_SERVERS=servers, ES_INDICES=indices):
            from djangoes import connections

            setup_djangoes()

            conn = connections['default']
            conn_copy = connections['copy']

            expected_indices = ['index_test']
            assert conn.indices == expected_indices
            assert conn_copy.indices == expected_indices
Beispiel #12
0
    def test_setup_elasticsearch_reuse_index(self):
        """Assert indices and index names are replaced by test values.

        In this case, we want to be sure that two connections can access the
        same index, and that the configuration of one won't cause any issue.
        """
        servers = {
            'default': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['index']
            },
            'copy': {
                'ENGINE': 'tests.backend.ConnectionWrapper',
                'INDICES': ['index']
            }
        }
        indices = {
            'index': {
                'NAME': 'index_prod',
                'TEST': {
                    'NAME': 'index_test'
                }
            },
        }

        with override_settings(ES_SERVERS=servers, ES_INDICES=indices):
            from djangoes import connections

            setup_djangoes()

            conn = connections['default']
            conn_copy = connections['copy']

            expected_indices = ['index_test']
            assert conn.indices == expected_indices
            assert conn_copy.indices == expected_indices