Skip to content

Commit aa866f1

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

File tree

2 files changed

+91
-124
lines changed

2 files changed

+91
-124
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

+91-95
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import com.orientechnologies.orient.core.record.impl.ODocument;
88
import com.orientechnologies.orient.core.sql.executor.OResult;
99
import com.orientechnologies.orient.core.sql.executor.OResultSet;
10-
import java.util.Collection;
1110
import java.util.HashMap;
12-
import java.util.Iterator;
1311
import java.util.List;
1412
import java.util.Map;
1513
import org.junit.Assert;
@@ -33,42 +31,46 @@ public void beforeTest() {
3331
@Test
3432
public void testQuery() throws Exception {
3533
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();
34+
script.append("begin;");
35+
script.append("let $a = select from foo;");
36+
script.append("commit;");
37+
script.append("return $a;");
38+
List<OResult> qResult = db.execute("sql", script.toString()).stream().toList();
4139

4240
Assert.assertEquals(qResult.size(), 3);
4341
}
4442

4543
@Test
4644
public void testTx() throws Exception {
4745
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();
46+
script.append("begin isolation REPEATABLE_READ;");
47+
script.append("let $a = insert into V set test = 'sql script test';");
48+
script.append("commit retry 10;");
49+
script.append("return $a;");
50+
OResultSet qResult = db.execute("sql", script.toString());
5351

54-
Assert.assertNotNull(qResult);
52+
Assert.assertNotNull(qResult.hashCode());
53+
qResult.close();
5554
}
5655

5756
@Test
5857
public void testReturnExpanded() throws Exception {
5958
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();
59+
script.append("let $a = insert into V set test = 'sql script test';");
60+
script.append("return $a.toJSON() ");
61+
62+
List<OResult> qResultSet = db.execute("sql", script.toString()).stream().toList();
63+
String qResult = qResultSet.get(0).getProperty("value");
6364
Assert.assertNotNull(qResult);
6465

65-
new ODocument().fromJSON(qResult);
66+
new ODocument().fromJSON(qResult.substring(1, qResult.length() - 1));
6667

6768
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();
69+
script.append("let $a = select from V limit 2;");
70+
script.append("return $a.toJSON() ;");
71+
List<OResult> resultSet = db.execute("sql", script.toString()).stream().toList();
7172

73+
String result = resultSet.get(0).getProperty("value");
7274
Assert.assertNotNull(result);
7375
result = result.trim();
7476
Assert.assertTrue(result.startsWith("["));
@@ -82,53 +84,51 @@ public void testSleep() throws Exception {
8284

8385
StringBuilder script = new StringBuilder();
8486
script.append("sleep 500");
85-
db.command(new OCommandScript("sql", script.toString())).execute();
87+
db.execute("sql", script.toString()).close();
8688

8789
Assert.assertTrue(System.currentTimeMillis() - begin >= 500);
8890
}
8991

9092
@Test
9193
public void testConsoleLog() throws Exception {
9294
StringBuilder script = new StringBuilder();
93-
script.append("LET $a = 'log'\n");
95+
script.append("LET $a = 'log';");
9496
script.append("console.log 'This is a test of log for ${a}'");
95-
db.command(new OCommandScript("sql", script.toString())).execute();
97+
db.execute("sql", script.toString()).close();
9698
}
9799

98100
@Test
99101
public void testConsoleOutput() throws Exception {
100102
StringBuilder script = new StringBuilder();
101-
script.append("LET $a = 'output'\n");
103+
script.append("LET $a = 'output';");
102104
script.append("console.output 'This is a test of log for ${a}'");
103-
db.command(new OCommandScript("sql", script.toString())).execute();
105+
db.execute("sql", script.toString()).close();
104106
}
105107

106108
@Test
107109
public void testConsoleError() throws Exception {
108110
StringBuilder script = new StringBuilder();
109-
script.append("LET $a = 'error'\n");
111+
script.append("LET $a = 'error';");
110112
script.append("console.error 'This is a test of log for ${a}'");
111-
db.command(new OCommandScript("sql", script.toString())).execute();
113+
db.execute("sql", script.toString()).close();
112114
}
113115

114116
@Test
115117
public void testReturnObject() throws Exception {
116118
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);
119+
script.append("return [{ a: 'b' }];");
120+
OResultSet result = db.execute("sql", script.toString());
121121

122-
Assert.assertEquals(result.size(), 1);
123-
124-
Assert.assertTrue(result.iterator().next() instanceof Map);
122+
OResult res = result.next();
123+
Assert.assertTrue(res.getProperty("value") instanceof List);
124+
Assert.assertTrue(((List) res.getProperty("value")).get(0) instanceof Map);
125125
}
126126

127127
@Test
128128
public void testIncrementAndLet() throws Exception {
129129

130130
StringBuilder script = new StringBuilder();
131-
script.append("CREATE CLASS TestCounter;\n");
131+
script.append("CREATE CLASS TestCounter;");
132132
script.append("INSERT INTO TestCounter set weight = 3;\n");
133133
script.append("LET counter = SELECT count(*) FROM TestCounter;\n");
134134
script.append("UPDATE TestCounter INCREMENT weight = $counter[0].count RETURN AfTER @this;\n");
@@ -155,121 +155,117 @@ public void testIncrementAndLetNewApi() throws Exception {
155155
public void testIf1() throws Exception {
156156
StringBuilder script = new StringBuilder();
157157

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();
158+
script.append("let $a = select 1 as one;");
159+
script.append("if($a[0].one = 1){");
160+
script.append(" return 'OK' ;");
161+
script.append("}");
162+
script.append("return 'FAIL' ;");
163+
List<OResult> qResult = db.execute("sql", script.toString()).stream().toList();
164164

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

169168
@Test
170169
public void testIf2() throws Exception {
171170
StringBuilder script = new StringBuilder();
172171

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();
172+
script.append("let $a = select 1 as one;");
173+
script.append("if ($a[0].one = 1) { ");
174+
script.append(" return 'OK' ;");
175+
script.append(" } ");
176+
script.append("return 'FAIL';");
177+
List<OResult> qResult = db.execute("sql", script.toString()).stream().toList();
179178

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

184182
@Test
185183
public void testIf3() throws Exception {
186184
StringBuilder script = new StringBuilder();
187185
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");
186+
List<OResult> qResult = db.execute("sql", script.toString()).stream().toList();
187+
188+
Assert.assertEquals(qResult.get(0).getProperty("value"), "OK");
191189
}
192190

193191
@Test
194192
public void testNestedIf2() throws Exception {
195193
StringBuilder script = new StringBuilder();
196194

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");
195+
script.append("let $a = select 1 as one;");
196+
script.append("if($a[0].one = 1){");
197+
script.append(" if($a[0].one = 'zz'){");
198+
script.append(" return 'FAIL';");
199+
script.append(" }");
200+
script.append(" return 'OK';");
203201
script.append("}\n");
204-
script.append("return 'FAIL'\n");
205-
Object qResult = db.command(new OCommandScript("sql", script.toString())).execute();
202+
script.append("return 'FAIL';");
206203

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

211209
@Test
212210
public void testNestedIf3() throws Exception {
213211
StringBuilder script = new StringBuilder();
214212

215-
script.append("let $a = select 1 as one\n");
213+
script.append("let $a = select 1 as one ;\n");
216214
script.append("if($a[0].one = 'zz'){\n");
217215
script.append(" if($a[0].one = 1){\n");
218-
script.append(" return 'FAIL'\n");
216+
script.append(" return 'FAIL' ;\n");
219217
script.append(" }\n");
220-
script.append(" return 'FAIL'\n");
218+
script.append(" return 'FAIL' ; \n");
221219
script.append("}\n");
222-
script.append("return 'OK'\n");
223-
Object qResult = db.command(new OCommandScript("sql", script.toString())).execute();
220+
script.append("return 'OK' ; \n");
221+
List<OResult> qResult = db.execute("sql", script.toString()).stream().toList();
224222

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

229226
@Test
230227
public void testIfRealQuery() throws Exception {
231228
StringBuilder script = new StringBuilder();
232229

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

240237
Assert.assertNotNull(qResult);
241-
Assert.assertEquals(((List) qResult).size(), 3);
238+
Assert.assertEquals(qResult.size(), 3);
242239
}
243240

244241
@Test
245242
public void testIfMultipleStatements() throws Exception {
246243
StringBuilder script = new StringBuilder();
247244

248-
script.append("let $a = select 1 as one\n");
245+
script.append("let $a = select 1 as one;\n");
249246
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");
247+
script.append(" let $b = select 'OK' as ok;\n");
248+
script.append(" return $b[0].ok; \n");
252249
script.append("}\n");
253-
script.append("return 'FAIL'\n");
254-
Object qResult = db.command(new OCommandScript("sql", script.toString())).execute();
250+
script.append("return 'FAIL';");
251+
List<OResult> qResult = db.execute("sql", script.toString()).stream().toList();
255252

256253
Assert.assertNotNull(qResult);
257-
Assert.assertEquals(qResult, "OK");
254+
Assert.assertEquals(qResult.get(0).getProperty("value"), "OK");
258255
}
259256

260257
@Test
261258
public void testScriptSubContext() throws Exception {
262259
StringBuilder script = new StringBuilder();
263260

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

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

275271
@Test
@@ -278,12 +274,12 @@ public void testSemicolonInString() throws Exception {
278274
// testing parsing problem
279275
StringBuilder script = new StringBuilder();
280276

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

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();
280+
script.append("let $a = select \"foo ; bar\" as one;");
281+
script.append("let $b = select \"foo \\\"; bar\" as one;");
282+
db.execute("sql", script.toString()).close();
287283
}
288284

289285
@Test
@@ -292,7 +288,7 @@ public void testQuotedRegex() {
292288
db.command("CREATE CLASS QuotedRegex2").close();
293289
String batch = "INSERT INTO QuotedRegex2 SET regexp=\"'';\"";
294290

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

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

0 commit comments

Comments
 (0)