def check_basic_setup_and_get_models(self, index_name='movies', expected_num_versions=1, pre_message="At test setup") -> \ Tuple[Index, IndexVersion, DEMIndex]: dem_index = DEMIndexManager.get_dem_index(index_name) index_model = dem_index.get_index_model() version_model = dem_index.get_version_model() expected_msg = "The {} index should have an active version.".format( index_name) self.assertIsNotNone(version_model, expected_msg) self.check_num_available_versions(index_model, pre_message, expected_num_versions) expected_msg = "The {} index should already exist in elasticsearch.".format( version_model.name) self.assertTrue(version_model.exists_in_es(), expected_msg) return index_model, version_model, dem_index
def test_newer_flag(self): """ Test that when two versions of the "movies" index are available, and the older one is activated, `./manage.py es_update movies --newer` indexes into the newer, unactivated version """ index_model, version_model, dem_index = self.check_basic_setup_and_get_models( ) # create two newer versions of the movies index call_command(*"es_create movies --force".split()) call_command(*"es_create movies --force".split()) avail_versions = self.check_num_available_versions( index_model, "After 2x './manage.py es_create movies --force',", 3) command = "es_update movies --newer" call_command(*command.split()) after_phrase = "After `{}`,".format(command) last_actions = self.check_last_index_actions( index_model, after_phrase, 5, expected_actions=[ # the parent update index action IndexAction.ACTION_UPDATE_INDEX, # 1st newer index update index action IndexAction.ACTION_UPDATE_INDEX, IndexAction.ACTION_PARTIAL_UPDATE_INDEX, # 2nd newer index update index actions IndexAction.ACTION_UPDATE_INDEX, IndexAction.ACTION_PARTIAL_UPDATE_INDEX, ]) first_action = last_actions[0] first_action_version = first_action.index_version self.assertIsNone( first_action_version, "{} expected parent UpdateIndexAction to be None, " "but was {}".format(after_phrase, str(first_action_version))) self.assertEqual( first_action.docs_affected, 4, "{} expected the parent UpdateIndexAction to have " "4 docs affected, but was {}".format(after_phrase, first_action.docs_affected)) actual_num_docs = dem_index.get_num_docs() self.assertEqual( actual_num_docs, 0, "{after_phrase} " "The original IndexVersion {index_name} was expected " "to have 0 docs, instead, it had {actual_num}".format( after_phrase=after_phrase, index_name=version_model.name, actual_num=actual_num_docs)) for i in [1, 3]: action = last_actions[i] self.assertEqual(action.docs_affected, 2) new_version_model = last_actions[i].index_version new_dem_index = DEMIndexManager.get_dem_index( new_version_model.name, exact_mode=True) actual_num_docs = new_dem_index.get_num_docs() self.assertEqual( actual_num_docs, 2, "{after_phrase} " "{index_name} was expected to have " "2 docs, instead, it had {actual_num}".format( after_phrase=after_phrase, index_name=new_version_model, actual_num=actual_num_docs))