|
| 1 | +/* |
| 2 | + * Copyright © 2021-present Arcade Data Ltd (info@arcadedata.com) |
| 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 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-FileCopyrightText: 2021-present Arcade Data Ltd (info@arcadedata.com) |
| 17 | + * SPDX-License-Identifier: Apache-2.0 |
| 18 | + */ |
| 19 | +package com.arcadedb.query.opencypher; |
| 20 | + |
| 21 | +import com.arcadedb.database.Database; |
| 22 | +import com.arcadedb.database.DatabaseFactory; |
| 23 | +import com.arcadedb.query.sql.executor.Result; |
| 24 | +import com.arcadedb.query.sql.executor.ResultSet; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * Regression tests for GitHub issue #3996: |
| 36 | + * CALL ... YIELD may null out variables carried in from WITH. |
| 37 | + */ |
| 38 | +class CypherCallYieldWithVariablesTest { |
| 39 | + private Database database; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + void setUp() { |
| 43 | + database = new DatabaseFactory("./target/databases/testopencypher-call-yield-with").create(); |
| 44 | + final var personType = database.getSchema().createVertexType("Person"); |
| 45 | + personType.createProperty("name", String.class); |
| 46 | + database.getSchema().createEdgeType("KNOWS"); |
| 47 | + database.transaction(() -> database.command("opencypher", "CREATE (:Person {name: 'Alice'})")); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterEach |
| 51 | + void tearDown() { |
| 52 | + if (database != null) { |
| 53 | + database.drop(); |
| 54 | + database = null; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void withLiteralPreservedAcrossCallDbLabels() { |
| 60 | + final ResultSet rs = database.query("opencypher", |
| 61 | + "WITH 1 AS x CALL db.labels() YIELD label RETURN x, label"); |
| 62 | + |
| 63 | + final List<Result> rows = new ArrayList<>(); |
| 64 | + while (rs.hasNext()) |
| 65 | + rows.add(rs.next()); |
| 66 | + |
| 67 | + assertThat(rows).isNotEmpty(); |
| 68 | + for (final Result row : rows) { |
| 69 | + final Number x = row.getProperty("x"); |
| 70 | + assertThat(x).as("x must not be null across CALL db.labels()").isNotNull(); |
| 71 | + assertThat(x.longValue()).isEqualTo(1L); |
| 72 | + assertThat((Object) row.getProperty("label")).as("label must not be null").isNotNull(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void withLiteralPreservedAcrossCallDbRelationshipTypes() { |
| 78 | + final ResultSet rs = database.query("opencypher", |
| 79 | + "WITH 1 AS x CALL db.relationshipTypes() YIELD relationshipType RETURN x, relationshipType"); |
| 80 | + |
| 81 | + final List<Result> rows = new ArrayList<>(); |
| 82 | + while (rs.hasNext()) |
| 83 | + rows.add(rs.next()); |
| 84 | + |
| 85 | + assertThat(rows).isNotEmpty(); |
| 86 | + for (final Result row : rows) { |
| 87 | + final Number x = row.getProperty("x"); |
| 88 | + assertThat(x).as("x must not be null across CALL db.relationshipTypes()").isNotNull(); |
| 89 | + assertThat(x.longValue()).isEqualTo(1L); |
| 90 | + assertThat((Object) row.getProperty("relationshipType")).as("relationshipType must not be null").isNotNull(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void withLiteralPreservedAcrossCallDbPropertyKeys() { |
| 96 | + final ResultSet rs = database.query("opencypher", |
| 97 | + "WITH 1 AS x CALL db.propertyKeys() YIELD propertyKey RETURN x, propertyKey"); |
| 98 | + |
| 99 | + final List<Result> rows = new ArrayList<>(); |
| 100 | + while (rs.hasNext()) |
| 101 | + rows.add(rs.next()); |
| 102 | + |
| 103 | + assertThat(rows).isNotEmpty(); |
| 104 | + for (final Result row : rows) { |
| 105 | + final Number x = row.getProperty("x"); |
| 106 | + assertThat(x).as("x must not be null across CALL db.propertyKeys()").isNotNull(); |
| 107 | + assertThat(x.longValue()).isEqualTo(1L); |
| 108 | + assertThat((Object) row.getProperty("propertyKey")).as("propertyKey must not be null").isNotNull(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void aggregatedValuePreservedAcrossCallDbLabels() { |
| 114 | + // Aggregated values carried through WITH must also survive CALL ... YIELD |
| 115 | + final ResultSet rs = database.query("opencypher", |
| 116 | + "MATCH (:Person) WITH count(*) AS c CALL db.labels() YIELD label RETURN c, label"); |
| 117 | + |
| 118 | + final List<Result> rows = new ArrayList<>(); |
| 119 | + while (rs.hasNext()) |
| 120 | + rows.add(rs.next()); |
| 121 | + |
| 122 | + assertThat(rows).isNotEmpty(); |
| 123 | + for (final Result row : rows) { |
| 124 | + final Number c = row.getProperty("c"); |
| 125 | + assertThat(c).as("count(*) must not be null across CALL db.labels()").isNotNull(); |
| 126 | + assertThat(c.longValue()).as("count must be 1").isEqualTo(1L); |
| 127 | + assertThat((Object) row.getProperty("label")).as("label must not be null").isNotNull(); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments