diff --git a/wrangler-core/src/test/java/io/cdap/MockEngine.java b/wrangler-core/src/test/java/io/cdap/MockEngine.java new file mode 100644 index 000000000..fd9fdc18b --- /dev/null +++ b/wrangler-core/src/test/java/io/cdap/MockEngine.java @@ -0,0 +1,38 @@ +/* + * Copyright © 2023 Cask Data, Inc. + * + * Licensed 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 io.cdap; + +import io.cdap.cdap.etl.api.relational.Capability; +import io.cdap.cdap.etl.api.relational.Engine; +import io.cdap.cdap.etl.api.relational.ExpressionFactory; +import io.cdap.cdap.etl.api.relational.StringExpressionFactoryType; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +public class MockEngine implements Engine { + + @Override + public Set getCapabilities() { + return Collections.singleton(StringExpressionFactoryType.SQL); + } + + @Override + public List> getExpressionFactories() { + return Collections.singletonList(new MockExpressionFactory()); + } +} diff --git a/wrangler-core/src/test/java/io/cdap/MockExpression.java b/wrangler-core/src/test/java/io/cdap/MockExpression.java new file mode 100644 index 000000000..0f5d2960d --- /dev/null +++ b/wrangler-core/src/test/java/io/cdap/MockExpression.java @@ -0,0 +1,42 @@ +/* + * Copyright © 2023 Cask Data, Inc. + * + * Licensed 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 io.cdap; + +import io.cdap.cdap.etl.api.relational.Expression; + +public class MockExpression implements Expression { + + private final String expression; + + + public MockExpression(String expression) { + this.expression = expression; + } + + public boolean isValid() { + return true; + } + + public String getExpression() { + return this.expression; + } + + @Override + public String getValidationError() { + return null; + } +} diff --git a/wrangler-core/src/test/java/io/cdap/MockExpressionFactory.java b/wrangler-core/src/test/java/io/cdap/MockExpressionFactory.java new file mode 100644 index 000000000..0a8f11404 --- /dev/null +++ b/wrangler-core/src/test/java/io/cdap/MockExpressionFactory.java @@ -0,0 +1,52 @@ +/* + * Copyright © 2023 Cask Data, Inc. + * + * Licensed 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 io.cdap; + +import io.cdap.cdap.etl.api.relational.Capability; +import io.cdap.cdap.etl.api.relational.CoreExpressionCapabilities; +import io.cdap.cdap.etl.api.relational.Expression; +import io.cdap.cdap.etl.api.relational.ExpressionFactory; +import io.cdap.cdap.etl.api.relational.ExpressionFactoryType; +import io.cdap.cdap.etl.api.relational.StringExpressionFactoryType; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class MockExpressionFactory implements ExpressionFactory { + + @Override +public ExpressionFactoryType getType() { + return StringExpressionFactoryType.SQL; +} + private static final Set CAPABILITIES = Collections.unmodifiableSet( + new HashSet() {{ + add(StringExpressionFactoryType.SQL); + add(CoreExpressionCapabilities.CAN_GET_QUALIFIED_DATASET_NAME); + add(CoreExpressionCapabilities.CAN_GET_QUALIFIED_COLUMN_NAME); + add(CoreExpressionCapabilities.CAN_SET_DATASET_ALIAS); + }}); + + @Override + public Set getCapabilities() { + return CAPABILITIES; + } + + @Override + public Expression compile(String expression) { + return new MockExpression(expression); + } +} diff --git a/wrangler-core/src/test/java/io/cdap/MockRelation.java b/wrangler-core/src/test/java/io/cdap/MockRelation.java new file mode 100644 index 000000000..ebaa097d5 --- /dev/null +++ b/wrangler-core/src/test/java/io/cdap/MockRelation.java @@ -0,0 +1,81 @@ +/* + * Copyright © 2023 Cask Data, Inc. + * + * Licensed 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 io.cdap; + +import io.cdap.cdap.etl.api.relational.Expression; +import io.cdap.cdap.etl.api.relational.Relation; +import java.util.Map; +import java.util.stream.Collectors; +import javax.annotation.Nullable; + +public class MockRelation implements Relation { + String column; + + Expression expression; + + public MockRelation(@Nullable String column, @Nullable Expression expression) { + this.column = column; + this.expression = expression; + } + @Override + public boolean isValid() { + return true; + } + + @Override + public String getValidationError() { + return null; + } + + @Override + public Relation setColumn(String column, Expression value) { + return new MockRelation(column, value); + } + + @Override + public Relation dropColumn(String column) { + return new MockRelation(column, null); + } + + @Override + public Relation select(Map columnExpMap) { + String columns = columnExpMap.entrySet() + .stream() + .map(column -> column.getKey()) + .collect(Collectors.joining(",")); + + String expressions = columnExpMap.entrySet() + .stream() + .map(column -> ((MockExpression) column.getValue()).getExpression()) + .collect(Collectors.joining(",")); + + return new MockRelation(columns, new MockExpression(expressions)); + } + + @Override + public Relation filter(Expression filter) { + return new MockRelation(null, filter); + } + + public String getColumn() { + return this.column; + } + + public Expression getExpression() { + return this.expression; + } +} diff --git a/wrangler-core/src/test/java/io/cdap/MockRelationalTransformContext.java b/wrangler-core/src/test/java/io/cdap/MockRelationalTransformContext.java new file mode 100644 index 000000000..8fb066802 --- /dev/null +++ b/wrangler-core/src/test/java/io/cdap/MockRelationalTransformContext.java @@ -0,0 +1,95 @@ +/* + * Copyright © 2023 Cask Data, Inc. + * + * Licensed 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 io.cdap; + +import io.cdap.cdap.api.data.schema.Schema; +import io.cdap.cdap.api.feature.FeatureFlagsProvider; +import io.cdap.cdap.etl.api.engine.sql.StandardSQLCapabilities; +import io.cdap.cdap.etl.api.relational.Capability; +import io.cdap.cdap.etl.api.relational.Engine; +import io.cdap.cdap.etl.api.relational.Relation; +import io.cdap.cdap.etl.api.relational.RelationalTranformContext; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.Set; + +public class MockRelationalTransformContext implements RelationalTranformContext { + private Engine engine; + private Map inputMap; + private Map inputSchemas; + private Schema outputSchema; + private FeatureFlagsProvider featureFlagsProvider; + private Relation outputRelation; + + public MockRelationalTransformContext(Engine engine, + Map inputMap, + Map inputSchemas, + Schema outputSchema, + FeatureFlagsProvider featureFlagsProvider) { + this.engine = engine; + this.inputMap = inputMap; + this.inputSchemas = inputSchemas; + this.outputSchema = outputSchema; + this.featureFlagsProvider = featureFlagsProvider; + } + + @Override + public Engine getEngine() { + return engine; + } + + @Override + public Relation getInputRelation(String inputStage) { + return inputMap.get(inputStage); + } + + @Override + public Set getInputRelationNames() { + return Collections.unmodifiableSet(inputMap.keySet()); + } + + @Override + public Schema getInputSchema(String inputStage) { + return inputSchemas.get(inputStage); + } + + @Override + public Schema getOutputSchema() { + return outputSchema; + } + + @Override + public void setOutputRelation(Relation outputRelation) { + this.outputRelation = outputRelation; + } + + @Override + public void setOutputRelation(String portName, Relation outputDataSet) { + throw new UnsupportedOperationException("Only single output is supported"); + } + + @Override + public boolean isFeatureEnabled(String name) { + return this.featureFlagsProvider.isFeatureEnabled(name); + } + + + public Collection getDefaultLanguageCapabilityList() { + return Collections.singleton(StandardSQLCapabilities.POSTGRES); + } +} diff --git a/wrangler-core/src/test/java/io/cdap/RelationalDirectiveTest.java b/wrangler-core/src/test/java/io/cdap/RelationalDirectiveTest.java new file mode 100644 index 000000000..ea9bc4e52 --- /dev/null +++ b/wrangler-core/src/test/java/io/cdap/RelationalDirectiveTest.java @@ -0,0 +1,53 @@ +/* + * Copyright © 2023 Cask Data, Inc. + * + * Licensed 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 io.cdap; + +import io.cdap.cdap.etl.api.relational.Relation; +import io.cdap.cdap.etl.api.relational.RelationalTranformContext; +import io.cdap.wrangler.api.Directive; +import io.cdap.wrangler.api.DirectiveLoadException; +import io.cdap.wrangler.api.DirectiveParseException; +import io.cdap.wrangler.api.RecipeException; +import io.cdap.wrangler.parser.GrammarBasedParser; +import io.cdap.wrangler.parser.MigrateToV2; +import io.cdap.wrangler.registry.DirectiveRegistry; +import io.cdap.wrangler.registry.SystemDirectiveRegistry; +import java.util.List; + +public class RelationalDirectiveTest { + + public static Relation runTransform(String[] recipe, + RelationalTranformContext relationalTranformContext, + Relation relation) + throws DirectiveParseException, RecipeException { + DirectiveRegistry registry; + registry = SystemDirectiveRegistry.INSTANCE; + try { + registry.reload("default"); + } catch (DirectiveLoadException e) { + throw new RuntimeException(e); + } + + GrammarBasedParser parser = new GrammarBasedParser("default", + new MigrateToV2(recipe).migrate(), registry); + List directives = parser.parse(); + for (Directive directive : directives) { + relation = directive.transform(relationalTranformContext, relation); + } + return relation; + } +} diff --git a/wrangler-core/src/test/java/io/cdap/directives/column/CopyTest.java b/wrangler-core/src/test/java/io/cdap/directives/column/CopyTest.java index daed64b86..0e3418ff3 100644 --- a/wrangler-core/src/test/java/io/cdap/directives/column/CopyTest.java +++ b/wrangler-core/src/test/java/io/cdap/directives/column/CopyTest.java @@ -16,7 +16,15 @@ package io.cdap.directives.column; +import io.cdap.MockEngine; +import io.cdap.MockExpression; +import io.cdap.MockRelation; +import io.cdap.MockRelationalTransformContext; +import io.cdap.cdap.etl.api.relational.Engine; +import io.cdap.cdap.etl.api.relational.Relation; +import io.cdap.cdap.etl.api.relational.RelationalTranformContext; import io.cdap.wrangler.TestingRig; +import io.cdap.wrangler.api.DirectiveParseException; import io.cdap.wrangler.api.RecipeException; import io.cdap.wrangler.api.Row; import org.junit.Assert; @@ -25,6 +33,8 @@ import java.util.Arrays; import java.util.List; +import static io.cdap.RelationalDirectiveTest.runTransform; + /** * Tests {@link io.cdap.directives.column.Copy} */ @@ -96,4 +106,26 @@ public void testForceCopy() throws Exception { Assert.assertEquals(rows.get(2).getValue("body_2"), rows.get(2).getValue("body_1")); } + @Test + public void testRelationColumn() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"copy testColumn testColumn_copy"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockRelation) relation1).getColumn(), "testColumn_copy"); + } + + @Test + public void testRelationExpression() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"copy testColumn testColumn_copy"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockExpression) ((MockRelation) relation1).getExpression()).getExpression(), + "testColumn"); + } } diff --git a/wrangler-core/src/test/java/io/cdap/directives/column/KeepTest.java b/wrangler-core/src/test/java/io/cdap/directives/column/KeepTest.java index 709e0a19c..77cda9347 100644 --- a/wrangler-core/src/test/java/io/cdap/directives/column/KeepTest.java +++ b/wrangler-core/src/test/java/io/cdap/directives/column/KeepTest.java @@ -16,7 +16,16 @@ package io.cdap.directives.column; +import io.cdap.MockEngine; +import io.cdap.MockExpression; +import io.cdap.MockRelation; +import io.cdap.MockRelationalTransformContext; +import io.cdap.cdap.etl.api.relational.Engine; +import io.cdap.cdap.etl.api.relational.Relation; +import io.cdap.cdap.etl.api.relational.RelationalTranformContext; import io.cdap.wrangler.TestingRig; +import io.cdap.wrangler.api.DirectiveParseException; +import io.cdap.wrangler.api.RecipeException; import io.cdap.wrangler.api.Row; import org.junit.Assert; import org.junit.Test; @@ -24,6 +33,8 @@ import java.util.Arrays; import java.util.List; +import static io.cdap.RelationalDirectiveTest.runTransform; + /** * Tests {@link Keep} */ @@ -45,4 +56,41 @@ public void testKeep() throws Exception { Assert.assertTrue(rows.size() == 1); Assert.assertEquals(2, rows.get(0).width()); } + + @Test + public void testRelationColumn() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"keep column1"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockRelation) relation1).getColumn(), "column1"); + } + + @Test + public void testRelationExpression() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"keep column1"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockExpression) ((MockRelation) relation1).getExpression()).getExpression(), + "column1"); + } + + @Test + public void testMultipleColumns() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"keep column1,column2,column3"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + String[] outputColumns = ((MockExpression) ((MockRelation) relation1) + .getExpression()).getExpression().split(","); + Assert.assertEquals(outputColumns.length, 3); + } + } diff --git a/wrangler-core/src/test/java/io/cdap/directives/column/MergeTest.java b/wrangler-core/src/test/java/io/cdap/directives/column/MergeTest.java index 66224f473..3597253ea 100644 --- a/wrangler-core/src/test/java/io/cdap/directives/column/MergeTest.java +++ b/wrangler-core/src/test/java/io/cdap/directives/column/MergeTest.java @@ -16,7 +16,16 @@ package io.cdap.directives.column; +import io.cdap.MockEngine; +import io.cdap.MockExpression; +import io.cdap.MockRelation; +import io.cdap.MockRelationalTransformContext; +import io.cdap.cdap.etl.api.relational.Engine; +import io.cdap.cdap.etl.api.relational.Relation; +import io.cdap.cdap.etl.api.relational.RelationalTranformContext; import io.cdap.wrangler.TestingRig; +import io.cdap.wrangler.api.DirectiveParseException; +import io.cdap.wrangler.api.RecipeException; import io.cdap.wrangler.api.Row; import org.junit.Assert; import org.junit.Ignore; @@ -25,6 +34,8 @@ import java.util.Arrays; import java.util.List; +import static io.cdap.RelationalDirectiveTest.runTransform; + /** * Tests {@link Merge} */ @@ -106,4 +117,27 @@ public void testWithMultipleCharactersAsSeparator() throws Exception { Assert.assertEquals(1, rows.size()); Assert.assertEquals("Root---Joltie", rows.get(0).getValue("C")); } + + @Test + public void testRelationColumn() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"merge testColumn1 testColumn2 destColumn ','"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockRelation) relation1).getColumn(), "destColumn"); + } + + @Test + public void testRelationExpression() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"merge testColumn1 testColumn2 destColumn ','"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockExpression) ((MockRelation) relation1).getExpression()).getExpression(), + "CONCAT(testColumn1,',',testColumn2)"); + } } diff --git a/wrangler-core/src/test/java/io/cdap/directives/transformation/LeftTrimTest.java b/wrangler-core/src/test/java/io/cdap/directives/transformation/LeftTrimTest.java index 0fb060ee7..f325370f4 100644 --- a/wrangler-core/src/test/java/io/cdap/directives/transformation/LeftTrimTest.java +++ b/wrangler-core/src/test/java/io/cdap/directives/transformation/LeftTrimTest.java @@ -16,7 +16,16 @@ package io.cdap.directives.transformation; +import io.cdap.MockEngine; +import io.cdap.MockExpression; +import io.cdap.MockRelation; +import io.cdap.MockRelationalTransformContext; +import io.cdap.cdap.etl.api.relational.Engine; +import io.cdap.cdap.etl.api.relational.Relation; +import io.cdap.cdap.etl.api.relational.RelationalTranformContext; import io.cdap.wrangler.TestingRig; +import io.cdap.wrangler.api.DirectiveParseException; +import io.cdap.wrangler.api.RecipeException; import io.cdap.wrangler.api.Row; import org.junit.Assert; import org.junit.Test; @@ -24,6 +33,8 @@ import java.util.Arrays; import java.util.List; +import static io.cdap.RelationalDirectiveTest.runTransform; + /** * Tests {@link LeftTrim} */ @@ -74,4 +85,27 @@ public void testSentenceLeftTrim() throws Exception { Assert.assertEquals("TITLE IS TITLE ", rows.get(3).getValue("body")); Assert.assertEquals("TITLE IS TITLE ", rows.get(4).getValue("body")); } + + @Test + public void testRelationColumn() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"ltrim testColumn"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockRelation) relation1).getColumn(), "testColumn"); + } + + @Test + public void testRelationExpression() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"ltrim testColumn"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockExpression) ((MockRelation) relation1).getExpression()).getExpression(), + "LTRIM(testColumn)"); + } } diff --git a/wrangler-core/src/test/java/io/cdap/directives/transformation/LowerTest.java b/wrangler-core/src/test/java/io/cdap/directives/transformation/LowerTest.java index 674fed2a9..b7fdce1de 100644 --- a/wrangler-core/src/test/java/io/cdap/directives/transformation/LowerTest.java +++ b/wrangler-core/src/test/java/io/cdap/directives/transformation/LowerTest.java @@ -16,7 +16,16 @@ package io.cdap.directives.transformation; +import io.cdap.MockEngine; +import io.cdap.MockExpression; +import io.cdap.MockRelation; +import io.cdap.MockRelationalTransformContext; +import io.cdap.cdap.etl.api.relational.Engine; +import io.cdap.cdap.etl.api.relational.Relation; +import io.cdap.cdap.etl.api.relational.RelationalTranformContext; import io.cdap.wrangler.TestingRig; +import io.cdap.wrangler.api.DirectiveParseException; +import io.cdap.wrangler.api.RecipeException; import io.cdap.wrangler.api.Row; import org.junit.Assert; import org.junit.Test; @@ -24,6 +33,8 @@ import java.util.Arrays; import java.util.List; +import static io.cdap.RelationalDirectiveTest.runTransform; + /** * Tests {@link Lower} */ @@ -71,4 +82,28 @@ public void testSentenceLowercasing() throws Exception { Assert.assertEquals("title is title", rows.get(2).getValue("body")); Assert.assertEquals("title is title", rows.get(3).getValue("body")); } + + @Test + public void testRelationColumn() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"lowercase testColumn"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockRelation) relation1).getColumn(), "testColumn"); + } + + @Test + public void testRelationExpression() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"lowercase testColumn"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockExpression) ((MockRelation) relation1).getExpression()).getExpression(), + "LOWER(testColumn)"); + } + } diff --git a/wrangler-core/src/test/java/io/cdap/directives/transformation/RightTrimTest.java b/wrangler-core/src/test/java/io/cdap/directives/transformation/RightTrimTest.java index 749ee3dd1..993eb6a55 100644 --- a/wrangler-core/src/test/java/io/cdap/directives/transformation/RightTrimTest.java +++ b/wrangler-core/src/test/java/io/cdap/directives/transformation/RightTrimTest.java @@ -16,7 +16,16 @@ package io.cdap.directives.transformation; +import io.cdap.MockEngine; +import io.cdap.MockExpression; +import io.cdap.MockRelation; +import io.cdap.MockRelationalTransformContext; +import io.cdap.cdap.etl.api.relational.Engine; +import io.cdap.cdap.etl.api.relational.Relation; +import io.cdap.cdap.etl.api.relational.RelationalTranformContext; import io.cdap.wrangler.TestingRig; +import io.cdap.wrangler.api.DirectiveParseException; +import io.cdap.wrangler.api.RecipeException; import io.cdap.wrangler.api.Row; import org.junit.Assert; import org.junit.Test; @@ -24,6 +33,8 @@ import java.util.Arrays; import java.util.List; +import static io.cdap.RelationalDirectiveTest.runTransform; + /** * Tests {@link RightTrim} */ @@ -74,4 +85,27 @@ public void testSentenceRightTrim() throws Exception { Assert.assertEquals(" TITLE IS TITLE", rows.get(3).getValue("body")); Assert.assertEquals(" TITLE IS TITLE", rows.get(4).getValue("body")); } + + @Test + public void testRelationColumn() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, + null, null, null, null); + String[] recipe = {"rtrim testColumn"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockRelation) relation1).getColumn(), "testColumn"); + } + + @Test + public void testRelationExpression() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, + null, null, null, null); + String[] recipe = {"rtrim testColumn"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockExpression) ((MockRelation) relation1).getExpression()).getExpression(), + "RTRIM(testColumn)"); + } } diff --git a/wrangler-core/src/test/java/io/cdap/directives/transformation/TitleCaseTest.java b/wrangler-core/src/test/java/io/cdap/directives/transformation/TitleCaseTest.java index f9e671954..695b78080 100644 --- a/wrangler-core/src/test/java/io/cdap/directives/transformation/TitleCaseTest.java +++ b/wrangler-core/src/test/java/io/cdap/directives/transformation/TitleCaseTest.java @@ -16,7 +16,16 @@ package io.cdap.directives.transformation; +import io.cdap.MockEngine; +import io.cdap.MockExpression; +import io.cdap.MockRelation; +import io.cdap.MockRelationalTransformContext; +import io.cdap.cdap.etl.api.relational.Engine; +import io.cdap.cdap.etl.api.relational.Relation; +import io.cdap.cdap.etl.api.relational.RelationalTranformContext; import io.cdap.wrangler.TestingRig; +import io.cdap.wrangler.api.DirectiveParseException; +import io.cdap.wrangler.api.RecipeException; import io.cdap.wrangler.api.Row; import org.junit.Assert; import org.junit.Test; @@ -24,6 +33,8 @@ import java.util.Arrays; import java.util.List; +import static io.cdap.RelationalDirectiveTest.runTransform; + /** * Tests {@link TitleCase} */ @@ -73,4 +84,28 @@ public void testSentencesCasing() throws Exception { Assert.assertEquals("Title Is Title", rows.get(3).getValue("body")); } + + @Test + public void testRelationColumn() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"titlecase testColumn"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockRelation) relation1).getColumn(), "testColumn"); + } + + @Test + public void testRelationExpression() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"titlecase testColumn"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockExpression) ((MockRelation) relation1).getExpression()).getExpression(), + "initcap(testColumn)"); + } + } diff --git a/wrangler-core/src/test/java/io/cdap/directives/transformation/TrimTest.java b/wrangler-core/src/test/java/io/cdap/directives/transformation/TrimTest.java index 1fddd69ac..476177c5f 100644 --- a/wrangler-core/src/test/java/io/cdap/directives/transformation/TrimTest.java +++ b/wrangler-core/src/test/java/io/cdap/directives/transformation/TrimTest.java @@ -16,7 +16,16 @@ package io.cdap.directives.transformation; +import io.cdap.MockEngine; +import io.cdap.MockExpression; +import io.cdap.MockRelation; +import io.cdap.MockRelationalTransformContext; +import io.cdap.cdap.etl.api.relational.Engine; +import io.cdap.cdap.etl.api.relational.Relation; +import io.cdap.cdap.etl.api.relational.RelationalTranformContext; import io.cdap.wrangler.TestingRig; +import io.cdap.wrangler.api.DirectiveParseException; +import io.cdap.wrangler.api.RecipeException; import io.cdap.wrangler.api.Row; import org.junit.Assert; import org.junit.Test; @@ -24,6 +33,8 @@ import java.util.Arrays; import java.util.List; +import static io.cdap.RelationalDirectiveTest.runTransform; + /** * Tests {@link Trim} */ @@ -94,4 +105,27 @@ public void testSentenceTrim() throws Exception { Assert.assertEquals(expected, rows.get(i).getValue("body")); } } + + @Test + public void testRelationColumn() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"trim testColumn"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockRelation) relation1).getColumn(), "testColumn"); + } + + @Test + public void testRelationExpression() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"trim testColumn"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockExpression) ((MockRelation) relation1).getExpression()).getExpression(), + "TRIM(testColumn)"); + } } diff --git a/wrangler-core/src/test/java/io/cdap/directives/transformation/UpperTest.java b/wrangler-core/src/test/java/io/cdap/directives/transformation/UpperTest.java index e531c441c..1569d2eb9 100644 --- a/wrangler-core/src/test/java/io/cdap/directives/transformation/UpperTest.java +++ b/wrangler-core/src/test/java/io/cdap/directives/transformation/UpperTest.java @@ -16,7 +16,16 @@ package io.cdap.directives.transformation; +import io.cdap.MockEngine; +import io.cdap.MockExpression; +import io.cdap.MockRelation; +import io.cdap.MockRelationalTransformContext; +import io.cdap.cdap.etl.api.relational.Engine; +import io.cdap.cdap.etl.api.relational.Relation; +import io.cdap.cdap.etl.api.relational.RelationalTranformContext; import io.cdap.wrangler.TestingRig; +import io.cdap.wrangler.api.DirectiveParseException; +import io.cdap.wrangler.api.RecipeException; import io.cdap.wrangler.api.Row; import org.junit.Assert; import org.junit.Test; @@ -24,6 +33,8 @@ import java.util.Arrays; import java.util.List; +import static io.cdap.RelationalDirectiveTest.runTransform; + /** * Tests {@link Upper} */ @@ -71,4 +82,30 @@ public void testSentenceLowercasing() throws Exception { Assert.assertEquals("TITLE IS TITLE", rows.get(2).getValue("body")); Assert.assertEquals("TITLE IS TITLE", rows.get(3).getValue("body")); } + + @Test + public void testRelationColumn() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"uppercase testColumn"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockRelation) relation1).getColumn(), "testColumn"); + Assert.assertEquals(((MockExpression) ((MockRelation) relation1).getExpression()).getExpression(), + "UPPER(testColumn)"); + } + + @Test + public void testRelationExpression() throws DirectiveParseException, RecipeException { + MockRelation relation = new MockRelation(null, null); + Engine engine = new MockEngine(); + RelationalTranformContext relationalTranformContext = new MockRelationalTransformContext(engine, null, null, + null, null); + String[] recipe = {"uppercase testColumn"}; + Relation relation1 = runTransform(recipe, relationalTranformContext, relation); + Assert.assertEquals(((MockExpression) ((MockRelation) relation1).getExpression()).getExpression(), + "UPPER(testColumn)"); + } + } diff --git a/wrangler-transform/pom.xml b/wrangler-transform/pom.xml index c99f8e3d8..cd314bcfa 100644 --- a/wrangler-transform/pom.xml +++ b/wrangler-transform/pom.xml @@ -84,6 +84,12 @@ + + org.mockito + mockito-core + 2.23.0 + test + diff --git a/wrangler-transform/src/test/java/io/cdap/wrangler/WranglerTransformTest.java b/wrangler-transform/src/test/java/io/cdap/wrangler/WranglerTransformTest.java new file mode 100644 index 000000000..f843931c4 --- /dev/null +++ b/wrangler-transform/src/test/java/io/cdap/wrangler/WranglerTransformTest.java @@ -0,0 +1,52 @@ +/* + * Copyright © 2023 Cask Data, Inc. + * + * Licensed 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 io.cdap.wrangler; + +import io.cdap.cdap.etl.api.relational.InvalidRelation; +import io.cdap.cdap.etl.api.relational.Relation; + +import org.junit.Assert; +import org.junit.Test; + +import static org.mockito.Mockito.mock; + + +public class WranglerTransformTest { + + @Test(expected = RuntimeException.class) + public void relationalTransformTest () throws Exception { + Wrangler wrangler = new Wrangler(new Wrangler.Config("sql", "Yes", + "true", "uppercase :Name", null, + null, null, null, null)); + Relation relation = mock(Relation.class); + Relation resultRelation = mock(Relation.class); + resultRelation = wrangler.transform(null, relation); + } + + @Test + public void invalidRelationTest () { + Wrangler wrangler = new Wrangler(new Wrangler.Config("jexl", "No", + "false", "uppercase :Name", null, + null, null, null, null)); + Relation relation = mock(Relation.class); + Relation invalidrelation = new InvalidRelation("Plugin is not con" + + "figured for relational transformation"); + Assert.assertEquals(invalidrelation.getValidationError(), + wrangler.transform(null, relation).getValidationError()); + } + +}