package org.nlpcn.es4sql;


import junit.framework.Assert;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.nlpcn.es4sql.exception.SqlParseException;

import java.io.IOException;
import java.sql.SQLFeatureNotSupportedException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.nlpcn.es4sql.TestsConstants.TEST_INDEX;

public class DeleteTest {

	@Before
	public void loadTempData() throws Exception {
		MainTestSuite.loadBulk("src/test/resources/accounts_temp.json");
	}

	@After
	public void deleteTempData() throws Exception {
		MainTestSuite.deleteQuery(TEST_INDEX, "account_temp");
	}


	@Test
	public void deleteAllTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
		delete(String.format("DELETE FROM %s/account_temp", TEST_INDEX));

		// Assert no results exist for this type.
		SearchRequestBuilder request = MainTestSuite.getClient().prepareSearch(TEST_INDEX);
		request.setTypes("account_temp");
		SearchResponse response = request.setQuery(QueryBuilders.matchAllQuery()).get();
		assertThat(response.getHits().getTotalHits(), equalTo(0L));
	}


	private void delete(String deleteStatement) throws SqlParseException, SQLFeatureNotSupportedException {
		SearchDao searchDao = MainTestSuite.getSearchDao();
		searchDao.explain(deleteStatement).get();
	}
}

@@ -1391,16 +1391,541 @@
 );%0A%09%7D%0A%0A%0A
+%09@Test%0A%09public void deleteWithConditionTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException %7B%0A%09%09delete(String.format(%22DELETE FROM %25s/phrase WHERE phrase = 'quick fox here' %22, TEST_INDEX));%0A%0A%09%09// Assert no results exist for this type.%0A%09%09SearchRequestBuilder request = MainTestSuite.getClient().prepareSearch(TEST_INDEX);%0A%09%09request.setTypes(%22phrase%22);%0A%09%09SearchResponse response = request.setQuery(QueryBuilders.matchAllQuery()).get();%0A%09%09assertThat(response.getHits().getTotalHits(), equalTo(3L));%0A%09%7D%0A%0A%0A
 %09private

add delete test with condition