Skip to content

Commit c39154f

Browse files
committed
refactor(test): removed most of the command script reference from tests
1 parent 33c7d06 commit c39154f

File tree

3 files changed

+107
-137
lines changed

3 files changed

+107
-137
lines changed

core/src/test/java/com/orientechnologies/orient/core/sql/BatchUniqueProjectionRid.java

-29
This file was deleted.

core/src/test/java/com/orientechnologies/orient/core/sql/OCommandExecutorSQLScriptTest.java

+98-100
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

5-
import com.orientechnologies.BaseMemoryDatabase;
6-
import com.orientechnologies.orient.core.command.script.OCommandScript;
7-
import com.orientechnologies.orient.core.record.impl.ODocument;
8-
import com.orientechnologies.orient.core.sql.executor.OResult;
9-
import com.orientechnologies.orient.core.sql.executor.OResultSet;
10-
import java.util.Collection;
115
import java.util.HashMap;
12-
import java.util.Iterator;
136
import java.util.List;
147
import java.util.Map;
8+
159
import org.junit.Assert;
1610
import org.junit.Ignore;
1711
import org.junit.Test;
1812

13+
import com.orientechnologies.BaseMemoryDatabase;
14+
import com.orientechnologies.orient.core.command.script.OCommandScript;
15+
import com.orientechnologies.orient.core.record.impl.ODocument;
16+
import com.orientechnologies.orient.core.sql.executor.OResult;
17+
import com.orientechnologies.orient.core.sql.executor.OResultSet;
18+
1919
public class OCommandExecutorSQLScriptTest extends BaseMemoryDatabase {
2020

2121
public void beforeTest() {
@@ -33,42 +33,46 @@ public void beforeTest() {
3333
@Test
3434
public void testQuery() throws Exception {
3535
StringBuilder script = new StringBuilder();
36-
script.append("begin\n");
37-
script.append("let $a = select from foo\n");
38-
script.append("commit\n");
39-
script.append("return $a\n");
40-
List<ODocument> qResult = db.command(new OCommandScript("sql", script.toString())).execute();
36+
script.append("begin;");
37+
script.append("let $a = select from foo;");
38+
script.append("commit;");
39+
script.append("return $a;");
40+
List<OResult> qResult = db.execute("sql", script.toString()).stream().toList();
4141

4242
Assert.assertEquals(qResult.size(), 3);
4343
}
4444

4545
@Test
4646
public void testTx() throws Exception {
4747
StringBuilder script = new StringBuilder();
48-
script.append("begin isolation REPEATABLE_READ\n");
49-
script.append("let $a = insert into V set test = 'sql script test'\n");
50-
script.append("commit retry 10\n");
51-
script.append("return $a\n");
52-
ODocument qResult = db.command(new OCommandScript("sql", script.toString())).execute();
48+
script.append("begin isolation REPEATABLE_READ;");
49+
script.append("let $a = insert into V set test = 'sql script test';");
50+
script.append("commit retry 10;");
51+
script.append("return $a;");
52+
OResultSet qResult = db.execute("sql", script.toString());
5353

54-
Assert.assertNotNull(qResult);
54+
Assert.assertNotNull(qResult.hashCode());
55+
qResult.close();
5556
}
5657

5758
@Test
5859
public void testReturnExpanded() throws Exception {
5960
StringBuilder script = new StringBuilder();
60-
script.append("let $a = insert into V set test = 'sql script test'\n");
61-
script.append("return $a.toJSON()\n");
62-
String qResult = db.command(new OCommandScript("sql", script.toString())).execute();
61+
script.append("let $a = insert into V set test = 'sql script test';");
62+
script.append("return $a.toJSON() ");
63+
64+
List<OResult> qResultSet = db.execute("sql", script.toString()).stream().toList();
65+
String qResult = qResultSet.get(0).getProperty("value");
6366
Assert.assertNotNull(qResult);
6467

65-
new ODocument().fromJSON(qResult);
68+
new ODocument().fromJSON(qResult.substring(1, qResult.length() - 1));
6669

6770
script = new StringBuilder();
68-
script.append("let $a = select from V limit 2\n");
69-
script.append("return $a.toJSON()\n");
70-
String result = db.command(new OCommandScript("sql", script.toString())).execute();
71+
script.append("let $a = select from V limit 2;");
72+
script.append("return $a.toJSON() ;");
73+
List<OResult> resultSet = db.execute("sql", script.toString()).stream().toList();
7174

75+
String result = resultSet.get(0).getProperty("value");
7276
Assert.assertNotNull(result);
7377
result = result.trim();
7478
Assert.assertTrue(result.startsWith("["));
@@ -82,53 +86,51 @@ public void testSleep() throws Exception {
8286

8387
StringBuilder script = new StringBuilder();
8488
script.append("sleep 500");
85-
db.command(new OCommandScript("sql", script.toString())).execute();
89+
db.execute("sql", script.toString()).close();
8690

8791
Assert.assertTrue(System.currentTimeMillis() - begin >= 500);
8892
}
8993

9094
@Test
9195
public void testConsoleLog() throws Exception {
9296
StringBuilder script = new StringBuilder();
93-
script.append("LET $a = 'log'\n");
97+
script.append("LET $a = 'log';");
9498
script.append("console.log 'This is a test of log for ${a}'");
95-
db.command(new OCommandScript("sql", script.toString())).execute();
99+
db.execute("sql", script.toString()).close();
96100
}
97101

98102
@Test
99103
public void testConsoleOutput() throws Exception {
100104
StringBuilder script = new StringBuilder();
101-
script.append("LET $a = 'output'\n");
105+
script.append("LET $a = 'output';");
102106
script.append("console.output 'This is a test of log for ${a}'");
103-
db.command(new OCommandScript("sql", script.toString())).execute();
107+
db.execute("sql", script.toString()).close();
104108
}
105109

106110
@Test
107111
public void testConsoleError() throws Exception {
108112
StringBuilder script = new StringBuilder();
109-
script.append("LET $a = 'error'\n");
113+
script.append("LET $a = 'error';");
110114
script.append("console.error 'This is a test of log for ${a}'");
111-
db.command(new OCommandScript("sql", script.toString())).execute();
115+
db.execute("sql", script.toString()).close();
112116
}
113117

114118
@Test
115119
public void testReturnObject() throws Exception {
116120
StringBuilder script = new StringBuilder();
117-
script.append("return [{ a: 'b' }]");
118-
Collection<Object> result = db.command(new OCommandScript("sql", script.toString())).execute();
119-
120-
Assert.assertNotNull(result);
121-
122-
Assert.assertEquals(result.size(), 1);
121+
script.append("return [{ a: 'b' }];");
122+
OResultSet result = db.execute("sql", script.toString());
123123

124-
Assert.assertTrue(result.iterator().next() instanceof Map);
124+
OResult res = result.next();
125+
Assert.assertTrue(res.getProperty("value") instanceof List);
126+
Assert.assertTrue(((List)res.getProperty("value")).get(0) instanceof Map);
125127
}
126128

127129
@Test
128130
public void testIncrementAndLet() throws Exception {
129131

130132
StringBuilder script = new StringBuilder();
131-
script.append("CREATE CLASS TestCounter;\n");
133+
script.append("CREATE CLASS TestCounter;");
132134
script.append("INSERT INTO TestCounter set weight = 3;\n");
133135
script.append("LET counter = SELECT count(*) FROM TestCounter;\n");
134136
script.append("UPDATE TestCounter INCREMENT weight = $counter[0].count RETURN AfTER @this;\n");
@@ -155,121 +157,117 @@ public void testIncrementAndLetNewApi() throws Exception {
155157
public void testIf1() throws Exception {
156158
StringBuilder script = new StringBuilder();
157159

158-
script.append("let $a = select 1 as one\n");
159-
script.append("if($a[0].one = 1){\n");
160-
script.append(" return 'OK'\n");
161-
script.append("}\n");
162-
script.append("return 'FAIL'\n");
163-
Object qResult = db.command(new OCommandScript("sql", script.toString())).execute();
160+
script.append("let $a = select 1 as one;");
161+
script.append("if($a[0].one = 1){");
162+
script.append(" return 'OK' ;");
163+
script.append("}");
164+
script.append("return 'FAIL' ;");
165+
List<OResult> qResult = db.execute("sql", script.toString()).stream().toList();
164166

165-
Assert.assertNotNull(qResult);
166-
Assert.assertEquals(qResult, "OK");
167+
Assert.assertEquals(qResult.get(0).getProperty("value"), "OK");
167168
}
168169

169170
@Test
170171
public void testIf2() throws Exception {
171172
StringBuilder script = new StringBuilder();
172173

173-
script.append("let $a = select 1 as one\n");
174-
script.append("if ($a[0].one = 1) { \n");
175-
script.append(" return 'OK'\n");
176-
script.append(" } \n");
177-
script.append("return 'FAIL'\n");
178-
Object qResult = db.command(new OCommandScript("sql", script.toString())).execute();
174+
script.append("let $a = select 1 as one;");
175+
script.append("if ($a[0].one = 1) { ");
176+
script.append(" return 'OK' ;");
177+
script.append(" } ");
178+
script.append("return 'FAIL';");
179+
List<OResult> qResult = db.execute("sql", script.toString()).stream().toList();
179180

180-
Assert.assertNotNull(qResult);
181-
Assert.assertEquals(qResult, "OK");
181+
Assert.assertEquals(qResult.get(0).getProperty("value"), "OK");
182182
}
183183

184184
@Test
185185
public void testIf3() throws Exception {
186186
StringBuilder script = new StringBuilder();
187187
script.append("let $a = select 1 as one; if($a[0].one = 1){return 'OK';}return 'FAIL';");
188-
Object qResult = db.command(new OCommandScript("sql", script.toString())).execute();
189-
Assert.assertNotNull(qResult);
190-
Assert.assertEquals(qResult, "OK");
188+
List<OResult> qResult = db.execute("sql", script.toString()).stream().toList();
189+
190+
Assert.assertEquals(qResult.get(0).getProperty("value"), "OK");
191191
}
192192

193193
@Test
194194
public void testNestedIf2() throws Exception {
195195
StringBuilder script = new StringBuilder();
196196

197-
script.append("let $a = select 1 as one\n");
198-
script.append("if($a[0].one = 1){\n");
199-
script.append(" if($a[0].one = 'zz'){\n");
200-
script.append(" return 'FAIL'\n");
201-
script.append(" }\n");
202-
script.append(" return 'OK'\n");
197+
script.append("let $a = select 1 as one;");
198+
script.append("if($a[0].one = 1){");
199+
script.append(" if($a[0].one = 'zz'){");
200+
script.append(" return 'FAIL';");
201+
script.append(" }");
202+
script.append(" return 'OK';");
203203
script.append("}\n");
204-
script.append("return 'FAIL'\n");
205-
Object qResult = db.command(new OCommandScript("sql", script.toString())).execute();
204+
script.append("return 'FAIL';");
206205

207-
Assert.assertNotNull(qResult);
208-
Assert.assertEquals(qResult, "OK");
206+
List<OResult> qResult = db.execute("sql", script.toString()).stream().toList();
207+
208+
Assert.assertEquals(qResult.get(0).getProperty("value"), "OK");
209209
}
210210

211211
@Test
212212
public void testNestedIf3() throws Exception {
213213
StringBuilder script = new StringBuilder();
214214

215-
script.append("let $a = select 1 as one\n");
215+
script.append("let $a = select 1 as one ;\n");
216216
script.append("if($a[0].one = 'zz'){\n");
217217
script.append(" if($a[0].one = 1){\n");
218-
script.append(" return 'FAIL'\n");
218+
script.append(" return 'FAIL' ;\n");
219219
script.append(" }\n");
220-
script.append(" return 'FAIL'\n");
220+
script.append(" return 'FAIL' ; \n");
221221
script.append("}\n");
222-
script.append("return 'OK'\n");
223-
Object qResult = db.command(new OCommandScript("sql", script.toString())).execute();
222+
script.append("return 'OK' ; \n");
223+
List<OResult> qResult = db.execute("sql", script.toString()).stream().toList();
224224

225-
Assert.assertNotNull(qResult);
226-
Assert.assertEquals(qResult, "OK");
225+
Assert.assertEquals(qResult.get(0).getProperty("value"), "OK");
227226
}
228227

229228
@Test
230229
public void testIfRealQuery() throws Exception {
231230
StringBuilder script = new StringBuilder();
232231

233-
script.append("let $a = select from foo\n");
232+
script.append("let $a = select from foo;\n");
234233
script.append("if($a is not null and $a.size() = 3){\n");
235-
script.append(" return $a\n");
234+
script.append(" return $a ;\n");
236235
script.append("}\n");
237-
script.append("return 'FAIL'\n");
238-
Object qResult = db.command(new OCommandScript("sql", script.toString())).execute();
236+
script.append("return 'FAIL';\n");
237+
List<OResult> qResult = db.execute("sql", script.toString()).stream().toList();
239238

240239
Assert.assertNotNull(qResult);
241-
Assert.assertEquals(((List) qResult).size(), 3);
240+
Assert.assertEquals( qResult.size(), 3);
242241
}
243242

244243
@Test
245244
public void testIfMultipleStatements() throws Exception {
246245
StringBuilder script = new StringBuilder();
247246

248-
script.append("let $a = select 1 as one\n");
247+
script.append("let $a = select 1 as one;\n");
249248
script.append("if($a[0].one = 1){\n");
250-
script.append(" let $b = select 'OK' as ok\n");
251-
script.append(" return $b[0].ok\n");
249+
script.append(" let $b = select 'OK' as ok;\n");
250+
script.append(" return $b[0].ok; \n");
252251
script.append("}\n");
253-
script.append("return 'FAIL'\n");
254-
Object qResult = db.command(new OCommandScript("sql", script.toString())).execute();
252+
script.append("return 'FAIL';");
253+
List<OResult> qResult = db.execute("sql", script.toString()).stream().toList();
255254

256255
Assert.assertNotNull(qResult);
257-
Assert.assertEquals(qResult, "OK");
256+
Assert.assertEquals(qResult.get(0).getProperty("value"), "OK");
258257
}
259258

260259
@Test
261260
public void testScriptSubContext() throws Exception {
262261
StringBuilder script = new StringBuilder();
263262

264-
script.append("let $a = select from foo limit 1\n");
263+
script.append("let $a = select from foo limit 1;");
265264
script.append("select from (traverse doesnotexist from $a)\n");
266-
Iterable qResult = db.command(new OCommandScript("sql", script.toString())).execute();
265+
OResultSet qResult = db.execute("sql", script.toString());
267266

268-
Assert.assertNotNull(qResult);
269-
Iterator iterator = qResult.iterator();
270-
Assert.assertTrue(iterator.hasNext());
271-
iterator.next();
272-
Assert.assertFalse(iterator.hasNext());
267+
Assert.assertTrue(qResult.hasNext());
268+
qResult.next();
269+
Assert.assertFalse(qResult.hasNext());
270+
qResult.close();
273271
}
274272

275273
@Test
@@ -278,12 +276,12 @@ public void testSemicolonInString() throws Exception {
278276
// testing parsing problem
279277
StringBuilder script = new StringBuilder();
280278

281-
script.append("let $a = select 'foo ; bar' as one\n");
282-
script.append("let $b = select 'foo \\\'; bar' as one\n");
279+
script.append("let $a = select 'foo ; bar' as one;");
280+
script.append("let $b = select 'foo \\\'; bar' as one;");
283281

284-
script.append("let $a = select \"foo ; bar\" as one\n");
285-
script.append("let $b = select \"foo \\\"; bar\" as one\n");
286-
Object qResult = db.command(new OCommandScript("sql", script.toString())).execute();
282+
script.append("let $a = select \"foo ; bar\" as one;");
283+
script.append("let $b = select \"foo \\\"; bar\" as one;");
284+
db.execute("sql", script.toString()).close();
287285
}
288286

289287
@Test
@@ -292,7 +290,7 @@ public void testQuotedRegex() {
292290
db.command("CREATE CLASS QuotedRegex2").close();
293291
String batch = "INSERT INTO QuotedRegex2 SET regexp=\"'';\"";
294292

295-
db.command(new OCommandScript(batch.toString())).execute();
293+
db.execute("sql", batch.toString()).close();
296294

297295
OResultSet result = db.query("SELECT FROM QuotedRegex2");
298296
OResult doc = result.next();

0 commit comments

Comments
 (0)