-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test coverage for CSVToAvro util (#50)
- Loading branch information
Showing
9 changed files
with
242 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
5 changes: 5 additions & 0 deletions
5
utils/src/test/resources/PassengerDataContentTypeMisMatch.csv
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,5 @@ | ||
1,false,3,"Braund, Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S | ||
2,fail,1,"Cumings, Mrs. John Bradley (Florence Briggs Thayer)",female,38,1,0,PC 17599,71.2833,C85,C | ||
3,true,3,"Heikkinen, Miss. Laina",female,26,0,0,STON/O2. 3101282,7.925,,S | ||
4,true,1,"Futrelle, Mrs. Jacques Heath (Lily May Peel)",female,35,1,0,113803,53.1,C123,S | ||
5,false,3,"Allen, Mr. William Henry",male,35,0,0,373450,8.05,,S |
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,5 @@ | ||
1,false,3,"Braund, Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S | ||
2,true,1,"Cumings, Mrs. John Bradley (Florence Briggs Thayer)",female,38,1,0,PC 17599,71.2833,C85,C | ||
3,true,3,"Heikkinen, Miss. Laina",female,26,0,0,STON/O2. 3101282,7.925,,S | ||
4,true,1,"Futrelle, Mrs. Jacques Heath (Lily May Peel)",female,35,1,0,113803,53.1,C123,S | ||
5,false,3,"Allen, Mr. William Henry",male,35,0,0,373450,8.05,,S |
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,46 @@ | ||
{ | ||
"type" : "record", | ||
"name" : "Passenger", | ||
"namespace" : "com.salesforce.app.schema", | ||
"fields" : [ { | ||
"name" : "PassengerId", | ||
"type" : [ "int", "null" ] | ||
}, { | ||
"name" : "Survived", | ||
"type" : "boolean", | ||
"default": false | ||
}, { | ||
"name" : "Pclass", | ||
"type" : [ "int", "null" ] | ||
}, { | ||
"name" : "Name", | ||
"type" : [ "string", "null" ] | ||
}, { | ||
"name" : "Sex", | ||
"type" : [ "string", "null" ] | ||
}, { | ||
"name" : "Age", | ||
"type" : [ "double", "null" ] | ||
}, { | ||
"name" : "SibSp", | ||
"type" : [ "int", "null" ] | ||
}, { | ||
"name" : "Parch", | ||
"type" : [ "long", "null" ] | ||
}, { | ||
"name" : "Ticket", | ||
"type" : [ "string", "null" ] | ||
}, { | ||
"name" : "Fare", | ||
"type" : [ "float", "null" ] | ||
}, { | ||
"name" : "Cabin", | ||
"type" : [ "string", "null" ] | ||
}, { | ||
"name" : "Embarked", | ||
"type" : [ "string", "null" ] | ||
}, { | ||
"name" : "FailTest", | ||
"type" : [ "string", "null" ] | ||
} ] | ||
} |
43 changes: 43 additions & 0 deletions
43
utils/src/test/resources/PassengerSchemaModifiedDataTypes.avsc
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,43 @@ | ||
{ | ||
"type" : "record", | ||
"name" : "Passenger", | ||
"namespace" : "com.salesforce.app.schema", | ||
"fields" : [ { | ||
"name" : "PassengerId", | ||
"type" : [ "int", "null" ] | ||
}, { | ||
"name" : "Survived", | ||
"type" : "boolean", | ||
"default": false | ||
}, { | ||
"name" : "Pclass", | ||
"type" : [ "int", "null" ] | ||
}, { | ||
"name" : "Name", | ||
"type" : [ "string", "null" ] | ||
}, { | ||
"name" : "Sex", | ||
"type" : [ "string", "null" ] | ||
}, { | ||
"name" : "Age", | ||
"type" : [ "double", "null" ] | ||
}, { | ||
"name" : "SibSp", | ||
"type" : [ "int", "null" ] | ||
}, { | ||
"name" : "Parch", | ||
"type" : [ "long", "null" ] | ||
}, { | ||
"name" : "Ticket", | ||
"type" : [ "string", "null" ] | ||
}, { | ||
"name" : "Fare", | ||
"type" : [ "float", "null" ] | ||
}, { | ||
"name" : "Cabin", | ||
"type" : [ "string", "null" ] | ||
}, { | ||
"name" : "Embarked", | ||
"type" : [ "string", "null" ] | ||
} ] | ||
} |
47 changes: 47 additions & 0 deletions
47
utils/src/test/resources/PassengerSchemaNestedTypeCSV.avsc
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,47 @@ | ||
{ | ||
"type" : "record", | ||
"name" : "Passenger", | ||
"namespace" : "com.salesforce.app.schema", | ||
"fields" : [ { | ||
"name" : "PassengerId", | ||
"type" : [ "int", "null" ] | ||
}, { | ||
"name" : "Survived", | ||
"type" : "boolean", | ||
"default": false | ||
}, { | ||
"name" : "Pclass", | ||
"type" : [ "int", "null" ] | ||
}, { | ||
"name" : "Name", | ||
"type" : [ "string", "null" ] | ||
}, { | ||
"name" : "Sex", | ||
"type" : { | ||
"name": "Sex", | ||
"type": "enum", | ||
"symbols": [ "male", "female" ] | ||
} | ||
}, { | ||
"name" : "Age", | ||
"type" : [ "double", "null" ] | ||
}, { | ||
"name" : "SibSp", | ||
"type" : [ "int", "null" ] | ||
}, { | ||
"name" : "Parch", | ||
"type" : [ "int", "null" ] | ||
}, { | ||
"name" : "Ticket", | ||
"type" : [ "string", "null" ] | ||
}, { | ||
"name" : "Fare", | ||
"type" : [ "double", "null" ] | ||
}, { | ||
"name" : "Cabin", | ||
"type" : [ "string", "null" ] | ||
}, { | ||
"name" : "Embarked", | ||
"type" : [ "string", "null" ] | ||
} ] | ||
} |
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
77 changes: 77 additions & 0 deletions
77
utils/src/test/scala/com/salesforce/op/utils/io/csv/CSVToAvroTest.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,77 @@ | ||
/* | ||
* Copyright (c) 2017, Salesforce.com, Inc. | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package com.salesforce.op.utils.io.csv | ||
|
||
import com.salesforce.op.test.{Passenger, TestSparkContext} | ||
import org.apache.spark.SparkException | ||
import org.apache.spark.rdd.RDD | ||
import org.junit.runner.RunWith | ||
import org.scalatest.FlatSpec | ||
import org.scalatest.junit.JUnitRunner | ||
|
||
@RunWith(classOf[JUnitRunner]) | ||
class CSVToAvroTest extends FlatSpec with TestSparkContext { | ||
val avroSchema: String = loadFile(s"$resourceDir/PassengerSchemaModifiedDataTypes.avsc") | ||
val csvReader: CSVInOut = new CSVInOut(CSVOptions(header = true)) | ||
lazy val csvRDD: RDD[Seq[String]] = csvReader.readRDD(s"$resourceDir/PassengerDataModifiedDataTypes.csv") | ||
lazy val csvFileRecordCount: Long = csvRDD.count | ||
|
||
Spec(CSVToAvro.getClass) should "convert RDD[Seq[String]] to RDD[GenericRecord]" in { | ||
val res = CSVToAvro.toAvro(csvRDD, avroSchema) | ||
res shouldBe a[RDD[_]] | ||
res.count shouldBe csvFileRecordCount | ||
} | ||
|
||
it should "convert RDD[Seq[String]] to RDD[T]" in { | ||
val res = CSVToAvro.toAvroTyped[Passenger](csvRDD, avroSchema) | ||
res shouldBe a[RDD[_]] | ||
res.count shouldBe csvFileRecordCount | ||
} | ||
|
||
it should "throw an error for nested schema" in { | ||
val invalidAvroSchema = loadFile(s"$resourceDir/PassengerSchemaNestedTypeCSV.avsc") | ||
val exceptionMsg = "CSV should be a flat file and not have nested records (unsupported column(Sex schemaType=ENUM)" | ||
val error = intercept[SparkException](CSVToAvro.toAvro(csvRDD, invalidAvroSchema).count()) | ||
error.getCause.getMessage shouldBe exceptionMsg | ||
} | ||
|
||
it should "throw an error for mis-matching schema fields" in { | ||
val invalidAvroSchema = loadFile(s"$resourceDir/PassengerSchemaInvalidField.avsc") | ||
val error = intercept[SparkException](CSVToAvro.toAvro(csvRDD, invalidAvroSchema).count()) | ||
error.getCause.getMessage shouldBe "Mismatch number of fields in csv record and avro schema" | ||
} | ||
|
||
it should "throw an error for bad data" in { | ||
val invalidDataRDD = csvReader.readRDD(s"$resourceDir/PassengerDataContentTypeMisMatch.csv") | ||
val error = intercept[SparkException](CSVToAvro.toAvro(invalidDataRDD, avroSchema).count()) | ||
error.getCause.getMessage shouldBe "Boolean column not actually a boolean. Invalid value: 'fail'" | ||
} | ||
} |