From 337b04ae4dee2ec4ea7c22f702e02eadae57e752 Mon Sep 17 00:00:00 2001 From: Tglman Date: Thu, 3 Oct 2024 18:01:47 +0100 Subject: [PATCH] refactor(test): reduced use of query old APIs from query engine --- .../auto/SQLSelectProjectionsTest.java | 300 +++++++----------- 1 file changed, 117 insertions(+), 183 deletions(-) diff --git a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/SQLSelectProjectionsTest.java b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/SQLSelectProjectionsTest.java index 95dfd048138..d6dd60d3aac 100755 --- a/tests/src/test/java/com/orientechnologies/orient/test/database/auto/SQLSelectProjectionsTest.java +++ b/tests/src/test/java/com/orientechnologies/orient/test/database/auto/SQLSelectProjectionsTest.java @@ -19,16 +19,14 @@ import com.orientechnologies.orient.core.db.object.ODatabaseObject; import com.orientechnologies.orient.core.db.record.OIdentifiable; import com.orientechnologies.orient.core.id.ORID; -import com.orientechnologies.orient.core.record.ORecordInternal; import com.orientechnologies.orient.core.record.impl.ODocument; import com.orientechnologies.orient.core.record.impl.ODocumentHelper; +import com.orientechnologies.orient.core.sql.executor.OResult; import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; import com.orientechnologies.orient.object.db.OObjectDatabaseTx; import java.io.IOException; -import java.util.HashSet; import java.util.List; import java.util.Locale; -import java.util.Set; import org.testng.Assert; import org.testng.annotations.Optional; import org.testng.annotations.Parameters; @@ -44,25 +42,24 @@ public SQLSelectProjectionsTest(@Optional String url) { @Test public void queryProjectionOk() { - List result = + List result = database .command( - new OSQLSynchQuery( - " select nick, followings, followers from Profile where nick is defined and" - + " followings is defined and followers is defined")) - .execute(); + " select nick, followings, followers from Profile where nick is defined and" + + " followings is defined and followers is defined") + .stream() + .toList(); Assert.assertTrue(result.size() != 0); - for (ODocument d : result) { - String[] colNames = d.fieldNames(); + for (OResult d : result) { + String[] colNames = d.getPropertyNames().toArray(new String[] {}); Assert.assertEquals(colNames.length, 3, "document: " + d); Assert.assertEquals(colNames[0], "nick", "document: " + d); Assert.assertEquals(colNames[1], "followings", "document: " + d); Assert.assertEquals(colNames[2], "followers", "document: " + d); - Assert.assertNull(d.getClassName()); - Assert.assertEquals(ORecordInternal.getRecordType(d), ODocument.RECORD_TYPE); + Assert.assertFalse(d.getElement().isPresent()); } } @@ -71,17 +68,15 @@ public void queryProjectionObjectLevel() { ODatabaseObject db = new OObjectDatabaseTx(url); db.open("admin", "admin"); - List result = - db.getUnderlying() - .query( - new OSQLSynchQuery(" select nick, followings, followers from Profile ")); + List result = + db.getUnderlying().query(" select nick, followings, followers from Profile ").stream() + .toList(); Assert.assertTrue(result.size() != 0); - for (ODocument d : result) { - Assert.assertTrue(d.fieldNames().length <= 3); - Assert.assertNull(d.getClassName()); - Assert.assertEquals(ORecordInternal.getRecordType(d), ODocument.RECORD_TYPE); + for (OResult d : result) { + Assert.assertTrue(d.getPropertyNames().size() <= 3); + Assert.assertFalse(d.getElement().isPresent()); } db.close(); @@ -89,110 +84,106 @@ public void queryProjectionObjectLevel() { @Test public void queryProjectionLinkedAndFunction() { - List result = + List result = database .command( - new OSQLSynchQuery( - "select name.toUpperCase(Locale.ENGLISH), address.city.country.name from" - + " Profile")) - .execute(); + "select name.toUpperCase(Locale.ENGLISH), address.city.country.name from" + + " Profile") + .stream() + .toList(); Assert.assertTrue(result.size() != 0); - for (ODocument d : result) { - Assert.assertTrue(d.fieldNames().length <= 2); - if (d.field("name") != null) + for (OResult d : result) { + Assert.assertTrue(d.getPropertyNames().size() <= 2); + if (d.getProperty("name") != null) Assert.assertTrue( - d.field("name").equals(((String) d.field("name")).toUpperCase(Locale.ENGLISH))); + d.getProperty("name") + .equals(((String) d.getProperty("name")).toUpperCase(Locale.ENGLISH))); - Assert.assertNull(d.getClassName()); - Assert.assertEquals(ORecordInternal.getRecordType(d), ODocument.RECORD_TYPE); + Assert.assertFalse(d.getElement().isPresent()); } } @Test public void queryProjectionSameFieldTwice() { - List result = + List result = database .command( - new OSQLSynchQuery( - "select name, name.toUpperCase(Locale.ENGLISH) from Profile where name is not" - + " null")) - .execute(); + "select name, name.toUpperCase(Locale.ENGLISH) as name2 from Profile where name is" + + " not null") + .stream() + .toList(); Assert.assertTrue(result.size() != 0); - for (ODocument d : result) { - Assert.assertTrue(d.fieldNames().length <= 2); - Assert.assertNotNull(d.field("name")); - Assert.assertNotNull(d.field("name2")); + for (OResult d : result) { + Assert.assertTrue(d.getPropertyNames().size() <= 2); + Assert.assertNotNull(d.getProperty("name")); + Assert.assertNotNull(d.getProperty("name2")); - Assert.assertNull(d.getClassName()); - Assert.assertEquals(ORecordInternal.getRecordType(d), ODocument.RECORD_TYPE); + Assert.assertFalse(d.getElement().isPresent()); } } @Test public void queryProjectionStaticValues() { - List result = + List result = database .command( - new OSQLSynchQuery( - "select location.city.country.name, address.city.country.name from Profile" - + " where location.city.country.name is not null")) - .execute(); + "select location.city.country.name as location, address.city.country.name from" + + " Profile where location.city.country.name is not null") + .stream() + .toList(); Assert.assertTrue(result.size() != 0); - for (ODocument d : result) { + for (OResult d : result) { - Assert.assertNotNull(d.field("location")); - Assert.assertNull(d.field("address")); + Assert.assertNotNull(d.getProperty("location")); + Assert.assertNull(d.getProperty("address")); - Assert.assertNull(d.getClassName()); - Assert.assertEquals(ORecordInternal.getRecordType(d), ODocument.RECORD_TYPE); + Assert.assertFalse(d.getElement().isPresent()); } } @Test public void queryProjectionPrefixAndAppend() { - List result = + List result = database .command( - new OSQLSynchQuery( - "select *, name.prefix('Mr. ').append(' ').append(surname).append('!') as test" - + " from Profile where name is not null")) - .execute(); + "select *, name.prefix('Mr. ').append(' ').append(surname).append('!') as test" + + " from Profile where name is not null") + .stream() + .toList(); Assert.assertTrue(result.size() != 0); - for (ODocument d : result) { + for (OResult d : result) { Assert.assertEquals( - d.field("test").toString(), "Mr. " + d.field("name") + " " + d.field("surname") + "!"); - - Assert.assertEquals(ORecordInternal.getRecordType(d), ODocument.RECORD_TYPE); + d.getProperty("test").toString(), + "Mr. " + d.getProperty("name") + " " + d.getProperty("surname") + "!"); } } @Test public void queryProjectionFunctionsAndFieldOperators() { - List result = + List result = database .command( - new OSQLSynchQuery( - "select name.append('.').prefix('Mr. ') as name from Profile where name is not" - + " null")) - .execute(); + "select name.append('.').prefix('Mr. ') as name from Profile where name is not" + + " null") + .stream() + .toList(); Assert.assertTrue(result.size() != 0); - for (ODocument d : result) { - Assert.assertTrue(d.fieldNames().length <= 1); - Assert.assertTrue(d.field("name").toString().startsWith("Mr. ")); - Assert.assertTrue(d.field("name").toString().endsWith(".")); + for (OResult d : result) { + Assert.assertTrue(d.getPropertyNames().size() <= 1); + Assert.assertTrue(d.getProperty("name").toString().startsWith("Mr. ")); + Assert.assertTrue(d.getProperty("name").toString().endsWith(".")); - Assert.assertNull(d.getClassName()); - Assert.assertEquals(ORecordInternal.getRecordType(d), ODocument.RECORD_TYPE); + Assert.assertFalse(d.getElement().isPresent()); } } @@ -218,50 +209,44 @@ public void queryProjectionFunctionsAndFieldOperators() { @Test public void queryProjectionSimpleValues() { - List result = - database - .command(new OSQLSynchQuery("select 10, 'ciao' from Profile LIMIT 1")) - .execute(); + List result = + database.command("select 10, 'ciao' from Profile LIMIT 1").stream().toList(); Assert.assertTrue(result.size() != 0); - for (ODocument d : result) { - Assert.assertTrue(d.fieldNames().length <= 2); - Assert.assertEquals(((Integer) d.field("10")).intValue(), 10l); - Assert.assertEquals(d.field("ciao"), "ciao"); + for (OResult d : result) { + Assert.assertTrue(d.getPropertyNames().size() <= 2); + Assert.assertEquals(((Integer) d.getProperty("10")).intValue(), 10l); + Assert.assertEquals(d.getProperty("'ciao'"), "ciao"); - Assert.assertNull(d.getClassName()); - Assert.assertEquals(ORecordInternal.getRecordType(d), ODocument.RECORD_TYPE); + Assert.assertFalse(d.getElement().isPresent()); } } @Test public void queryProjectionJSON() { - List result = - database - .command(new OSQLSynchQuery("select @this.toJson() as json from Profile")) - .execute(); + List result = + database.command("select @this.toJson() as json from Profile").stream().toList(); Assert.assertTrue(result.size() != 0); - for (ODocument d : result) { - Assert.assertTrue(d.fieldNames().length <= 1); - Assert.assertNotNull(d.field("json")); + for (OResult d : result) { + Assert.assertTrue(d.getPropertyNames().size() <= 1); + Assert.assertNotNull(d.getProperty("json")); - new ODocument().fromJSON((String) d.field("json")); + new ODocument().fromJSON((String) d.getProperty("json")); } } public void queryProjectionRid() { - List result = - database.command(new OSQLSynchQuery("select @rid FROM V")).execute(); + List result = database.command("select @rid as rid FROM V").stream().toList(); Assert.assertTrue(result.size() != 0); - for (ODocument d : result) { - Assert.assertTrue(d.fieldNames().length <= 1); - Assert.assertNotNull(d.field("rid")); + for (OResult d : result) { + Assert.assertTrue(d.getPropertyNames().size() <= 1); + Assert.assertNotNull(d.getProperty("rid")); - final ORID rid = d.field("rid", ORID.class); + final ORID rid = d.getProperty("rid"); Assert.assertTrue(rid.isValid()); } } @@ -278,14 +263,12 @@ public void queryProjectionOrigin() { } public void queryProjectionEval() { - List result = - database.command(new OSQLSynchQuery("select eval('1 + 4') as result")).execute(); + List result = database.command("select eval('1 + 4') as result").stream().toList(); Assert.assertEquals(result.size(), 1); - for (ODocument d : result) Assert.assertEquals(d.field("result"), 5); + for (OResult d : result) Assert.assertEquals(d.getProperty("result"), 5); } - @SuppressWarnings("unchecked") public void queryProjectionContextArray() { List result = database @@ -309,87 +292,69 @@ public void queryProjectionContextArray() { } public void ifNullFunction() { - List result = - database.command(new OSQLSynchQuery("SELECT ifnull('a', 'b')")).execute(); + List result = database.command("SELECT ifnull('a', 'b') as ifnull").stream().toList(); Assert.assertFalse(result.isEmpty()); - Assert.assertEquals(result.get(0).field("ifnull"), "a"); + Assert.assertEquals(result.get(0).getProperty("ifnull"), "a"); - result = - database.command(new OSQLSynchQuery("SELECT ifnull('a', 'b', 'c')")).execute(); + result = database.command("SELECT ifnull('a', 'b', 'c') as ifnull ").stream().toList(); Assert.assertFalse(result.isEmpty()); - Assert.assertEquals(result.get(0).field("ifnull"), "c"); + Assert.assertEquals(result.get(0).getProperty("ifnull"), "c"); - result = database.command(new OSQLSynchQuery("SELECT ifnull(null, 'b')")).execute(); + result = database.command("SELECT ifnull(null, 'b') as ifnull").stream().toList(); Assert.assertFalse(result.isEmpty()); - Assert.assertEquals(result.get(0).field("ifnull"), "b"); + Assert.assertEquals(result.get(0).getProperty("ifnull"), "b"); } public void filteringArrayInChain() { - List result = - database - .command(new OSQLSynchQuery("SELECT set(name)[0-1] as set from OUser")) - .execute(); + List result = + database.command("SELECT set(name)[0..1] as set from OUser").stream().toList(); Assert.assertEquals(result.size(), 1); - for (ODocument d : result) { - Assert.assertTrue(OMultiValue.isMultiValue(d.field("set"))); - Assert.assertTrue(OMultiValue.getSize(d.field("set")) <= 2); + for (OResult d : result) { + Assert.assertTrue(OMultiValue.isMultiValue(d.getProperty("set"))); + Assert.assertTrue(OMultiValue.getSize(d.getProperty("set")) <= 2); } - result = - database - .command(new OSQLSynchQuery("SELECT set(name)[0,1] as set from OUser")) - .execute(); + result = database.command("SELECT set(name)[0,1] as set from OUser").stream().toList(); Assert.assertEquals(result.size(), 1); - for (ODocument d : result) { - Assert.assertTrue(OMultiValue.isMultiValue(d.field("set"))); - Assert.assertTrue(OMultiValue.getSize(d.field("set")) <= 2); + for (OResult d : result) { + Assert.assertTrue(OMultiValue.isMultiValue(d.getProperty("set"))); + Assert.assertTrue(OMultiValue.getSize(d.getProperty("set")) <= 2); } - result = - database - .command(new OSQLSynchQuery("SELECT set(name)[0] as unique from OUser")) - .execute(); + result = database.command("SELECT set(name)[0] as unique from OUser").stream().toList(); Assert.assertEquals(result.size(), 1); - for (ODocument d : result) { - Assert.assertFalse(OMultiValue.isMultiValue(d.field("unique"))); + for (OResult d : result) { + Assert.assertFalse(OMultiValue.isMultiValue(d.getProperty("unique"))); } } public void projectionWithNoTarget() { - List result = - database.command(new OSQLSynchQuery("select 'Ay' as a , 'bEE'")).execute(); + List result = database.command("select 'Ay' as a , 'bEE'").stream().toList(); Assert.assertEquals(result.size(), 1); - for (ODocument d : result) { - Assert.assertTrue(d.field("a").equals("Ay")); - Assert.assertTrue(d.field("bEE").equals("bEE")); + for (OResult d : result) { + Assert.assertTrue(d.getProperty("a").equals("Ay")); + Assert.assertTrue(d.getProperty("'bEE'").equals("bEE")); } - result = - database.command(new OSQLSynchQuery("select 'Ay' as a , 'bEE' as b")).execute(); + result = database.command("select 'Ay' as a , 'bEE' as b").stream().toList(); Assert.assertEquals(result.size(), 1); - for (ODocument d : result) { - Assert.assertTrue(d.field("a").equals("Ay")); - Assert.assertTrue(d.field("b").equals("bEE")); + for (OResult d : result) { + Assert.assertTrue(d.getProperty("a").equals("Ay")); + Assert.assertTrue(d.getProperty("b").equals("bEE")); } - result = - database - .command(new OSQLSynchQuery("select 'Ay' as a , 'bEE' as b fetchplan *:1")) - .execute(); + result = database.command("select 'Ay' as a , 'bEE' as b fetchplan *:1").stream().toList(); Assert.assertEquals(result.size(), 1); - for (ODocument d : result) { - Assert.assertTrue(d.field("a").equals("Ay")); - Assert.assertTrue(d.field("b").equals("bEE")); + for (OResult d : result) { + Assert.assertTrue(d.getProperty("a").equals("Ay")); + Assert.assertTrue(d.getProperty("b").equals("bEE")); } - result = - database - .command(new OSQLSynchQuery("select 'Ay' as a , 'bEE' fetchplan *:1")) - .execute(); + result = database.command("select 'Ay' as a , 'bEE' fetchplan *:1").stream().toList(); Assert.assertEquals(result.size(), 1); - for (ODocument d : result) { - Assert.assertTrue(d.field("a").equals("Ay")); - Assert.assertTrue(d.field("bEE").equals("bEE")); + for (OResult d : result) { + Assert.assertTrue(d.getProperty("a").equals("Ay")); + Assert.assertTrue(d.getProperty("'bEE'").equals("bEE")); } } @@ -483,35 +448,4 @@ public void testSimpleExpandExclude() { database.command("drop class C unsafe ").close(); } } - - @Test - public void testTempRIDsAreNotRecycledInResultSet() { - final List resultset = - database.query( - new OSQLSynchQuery( - "select name, $l as l from OUser let $l = (select name from OuSer)")); - - Assert.assertNotNull(resultset); - - Set rids = new HashSet(); - for (OIdentifiable d : resultset) { - final ORID rid = d.getIdentity(); - Assert.assertFalse(rids.contains(rid)); - - rids.add(rid); - - final List embeddedList = ((ODocument) d.getRecord()).field("l"); - Assert.assertNotNull(embeddedList); - Assert.assertFalse(embeddedList.isEmpty()); - - for (OIdentifiable embedded : embeddedList) { - if (embedded != null) { - final ORID embeddedRid = embedded.getIdentity(); - - Assert.assertFalse(rids.contains(embeddedRid)); - rids.add(rid); - } - } - } - } }