lowka commented on code in PR #2768: URL: https://github.com/apache/ignite-3/pull/2768#discussion_r1384780457
########## modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/mapping/MappingTestRunner.java: ########## @@ -0,0 +1,454 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.sql.engine.exec.mapping; + +import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; +import static org.apache.ignite.internal.testframework.IgniteTestUtils.await; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.StringTokenizer; +import java.util.UUID; +import java.util.concurrent.CompletionException; +import java.util.function.BiFunction; +import java.util.function.Supplier; +import org.apache.calcite.plan.RelOptUtil; +import org.apache.ignite.internal.cluster.management.topology.api.LogicalTopologySnapshot; +import org.apache.ignite.internal.lang.IgniteSystemProperties; +import org.apache.ignite.internal.sql.api.ResultSetMetadataImpl; +import org.apache.ignite.internal.sql.engine.SqlQueryType; +import org.apache.ignite.internal.sql.engine.prepare.MultiStepPlan; +import org.apache.ignite.internal.sql.engine.prepare.PlanId; +import org.apache.ignite.internal.sql.engine.rel.IgniteRel; +import org.apache.ignite.internal.sql.engine.rel.IgniteTableModify; +import org.apache.ignite.internal.sql.engine.schema.IgniteSchema; +import org.apache.ignite.internal.sql.engine.util.EmptyCacheFactory; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.TestOnly; + +/** + * A runner for mapping test cases. + * + * <p>Test case format: + * <pre> + * #<optional multiline description> + * <node_name> + * --- + * <SQL statement (single line or multiline)> + * --- + * <expected fragments> + * --- + * </pre> + * + * <p>To bulk update test case results set system property {@code MAPPING_TESTS_OVERWRITE_RESULTS} to {@code true}: + * + * <p>As a result all test cases will have results that match actual ones. + * If there are mismatches tests still fail to report and possibly review them. + * + * <p>To remove all results set system property {@code MAPPING_TESTS_STRIP_RESULTS} to {@code true}: + * + * <p>As a result all test cases' results will be replaced with a dummy result. + * If there are mismatches tests still fail to report and possibly review them. + * + * @see FragmentPrinter + */ +final class MappingTestRunner { + + private static final String OVERWRITE_RESULTS = "MAPPING_TESTS_OVERWRITE_RESULTS"; + + private static final String STRIP_RESULTS = "MAPPING_TESTS_STRIP_RESULTS"; + + /** Test setup. */ + static final class TestSetup { + + private final ExecutionTargetProvider executionTargetProvider; + + private final IgniteSchema schema; + + private final LogicalTopologySnapshot topologySnapshot; + + TestSetup(ExecutionTargetProvider executionTargetProvider, IgniteSchema schema, LogicalTopologySnapshot topologySnapshot) { + this.executionTargetProvider = executionTargetProvider; + this.schema = schema; + this.topologySnapshot = topologySnapshot; + } + } + + private final Path location; + + private final BiFunction<IgniteSchema, String, IgniteRel> parseValidate; + + private boolean overwriteResults; + + private boolean stripResults; + + private Path outputFile; + + MappingTestRunner(Path location, + BiFunction<IgniteSchema, String, IgniteRel> parseValidate) { + + this.location = location; + this.parseValidate = parseValidate; + } + + /** + * Specifies a location to write results to. If not specified, all results are written to a test file, + * + * @param path Location + * @return this for chaining. + */ + MappingTestRunner outputFile(Path path) { + this.outputFile = path; + return this; + } + + void runTest(Supplier<TestSetup> init, String fileName) { + TestSetup setup = init.get(); + + initFlags(); + + Path testFile = location.resolve(fileName); + List<TestCaseDef> testCases = loadTestCases(testFile); + + runTestCases(testFile, setup.schema, setup.executionTargetProvider, setup.topologySnapshot, parseValidate, testCases); + } + + @TestOnly + void initFlags() { + overwriteResults = IgniteSystemProperties.getBoolean(OVERWRITE_RESULTS, false); + stripResults = IgniteSystemProperties.getBoolean(STRIP_RESULTS, false); + + if (overwriteResults && stripResults) { + throw new IllegalStateException("Both overwriteResults and stripResults have been specified"); Review Comment: fixed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
