Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit 8f8e1b0

Browse files
authored
Migrating DateFunctionsTest, DeleteTest (#70)
* Migrate intgtest/DateFunctionsTest to esintgtest/DateFunctionIT * Migrate intgtest/DeleteTest to esintgtest/DeleteIT
1 parent 4048fab commit 8f8e1b0

File tree

5 files changed

+141
-125
lines changed

5 files changed

+141
-125
lines changed

src/test/java/com/amazon/opendistroforelasticsearch/sql/intgtest/DateFunctionsTest.java renamed to src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/DateFunctionsIT.java

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,28 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16-
package com.amazon.opendistroforelasticsearch.sql.intgtest;
16+
package com.amazon.opendistroforelasticsearch.sql.esintgtest;
1717

18-
import com.alibaba.druid.sql.parser.ParserException;
19-
import com.amazon.opendistroforelasticsearch.sql.plugin.SearchDao;
20-
import com.amazon.opendistroforelasticsearch.sql.exception.SqlParseException;
21-
import com.amazon.opendistroforelasticsearch.sql.query.SqlElasticSearchRequestBuilder;
2218
import org.elasticsearch.action.search.SearchResponse;
19+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
20+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
21+
import org.elasticsearch.common.xcontent.XContentFactory;
22+
import org.elasticsearch.common.xcontent.XContentParser;
23+
import org.elasticsearch.common.xcontent.XContentType;
2324
import org.elasticsearch.search.SearchHit;
2425
import org.joda.time.DateTime;
2526
import org.joda.time.format.DateTimeFormat;
2627
import org.joda.time.format.DateTimeFormatter;
28+
import org.json.JSONObject;
2729
import org.junit.Test;
2830

29-
import java.sql.SQLFeatureNotSupportedException;
31+
import java.io.IOException;
3032

31-
import static org.hamcrest.MatcherAssert.assertThat;
3233
import static org.hamcrest.Matchers.equalTo;
3334
import static org.hamcrest.Matchers.greaterThan;
3435
import static org.hamcrest.Matchers.lessThan;
3536

36-
public class DateFunctionsTest {
37+
public class DateFunctionsIT extends SQLIntegTestCase {
3738

3839
private static final String FROM = "FROM " + TestsConstants.TEST_INDEX_ONLINE + "/online";
3940

@@ -45,10 +46,15 @@ public class DateFunctionsTest {
4546
* can be expanded on by supporting CAST and casting dates given as Strings to TIMESTAMP (SQL's date type).
4647
*/
4748

49+
@Override
50+
protected void init() throws Exception {
51+
loadIndex(Index.ONLINE);
52+
}
53+
4854
@Test
49-
public void year() {
55+
public void year() throws IOException {
5056
SearchHit[] hits = query(
51-
"SELECT YEAR(insert_time) as year"
57+
"SELECT YEAR(insert_time) as year"
5258
);
5359
for (SearchHit hit : hits) {
5460
int year = (int) getField(hit, "year");
@@ -58,9 +64,9 @@ public void year() {
5864
}
5965

6066
@Test
61-
public void monthOfYear() {
67+
public void monthOfYear() throws IOException {
6268
SearchHit[] hits = query(
63-
"SELECT MONTH_OF_YEAR(insert_time) as month_of_year"
69+
"SELECT MONTH_OF_YEAR(insert_time) as month_of_year"
6470
);
6571
for (SearchHit hit : hits) {
6672
int monthOfYear = (int) getField(hit, "month_of_year");
@@ -70,9 +76,9 @@ public void monthOfYear() {
7076
}
7177

7278
@Test
73-
public void weekOfYearInSelect() {
79+
public void weekOfYearInSelect() throws IOException {
7480
SearchHit[] hits = query(
75-
"SELECT WEEK_OF_YEAR(insert_time) as week_of_year"
81+
"SELECT WEEK_OF_YEAR(insert_time) as week_of_year"
7682
);
7783
for (SearchHit hit : hits) {
7884
int weekOfYear = (int) getField(hit, "week_of_year");
@@ -82,12 +88,12 @@ public void weekOfYearInSelect() {
8288
}
8389

8490
@Test
85-
public void weekOfYearInWhere() {
91+
public void weekOfYearInWhere() throws IOException {
8692
SearchHit[] hits = query(
87-
"SELECT insert_time",
88-
"WHERE DATE_FORMAT(insert_time, 'YYYY-MM-dd') < '2014-08-19' AND " +
89-
"WEEK_OF_YEAR(insert_time) > 33",
90-
"LIMIT 2000"
93+
"SELECT insert_time",
94+
"WHERE DATE_FORMAT(insert_time, 'YYYY-MM-dd') < '2014-08-19' AND " +
95+
"WEEK_OF_YEAR(insert_time) > 33",
96+
"LIMIT 2000"
9197
);
9298
for (SearchHit hit : hits) {
9399
DateTime insertTime = getDateFromSource(hit, "insert_time");
@@ -96,9 +102,9 @@ public void weekOfYearInWhere() {
96102
}
97103

98104
@Test
99-
public void dayOfYearInSelect() {
105+
public void dayOfYearInSelect() throws IOException {
100106
SearchHit[] hits = query(
101-
"SELECT DAY_OF_YEAR(insert_time) as day_of_year", "LIMIT 2000"
107+
"SELECT DAY_OF_YEAR(insert_time) as day_of_year", "LIMIT 2000"
102108
);
103109
for (SearchHit hit : hits) {
104110
int dayOfYear = (int) getField(hit, "day_of_year");
@@ -108,9 +114,9 @@ public void dayOfYearInSelect() {
108114
}
109115

110116
@Test
111-
public void dayOfYearInWhere() {
117+
public void dayOfYearInWhere() throws IOException {
112118
SearchHit[] hits = query(
113-
"SELECT insert_time", "WHERE DAY_OF_YEAR(insert_time) < 233", "LIMIT 10000"
119+
"SELECT insert_time", "WHERE DAY_OF_YEAR(insert_time) < 233", "LIMIT 10000"
114120
);
115121
for (SearchHit hit : hits) {
116122
DateTime insertTime = getDateFromSource(hit, "insert_time");
@@ -119,9 +125,9 @@ public void dayOfYearInWhere() {
119125
}
120126

121127
@Test
122-
public void dayOfMonthInSelect() {
128+
public void dayOfMonthInSelect() throws IOException {
123129
SearchHit[] hits = query(
124-
"SELECT DAY_OF_MONTH(insert_time) as day_of_month", "LIMIT 2000"
130+
"SELECT DAY_OF_MONTH(insert_time) as day_of_month", "LIMIT 2000"
125131
);
126132
for (SearchHit hit : hits) {
127133
int dayOfMonth = (int) getField(hit, "day_of_month");
@@ -131,9 +137,9 @@ public void dayOfMonthInSelect() {
131137
}
132138

133139
@Test
134-
public void dayOfMonthInWhere() {
140+
public void dayOfMonthInWhere() throws IOException {
135141
SearchHit[] hits = query(
136-
"SELECT insert_time", "WHERE DAY_OF_MONTH(insert_time) < 21", "LIMIT 10000"
142+
"SELECT insert_time", "WHERE DAY_OF_MONTH(insert_time) < 21", "LIMIT 10000"
137143
);
138144
for (SearchHit hit : hits) {
139145
DateTime insertTime = getDateFromSource(hit, "insert_time");
@@ -142,9 +148,9 @@ public void dayOfMonthInWhere() {
142148
}
143149

144150
@Test
145-
public void dayOfWeek() {
151+
public void dayOfWeek() throws IOException {
146152
SearchHit[] hits = query(
147-
"SELECT DAY_OF_WEEK(insert_time) as day_of_week", "LIMIT 2000"
153+
"SELECT DAY_OF_WEEK(insert_time) as day_of_week", "LIMIT 2000"
148154
);
149155
for (SearchHit hit : hits) {
150156
int dayOfWeek = (int) getField(hit, "day_of_week");
@@ -154,9 +160,9 @@ public void dayOfWeek() {
154160
}
155161

156162
@Test
157-
public void hourOfDay() {
163+
public void hourOfDay() throws IOException {
158164
SearchHit[] hits = query(
159-
"SELECT HOUR_OF_DAY(insert_time) as hour_of_day", "LIMIT 1000"
165+
"SELECT HOUR_OF_DAY(insert_time) as hour_of_day", "LIMIT 1000"
160166
);
161167
for (SearchHit hit : hits) {
162168
int hourOfDay = (int) getField(hit, "hour_of_day");
@@ -166,9 +172,9 @@ public void hourOfDay() {
166172
}
167173

168174
@Test
169-
public void minuteOfDay() {
175+
public void minuteOfDay() throws IOException {
170176
SearchHit[] hits = query(
171-
"SELECT MINUTE_OF_DAY(insert_time) as minute_of_day", "LIMIT 500"
177+
"SELECT MINUTE_OF_DAY(insert_time) as minute_of_day", "LIMIT 500"
172178
);
173179
for (SearchHit hit : hits) {
174180
int minuteOfDay = (int) getField(hit, "minute_of_day");
@@ -178,9 +184,9 @@ public void minuteOfDay() {
178184
}
179185

180186
@Test
181-
public void minuteOfHour() {
187+
public void minuteOfHour() throws IOException {
182188
SearchHit[] hits = query(
183-
"SELECT MINUTE_OF_HOUR(insert_time) as minute_of_hour", "LIMIT 500"
189+
"SELECT MINUTE_OF_HOUR(insert_time) as minute_of_hour", "LIMIT 500"
184190
);
185191
for (SearchHit hit : hits) {
186192
int minuteOfHour = (int) getField(hit, "minute_of_hour");
@@ -190,9 +196,9 @@ public void minuteOfHour() {
190196
}
191197

192198
@Test
193-
public void secondOfMinute() {
199+
public void secondOfMinute() throws IOException {
194200
SearchHit[] hits = query(
195-
"SELECT SECOND_OF_MINUTE(insert_time) as second_of_minute", "LIMIT 500"
201+
"SELECT SECOND_OF_MINUTE(insert_time) as second_of_minute", "LIMIT 500"
196202
);
197203
for (SearchHit hit : hits) {
198204
int secondOfMinute = (int) getField(hit, "second_of_minute");
@@ -201,19 +207,19 @@ public void secondOfMinute() {
201207
}
202208
}
203209

204-
private SearchHit[] query(String select, String... statements) {
210+
private SearchHit[] query(String select, String... statements) throws IOException {
205211
return execute(select + " " + FROM + " " + String.join(" ", statements));
206212
}
207213

208-
private SearchHit[] execute(String sql) {
209-
try {
210-
SearchDao searchDao = MainTestSuite.getSearchDao();
211-
SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(sql).explain();
212-
return ((SearchResponse) select.get()).getHits().getHits();
213-
} catch (SQLFeatureNotSupportedException | SqlParseException e) {
214-
throw new ParserException("Unable to parse query: " + sql);
215-
}
214+
// TODO: I think this code is now re-used in multiple classes, would be good to move to the base class.
215+
private SearchHit[] execute(String sqlRequest) throws IOException {
216+
final JSONObject jsonObject = executeRequest(makeRequest(sqlRequest));
216217

218+
final XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(
219+
NamedXContentRegistry.EMPTY,
220+
LoggingDeprecationHandler.INSTANCE,
221+
jsonObject.toString());
222+
return SearchResponse.fromXContent(parser).getHits().getHits();
217223
}
218224

219225
private Object getField(SearchHit hit, String fieldName) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amazon.opendistroforelasticsearch.sql.esintgtest;
17+
18+
import org.json.JSONObject;
19+
import org.junit.Test;
20+
21+
import java.io.IOException;
22+
import java.util.Locale;
23+
24+
import static org.hamcrest.core.IsEqual.equalTo;
25+
26+
public class DeleteIT extends SQLIntegTestCase {
27+
28+
@Override
29+
protected void init() throws Exception {
30+
loadIndex(Index.ACCOUNT);
31+
loadIndex(Index.PHRASE);
32+
}
33+
34+
@Test
35+
public void deleteAllTest() throws IOException, InterruptedException {
36+
String selectQuery = String.format(
37+
Locale.ROOT,
38+
"SELECT * FROM %s/account",
39+
TestsConstants.TEST_INDEX_ACCOUNT
40+
);
41+
JSONObject response = executeRequest(makeRequest(selectQuery));
42+
int totalHits = getTotalHits(response);
43+
44+
String deleteQuery = String.format(
45+
Locale.ROOT,
46+
"DELETE FROM %s/account",
47+
TestsConstants.TEST_INDEX_ACCOUNT);
48+
response = executeRequest(makeRequest(deleteQuery));
49+
assertThat(response.getInt("deleted"), equalTo(totalHits));
50+
51+
// The documents are not deleted immediately, causing the next search call to return all results.
52+
// To prevent flakiness, the minimum value of 2000 msec works fine.
53+
Thread.sleep(2000);
54+
55+
response = executeRequest(makeRequest(selectQuery));
56+
assertThat(getTotalHits(response), equalTo(0));
57+
}
58+
59+
@Test
60+
public void deleteWithConditionTest() throws IOException, InterruptedException {
61+
String selectQuery = String.format(
62+
Locale.ROOT,
63+
"SELECT * FROM %s/phrase WHERE match_phrase(phrase, 'quick fox here')",
64+
TestsConstants.TEST_INDEX_PHRASE
65+
);
66+
JSONObject response = executeRequest(makeRequest(selectQuery));
67+
int totalHits = getTotalHits(response);
68+
69+
String deleteQuery = String.format(
70+
Locale.ROOT,
71+
"DELETE FROM %s/phrase WHERE match_phrase(phrase, 'quick fox here')",
72+
TestsConstants.TEST_INDEX_PHRASE
73+
);
74+
response = executeRequest(makeRequest(deleteQuery));
75+
assertThat(response.getInt("deleted"), equalTo(totalHits));
76+
// The documents are not deleted immediately, causing the next search call to return all results.
77+
// To prevent flakiness, the minimum value of 2000 msec works fine.
78+
Thread.sleep(2000);
79+
80+
selectQuery = String.format(
81+
Locale.ROOT,
82+
"SELECT * FROM %s/phrase",
83+
TestsConstants.TEST_INDEX_PHRASE
84+
);
85+
response = executeRequest(makeRequest(selectQuery));
86+
assertThat(getTotalHits(response), equalTo(5));
87+
}
88+
}

src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/SQLIntegTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ protected JSONObject executeQueryWithGetRequest(final String sqlQuery) throws IO
169169
return new JSONObject(result);
170170
}
171171

172-
private String makeRequest(String query) {
172+
protected String makeRequest(String query) {
173173
return String.format("{\n" +
174174
" \"query\": \"%s\"\n" +
175175
"}", query);

0 commit comments

Comments
 (0)