-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fixed few minor things discovered by the UTs
- Loading branch information
Showing
4 changed files
with
150 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...mmons/src/test/scala/za/co/absa/spark/commons/errorhandling/types/ColumnOrValueForm.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2023 ABSA Group Limited | ||
* | ||
* 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 za.co.absa.spark.commons.errorhandling.types | ||
|
||
import org.apache.spark.sql.Column | ||
import org.scalatest.Assertions | ||
|
||
case class ColumnOrValueForm[T] ( | ||
column: Column, | ||
isColumn: Boolean, | ||
isValue: Boolean, | ||
columnNames: Set[String], | ||
value: Option[T] | ||
) extends Assertions { | ||
def assertTo(columnOrValue: ColumnOrValue[T]): Unit ={ | ||
assert(column == columnOrValue.column) | ||
assert(isColumn == columnOrValue.isColumn) | ||
assert(isValue == columnOrValue.isValue) | ||
assert(columnNames == columnOrValue.columnNames) | ||
assert(value == columnOrValue.getValue) | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
...mmons/src/test/scala/za/co/absa/spark/commons/errorhandling/types/ColumnOrValueTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright 2023 ABSA Group Limited | ||
* | ||
* 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 za.co.absa.spark.commons.errorhandling.types | ||
|
||
import net.bytebuddy.dynamic.scaffold.MethodGraph.Empty | ||
import org.apache.spark.sql.Column | ||
import org.apache.spark.sql.functions.{array, col, current_date, lit, map_from_arrays} | ||
import org.apache.spark.sql.types.StringType | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import za.co.absa.spark.commons.sql.functions.null_col | ||
|
||
class ColumnOrValueTest extends AnyFunSuite { | ||
test("Creation of column based on its name"){ | ||
val colName = "my_column" | ||
val expected = ColumnOrValueForm(col(colName), isColumn = true, isValue = false, Set(colName), None) | ||
val result = ColumnOrValue(colName) | ||
expected assertTo result | ||
} | ||
|
||
test("Creation of column based on its definition") { | ||
val myColumn = current_date | ||
val expected = ColumnOrValueForm(myColumn, isColumn = true, isValue = false, Set(), None) | ||
val result = ColumnOrValue(myColumn) | ||
expected assertTo result | ||
} | ||
|
||
test("Creation of map from given column names") { | ||
val colNames = Set("Col1", "Col2", "Col3") | ||
val colTransformer: String => Column = col | ||
val expectedColumn = map_from_arrays( | ||
array( | ||
lit("Col1"), lit("Col2"), lit("Col3") | ||
), array( | ||
col("Col1"), col("Col2"), col("Col3") | ||
)) | ||
val expected = ColumnOrValueForm[Map[String, Any]](expectedColumn, isColumn = true, isValue = false, colNames, None) | ||
val result = ColumnOrValue[Any](colNames, colTransformer) | ||
expected assertTo(result) | ||
} | ||
|
||
test("Creating ColumnOrValue from a defined Option") { | ||
val value = "Foo" | ||
val expected = ColumnOrValueForm(lit(value), isColumn = false, isValue = true, Set(), Option(Option(value))) | ||
val result = ColumnOrValue.withOption(Option(value)) | ||
expected assertTo result | ||
} | ||
|
||
test("Creating ColumnOrValue from an empty Option") { | ||
val expected = ColumnOrValueForm[Option[String]](null_col(StringType), isColumn = false, isValue = true, Set(), None) | ||
val result = ColumnOrValue.withOption(None) | ||
expected assertTo result | ||
} | ||
|
||
test("Creating ColumnOrValue from a given value") { | ||
val value = 42 | ||
val expected = ColumnOrValueForm(lit(value), isColumn = false, isValue = true, Set(), Some(value)) | ||
val result = ColumnOrValue.withValue(value) | ||
expected assertTo result | ||
} | ||
|
||
test("Creating ColumnOrValue as an undefined (empty) value") { | ||
|
||
val myColumn = null_col(StringType) | ||
val expected = ColumnOrValueForm[Option[String]](myColumn, isColumn = false, isValue = true, Set(), None) | ||
val result = ColumnOrValue.asEmpty | ||
expected assertTo result | ||
} | ||
|
||
test("Creating ColumnOrValue as a map of string columns") { | ||
val colNames = Set("Col1", "Col2", "Col3") | ||
val expectedColumn = map_from_arrays( | ||
array( | ||
lit("Col1"), lit("Col2"), lit("Col3") | ||
), array( | ||
col("Col1").cast(StringType), col("Col2").cast(StringType), col("Col3").cast(StringType) | ||
)) | ||
val expected = ColumnOrValueForm[Map[String, String]](expectedColumn, isColumn = true, isValue = false, colNames, None) | ||
val result = ColumnOrValue.asMapOfStringColumns(colNames) | ||
expected assertTo(result) | ||
} | ||
|
||
|
||
} |